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