View Javadoc
1   /**
2    * This Source Code Form is subject to the terms of the Mozilla Public
3    * License, v. 2.0. If a copy of the MPL was not distributed with this
4    * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5    *
6    * If it is not possible or desirable to put the notice in a particular
7    * file, then You may include the notice in a location (such as a LICENSE
8    * file in a relevant directory) where a recipient would be likely to look
9    * for such a notice.
10  
11   * 
12   */
13   
14  /*  ---------------------------------------------------------------------------
15   *  U.S. Government, Department of the Army
16   *  Army Materiel Command
17   *  Research Development Engineering Command
18   *  Communications Electronics Research Development and Engineering Center
19   *  ---------------------------------------------------------------------------
20   */
21   
22  package org.miloss.fgsms.sla.rules;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.concurrent.atomic.AtomicReference;
27  import org.miloss.fgsms.common.Utility;
28  import org.miloss.fgsms.plugins.sla.AlertType;
29  import org.miloss.fgsms.plugins.sla.SLARuleInterface;
30  import org.miloss.fgsms.services.interfaces.common.MachinePerformanceData;
31  import org.miloss.fgsms.services.interfaces.common.NameValuePair;
32  import org.miloss.fgsms.services.interfaces.common.PolicyType;
33  import org.miloss.fgsms.services.interfaces.common.ProcessPerformanceData;
34  import org.miloss.fgsms.services.interfaces.datacollector.AddDataRequestMsg;
35  import org.miloss.fgsms.services.interfaces.datacollector.BrokerData;
36  import org.miloss.fgsms.services.interfaces.policyconfiguration.ServicePolicy;
37  import org.miloss.fgsms.services.interfaces.policyconfiguration.TransactionalWebServicePolicy;
38  import org.miloss.fgsms.services.interfaces.status.SetStatusRequestMsg;
39  
40  /**
41   *
42   * @author AO
43   */
44  public class ResponseTimeGreaterThan  implements SLARuleInterface {
45  
46      @Override
47      public boolean CheckTransactionalRule(SetStatusRequestMsg req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
48          return false;
49      }
50  
51      @Override
52      public boolean CheckTransactionalRule(ProcessPerformanceData req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
53          return false;
54      }
55  
56      @Override
57      public boolean CheckTransactionalRule(MachinePerformanceData req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
58          return false;
59      }
60  
61      @Override
62      public boolean CheckTransactionalRule(AddDataRequestMsg req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
63          if (nullableFaultMsg == null) {
64              nullableFaultMsg = new AtomicReference<String>();
65          }
66          NameValuePair GetNameValuePairByName = Utility.getNameValuePairByName(params, "value");
67          long val=0;
68          String item = GetNameValuePairByName.getValue();
69          if (GetNameValuePairByName.isEncrypted()) {
70              item = Utility.DE(GetNameValuePairByName.getValue());
71          }
72          val = Long.parseLong(item);
73          if (req.getResponseTime() > val) {
74              nullableFaultMsg.set("The response time size, " + req.getResponseTime() + ", is greater that the value " + val + ", "+ nullableFaultMsg.get());
75          }
76          return false;
77      }
78  
79      @Override
80      public boolean CheckTransactionalRule(String url, List<BrokerData> data, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
81          return false;
82      }
83  
84      @Override
85      public boolean CheckNonTransactionalRule(ServicePolicy pol, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg, boolean pooled) {
86          return false;
87      }
88  
89      @Override
90      public String GetDisplayName() {
91          return "Response time is greater than value";
92      }
93  
94      @Override
95      public String GetHtmlFormattedHelp() {
96          return "If a transaction's response time is greater than the specified value, then this rule will trigger.<Br>"
97                  + "Applies to transactional service policies only.<br><br>"
98                  + "Required parameters:"
99                  + "<ul>"
100                 + "<li>value - a positive integer to compare against (ms)</li>"
101                 + "</ul>";
102     }
103 
104     @Override
105     public List<NameValuePair> GetRequiredParameters() {
106         ArrayList<NameValuePair> arrayList = new ArrayList<NameValuePair>();
107         arrayList.add(Utility.newNameValuePair("value", null, false, false));
108         return arrayList;
109     }
110 
111     @Override
112     public List<NameValuePair> GetOptionalParameters() {
113         return new ArrayList<NameValuePair>();
114     }
115 
116     @Override
117     public boolean ValidateConfiguration(List<NameValuePair> params, AtomicReference<String> outError,ServicePolicy policy) {
118         if (outError == null) {
119             outError = new AtomicReference<String>();
120         }
121         if (params == null || params.isEmpty()) {
122             outError.set("The parameter 'value' is required. " + outError.get());
123         }
124                if (!(policy instanceof TransactionalWebServicePolicy)) {
125             outError.set("This rule only applies to Transactional Service Policies. " + outError.get());
126         }
127         boolean foundLogger = false;
128         for (int i = 0; i < params.size(); i++) {
129             if (params.get(i).getName().equals("value")) {
130                 foundLogger = true;
131                 if (Utility.stringIsNullOrEmpty(params.get(i).getValue())) {
132                     outError.set("A value must be specified for the parameter 'value'. " + outError.get());
133                 }
134             }
135             try {
136                     long x = Long.parseLong(params.get(i).getValue());
137                     if (x <= 0) {
138                         outError.set("The parameter 'value' must be greater than zero. " + outError.get());
139                     }
140                 } catch (Exception ex) {
141                     outError.set("Bad value for parameter 'value'. It must be a integer or long. Message:" + ex.getMessage() + ". " + outError.get());
142                 }
143         }
144         if (!foundLogger) {
145             outError.set("The parameter 'value' is required. " + outError.get());
146         }
147         if (Utility.stringIsNullOrEmpty(outError.get())) {
148             return true;
149         } else {
150             return false;
151         }
152 
153     }
154     
155        @Override
156     public AlertType GetType() {
157        return AlertType.Performance;
158     }
159        
160        
161     @Override
162     public String GetHtmlFormattedDisplay(List<NameValuePair> params) {
163         NameValuePair mc = Utility.getNameValuePairByName(params, "value");
164         String item = UNDEFINED_VALUE;
165         if (mc != null) {
166             item = mc.getValue();
167             if (mc.isEncrypted() || mc.isEncryptOnSave()) {
168                 item = ENCRYPTED_MASK;
169             }
170         }
171         return Utility.encodeHTML(GetDisplayName() + " " + item+"ms");
172     }
173     
174     @Override
175     public List<PolicyType> GetAppliesTo() {
176         List<PolicyType> ret = new ArrayList<PolicyType>();
177         ret.add(PolicyType.TRANSACTIONAL);
178         return ret;
179     }
180 }