2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.fe.impl;
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;
37 import java.io.IOException;
39 public class PluginStatusBL {
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;
47 public PluginStatusBL() {
48 this.pluginsConfiguration = ConfigurationManager.getConfigurationManager().getPluginsConfiguration();
49 this.client = HttpClients.createDefault();
50 this.gson = new GsonBuilder().setPrettyPrinting().create();
53 public PluginStatusBL(CloseableHttpClient client) {
54 this.pluginsConfiguration = ConfigurationManager.getConfigurationManager().getPluginsConfiguration();
57 this.gson = new GsonBuilder().setPrettyPrinting().create();
61 public String getPluginsList() {
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.");
69 log.debug("The value returned from getConfig is {}", pluginsConfiguration);
71 result = gson.toJson(pluginsConfiguration.getPluginsList());
76 public String getPluginAvailability(String pluginId) {
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.");
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();
92 Plugin wantedPlugin = pluginsConfiguration.getPluginsList().stream()
93 .filter(plugin -> plugin.getPluginId().equals(pluginId))
97 if (wantedPlugin != null) {
98 result = gson.toJson(checkPluginAvailability(wantedPlugin));
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());
109 head.setConfig(this.requestConfig);
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);