wrongly setting version
[sdc.git] / catalog-fe / src / main / java / org / openecomp / sdc / fe / impl / DesignerStatusBL.java
1 package org.openecomp.sdc.fe.impl;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.apache.http.client.methods.CloseableHttpResponse;
8 import org.apache.http.client.methods.HttpHead;
9 import org.apache.http.impl.client.CloseableHttpClient;
10 import org.apache.http.impl.client.HttpClients;
11 import org.openecomp.sdc.fe.config.ConfigurationManager;
12 import org.openecomp.sdc.fe.config.DesignersConfiguration;
13 import org.openecomp.sdc.fe.config.DesignersConfiguration.Designer;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 import com.google.gson.Gson;
18 import com.google.gson.GsonBuilder;
19
20 public class DesignerStatusBL {
21
22         private static Logger log = LoggerFactory.getLogger(DesignerStatusBL.class.getName());
23         private static  Gson gson = new GsonBuilder().setPrettyPrinting().create();
24         private CloseableHttpClient client = null;
25         
26         public DesignerStatusBL() {
27                 this.client = HttpClients.createDefault();
28         }
29         
30         public DesignerStatusBL(CloseableHttpClient client) {
31                 this.client = client;
32                                 
33         }
34
35         public String checkDesignerListAvailability() {
36                 String result = null;
37
38                 DesignersConfiguration designersConfiguration = ConfigurationManager.getConfigurationManager()
39                                 .getDesignersConfiguration();
40
41                 if (designersConfiguration == null || designersConfiguration.getDesignersList() == null) {
42                         log.warn("Configuration of type {} was not found", DesignersConfiguration.class);
43                 } else {
44                         log.debug("The value returned from getConfig is {}", designersConfiguration);
45
46                         List<Designer> availableDesignersList = new ArrayList<>();
47
48                         designersConfiguration.getDesignersList().forEach(value -> {
49                                 if (checkDesignerAvailability(value)) {
50                                         availableDesignersList.add(value);
51                                 }
52
53                         });
54                         result = gson.toJson(availableDesignersList);
55                 }
56                 return result;
57         }
58
59         private boolean checkDesignerAvailability(Designer designer) {
60
61                 StringBuilder requestString = new StringBuilder();
62                 boolean result = false;
63
64                 requestString.append(designer.getDesignerProtocol()).append("://").append(designer.getDesignerHost()).append(":")
65                                 .append(designer.getDesignerPort()).append(designer.getDesignerPath());
66
67                 HttpHead head = new HttpHead(requestString.toString());
68
69                 try (CloseableHttpResponse response = this.client.execute(head)) {
70                         result = response != null && response.getStatusLine().getStatusCode() == 200;
71                 } catch (IOException e) {
72                         log.debug("The designer {} is offline", designer);
73                 }
74
75                 return result;
76         }
77
78 }