Catalog alignment
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / fe / monitoring / FeMonitoringService.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.fe.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.common.api.Constants;
30 import org.openecomp.sdc.common.http.client.api.HttpRequest;
31 import org.openecomp.sdc.common.http.client.api.HttpResponse;
32 import org.openecomp.sdc.common.http.config.HttpClientConfig;
33 import org.openecomp.sdc.common.http.config.Timeouts;
34 import org.openecomp.sdc.common.monitoring.MonitoringEvent;
35 import org.openecomp.sdc.common.monitoring.MonitoringMetricsFetcher;
36 import org.openecomp.sdc.fe.config.Configuration;
37 import org.openecomp.sdc.fe.config.ConfigurationManager;
38 import org.openecomp.sdc.common.log.wrappers.Logger;
39
40 import javax.servlet.ServletContext;
41 import java.util.concurrent.Executors;
42 import java.util.concurrent.ScheduledExecutorService;
43 import java.util.concurrent.ThreadFactory;
44 import java.util.concurrent.TimeUnit;
45
46 public class FeMonitoringService {
47
48         private static final String URL = "%s://%s:%s/sdc2/rest/monitoring";
49     private static Logger monitoringLogger = Logger.getLogger("asdc.fe.monitoring.service");
50     private static Logger log = Logger.getLogger(FeMonitoringService.class.getName());
51         private static Gson gson = new GsonBuilder().setPrettyPrinting().create();
52
53         private class MonitoringScheduledTask implements Runnable {
54                 @Override
55                 public void run() {
56                         monitoringLogger.trace("Executing FE Monitoring Task - Start");
57                         MonitoringEvent monitoringMetrics = MonitoringMetricsFetcher.getInstance().getMonitoringMetrics();
58                         processMonitoringEvent(monitoringMetrics);
59                         monitoringLogger.trace("Executing FE Monitoring Task - Status = {}", monitoringMetrics.toString());
60                 }
61         }
62
63         /**
64          * This executor will execute the Monitoring task.
65          */
66         ScheduledExecutorService monitoringExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
67                 @Override
68                 public Thread newThread(Runnable r) {
69                         return new Thread(r, "FE-Monitoring-Thread");
70                 }
71         });
72         private ServletContext context;
73
74         public FeMonitoringService(ServletContext context) {
75                 this.context = context;
76         }
77
78         public void start(int interval) {
79                 Configuration config = ((ConfigurationManager) context.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR))
80                                 .getConfiguration();
81                 if (config.getSystemMonitoring().getEnabled()) {
82                         log.info("FE monitoring service enabled, interval is {} seconds", interval);
83                         this.monitoringExecutor.scheduleAtFixedRate(new MonitoringScheduledTask(), 0, interval, TimeUnit.SECONDS);
84                 } else {
85                         log.info("FE monitoring service is disabled");
86                 }
87         }
88
89         private void processMonitoringEvent(MonitoringEvent monitoringMetrics) {
90                 try {
91                         Configuration config = ((ConfigurationManager) context.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR))
92                                         .getConfiguration();
93                         String redirectedUrl = String.format(URL, config.getBeProtocol(), config.getBeHost(),
94                                 Constants.HTTPS.equals(config.getBeProtocol()) ? config.getBeSslPort() : config.getBeHttpPort());
95
96                         int timeout = 3000;
97                         String monitoringMetricsJson = gson.toJson(monitoringMetrics);
98                         HttpEntity myEntity = new StringEntity(monitoringMetricsJson, ContentType.APPLICATION_JSON);
99                         HttpResponse<String> resposne = HttpRequest.post(redirectedUrl, myEntity, new HttpClientConfig(new Timeouts(timeout, timeout)));
100             int beResponseStatus = resposne.getStatusCode();
101             if (beResponseStatus != HttpStatus.SC_OK) {
102                 monitoringLogger.error("Unexpected HTTP response from BE : {}", beResponseStatus);
103             }
104                 } catch (Exception e) {
105                     monitoringLogger.error("Monitoring BE failed with exception ", e);
106                 }
107         }
108 }