2 * Copyright 2017-2020 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.JsonObject;
\r
19 import com.google.gson.JsonParser;
\r
20 import org.onap.holmes.common.constant.AlarmConst;
\r
21 import org.slf4j.Logger;
\r
22 import org.slf4j.LoggerFactory;
\r
24 import javax.ws.rs.client.Client;
\r
25 import javax.ws.rs.client.ClientBuilder;
\r
26 import javax.ws.rs.core.Response;
\r
27 import java.util.regex.Pattern;
\r
29 public class MicroServiceConfig {
\r
31 final static public String CONSUL_ADDR_SUF = ":8500/v1/catalog/service/";
\r
32 final static public String CONSUL_HOST = "CONSUL_HOST";
\r
33 final static public String HOSTNAME = "HOSTNAME";
\r
34 final static public String POD_IP = "POD_IP";
\r
35 final static public String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";
\r
36 final static public String DOCKER_HOST = "DOCKER_HOST";
\r
37 final static public String MSB_ADDR = "MSB_ADDR";
\r
38 final static public Pattern IP_REG = Pattern.compile("(http(s)?://)?(\\d+\\.\\d+\\.\\d+\\.\\d+)(:(\\d+))?");
\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 getEnv(String name) {
\r
44 String value = System.getenv(name);
\r
45 if (value == null) {
\r
46 value = System.getProperty(name);
\r
51 public static String getConsulAddrInfo() {
\r
52 return "http://" + getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;
\r
55 public static String getServiceAddrInfoFromDcaeConsulByHostName(String hostname) {
\r
57 String queryString = getConsulAddrInfo() + hostname;
\r
58 log.info("Query the " + hostname + " address using the URL: " + queryString);
\r
60 JsonObject addrJson = JsonParser.parseString(execQuery(queryString))
\r
64 if (addrJson != null && addrJson.get("ServiceAddress") != null
\r
65 && addrJson.get("ServicePort") != null) {
\r
66 ret = "http://" + addrJson.get("ServiceAddress").getAsString() + ":" + addrJson
\r
67 .get("ServicePort").getAsString();
\r
69 } catch (Exception e) {
\r
70 log.warn(e.getMessage(), e);
\r
72 log.info("The " + hostname + " address is " + ret);
\r
76 private static String execQuery(String queryString) {
\r
77 Client client = ClientBuilder.newBuilder().build();
\r
78 Response response = client.target(queryString).request().get();
\r
79 return response.readEntity(String.class);
\r
82 public static String getServiceConfigInfoFromCBS(String hostname) {
\r
84 String url = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(CONFIG_BINDING_SERVICE)) + "/service_component/" + hostname;
\r
86 ret = execQuery(url);
\r
87 } catch (Exception e) {
\r
88 log.warn(e.getMessage(), e);
\r
90 log.info("The query url is: " + url + ". The corresponding configurations are " + ret);
\r
94 public static String getMsbServerAddrWithHttpPrefix() {
\r
95 String[] addrInfo = getMsbIpAndPort();
\r
96 String ret = addrInfo[0] + ":" + addrInfo[1];
\r
97 if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)) {
\r
98 ret = AlarmConst.HTTP + ret;
\r
103 public static String getAaiAddr() {
\r
104 return AlarmConst.HTTPS + AAI_HOSTNAME + ":8443";
\r
107 public static String[] getMsbIpAndPort() {
\r
108 return split(getEnv(MSB_ADDR));
\r
111 public static String[] getMicroServiceIpAndPort() {
\r
112 String[] serviceAddrInfo = null;
\r
113 String info = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(HOSTNAME));
\r
114 log.info("Got the service information of \"" + getEnv(HOSTNAME) + "\" from Consul. The response is " + info + ".");
\r
116 if (info != null && !info.isEmpty()) {
\r
117 if (!isIpAddress(info)) {
\r
118 info = getEnv(POD_IP);
\r
120 serviceAddrInfo = split(info);
\r
122 serviceAddrInfo = split(getEnv(HOSTNAME));
\r
124 return serviceAddrInfo;
\r
127 private static boolean isIpAddress(String info) {
\r
128 return IP_REG.matcher(info).matches();
\r
131 private static String[] split(String addr) {
\r
133 String port = "80";
\r
134 if (addr.lastIndexOf(":") == -1) {
\r
136 } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {
\r
137 ip = addr.substring(addr.indexOf("//") + 2); //remove the http(s):// prefix
\r
139 ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));
\r
140 port = addr.substring(addr.lastIndexOf(":") + 1);
\r
142 return new String[]{ip, port};
\r