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.api.stat.Alarm;
\r
26 import org.onap.holmes.common.constant.AlarmConst;
\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 CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";
\r
35 final static public String DOCKER_HOST = "DOCKER_HOST";
\r
36 final static public String MSB_ADDR = "MSB_ADDR";
\r
38 private static String getEnv(String name) {
\r
39 String value = System.getenv(name);
\r
40 if (value == null) {
\r
41 value = System.getProperty(name);
\r
46 public static String getConsulAddrInfo() {
\r
47 return getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;
\r
50 public static String getConfigBindingServiceAddrInfo() {
\r
52 String queryString = getConsulAddrInfo() + CONFIG_BINDING_SERVICE;
\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
64 private static String execQuery(String queryString) {
\r
65 Client client = ClientBuilder.newClient(new ClientConfig());
\r
66 Response response = client.target(queryString).request().get();
\r
67 return response.readEntity(String.class);
\r
70 public static String getServiceAddrInfoFromCBS(String serviceName) {
\r
72 String url = getConfigBindingServiceAddrInfo() + "/service_component/" +serviceName;
\r
74 JSONObject jsonObject = JSONObject.fromObject(execQuery(url));
\r
75 if (jsonObject.has(serviceName)) {
\r
76 ret = (String) jsonObject.getJSONArray(serviceName).get(0);
\r
78 } catch (Exception e) {
\r
79 log.warn(e.getMessage(), e);
\r
84 public static String getMsbServerAddr() {
\r
85 String[] addrInfo = getMsbAddrInfo();
\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[] getMsbAddrInfo() {
\r
94 String[] msbServerInfo = null;
\r
96 String info = getServiceAddrInfoFromCBS(MSB_ADDR);
\r
98 msbServerInfo = split(info);
\r
100 msbServerInfo = split(getEnv(MSB_ADDR));
\r
103 return msbServerInfo;
\r
106 public static String[] getServiceAddrInfo() {
\r
107 String[] serviceAddrInfo = null;
\r
108 String info = getServiceAddrInfoFromCBS(getEnv(HOSTNAME));
\r
110 serviceAddrInfo = split(info);
\r
112 serviceAddrInfo = split(getEnv(HOSTNAME));
\r
114 return serviceAddrInfo;
\r
117 private static String[] split(String addr) {
\r
119 String port = "80";
\r
120 if (addr.lastIndexOf(":") == -1){
\r
122 } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {
\r
123 ip = addr.substring(addr.indexOf("//") + 2); //remove the http(s):// prefix
\r
125 ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));
\r
126 port = addr.substring(addr.lastIndexOf(":") + 1);
\r
128 return new String[] {ip, port};
\r