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.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;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.List;
41 public class PluginStatusBL {
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;
49 public PluginStatusBL() {
50 this.pluginsConfiguration = ConfigurationManager.getConfigurationManager().getPluginsConfiguration();
51 this.client = HttpClients.createDefault();
52 this.gson = new GsonBuilder().setPrettyPrinting().create();
55 public PluginStatusBL(CloseableHttpClient client) {
56 this.pluginsConfiguration = ConfigurationManager.getConfigurationManager().getPluginsConfiguration();
59 this.gson = new GsonBuilder().setPrettyPrinting().create();
63 public String getPluginsList() {
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.");
71 log.debug("The value returned from getConfig is {}", pluginsConfiguration);
73 result = gson.toJson(pluginsConfiguration.getPluginsList());
78 public String getPluginAvailability(String pluginId) {
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.");
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();
94 Plugin wantedPlugin = pluginsConfiguration.getPluginsList().stream()
95 .filter(plugin -> plugin.getPluginId().equals(pluginId))
99 if (wantedPlugin != null) {
100 result = gson.toJson(checkPluginAvailability(wantedPlugin));
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());
111 head.setConfig(this.requestConfig);
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);