2 * Copyright 2017-2021 ZTE Corporation.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
16 package org.onap.holmes.common.config;
\r
18 import com.google.gson.JsonArray;
\r
19 import com.google.gson.JsonObject;
\r
20 import com.google.gson.JsonParser;
\r
21 import org.onap.holmes.common.constant.AlarmConst;
\r
22 import org.onap.holmes.common.utils.JerseyClient;
\r
23 import org.slf4j.Logger;
\r
24 import org.slf4j.LoggerFactory;
\r
26 import static org.onap.holmes.common.utils.CommonUtils.getEnv;
\r
28 public class MicroServiceConfig {
\r
30 final static public String CONSUL_ADDR_SUF = ":8500/v1/catalog/service/";
\r
31 final static public String CONSUL_HOST = "CONSUL_HOST";
\r
32 final static public String HOSTNAME = "HOSTNAME";
\r
33 final static public String POD_IP = "POD_IP";
\r
34 final static public String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";
\r
35 final static public String MSB_ADDR = "MSB_ADDR";
\r
36 final static public String MSB_IAG_SERVICE_HOST = "MSB_IAG_SERVICE_HOST";
\r
37 final static public String MSB_IAG_SERVICE_PORT = "MSB_IAG_SERVICE_PORT";
\r
39 final static public String AAI_HOSTNAME = "aai.onap";
\r
41 final static public Logger log = LoggerFactory.getLogger(MicroServiceConfig.class);
\r
43 public static String getConsulAddrInfo() {
\r
44 return "http://" + getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;
\r
47 public static String getServiceAddrInfoFromDcaeConsulByHostName(String hostname) {
\r
49 String queryString = getConsulAddrInfo() + hostname;
\r
50 log.info("Query the " + hostname + " address using the URL: " + queryString);
\r
52 JsonArray addrArray = JsonParser.parseString(execQuery(queryString)).getAsJsonArray();
\r
53 if (addrArray.size() > 0) {
\r
54 JsonObject addrJson = addrArray.get(0).getAsJsonObject();
\r
55 if (addrJson != null && addrJson.get("ServiceAddress") != null
\r
56 && addrJson.get("ServicePort") != null) {
\r
57 ret = "http://" + addrJson.get("ServiceAddress").getAsString() + ":" + addrJson
\r
58 .get("ServicePort").getAsString();
\r
61 log.info("No service info is returned from DCAE Consul. Hostname: {}", hostname);
\r
63 } catch (Exception e) {
\r
64 log.warn(e.getMessage(), e);
\r
66 log.info("The " + hostname + " address is " + ret);
\r
70 private static String execQuery(String queryString) {
\r
71 return JerseyClient.newInstance().get(queryString);
\r
74 public static String getServiceConfigInfoFromCBS(String hostname) {
\r
75 String url = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(CONFIG_BINDING_SERVICE)) + "/service_component/" + hostname;
\r
76 String ret = execQuery(url);
\r
77 log.info("The query url is: " + url + ". The corresponding configurations are " + ret);
\r
81 public static String getMsbServerAddrWithHttpPrefix() {
\r
82 String[] addrInfo = getMsbIpAndPort();
\r
83 String ret = addrInfo[0] + ":" + addrInfo[1];
\r
84 if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)) {
\r
85 ret = AlarmConst.HTTP + ret;
\r
90 public static String getAaiAddr() {
\r
91 return AlarmConst.HTTPS + AAI_HOSTNAME + ":8443";
\r
94 public static String[] getMsbIpAndPort() {
\r
95 return new String[]{getEnv(MSB_IAG_SERVICE_HOST), getEnv(MSB_IAG_SERVICE_PORT)};
\r
98 public static String[] getMicroServiceIpAndPort() {
\r
99 String info = getEnv(POD_IP);
\r
100 if (info != null) {
\r
101 return split(info);
\r
103 return split(getEnv(HOSTNAME));
\r
107 private static String[] split(String addr) {
\r
109 String port = "80";
\r
110 if (addr.lastIndexOf(":") == -1) {
\r
112 } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {
\r
113 ip = addr.substring(addr.indexOf("//") + 2); //remove the http(s):// prefix
\r
115 ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));
\r
116 port = addr.substring(addr.lastIndexOf(":") + 1);
\r
118 return new String[]{ip, port};
\r