fixing warnings from checkstyle in common-app-api
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / be / monitoring / BeMonitoringService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.monitoring;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import org.apache.http.HttpEntity;
26 import org.apache.http.HttpStatus;
27 import org.apache.http.entity.ContentType;
28 import org.apache.http.entity.StringEntity;
29 import org.openecomp.sdc.be.config.Configuration;
30 import org.openecomp.sdc.be.config.ConfigurationManager;
31 import org.openecomp.sdc.common.api.Constants;
32 import org.openecomp.sdc.common.http.client.api.HttpRequest;
33 import org.openecomp.sdc.common.http.client.api.HttpResponse;
34 import org.openecomp.sdc.common.http.config.HttpClientConfig;
35 import org.openecomp.sdc.common.http.config.Timeouts;
36 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
37 import org.openecomp.sdc.common.log.wrappers.Logger;
38 import org.openecomp.sdc.common.monitoring.MonitoringEvent;
39 import org.openecomp.sdc.common.monitoring.MonitoringMetricsFetcher;
40
41 import javax.servlet.ServletContext;
42 import java.util.concurrent.Executors;
43 import java.util.concurrent.ScheduledExecutorService;
44 import java.util.concurrent.ThreadFactory;
45 import java.util.concurrent.TimeUnit;
46
47 public class BeMonitoringService {
48
49     private static final String URL = "%s://%s:%s/sdc2/rest/monitoring";
50     private static Logger monitoringLogger = Logger.getLogger("asdc.be.monitoring.service");
51     private static Logger log = Logger.getLogger(BeMonitoringService.class.getName());
52     private static Gson gson = new GsonBuilder().setPrettyPrinting().create();
53
54     private class MonitoringScheduledTask implements Runnable {
55         @Override
56         public void run() {
57             monitoringLogger.trace("Executing BE Monitoring Task - Start");
58             MonitoringEvent monitoringMetrics = MonitoringMetricsFetcher.getInstance().getMonitoringMetrics();
59             processMonitoringEvent(monitoringMetrics);
60             monitoringLogger.trace("Executing BE Monitoring Task - Status = {}", monitoringMetrics.toString());
61         }
62     }
63
64     /**
65      * This executor will execute the Monitoring task.
66      */
67     private ScheduledExecutorService monitoringExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
68         @Override
69         public Thread newThread(Runnable r) {
70             return new Thread(r, "BE-Monitoring-Thread");
71         }
72     });
73     private ServletContext context;
74
75     public BeMonitoringService(ServletContext context) {
76         this.context = context;
77     }
78
79     public void start(int interval) {
80         Configuration config = ((ConfigurationManager) context.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR))
81                 .getConfiguration();
82         if (config.getSystemMonitoring().getEnabled()) {
83             log.info("BE monitoring service is enabled, interval is {} seconds", interval);
84             this.monitoringExecutor.scheduleAtFixedRate(new MonitoringScheduledTask(), 0, interval, TimeUnit.SECONDS);
85         } else {
86             log.info("BE monitoring service is disabled");
87         }
88     }
89
90     private void processMonitoringEvent(MonitoringEvent monitoringMetrics) {
91         try {
92             Configuration config = ((ConfigurationManager) context.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR))
93                     .getConfiguration();
94             String redirectedUrl = String.format(URL, config.getBeProtocol(), config.getBeFqdn(),
95                     config.getBeHttpPort());
96
97             final int timeout = 3000;
98             String monitoringMetricsJson = gson.toJson(monitoringMetrics);
99             HttpEntity myEntity = new StringEntity(monitoringMetricsJson, ContentType.APPLICATION_JSON);
100             HttpResponse<String> httpResponse = HttpRequest.post(redirectedUrl, myEntity, new HttpClientConfig(new Timeouts(timeout, timeout)));
101             int beResponseStatus = httpResponse.getStatusCode();
102             if (beResponseStatus != HttpStatus.SC_OK) {
103                 monitoringLogger.error(EcompLoggerErrorCode.UNKNOWN_ERROR, "", "", "Unexpected HTTP response from BE : {}", beResponseStatus);
104             }
105         } catch (Exception e) {
106             monitoringLogger.error(EcompLoggerErrorCode.UNKNOWN_ERROR, "", "", "Monitoring BE failed with exception ", e);
107         }
108     }
109 }