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  package org.miloss.fgsms.sla.rules;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.concurrent.atomic.AtomicReference;
26  import org.miloss.fgsms.common.Utility;
27  import org.miloss.fgsms.plugins.sla.SLARuleInterface;
28  import org.miloss.fgsms.services.interfaces.common.MachinePerformanceData;
29  import org.miloss.fgsms.services.interfaces.common.NameValuePair;
30  import org.miloss.fgsms.services.interfaces.common.PolicyType;
31  import org.miloss.fgsms.services.interfaces.common.ProcessPerformanceData;
32  import org.miloss.fgsms.services.interfaces.datacollector.AddDataRequestMsg;
33  import org.miloss.fgsms.services.interfaces.datacollector.BrokerData;
34  import org.miloss.fgsms.services.interfaces.policyconfiguration.ServicePolicy;
35  import org.miloss.fgsms.services.interfaces.policyconfiguration.StatisticalServicePolicy;
36  import org.miloss.fgsms.services.interfaces.status.SetStatusRequestMsg;
37  
38  /**
39   *
40   * @author AO
41   */
42  public class BrokerQueueSizeGreaterThan implements SLARuleInterface {
43  
44      @Override
45      public boolean CheckTransactionalRule(SetStatusRequestMsg req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
46          return false;
47      }
48  
49      @Override
50      public boolean CheckTransactionalRule(ProcessPerformanceData req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
51          return false;
52      }
53  
54      @Override
55      public boolean CheckTransactionalRule(MachinePerformanceData req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
56          return false;
57      }
58  
59      @Override
60      public boolean CheckTransactionalRule(AddDataRequestMsg req, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
61          return false;
62      }
63  
64      @Override
65      public boolean CheckTransactionalRule(String url, List<BrokerData> data, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg) {
66          if (nullableFaultMsg == null) {
67              nullableFaultMsg = new AtomicReference<String>();
68          }
69          NameValuePair val = Utility.getNameValuePairByName(params, "value");
70          long value = 0;
71          if (val.isEncrypted()) {
72              value = Long.parseLong(Utility.DE(val.getValue()));
73          } else {
74              value = Long.parseLong(val.getValue());
75          }
76          StringBuilder msg = new StringBuilder();
77          for (int i = 0; i < data.size(); i++) {
78              if (data.get(i).getDepth() > value) {
79  
80                  msg.append("The queue named ").append(data.get(i).getQueueOrTopicName()).append(" has a queue size of ").
81                          append(data.get(i).getDepth()).append(" which is greater than the SLA parameter of ").append(value).append(". ");
82              }
83          }
84  
85          String s = msg.toString();
86          if (Utility.stringIsNullOrEmpty(s)) {
87              return false;
88          } else {
89              nullableFaultMsg.set(s + "," + nullableFaultMsg.get());
90              return true;
91          }
92      }
93  
94      @Override
95      public boolean CheckNonTransactionalRule(ServicePolicy pol, List<NameValuePair> params, AtomicReference<String> nullableFaultMsg, boolean pooled) {
96          return false;
97      }
98  
99      @Override
100     public String GetDisplayName() {
101         return "Message Broker Queue Size is greater than ";
102     }
103 
104     @Override
105     public String GetHtmlFormattedHelp() {
106         return "This rule will trigger if a message broker's queue size is larger than the specified value. "
107                 + "Applies to statistical message broker policies only.<br><br>"
108                 + "Required parameters:"
109                 + "<ul>"
110                 + "<li>value - some positive integer</li>"
111                 + "</ul>"
112                 + "Optional parameters:"
113                 + "<ul>"
114                 + "<li>name - Optional, if defined, this value must equal that of a specific queue or topic</li>"
115                 + "</ul>";
116     }
117 
118     @Override
119     public List<NameValuePair> GetRequiredParameters() {
120         ArrayList<NameValuePair> arrayList = new ArrayList<NameValuePair>();
121         arrayList.add(Utility.newNameValuePair("value", null, false, false));
122         return arrayList;
123     }
124 
125     @Override
126     public List<NameValuePair> GetOptionalParameters() {
127         ArrayList<NameValuePair> arrayList = new ArrayList<NameValuePair>();
128         arrayList.add(Utility.newNameValuePair("name", null, false, false));
129         return arrayList;
130     }
131 
132     @Override
133     public boolean ValidateConfiguration(List<NameValuePair> params, AtomicReference<String> outError, ServicePolicy policy) {
134         if (outError == null) {
135             outError = new AtomicReference<String>();
136         }
137         if (params == null || params.isEmpty()) {
138             outError.set("The parameter 'value' is required. " + outError.get());
139         }
140         if (!(policy instanceof StatisticalServicePolicy)) {
141             outError.set("This rule only applies to Statistical Service Policies. " + outError.get());
142         }
143         boolean foundLogger = false;
144         for (int i = 0; i < params.size(); i++) {
145             if (params.get(i).getName().equals("value")) {
146                 foundLogger = true;
147                 if (Utility.stringIsNullOrEmpty(params.get(i).getValue())) {
148                     outError.set("A value must be specified for the parameter 'value'. " + outError.get());
149                 }
150                 try {
151                     long x = Long.parseLong(params.get(i).getValue());
152                     if (x <= 0) {
153                         outError.set("The parameter 'value' must be greater than zero. " + outError.get());
154                     }
155                 } catch (Exception ex) {
156                     outError.set("Bad value for parameter 'value'. It must be a integer or long. Message:" + ex.getMessage() + ". " + outError.get());
157                 }
158             }
159             if (params.get(i).getName().equals("name")) {
160                 if (Utility.stringIsNullOrEmpty(params.get(i).getValue())) {
161                     outError.set("A value must be specified for the parameter 'name'. " + outError.get());
162                 }
163             }
164         }
165         if (!foundLogger) {
166             outError.set("The parameter 'value' is required. " + outError.get());
167         }
168         if (Utility.stringIsNullOrEmpty(outError.get())) {
169             return true;
170         } else {
171             return false;
172         }
173 
174     }
175     @Override
176     public org.miloss.fgsms.plugins.sla.AlertType GetType() {
177        return org.miloss.fgsms.plugins.sla.AlertType.Performance;
178     }
179     
180      @Override
181     public String GetHtmlFormattedDisplay(List<NameValuePair> params) {
182         NameValuePair mc = Utility.getNameValuePairByName(params, "value");
183         String item = UNDEFINED_VALUE;
184         if (mc != null) {
185             item = mc.getValue();
186             if (mc.isEncrypted() || mc.isEncryptOnSave()) {
187                 item = ENCRYPTED_MASK;
188             }
189         }
190         
191         NameValuePair mc2 = Utility.getNameValuePairByName(params, "name");
192         String item2 = UNDEFINED_VALUE;
193         if (mc2 != null) {
194             item2 = mc2.getValue();
195             if (mc2.isEncrypted() || mc2.isEncryptOnSave()) {
196                 item2 = ENCRYPTED_MASK;
197             }
198         }
199         return Utility.encodeHTML(GetDisplayName() + " " + item + " on " + item2);
200     }
201      
202        @Override
203     public List<PolicyType> GetAppliesTo() {
204          List<PolicyType> x = new ArrayList<PolicyType>();
205          x.add(PolicyType.STATISTICAL);
206          return x;
207     }
208 }