From 9d2cccba20f32fe54e4ccf1f433f68b5cee95bd5 Mon Sep 17 00:00:00 2001 From: Guangrong Fu Date: Sat, 16 Sep 2017 14:48:57 +0800 Subject: Get the MSB info from DCAE Consul Change-Id: I67206b028abefd99ff4b249a254882f08a642a4d Issue-ID: HOLMES-45 Signed-off-by: Guangrong Fu --- .../org/onap/holmes/common/api/stat/VesAlarm.java | 2 +- .../holmes/common/config/MicroServiceConfig.java | 103 ++++++++++++++++++--- .../onap/holmes/common/constant/AlarmConst.java | 2 + .../onap/holmes/common/dmaap/entity/VesAlarm.java | 29 ------ .../onap/holmes/common/utils/MSBRegisterUtil.java | 40 +------- 5 files changed, 99 insertions(+), 77 deletions(-) delete mode 100644 holmes-actions/src/main/java/org/onap/holmes/common/dmaap/entity/VesAlarm.java (limited to 'holmes-actions/src/main/java/org/onap') diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/VesAlarm.java b/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/VesAlarm.java index 3cdcb88..127903f 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/VesAlarm.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/VesAlarm.java @@ -27,7 +27,7 @@ import lombok.Setter; public class VesAlarm implements Cloneable, Serializable{ private String domain; private String eventId; - private String EventName; + private String eventName; private String eventType; private Object internalHeaderFields; private Long lastEpochMicrosec; diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java b/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java index 8f9c9e8..bb78288 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java @@ -15,11 +15,27 @@ */ package org.onap.holmes.common.config; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.core.Response; +import lombok.extern.slf4j.Slf4j; +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; +import org.glassfish.jersey.client.ClientConfig; +import org.onap.holmes.common.api.stat.Alarm; import org.onap.holmes.common.constant.AlarmConst; +@Slf4j public class MicroServiceConfig { - private static String getProperty(String name) { + final static public String CONSUL_ADDR_SUF = ":8500/v1/catalog/service/"; + final static public String CONSUL_HOST = "CONSUL_HOST"; + final static public String HOSTNAME = "HOSTNAME"; + final static public String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE"; + final static public String DOCKER_HOST = "DOCKER_HOST"; + final static public String MSB_ADDR = "MSB_ADDR"; + + private static String getEnv(String name) { String value = System.getenv(name); if (value == null) { value = System.getProperty(name); @@ -27,24 +43,89 @@ public class MicroServiceConfig { return value; } - public static String getMsbServerAddr() { - return AlarmConst.HTTP + getProperty("MSB_ADDR"); + public static String getConsulAddrInfo() { + return getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF; + } + + public static String getConfigBindingServiceAddrInfo() { + String ret = null; + String queryString = getConsulAddrInfo() + CONFIG_BINDING_SERVICE; + try { + JSONObject addrJson = (JSONObject) JSONArray.fromObject(execQuery(queryString)).get(0); + if (addrJson.has("ServiceAddress") && addrJson.has("ServicePort")) { + ret = addrJson.getString("ServiceAddress") + ":" + addrJson.getString("ServicePort"); + } + } catch (Exception e) { + log.warn(e.getMessage(), e); + } + return ret; } - public static String getMsbServerIp() { - return getProperty("MSB_ADDR"); + private static String execQuery(String queryString) { + Client client = ClientBuilder.newClient(new ClientConfig()); + Response response = client.target(queryString).request().get(); + return response.readEntity(String.class); } - public static int getMsbServerPort() { + public static String getServiceAddrInfoFromCBS(String serviceName) { + String ret = null; + String url = getConfigBindingServiceAddrInfo() + "/service_component/" +serviceName; try { - return Integer.valueOf(getProperty("MSB_PORT")); - } catch (NumberFormatException e) { - return 80; + JSONObject jsonObject = JSONObject.fromObject(execQuery(url)); + if (jsonObject.has(serviceName)) { + ret = (String) jsonObject.getJSONArray(serviceName).get(0); + } + } catch (Exception e) { + log.warn(e.getMessage(), e); + } + return ret; + } + + public static String getMsbServerAddr() { + String[] addrInfo = getMsbAddrInfo(); + String ret = addrInfo[0] + ":" + addrInfo[1]; + if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)){ + ret = AlarmConst.HTTP + ret; } + return ret; } - public static String getServiceIp() { - return getProperty("SERVICE_IP"); + public static String[] getMsbAddrInfo() { + String[] msbServerInfo = null; + + String info = getServiceAddrInfoFromCBS(MSB_ADDR); + if (info != null){ + msbServerInfo = split(info); + } else { + msbServerInfo = split(getEnv(MSB_ADDR)); + } + + return msbServerInfo; + } + + public static String[] getServiceAddrInfo() { + String[] serviceAddrInfo = null; + String info = getServiceAddrInfoFromCBS(getEnv(HOSTNAME)); + if (info != null){ + serviceAddrInfo = split(info); + } else { + serviceAddrInfo = split(getEnv(HOSTNAME)); + } + return serviceAddrInfo; + } + + private static String[] split(String addr) { + String ip; + String port = "80"; + if (addr.lastIndexOf(":") == -1){ + ip = addr; + } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) { + ip = addr.substring(addr.indexOf("//") + 2); //remove the http(s):// prefix + } else { + ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":")); + port = addr.substring(addr.lastIndexOf(":") + 1); + } + return new String[] {ip, port}; } } diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/constant/AlarmConst.java b/holmes-actions/src/main/java/org/onap/holmes/common/constant/AlarmConst.java index 690cde6..ba3d80e 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/constant/AlarmConst.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/constant/AlarmConst.java @@ -38,4 +38,6 @@ public interface AlarmConst { int MICRO_SERVICE_PORT = 8086; String HTTP = "http://"; + + String HTTPS = "https://"; } diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/entity/VesAlarm.java b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/entity/VesAlarm.java deleted file mode 100644 index b49fac2..0000000 --- a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/entity/VesAlarm.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2017 ZTE Corporation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onap.holmes.common.dmaap.entity; - -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -public class VesAlarm { - private String eventId; - private String sourceId; - private String specificProblem; - private long alarmRaisedTime; -} diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/MSBRegisterUtil.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/MSBRegisterUtil.java index 8052f78..1cd481e 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/utils/MSBRegisterUtil.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/MSBRegisterUtil.java @@ -35,44 +35,12 @@ import org.onap.msb.sdk.httpclient.msb.MSBServiceClient; @Service public class MSBRegisterUtil { - public void register(ServiceRegisterEntity entity) throws IOException { - log.info("Start register Holmes Service to MSB..."); - boolean flag = false; - int retry = 0; - while (!flag && retry < 20) { - log.info("Holmes Service Registration. Retry: " + retry); - retry++; - flag = innerRegister(entity); - if (!flag) { - log.warn("Failed to register the service to MSB. Sleep 30s and try again."); - threadSleep(30000); - } else { - log.info("Registration succeeded!"); - break; - } - } - log.info("Service registration completed."); - } - - private boolean innerRegister(ServiceRegisterEntity entity) { - try { - log.info("msbServerAddr:" + MicroServiceConfig.getMsbServerAddr()); - log.info("entity:" + entity); - MicroserviceBusRest resourceserviceproxy = ConsumerFactory.createConsumer( - MicroServiceConfig.getMsbServerAddr(), MicroserviceBusRest.class); - resourceserviceproxy.registerServce("false", entity); - } catch (Exception error) { - log.error("Micro-service registration failed!" + error.getMessage(), error); - return false; - } - return true; - } - public void register2Msb(MicroServiceInfo msinfo) throws CorrelationException { - MSBServiceClient msbClient = new MSBServiceClient(MicroServiceConfig.getMsbServerIp(), - MicroServiceConfig.getMsbServerPort()); + String[] msbAddrInfo = MicroServiceConfig.getMsbAddrInfo(); + MSBServiceClient msbClient = new MSBServiceClient(msbAddrInfo[0], + Integer.parseInt(msbAddrInfo[1])); - log.info("Start register Holmes Service to MSB..."); + log.info("Start to register Holmes Service to MSB..."); MicroServiceFullInfo microServiceFullInfo = null; int retry = 0; while (null == microServiceFullInfo && retry < 20) { -- cgit 1.2.3-korg