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 javax.ws.rs.client.Client;
\r
19 import javax.ws.rs.client.ClientBuilder;
\r
20 import javax.ws.rs.core.Response;
\r
21 import lombok.extern.slf4j.Slf4j;
\r
22 import net.sf.json.JSONArray;
\r
23 import net.sf.json.JSONObject;
\r
24 import org.glassfish.jersey.client.ClientConfig;
\r
25 import org.onap.holmes.common.constant.AlarmConst;
\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 CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";
\r
34 final static public String DOCKER_HOST = "DOCKER_HOST";
\r
35 final static public String MSB_ADDR = "MSB_ADDR";
\r
37 private static String getEnv(String name) {
\r
38 String value = System.getenv(name);
\r
39 if (value == null) {
\r
40 value = System.getProperty(name);
\r
45 public static String getConsulAddrInfo() {
\r
46 return getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;
\r
49 public static String getConfigBindingServiceAddrInfo() {
\r
51 String queryString = getConsulAddrInfo() + CONFIG_BINDING_SERVICE;
\r
52 log.info("Query the CBS address using the URL: " + queryString);
\r
54 JSONObject addrJson = (JSONObject) JSONArray.fromObject(execQuery(queryString)).get(0);
\r
55 if (addrJson.has("ServiceAddress") && addrJson.has("ServicePort")) {
\r
56 ret = addrJson.getString("ServiceAddress") + ":" + addrJson.getString("ServicePort");
\r
58 } catch (Exception e) {
\r
59 log.warn(e.getMessage(), e);
\r
61 log.info("The CBS address is " + ret);
\r
65 private static String execQuery(String queryString) {
\r
66 Client client = ClientBuilder.newClient(new ClientConfig());
\r
67 Response response = client.target(queryString).request().get();
\r
68 return response.readEntity(String.class);
\r
71 public static String getServiceAddrInfoFromCBS(String serviceName) {
\r
73 String url = getConfigBindingServiceAddrInfo() + "/service_component/" +serviceName;
\r
75 JSONObject jsonObject = JSONObject.fromObject(execQuery(url));
\r
76 log.info("The origin configurations (" + url + ") returned by DCAE is: " + jsonObject.toString());
\r
77 if (jsonObject.has(serviceName)) {
\r
78 ret = (String) jsonObject.getJSONArray(serviceName).get(0);
\r
80 } catch (Exception e) {
\r
81 log.warn(e.getMessage(), e);
\r
86 public static String getMsbServerAddr() {
\r
87 String[] addrInfo = getMsbAddrInfo();
\r
88 String ret = addrInfo[0] + ":" + addrInfo[1];
\r
89 if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)){
\r
90 ret = AlarmConst.HTTP + ret;
\r
95 public static String[] getMsbAddrInfo() {
\r
96 String[] msbServerInfo = null;
\r
98 //String info = getServiceAddrInfoFromCBS(MSB_ADDR);
\r
99 String info = getServiceAddrInfoFromCBS(getEnv(HOSTNAME));
\r
100 log.info("Got the service information of \"" + getEnv(HOSTNAME) + "\" from CBS. The response is " + info + ".");
\r
103 JSONObject infoObj = JSONObject.fromObject(info);
\r
104 String msbInfoTmp = infoObj.has("msb.hostname") ? infoObj.getString("msb.hostname") : null;
\r
105 if (msbInfoTmp != null) {
\r
106 msbServerInfo = split(msbInfoTmp);
\r
108 msbServerInfo = split(getEnv(MSB_ADDR));
\r
111 msbServerInfo = split(getEnv(MSB_ADDR));
\r
114 return msbServerInfo;
\r
117 public static String[] getServiceAddrInfo() {
\r
118 String[] serviceAddrInfo = null;
\r
119 String info = getServiceAddrInfoFromCBS(getEnv(HOSTNAME));
\r
120 log.info("Got the service information of \"" + getEnv(HOSTNAME) + "\" from CBS. The response is " + info + ".");
\r
122 serviceAddrInfo = split(info);
\r
124 serviceAddrInfo = split(getEnv(HOSTNAME));
\r
126 return serviceAddrInfo;
\r
129 private static String[] split(String addr) {
\r
131 String port = "80";
\r
132 if (addr.lastIndexOf(":") == -1){
\r
134 } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {
\r
135 ip = addr.substring(addr.indexOf("//") + 2); //remove the http(s):// prefix
\r
137 ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));
\r
138 port = addr.substring(addr.lastIndexOf(":") + 1);
\r
140 return new String[] {ip, port};
\r