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  import org.miloss.fgsms.sla.NonTransactionalSLAProcessor;
40  
41  /**
42   *
43   * @author AO
44   */
45  public class InvocationsOverTimeLessThan implements SLARuleInterface {
46  
47      @Override
48      public boolean CheckTransactionalRule(SetStatusRequestMsg req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
49          return false;
50      }
51  
52      @Override
53      public boolean CheckTransactionalRule(ProcessPerformanceData req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
54          return false;
55      }
56  
57      @Override
58      public boolean CheckTransactionalRule(MachinePerformanceData req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
59          return false;
60      }
61  
62      @Override
63      public boolean CheckTransactionalRule(AddDataRequestMsg req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
64          return false;
65      }
66  
67      @Override
68      public boolean CheckTransactionalRule(String url, List<BrokerData> data, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
69          return false;
70      }
71  
72      @Override
73      public boolean CheckNonTransactionalRule(ServicePolicy pol, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg, boolean pooled) {
74          if (nullableFaultMsg == null) {
75              nullableFaultMsg = new AtomicReference<String>();
76          }
77          NameValuePair GetNameValuePairByName = Utility.getNameValuePairByName(params, "value");
78          long rate = Long.parseLong(GetNameValuePairByName.getValue());
79          GetNameValuePairByName = Utility.getNameValuePairByName(params, "duration");
80          long duration = Long.parseLong(GetNameValuePairByName.getValue());
81          //    long rate = x.getFaults();//) / (double) (Utility.durationToTimeInMS(x.getTime())));
82          long faultrate = NonTransactionalSLAProcessor.GrabInvocationRate(duration, pol.getURL());
83          if (faultrate < rate) {
84              nullableFaultMsg.set("Invocations Over Time measured value of " + faultrate + " is greater than " + rate + ", " + nullableFaultMsg.get());
85              return true;
86          }
87          return false;
88      }
89  
90      @Override
91      public String GetDisplayName() {
92          return "Service Invocations (Transactions) over time is less than (value)";
93      }
94  
95      @Override
96      public String GetHtmlFormattedHelp() {
97          return "This rule will trigger when the number of service invocations (transactions) over time is greater than the specified value for the specified duration."
98                  + "This rule is processed periodically as part of the Non-Transactional SLA Processor, who execution cycle is controled by the administrator. "
99                  + "This rule applies to transactional service policies only.<Br><br>"
100                 + "Required parameters:"
101                 + "<ul>"
102                 + "<li>value - must be a positive integer. This is the threshold</li>"
103                 + "<li>duration - must be a positive integer or long. This represents time in milliseconds for the duration. It must be one of the values from which"
104                 + "statistics are aggregated. By default, the following durations are calculated 5 minutes (300000ms), 15 minutes (900000ms), 1 hour (3600000ms),"
105                 + "and 24 hours (1440000ms). Administrators can add additional time periods for aggregation via General Settings."
106                 + "</il>"
107                 + "</ul>";
108     }
109 
110     @Override
111     public List<NameValuePair> GetRequiredParameters() {
112         ArrayList<NameValuePair> arrayList = new ArrayList<NameValuePair>();
113         arrayList.add(Utility.newNameValuePair("value", null, false, false));
114         arrayList.add(Utility.newNameValuePair("duration", null, false, false));
115 
116         return arrayList;
117     }
118 
119     @Override
120     public List<NameValuePair> GetOptionalParameters() {
121         return new ArrayList<NameValuePair>();
122     }
123 
124     @Override
125     public boolean ValidateConfiguration(List<NameValuePair> params, AtomicReference<String> outError,ServicePolicy policy) {
126         if (outError == null) {
127             outError = new AtomicReference<String>();
128         }
129         if (params == null || params.isEmpty()) {
130             outError.set("The parameter 'value' is required. " + outError.get());
131         }
132                    if (!(policy instanceof TransactionalWebServicePolicy)) {
133             outError.set("This rule only applies to Transactional Service Policies. " + outError.get());
134         }
135         boolean foundLogger = false;
136         boolean foundduration = false;
137         for (int i = 0; i < params.size(); i++) {
138             if (params.get(i).getName().equals("value")) {
139                 foundLogger = true;
140                 if (Utility.stringIsNullOrEmpty(params.get(i).getValue())) {
141                     outError.set("A value must be specified for the parameter 'value'. " + outError.get());
142                 }
143                 try {
144                     long x = Long.parseLong(params.get(i).getValue());
145                     if (x <= 0) {
146                         outError.set("The parameter 'value' must be greater than zero. " + outError.get());
147                     }
148                 } catch (Exception ex) {
149                     outError.set("Bad value for parameter 'value'. It must be an integer or long. Message:" + ex.getMessage() + ". " + outError.get());
150                 }
151             }
152             if (params.get(i).getName().equals("duration")) {
153                 foundduration = true;
154                 if (Utility.stringIsNullOrEmpty(params.get(i).getValue())) {
155                     outError.set("A value must be specified for the parameter 'duration'. " + outError.get());
156                 }
157                 try {
158                     long x = Long.parseLong(params.get(i).getValue());
159                     if (x <= 0) {
160                         outError.set("The parameter 'duration' must be greater than zero. " + outError.get());
161                     }
162                 } catch (Exception ex) {
163                     outError.set("Bad value for parameter 'duration'. It must be an integer or long. Message:" + ex.getMessage() + ". " + outError.get());
164                 }
165             }
166         }
167         if (!foundLogger) {
168             outError.set("The parameter 'value' is required. " + outError.get());
169         }
170           if (!foundduration) {
171             outError.set("The parameter 'duration' is required. " + outError.get());
172         }
173         if (Utility.stringIsNullOrEmpty(outError.get())) {
174             return true;
175         } else {
176             return false;
177         }
178     }
179     
180        @Override
181     public AlertType GetType() {
182        return AlertType.Performance;
183     }
184        
185        @Override
186     public String GetHtmlFormattedDisplay(List<NameValuePair> params) {
187        NameValuePair mc = Utility.getNameValuePairByName(params, "value");
188         String item = UNDEFINED_VALUE;
189         if (mc != null) {
190             item = mc.getValue();
191             if (mc.isEncrypted() || mc.isEncryptOnSave()) {
192                 item = ENCRYPTED_MASK;
193             }
194         }
195         
196         NameValuePair mc2 = Utility.getNameValuePairByName(params, "duration");
197         String item2 = UNDEFINED_VALUE;
198         if (mc2 != null) {
199             item2 = mc2.getValue();
200             if (mc2.isEncrypted() || mc2.isEncryptOnSave()) {
201                 item2 = ENCRYPTED_MASK;
202             }
203         }
204         return Utility.encodeHTML(GetDisplayName() + " " + item + "/" + item2+"ms");
205     }
206        
207        @Override
208     public List<PolicyType> GetAppliesTo() {
209         List<PolicyType> ret = new ArrayList<PolicyType>();
210         ret.add(PolicyType.TRANSACTIONAL);
211         return ret;
212     }
213 }
214