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;
23  
24  import java.net.URLEncoder;
25  import java.util.Properties;
26  import java.util.UUID;
27  import java.util.concurrent.atomic.AtomicReference;
28  import org.miloss.fgsms.plugins.sla.SLARuleInterface;
29  import org.miloss.fgsms.services.interfaces.datacollector.AddDataRequestMsg;
30  import org.miloss.fgsms.services.interfaces.policyconfiguration.*;
31  import org.apache.log4j.Level;
32  import org.miloss.fgsms.common.Logger;;
33  import org.miloss.fgsms.plugins.sla.AlertType;
34  
35  /**
36   * Provides SLA processing for web service transactions
37   *
38   * @author AO
39   */
40  public class TransactionalSLAProcessor {
41  
42      private static Logger log = Logger.getLogger("fgsms.SLAProcessor");
43      private static Logger syslog = Logger.getLogger("fgsms.SLAProcessor.Syslog");
44      private static Logger eventlog = Logger.getLogger("fgsms.SLAProcessor.EventLog");
45      private static Logger udplog = Logger.getLogger("fgsms.SLAProcessor.UdpLog");
46      private static Logger filelog = Logger.getLogger("fgsms.SLAProcessor.FileLog");
47  
48      /*
49       * Use this for transactional updates
50       */
51      public void ProcessNewTransaction(AddDataRequestMsg req, String transactionid) {
52          ServicePolicy pol = SLACommon.LoadPolicyPooled(req.getURI());
53          Properties props = SLACommon.LoadSLAPropertiesPooled();
54  
55  
56          if (pol == null) {
57              return;
58          }
59          if (pol.getServiceLevelAggrements() == null
60                  || pol.getServiceLevelAggrements().getSLA() == null
61                  || pol.getServiceLevelAggrements().getSLA().isEmpty()) {
62              return;
63          }
64          for (int i = 0; i < pol.getServiceLevelAggrements().getSLA().size(); i++) {
65              boolean flag = false;
66  
67              String faultMsg = "";
68              AtomicReference<String> ref = new AtomicReference<String>(faultMsg);
69              if (pol.getServiceLevelAggrements().getSLA().get(i) == null) {
70                  continue;
71              }
72  
73  
74  
75  
76              //transactional checks
77              // log.log(Level.INFO, "Checking for Transactional SLA violations for the service at " + pol.getURL() + ". " + msglog.getTransactionLog().size() + " records to sift through");
78  
79  
80              flag = ProcessRules(req, ref, pol.getServiceLevelAggrements().getSLA().get(i).getRule());
81              long time = System.currentTimeMillis();
82              try {
83                  time = req.getRecordedat().getTimeInMillis();
84              } catch (Exception ex) {
85              }
86              if (flag) //record SLA fault
87              {
88                  String incident = UUID.randomUUID().toString();
89                  log.log(Level.INFO, "SLA violation for the service at " + pol.getURL() + " Transaction ID: " + transactionid + " " + ref.get());
90                  SLACommon.RecordSLAFault(ref, pol.getURL(), transactionid, time, incident, true);
91                  SLACommon.ProcessAlerts(ref.get(), ref.get() + "<br>" + GenerateLink(props.getProperty("fgsms.GUI.URL"), pol.getURL(),
92                          transactionid), pol.getURL(), transactionid, time, incident, true, false, pol.getServiceLevelAggrements().getSLA().get(i).getAction().getSLAAction(),
93                          pol.getServiceLevelAggrements().getSLA().get(i).getGuid(), pol, AlertType.Performance);
94  
95  
96              }
97          }
98      }
99  
100     private String GenerateLink(String relativeUrl, String ServiceURL, String transactionId) {
101         return "<a href=\"" + relativeUrl + "/SpecificTransactionLogViewer.jsp?ID=" + URLEncoder.encode(transactionId) + "\">View this transaction</a>";
102     }
103 
104 
105     /*
106      * Use this for timed jobs and for status changes
107      *
108      * public void ProcessNonTransactionalSLA() { List<ServicePolicy> pols =
109      * SLACommon.LoadServicePoliciesPooled();
110      *
111      * }
112      */
113     private boolean ProcessRules(AddDataRequestMsg get, AtomicReference<String> faultMsg, RuleBaseType rule) {
114         String s = faultMsg.get();
115         if (rule instanceof AndOrNot) {
116             AndOrNot x = (AndOrNot) rule;
117             if (x.getFlag() == JoiningType.AND) {
118                 return ProcessRules(get, faultMsg, x.getLHS()) && ProcessRules(get, faultMsg, x.getRHS());
119             }
120             if (x.getFlag() == JoiningType.OR) {
121                 return ProcessRules(get, faultMsg, x.getLHS()) || ProcessRules(get, faultMsg, x.getRHS());
122             }
123             if (x.getFlag() == JoiningType.NOT) {
124                 return !ProcessRules(get, faultMsg, x.getLHS());
125             }
126         }
127 
128         if (rule instanceof SLARuleGeneric) {
129             SLARuleGeneric x = (SLARuleGeneric) rule;
130             if (x.getProcessAt()==null || x.getProcessAt() == RunAtLocation.FGSMS_SERVER) {
131                 Class c = null;
132                 try {
133                     c = Thread.currentThread().getContextClassLoader().loadClass(x.getClassName());
134                 } catch (ClassNotFoundException ex) {
135                     log.log(Level.ERROR, SLACommon.getBundleString("ErrorSLAPluginRuleNCDF") + x.getClassName(), ex);
136                     return false;
137                 }
138                 Object obj = null;
139                 if (c != null) {
140                     try {
141                         obj = c.newInstance();
142                     } catch (InstantiationException ex) {
143                         log.log(Level.ERROR, SLACommon.getBundleString("ErrorSLAPluginRuleNCDF") + x.getClassName(), ex);
144                         return false;
145                     } catch (IllegalAccessException ex) {
146                         log.log(Level.ERROR, SLACommon.getBundleString("ErrorSLAPluginRuleNCDF") + x.getClassName(), ex);
147                         return false;
148                     }
149                     SLARuleInterface cast = null;
150                     try {
151                         cast = (SLARuleInterface) obj;
152                     } catch (ClassCastException ex) {
153                         log.log(Level.ERROR, String.format(SLACommon.getBundleString("ErrorSLAPluginRuleTypeCast"), x.getClassName(), SLARuleInterface.class.getCanonicalName()), ex);
154                         return false;
155                     }
156                     try {
157                         AtomicReference<String> smsg = new AtomicReference<String>();
158                         boolean CheckRule = cast.CheckTransactionalRule(get, x.getParameterNameValue(), smsg);
159                         if (CheckRule) {
160                             faultMsg.set(smsg.get());
161                         }
162                         return CheckRule;
163                     } catch (Exception ex) {
164                         log.log(Level.ERROR, String.format(SLACommon.getBundleString("ErrorSLAPluginRuleUnexpectedError"), x.getClassName()), ex);
165                     }
166                 }
167             }
168         }
169         return false;
170     }
171 }