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_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;
48 final static public Logger log = LoggerFactory.getLogger(MicroServiceConfig.class);
50 public static String getConsulAddrInfo() {
51 return "http://" + getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;
54 public static String getServiceAddrInfoFromDcaeConsulByHostName(String hostname) {
56 String queryString = getConsulAddrInfo() + hostname;
57 log.info("Query the " + hostname + " address using the URL: " + queryString);
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();
68 log.info("No service info is returned from DCAE Consul. Hostname: {}", hostname);
70 } catch (Exception e) {
71 log.warn(e.getMessage(), e);
73 log.info("The " + hostname + " address is " + ret);
77 private static String execQuery(String queryString) {
78 return JerseyClient.newInstance().get(queryString);
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);
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;
97 public static String getAaiAddr() {
98 boolean tlsEnabled = Boolean.valueOf(getEnv("ENABLE_ENCRYPT"));
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);
109 public static String[] getMsbIpAndPort() {
110 return new String[]{getEnv(MSB_IAG_SERVICE_HOST), getEnv(MSB_IAG_SERVICE_PORT)};
113 public static String[] getMicroServiceIpAndPort() {
114 String info = getEnv(POD_IP);
118 return split(getEnv(HOSTNAME));
122 private static String[] split(String addr) {
125 if (addr.lastIndexOf(":") == -1) {
127 } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {
128 ip = addr.substring(addr.indexOf("//") + 2); //remove the http(s):// prefix
130 ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));
131 port = addr.substring(addr.lastIndexOf(":") + 1);
133 return new String[]{ip, port};
136 private static String nullToEmptyString(String input) {
137 return input == null ? StringUtils.EMPTY : input;