Added oparent to sdc main
[sdc.git] / catalog-fe / src / main / java / org / openecomp / sdc / fe / impl / PluginStatusBL.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.impl;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import org.apache.http.client.config.RequestConfig;
26 import org.apache.http.client.methods.CloseableHttpResponse;
27 import org.apache.http.client.methods.HttpHead;
28 import org.apache.http.impl.client.CloseableHttpClient;
29 import org.apache.http.impl.client.HttpClients;
30 import org.openecomp.sdc.exception.InvalidArgumentException;
31 import org.openecomp.sdc.fe.config.ConfigurationManager;
32 import org.openecomp.sdc.fe.config.PluginsConfiguration;
33 import org.openecomp.sdc.fe.config.PluginsConfiguration.Plugin;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.List;
40
41 public class PluginStatusBL {
42
43         private static final Logger log = LoggerFactory.getLogger(PluginStatusBL.class.getName());
44         private final Gson gson;
45         private final CloseableHttpClient client;
46         private final PluginsConfiguration pluginsConfiguration;
47         private RequestConfig requestConfig;
48
49         public PluginStatusBL() {
50                 this.pluginsConfiguration = ConfigurationManager.getConfigurationManager().getPluginsConfiguration();
51                 this.client = HttpClients.createDefault();
52                 this.gson = new GsonBuilder().setPrettyPrinting().create();
53         }
54
55         public PluginStatusBL(CloseableHttpClient client) {
56                 this.pluginsConfiguration = ConfigurationManager.getConfigurationManager().getPluginsConfiguration();
57                 this.client = client;
58
59                 this.gson = new GsonBuilder().setPrettyPrinting().create();
60                                 
61         }
62
63         public String getPluginsList() {
64                 String result = null;
65
66                 if (pluginsConfiguration == null || pluginsConfiguration.getPluginsList() == null) {
67                         log.warn("Configuration of type {} was not found", PluginsConfiguration.class);
68                         throw new InvalidArgumentException("the plugin configuration was not read successfully.");
69
70                 } else {
71                         log.debug("The value returned from getConfig is {}", pluginsConfiguration);
72
73                         result = gson.toJson(pluginsConfiguration.getPluginsList());
74                 }
75                 return result;
76         }
77
78         public String getPluginAvailability(String pluginId) {
79                 String result = null;
80
81                 if (pluginsConfiguration == null || pluginsConfiguration.getPluginsList() == null) {
82                         log.warn("Configuration of type {} was not found", PluginsConfiguration.class);
83                         throw new InvalidArgumentException("the plugin configuration was not read successfully.");
84
85                 } else {
86                         log.debug("The value returned from getConfig is {}", pluginsConfiguration);
87                         Integer connectionTimeout = pluginsConfiguration.getConnectionTimeout();
88                         this.requestConfig = RequestConfig.custom()
89                                         .setSocketTimeout(connectionTimeout)
90                                         .setConnectTimeout(connectionTimeout)
91                                         .setConnectionRequestTimeout(connectionTimeout).build();
92
93
94                         Plugin wantedPlugin = pluginsConfiguration.getPluginsList().stream()
95                                         .filter(plugin -> plugin.getPluginId().equals(pluginId))
96                                         .findAny()
97                                         .orElse(null);
98
99                         if (wantedPlugin != null) {
100                                 result = gson.toJson(checkPluginAvailability(wantedPlugin));
101                         }
102                 }
103                 return result;
104         }
105
106         private boolean checkPluginAvailability(Plugin plugin) {
107                 boolean result = false;
108                 log.debug("sending head request to id:{} url:{}",plugin.getPluginId(),plugin.getPluginDiscoveryUrl());
109                 HttpHead head = new HttpHead(plugin.getPluginDiscoveryUrl());
110
111                 head.setConfig(this.requestConfig);
112
113                 try (CloseableHttpResponse response = this.client.execute(head)) {
114                         result = response != null && response.getStatusLine().getStatusCode() == 200;
115                         log.debug("The plugin {} is {} with result {}",plugin.getPluginId(),( result ? "online" : "offline" ), result);
116                 } catch (IOException e) {
117                         log.debug("The plugin {} is offline", plugin.getPluginId());
118                         log.debug("Exception:",e);
119                 }
120
121                 return result;
122         }
123
124 }