Change the value of AAI env var to AAI_ADDR
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / config / MicroServiceConfig.java
1 /**
2  * Copyright  2017-2021 ZTE Corporation.
3  * <p>
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.holmes.common.config;
17
18 import com.google.gson.JsonArray;
19 import com.google.gson.JsonObject;
20 import com.google.gson.JsonParser;
21 import org.apache.commons.lang3.StringUtils;
22 import org.onap.holmes.common.constant.AlarmConst;
23 import org.onap.holmes.common.utils.JerseyClient;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import static org.onap.holmes.common.utils.CommonUtils.getEnv;
28
29 public class MicroServiceConfig {
30
31     final static public String CONSUL_ADDR_SUF = ":8500/v1/catalog/service/";
32     final static public String CONSUL_HOST = "CONSUL_HOST";
33     final static public String HOSTNAME = "HOSTNAME";
34     final static public String POD_IP = "POD_IP";
35     final static public String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";
36     final static public String MSB_ADDR = "MSB_ADDR";
37     final static public String MSB_IAG_SERVICE_HOST = "MSB_IAG_SERVICE_HOST";
38     final static public String MSB_IAG_SERVICE_PORT = "MSB_IAG_SERVICE_PORT";
39     final static public String BASE_URL = "BASE_URL";
40     final static public String PRE_ADDR = "PRE_ADDR";
41     final static public String POST_ADDR = "POST_ADDR";
42     final static public String AAI_ADDR = "AAI_ADDR";
43     final static public String PROTOCOL_HTTP = "http";
44     final static public String PROTOCOL_HTTPS = "https";
45     final static public int PLAIN_PORT = 80;
46     final static public int TLS_PORT = 443;
47
48     final static public Logger log = LoggerFactory.getLogger(MicroServiceConfig.class);
49
50     public static String getConsulAddrInfo() {
51         return "http://" + getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;
52     }
53
54     public static String getServiceAddrInfoFromDcaeConsulByHostName(String hostname) {
55         String ret = null;
56         String queryString = getConsulAddrInfo() + hostname;
57         log.info("Query the " + hostname + " address using the URL: " + queryString);
58         try {
59             JsonArray addrArray = JsonParser.parseString(execQuery(queryString)).getAsJsonArray();
60             if (addrArray.size() > 0) {
61                 JsonObject addrJson = addrArray.get(0).getAsJsonObject();
62                 if (addrJson != null && addrJson.get("ServiceAddress") != null
63                         && addrJson.get("ServicePort") != null) {
64                     ret = "http://" + addrJson.get("ServiceAddress").getAsString() + ":" + addrJson
65                             .get("ServicePort").getAsString();
66                 }
67             } else {
68                 log.info("No service info is returned from DCAE Consul. Hostname: {}", hostname);
69             }
70         } catch (Exception e) {
71             log.warn(e.getMessage(), e);
72         }
73         log.info("The " + hostname + " address is " + ret);
74         return ret;
75     }
76
77     private static String execQuery(String queryString) {
78         return JerseyClient.newInstance().get(queryString);
79     }
80
81     public static String getServiceConfigInfoFromCBS(String hostname) {
82         String url = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(CONFIG_BINDING_SERVICE)) + "/service_component/" + hostname;
83         String ret = execQuery(url);
84         log.info("The query url is: " + url + ". The corresponding configurations are " + ret);
85         return ret;
86     }
87
88     public static String getMsbServerAddrWithHttpPrefix() {
89         String[] addrInfo = getMsbIpAndPort();
90         String ret = addrInfo[0] + ":" + addrInfo[1];
91         if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)) {
92             ret = AlarmConst.HTTP + ret;
93         }
94         return ret;
95     }
96
97     public static String getAaiAddr() {
98         boolean tlsEnabled = Boolean.valueOf(getEnv("ENABLE_ENCRYPT"));
99
100         return String.format("%s://%s%s%s.%s:%d",
101                 tlsEnabled ? PROTOCOL_HTTPS : PROTOCOL_HTTP,
102                 nullToEmptyString(getEnv(PRE_ADDR)),
103                 nullToEmptyString(getEnv(AAI_ADDR)),
104                 nullToEmptyString(getEnv(POST_ADDR)),
105                 nullToEmptyString(getEnv(BASE_URL)),
106                 tlsEnabled ? TLS_PORT : PLAIN_PORT);
107     }
108
109     public static String[] getMsbIpAndPort() {
110         return new String[]{getEnv(MSB_IAG_SERVICE_HOST), getEnv(MSB_IAG_SERVICE_PORT)};
111     }
112
113     public static String[] getMicroServiceIpAndPort() {
114         String info = getEnv(POD_IP);
115         if (info != null) {
116             return split(info);
117         } else {
118             return split(getEnv(HOSTNAME));
119         }
120     }
121
122     private static String[] split(String addr) {
123         String ip;
124         String port = "80";
125         if (addr.lastIndexOf(":") == -1) {
126             ip = addr;
127         } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {
128             ip = addr.substring(addr.indexOf("//") + 2);    //remove the http(s):// prefix
129         } else {
130             ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));
131             port = addr.substring(addr.lastIndexOf(":") + 1);
132         }
133         return new String[]{ip, port};
134     }
135
136     private static String nullToEmptyString(String input) {
137         return input == null ? StringUtils.EMPTY : input;
138     }
139
140 }