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   *  US Government, Department of the Army
15   *  Army Materiel Command
16   *  Research Development Engineering Command
17   *  Communications Electronics Research Development and Engineering Center
18   *  ---------------------------------------------------------------------------
19   */
20  package org.miloss.fgsms.sdks;
21  
22  import java.io.FileOutputStream;
23  import java.net.URL;
24  import java.util.GregorianCalendar;
25  import java.util.List;
26  import java.util.Map;
27  import javax.xml.datatype.DatatypeConfigurationException;
28  import javax.xml.datatype.DatatypeFactory;
29  import java.util.Calendar;
30  import javax.xml.ws.BindingProvider;
31  import org.miloss.fgsms.common.Constants;
32  import org.miloss.fgsms.common.Constants.AuthMode;
33  import org.miloss.fgsms.common.Utility;
34  import org.miloss.fgsms.services.interfaces.automatedreportingservice.AutomatedReportingService;
35  import org.miloss.fgsms.services.interfaces.automatedreportingservice.AutomatedReportingService_Service;
36  import org.miloss.fgsms.services.interfaces.common.PolicyType;
37  import org.miloss.fgsms.services.interfaces.common.SecurityWrapper;
38  import org.miloss.fgsms.services.interfaces.dataaccessservice.*;
39  
40  import org.miloss.fgsms.services.interfaces.policyconfiguration.*;
41  import org.miloss.fgsms.services.interfaces.reportingservice.ArrayOfReportTypeContainer;
42  import org.miloss.fgsms.services.interfaces.reportingservice.ExportCSVDataRequestMsg;
43  import org.miloss.fgsms.services.interfaces.reportingservice.ExportDataRequestMsg;
44  import org.miloss.fgsms.services.interfaces.reportingservice.ExportDataToCSVResponseMsg;
45  import org.miloss.fgsms.services.interfaces.reportingservice.ExportDataToHTMLResponse;
46  import org.miloss.fgsms.services.interfaces.reportingservice.ExportDataToHTMLResponseMsg;
47  import org.miloss.fgsms.services.interfaces.reportingservice.ExportRecordsEnum;
48  import org.miloss.fgsms.services.interfaces.reportingservice.ReportTypeContainer;
49  import org.miloss.fgsms.services.interfaces.reportingservice.ReportingService;
50  import org.miloss.fgsms.services.interfaces.reportingservice.ReportingService_Service;
51  import org.miloss.fgsms.services.interfaces.status.GetStatusRequestMsg;
52  import org.miloss.fgsms.services.interfaces.status.GetStatusResponseMsg;
53  import org.miloss.fgsms.services.interfaces.status.StatusService;
54  import org.miloss.fgsms.services.interfaces.status.StatusServiceService;
55  import org.apache.log4j.*;
56  import org.miloss.fgsms.services.interfaces.common.TimeRange;
57  import us.gov.ic.ism.v2.ClassificationType;
58  
59  /**
60   * This class is an over simplified SDK, it basically automates creating web
61   * service proxies for all of the FGSMS service endpoints from a configuration
62   * file and provides some wrappers for some of the mundane tasks that you may
63   * find yourself tackling.
64   *
65   * @author AO
66   */
67  public class FgsmsSDK {
68  
69      private DataAccessService das;
70      private PCS pcs;
71      private StatusService ss;
72      private ReportingService rs;
73      private AutomatedReportingService ars;
74  
75      final static Logger log = Logger.getLogger("fgsms.SDK");
76      public static final String USERNAME_PROPERTY = BindingProvider.USERNAME_PROPERTY;
77      public static final String PASSWORD_PROPERTY = BindingProvider.PASSWORD_PROPERTY;
78      public static final String DAS_ENDPOINT = "org.miloss.fgsms.dasEndpoint";
79      public static final String ARS_ENDPOINT = "org.miloss.fgsms.arsEndpoint";
80      public static final String PCS_ENDPOINT = "org.miloss.fgsms.pcsEndpoint";
81      public static final String SS_ENDPOINT = "org.miloss.fgsms.ssEndpoint";
82      public static final String RS_ENDPOINT = "org.miloss.fgsms.rsEndpoint";
83      public static final String AUTH_MODE = "org.miloss.fgsms.authMode";
84      public static final String CLASSIFICATION_LEVEL = "org.miloss.fgsms.classification";
85      public static final String CLASSIFICATION_CAVEAT = "org.miloss.fgsms.caveat";
86      private SecurityWrapper sc = new SecurityWrapper(ClassificationType.U, "");
87  
88      public FgsmsSDK(Map<String, Object> context) throws Exception {
89          AuthMode mode = (AuthMode) context.get(AUTH_MODE);
90          if (mode == null) {
91              throw new Exception("Auth Mode is not defined");
92          }
93  
94          DataAccessService_Service svc = new DataAccessService_Service();
95          das = svc.getDASPort();
96          BindingProvider bp = (BindingProvider) das;
97          bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, context.get(DAS_ENDPOINT));
98  
99          if (mode == AuthMode.UsernamePassword) {
100             bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, context.get(USERNAME_PROPERTY));
101             bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, context.get(PASSWORD_PROPERTY));
102         }
103 
104         PolicyConfigurationService svc2 = new PolicyConfigurationService();
105         pcs = svc2.getPCSPort();
106         bp = (BindingProvider) pcs;
107         bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, context.get(PCS_ENDPOINT));
108 
109         if (mode == AuthMode.UsernamePassword) {
110             bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, context.get(USERNAME_PROPERTY));
111             bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, context.get(PASSWORD_PROPERTY));
112         }
113 
114         ReportingService_Service svc3 = new ReportingService_Service();
115         rs = svc3.getReportingServicePort();
116         bp = (BindingProvider) rs;
117         bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, context.get(RS_ENDPOINT));
118 
119         if (mode == AuthMode.UsernamePassword) {
120             bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, context.get(USERNAME_PROPERTY));
121             bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, context.get(PASSWORD_PROPERTY));
122         }
123 
124         AutomatedReportingService_Service svc4 = new AutomatedReportingService_Service();
125         ars = svc4.getAutomatedReportingServicePort();
126         bp = (BindingProvider) ars;
127         bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, context.get(ARS_ENDPOINT));
128 
129         if (mode == AuthMode.UsernamePassword) {
130             bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, context.get(USERNAME_PROPERTY));
131             bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, context.get(PASSWORD_PROPERTY));
132         }
133 
134         StatusServiceService svc5 = new StatusServiceService();
135         ss = svc5.getStatusServicePort();
136         bp = (BindingProvider) ss;
137         bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, context.get(SS_ENDPOINT));
138 
139         if (mode == AuthMode.UsernamePassword) {
140             bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, context.get(USERNAME_PROPERTY));
141             bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, context.get(PASSWORD_PROPERTY));
142         }
143 
144         if (context.get(CLASSIFICATION_LEVEL) != null) {
145             sc.setClassification((ClassificationType) context.get(CLASSIFICATION_LEVEL));
146         }
147         if (context.get(CLASSIFICATION_CAVEAT) != null) {
148             sc.setCaveats((String) context.get(CLASSIFICATION_CAVEAT));
149         }
150 
151     }
152 
153     public List<ServiceType> GetListOfMonitoredServices() throws Exception {
154         GetMonitoredServiceListRequestMsg req = new GetMonitoredServiceListRequestMsg();
155         req.setClassification(sc);
156         GetMonitoredServiceListResponseMsg monitoredServiceList = das.getMonitoredServiceList(req);
157         if (monitoredServiceList != null && monitoredServiceList.getServiceList() != null && monitoredServiceList.getServiceList() != null) {
158             return monitoredServiceList.getServiceList().getServiceType();
159         }
160         return null;
161     }
162 
163     public ServicePolicy GetServicePolicy(String url) throws Exception {
164         ServicePolicyRequestMsg req = new ServicePolicyRequestMsg();
165         req.setClassification(sc);
166         req.setURI(url);
167         ServicePolicyResponseMsg servicePolicy = pcs.getServicePolicy(req);
168         return servicePolicy.getPolicy();
169     }
170 
171     public GetStatusResponseMsg GetStatus(String url) throws Exception {
172         GetStatusRequestMsg req = new GetStatusRequestMsg();
173         req.setURI(url);
174         req.setClassification(sc);
175         GetStatusResponseMsg status = ss.getStatus(req);
176         return status;
177     }
178 
179     public GetQuickStatsResponseMsg GetAggregatedStatistics(String url) throws Exception {
180         GetQuickStatsRequestMsg req = new GetQuickStatsRequestMsg();
181         req.setClassification(sc);
182         req.setUri(url);
183         GetQuickStatsResponseMsg quickStats = das.getQuickStats(req);
184         return quickStats;
185     }
186 
187     public GetMostRecentMachineDataResponseMsg GetMachineStatistics(String url) throws Exception {
188         GetMostRecentMachineDataRequestMsg req = new GetMostRecentMachineDataRequestMsg();
189         req.setUri(url);
190         req.setClassification(sc);
191         GetMostRecentMachineDataResponseMsg mostRecentMachineData = das.getMostRecentMachineData(req);
192         return mostRecentMachineData;
193     }
194 
195     public GetMostRecentProcessDataResponseMsg GetProcessStatistics(String url) throws Exception {
196         GetMostRecentProcessDataRequestMsg req = new GetMostRecentProcessDataRequestMsg();
197         req.setUri(url);
198         req.setClassification(sc);
199         GetMostRecentProcessDataResponseMsg mostRecentMachineData = das.getMostRecentProcessData(req);
200         return mostRecentMachineData;
201     }
202 
203     public GetCurrentBrokerDetailsResponseMsg GetBrokerStatistics(String url) throws Exception {
204         GetCurrentBrokerDetailsRequestMsg req = new GetCurrentBrokerDetailsRequestMsg();
205         req.setClassification(sc);
206         req.setUrl(url);
207         GetCurrentBrokerDetailsResponseMsg currentBrokerDetails = das.getCurrentBrokerDetails(req);
208         return currentBrokerDetails;
209     }
210 
211     public GetGlobalPolicyResponseMsg GetGlobalPolicy() throws Exception {
212         GetGlobalPolicyRequestMsg req = new GetGlobalPolicyRequestMsg();
213         req.setClassification(sc);
214         GetGlobalPolicyResponseMsg globalPolicy = pcs.getGlobalPolicy(req);
215         sc = globalPolicy.getClassification();
216         return globalPolicy;
217     }
218 
219     public GetMessageTransactionLogDetailsResponseMsg GetMessagePayload(String uuid) throws Exception {
220         GetMessageTransactionLogDetailsMsg req = new GetMessageTransactionLogDetailsMsg();
221         req.setTransactionID(uuid);
222         req.setClassification(sc);
223         GetMessageTransactionLogDetailsResponseMsg res = das.getMessageTransactionLogDetails(req);
224         return res;
225     }
226 
227     List<GetStatusResponseMsg> GetStatusAll() throws Exception {
228         GetStatusRequestMsg req = new GetStatusRequestMsg();
229         req.setClassification(sc);
230         List<GetStatusResponseMsg> allStatus = ss.getAllStatus(req);
231         return allStatus;
232     }
233 
234     void RemoveServicePolicyAndData(String url) throws Exception {
235         DeleteServicePolicyRequestMsg req = new DeleteServicePolicyRequestMsg();
236         req.setClassification(sc);
237         req.setURL(url);
238         req.setDeletePerformanceData(true);
239         pcs.deleteServicePolicy(req);
240     }
241 
242     GetGeneralSettingsResponseMsg getGeneralSettings() throws Exception {
243         GetGeneralSettingsRequestMsg req = new GetGeneralSettingsRequestMsg();
244         req.setClassification(sc);
245         return pcs.getGeneralSettings(req);
246     }
247 
248     void removeGeneralSetting(KeyNameValue get) throws Exception {
249         RemoveGeneralSettingsRequestMsg req = new RemoveGeneralSettingsRequestMsg();
250         req.setClassification(sc);
251         req.getKeyNameValue().add(get);
252         pcs.removeGeneralSettings(req);
253     }
254 
255     void addGeneralSetting(String key, String name, String value, boolean shouldEncrypt) throws Exception {
256         SetGeneralSettingsRequestMsg req = new SetGeneralSettingsRequestMsg();
257         req.setClassification(sc);
258         req.getKeyNameValueEnc().add(new KeyNameValueEnc());
259         req.getKeyNameValueEnc().get(0).setKeyNameValue(new KeyNameValue());
260         req.getKeyNameValueEnc().get(0).getKeyNameValue().setPropertyKey(key);
261         req.getKeyNameValueEnc().get(0).getKeyNameValue().setPropertyName(name);
262         req.getKeyNameValueEnc().get(0).getKeyNameValue().setPropertyValue(value);
263         req.getKeyNameValueEnc().get(0).setShouldEncrypt(shouldEncrypt);
264         pcs.setGeneralSettings(req);
265     }
266 
267     GetProcessesListByMachineResponseMsg getMachineInformation(ServiceType get) throws Exception {
268 
269         GetProcessesListByMachineRequestMsg req = new GetProcessesListByMachineRequestMsg();
270         req.setHostname(get.getHostname());
271         req.setClassification(sc);
272         GetProcessesListByMachineResponseMsg res = pcs.getProcessesListByMachine(req);
273         return res;
274     }
275 
276     GetMailSettingsResponseMsg getEmailsettings() throws Exception {
277         GetMailSettingsRequestMsg req = new GetMailSettingsRequestMsg();
278         req.setClassification(sc);
279         GetMailSettingsResponseMsg mailSettings = pcs.getMailSettings(req);
280         return mailSettings;
281     }
282 
283     SetServicePolicyResponseMsg StartProcessMonitor(String process, String machine, String domain, String displayname, String machineurl, long datattl, List<SLA> slas) throws Exception {
284         SetServicePolicyRequestMsg preq = new SetServicePolicyRequestMsg();
285         preq.setClassification(sc);
286         preq.setURL("urn:" + machine.toLowerCase() + ":" + process);
287         ProcessPolicy pp = new ProcessPolicy();
288         pp.setMachineName(machine.toLowerCase());
289         pp.setDomainName(domain);
290         pp.setDisplayName(displayname);
291         pp.setPolicyType(PolicyType.PROCESS);
292         pp.setAgentsEnabled(true);
293         pp.setURL("urn:" + machine.toLowerCase() + ":" + process);
294         pp.setParentObject(machineurl);
295         pp.setDataTTL(DatatypeFactory.newInstance().newDuration(datattl));
296         //30L * 24L * 60L * 60L * 1000L));
297         Utility.addStatusChangeSLA(pp);
298         if (slas != null) {
299             pp.getServiceLevelAggrements().getSLA().addAll(slas);
300         }
301         preq.setPolicy(pp);
302         return pcs.setServicePolicy(preq);
303     }
304 
305     List<TransactionLog> getTransactionLogs(String url, boolean faultsonly, boolean slaonly, int records, int offset) throws Exception {
306         GetRecentMessageLogsRequestMsg req = new GetRecentMessageLogsRequestMsg();
307         req.setClassification(sc);
308         req.setFaultsOnly(faultsonly);
309         req.setSlaViolationsOnly(slaonly);
310         req.setURL(url);
311         req.setOffset(offset);
312         req.setRecords(records);
313         GetMessageLogsResponseMsg recentMessageLogs = das.getRecentMessageLogs(req);
314         return recentMessageLogs.getLogs().getTransactionLog();
315     }
316 
317     void SetServicePolicies(List<ServicePolicy> pols) throws Exception {
318         for (int i = 0; i < pols.size(); i++) {
319             SetServicePolicyRequestMsg req = new SetServicePolicyRequestMsg();
320             req.setClassification(sc);
321             req.setURL(pols.get(i).getURL());
322             req.setPolicy(pols.get(i));
323             pcs.setServicePolicy(req);
324         }
325 
326     }
327 
328     void DeleteServicePolicies(List<String> urls) throws Exception {
329         for (int i = 0; i < urls.size(); i++) {
330             DeleteServicePolicyRequestMsg req = new DeleteServicePolicyRequestMsg();
331             req.setClassification(sc);
332             req.setURL(urls.get(i));
333             req.setDeletePerformanceData(true);
334             pcs.deleteServicePolicy(req);
335         }
336     }
337 
338     public void GetCSVExportNIC(List<String> urls, String NICID, long start, long end, String filename) throws Exception {
339         ExportCSVDataRequestMsg req = new ExportCSVDataRequestMsg();
340         req.setClassification(sc);
341         req.setAllServices(false);
342         req.setExportType(ExportRecordsEnum.MACHINE);
343         req.setRange(new TimeRange());
344         req.getRange().setStart(CreateXmlGreg(start));
345         req.getRange().setEnd(CreateXmlGreg(end));
346         req.getURLs().addAll(urls);
347         ExportDataToCSVResponseMsg exportDataToCSV = this.rs.exportDataToCSV(req);
348         WriteBytes(exportDataToCSV.getZipFile(), filename);
349     }
350     static DatatypeFactory df = null;
351 
352     private Calendar CreateXmlGreg(long time) throws DatatypeConfigurationException {
353         if (df == null) {
354             df = DatatypeFactory.newInstance();
355         }
356         GregorianCalendar gcal = new GregorianCalendar();
357         gcal.setTimeInMillis(time);
358         return (gcal);
359     }
360 
361     private void WriteBytes(byte[] zipFile, String filename) throws Exception {
362         FileOutputStream fos = new FileOutputStream(filename);
363         fos.write(zipFile);
364         fos.flush();
365         fos.close();
366     }
367 
368     String getReportClassname(String type) throws Exception {
369         GetPluginList r = new GetPluginList();
370         r.setRequest(new GetPluginListRequestMsg());
371         r.getRequest().setClassification(sc);
372         r.getRequest().setPlugintype("REPORTING");
373         try {
374             GetPluginListResponse pluginList = pcs.getPluginList(r);
375             for (int i = 0; i < pluginList.getResponse().getPlugins().size(); i++) {
376                 if (pluginList.getResponse().getPlugins().get(i).getClassname().endsWith(type)) {
377                     return type;
378                 }
379             }
380 
381         } catch (Exception ex) {
382             ex.printStackTrace();
383             throw ex;
384         }
385         throw new IllegalArgumentException(type);
386 
387     }
388 
389     void GetHtmlReportNIC(List<String> urls, String NICID, long start, long end, String filename) throws Exception {
390         ExportDataRequestMsg req = new ExportDataRequestMsg();
391         req.setClassification(sc);
392         req.setAllServices(false);
393         req.setReportTypes(new ArrayOfReportTypeContainer());
394 
395         ReportTypeContainer r = new ReportTypeContainer();
396         r.setType(getReportClassname("CpuUsageReport"));
397         req.getReportTypes().getReportTypeContainer().add(r);
398         r = new ReportTypeContainer();
399         r.setType(getReportClassname("NetworkIOReport"));
400         req.getReportTypes().getReportTypeContainer().add(r);
401 
402         r = new ReportTypeContainer();
403         r.setType(getReportClassname("MemoryUsageReport"));
404         req.getReportTypes().getReportTypeContainer().add(r);
405 
406         req.setRange(new TimeRange());
407         req.getRange().setStart(CreateXmlGreg(start));
408         req.getRange().setEnd(CreateXmlGreg(end));
409         req.getURLs().addAll(urls);
410         ExportDataToHTMLResponseMsg exportDataToCSV = this.rs.exportDataToHTML(req);
411         WriteBytes(exportDataToCSV.getZipFile(), filename);
412     }
413 }