2 * Copyright 2017 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.alibaba.fastjson.JSON;
\r
19 import com.alibaba.fastjson.JSONObject;
\r
20 import javax.ws.rs.client.Client;
\r
21 import javax.ws.rs.client.ClientBuilder;
\r
22 import javax.ws.rs.core.Response;
\r
23 import lombok.extern.slf4j.Slf4j;
\r
24 import org.onap.holmes.common.constant.AlarmConst;
\r
27 public class MicroServiceConfig {
\r
29 final static public String CONSUL_ADDR_SUF = ":8500/v1/catalog/service/";
\r
30 final static public String CONSUL_HOST = "CONSUL_HOST";
\r
31 final static public String HOSTNAME = "HOSTNAME";
\r
32 final static public String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";
\r
33 final static public String DOCKER_HOST = "DOCKER_HOST";
\r
34 final static public String MSB_ADDR = "MSB_ADDR";
\r
36 public static String getEnv(String name) {
\r
37 String value = System.getenv(name);
\r
38 if (value == null) {
\r
39 value = System.getProperty(name);
\r
44 public static String getConsulAddrInfo() {
\r
45 return "http://" + getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;
\r
48 public static String getServiceAddrInfoFromDcaeConsulByHostName(String hostname) {
\r
50 String queryString = getConsulAddrInfo() + hostname;
\r
51 log.info("Query the " + hostname + " address using the URL: " + queryString);
\r
53 JSONObject addrJson = (JSONObject) JSON.parseArray(execQuery(queryString)).get(0);
\r
54 if (addrJson != null && addrJson.get("ServiceAddress") != null
\r
55 && addrJson.get("ServicePort") != null) {
\r
56 ret = "http://" + addrJson.getString("ServiceAddress") + ":" + addrJson
\r
57 .getString("ServicePort");
\r
59 } catch (Exception e) {
\r
60 log.warn(e.getMessage(), e);
\r
62 log.info("The " + hostname + " address is " + ret);
\r
66 private static String execQuery(String queryString) {
\r
67 Client client = ClientBuilder.newBuilder().build();
\r
68 Response response = client.target(queryString).request().get();
\r
69 return response.readEntity(String.class);
\r
72 public static String getServiceConfigInfoFromCBS(String hostname) {
\r
74 String url = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(CONFIG_BINDING_SERVICE)) + "/service_component/" + hostname;
\r
76 ret = execQuery(url);
\r
77 } catch (Exception e) {
\r
78 log.warn(e.getMessage(), e);
\r
80 log.info("The query url is: " + url + ". The corresponding configurations are " + ret);
\r
84 public static String getMsbServerAddrWithHttpPrefix() {
\r
85 String[] addrInfo = getMsbIpAndPort();
\r
86 String ret = addrInfo[0] + ":" + addrInfo[1];
\r
87 if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)){
\r
88 ret = AlarmConst.HTTP + ret;
\r
93 public static String[] getMsbIpAndPort() {
\r
94 return split(getEnv(MSB_ADDR));
\r
97 public static String[] getMicroServiceIpAndPort() {
\r
98 String[] serviceAddrInfo = null;
\r
99 String info = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(HOSTNAME));
\r
100 log.info("Got the service information of \"" + getEnv(HOSTNAME) + "\" from Consul. The response is " + info + ".");
\r
101 if (info != null && !info.isEmpty()){
\r
102 serviceAddrInfo = split(info);
\r
104 serviceAddrInfo = split(getEnv(HOSTNAME));
\r
106 return serviceAddrInfo;
\r
109 private static String[] split(String addr) {
\r
111 String port = "80";
\r
112 if (addr.lastIndexOf(":") == -1){
\r
114 } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {
\r
115 ip = addr.substring(addr.indexOf("//") + 2); //remove the http(s):// prefix
\r
117 ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));
\r
118 port = addr.substring(addr.lastIndexOf(":") + 1);
\r
120 return new String[] {ip, port};
\r