2 * Copyright 2017-2021 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.holmes.common.config;
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;
27 import static org.onap.holmes.common.utils.CommonUtils.getEnv;
29 public class MicroServiceConfig {
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_IAG_SERVICE_HOST = "MSB_IAG_SERVICE_HOST";
37 final static public String MSB_IAG_SERVICE_PORT = "MSB_IAG_SERVICE_PORT";
38 final static public String AAI_ADDR = "AAI_ADDR";
39 final static public String NAMESPACE = "NAMESPACE";
40 final static public String PROTOCOL_HTTP = "http";
41 final static public String PROTOCOL_HTTPS = "https";
42 final static public int PLAIN_PORT = 80;
43 final static public int TLS_PORT = 443;
45 final static public Logger log = LoggerFactory.getLogger(MicroServiceConfig.class);
47 public static String getConsulAddrInfo() {
48 return "http://" + getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;
51 public static String getServiceAddrInfoFromDcaeConsulByHostName(String hostname) {
53 String queryString = getConsulAddrInfo() + hostname;
54 log.info("Query the " + hostname + " address using the URL: " + queryString);
56 JsonArray addrArray = JsonParser.parseString(execQuery(queryString)).getAsJsonArray();
57 if (addrArray.size() > 0) {
58 JsonObject addrJson = addrArray.get(0).getAsJsonObject();
59 if (addrJson != null && addrJson.get("ServiceAddress") != null
60 && addrJson.get("ServicePort") != null) {
61 ret = "http://" + addrJson.get("ServiceAddress").getAsString() + ":" + addrJson
62 .get("ServicePort").getAsString();
65 log.info("No service info is returned from DCAE Consul. Hostname: {}", hostname);
67 } catch (Exception e) {
68 log.warn(e.getMessage(), e);
70 log.info("The " + hostname + " address is " + ret);
74 private static String execQuery(String queryString) {
75 return JerseyClient.newInstance().get(queryString);
78 public static String getServiceConfigInfoFromCBS(String hostname) {
79 String url = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(CONFIG_BINDING_SERVICE)) + "/service_component/" + hostname;
80 String ret = execQuery(url);
81 log.info("The query url is: " + url + ". The corresponding configurations are " + ret);
85 public static String getMsbServerAddrWithHttpPrefix() {
86 String[] addrInfo = getMsbIpAndPort();
87 String ret = addrInfo[0] + ":" + addrInfo[1];
88 if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)) {
89 ret = AlarmConst.HTTP + ret;
94 public static String getAaiAddr() {
95 boolean tlsEnabled = Boolean.valueOf(getEnv("ENABLE_ENCRYPT"));
97 return String.format("%s://%s.%s:%d",
98 tlsEnabled ? PROTOCOL_HTTPS : PROTOCOL_HTTP,
99 nullToEmptyString(getEnv(AAI_ADDR)),
100 nullToEmptyString(getEnv(NAMESPACE)),
101 tlsEnabled ? TLS_PORT : PLAIN_PORT);
104 public static String[] getMsbIpAndPort() {
105 return new String[]{getEnv(MSB_IAG_SERVICE_HOST), getEnv(MSB_IAG_SERVICE_PORT)};
108 public static String[] getMicroServiceIpAndPort() {
109 String info = getEnv(POD_IP);
113 return split(getEnv(HOSTNAME));
117 private static String[] split(String addr) {
120 if (addr.lastIndexOf(":") == -1) {
122 } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {
123 ip = addr.substring(addr.indexOf("//") + 2); //remove the http(s):// prefix
125 ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));
126 port = addr.substring(addr.lastIndexOf(":") + 1);
128 return new String[]{ip, port};
131 private static String nullToEmptyString(String input) {
132 return input == null ? StringUtils.EMPTY : input;