From 1be6bdd6b55219294a9ce6f708433fef3737cc13 Mon Sep 17 00:00:00 2001
From: Guangrong Fu <fu.guangrong@zte.com.cn>
Date: Fri, 3 Nov 2017 14:17:20 +0800
Subject: Modify the Service Addr Query Logic

Change-Id: I9e97a301bc5ba85e366bddbfda2f0ad721470c5f
Issue-ID: HOLMES-81
Signed-off-by: Guangrong Fu <fu.guangrong@zte.com.cn>
---
 .../java/org/onap/holmes/common/aai/AaiQuery.java  |  6 +--
 .../holmes/common/config/MicroServiceConfig.java   | 57 +++++++---------------
 .../holmes/common/dcae/DcaeConfigurationQuery.java |  3 +-
 .../onap/holmes/common/utils/MSBRegisterUtil.java  |  6 +--
 4 files changed, 23 insertions(+), 49 deletions(-)

(limited to 'holmes-actions/src/main/java/org/onap')

diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java
index cee6236..b80c40e 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java
@@ -95,17 +95,17 @@ public class AaiQuery {
     private String getBaseUrl(String suffixUrl) {
         String url = "";
         try {
-            String[] msbUrl = MicroServiceConfig.getMsbServerAddr().split(":");
+            String[] msbUrl = MicroServiceConfig.getMsbServerAddrWithHttpPrefix().split(":");
             url = msbUrl[0] + ":" + msbUrl[1] + suffixUrl;
         } catch (Exception e) {
             log.info("Failed to get msb address");
         }
         if ("".equals(url)) {
             try {
-                url = "https://" + MicroServiceConfig.getServiceAddrInfoFromCBS("aai_config")
+                url = "https://" + MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config").replace("http://", "")
                         + suffixUrl;
             } catch (Exception e) {
-                log.info("Failed to get aai address");
+                log.info("Failed to get the address of A&AI.", e);
             }
         }
         return url;
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 d86f56c..2be8824 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
@@ -43,22 +43,22 @@ public class MicroServiceConfig {
     }
 
     public static String getConsulAddrInfo() {
-        return getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;
+        return "http://" + getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;
     }
 
-    public static String getConfigBindingServiceAddrInfo() {
+    public static String getServiceAddrInfoFromDcaeConsulByHostName(String hostname) {
         String ret = null;
-        String queryString = getConsulAddrInfo() + CONFIG_BINDING_SERVICE;
-        log.info("Query the CBS address using the URL: " + queryString);
+        String queryString = getConsulAddrInfo() + hostname;
+        log.info("Query the " + hostname + " address using the URL: " + queryString);
         try {
             JSONObject addrJson = (JSONObject) JSONArray.fromObject(execQuery(queryString)).get(0);
             if (addrJson.has("ServiceAddress") && addrJson.has("ServicePort")) {
-                ret = addrJson.getString("ServiceAddress") + ":" + addrJson.getString("ServicePort");
+                ret = "http://" + addrJson.getString("ServiceAddress") + ":" + addrJson.getString("ServicePort");
             }
         } catch (Exception e) {
             log.warn(e.getMessage(), e);
         }
-        log.info("The CBS address is " + ret);
+        log.info("The " + hostname + " address is " + ret);
         return ret;
     }
 
@@ -68,23 +68,20 @@ public class MicroServiceConfig {
         return response.readEntity(String.class);
     }
 
-    public static String getServiceAddrInfoFromCBS(String serviceName) {
+    public static String getServiceConfigInfoFromCBS(String hostname) {
         String ret = null;
-        String url = getConfigBindingServiceAddrInfo() + "/service_component/" +serviceName;
+        String url = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(CONFIG_BINDING_SERVICE)) + "/service_component/" + hostname;
         try {
-            JSONObject jsonObject = JSONObject.fromObject(execQuery(url));
-            log.info("The origin configurations (" + url + ") returned by DCAE is: " + jsonObject.toString());
-            if (jsonObject.has(serviceName)) {
-                ret = (String) jsonObject.getJSONArray(serviceName).get(0);
-            }
+            ret = execQuery(url);
         } catch (Exception e) {
             log.warn(e.getMessage(), e);
         }
+        log.info("The query url is: " + url + ". The corresponding configurations are " + ret);
         return ret;
     }
 
-    public static String getMsbServerAddr() {
-        String[] addrInfo = getMsbAddrInfo();
+    public static String getMsbServerAddrWithHttpPrefix() {
+        String[] addrInfo = getMsbIpAndPort();
         String ret = addrInfo[0] + ":" + addrInfo[1];
         if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)){
             ret = AlarmConst.HTTP + ret;
@@ -92,33 +89,15 @@ public class MicroServiceConfig {
         return ret;
     }
 
-    public static String[] getMsbAddrInfo() {
-        String[] msbServerInfo = null;
-
-        //String info = getServiceAddrInfoFromCBS(MSB_ADDR);
-        String info = getServiceAddrInfoFromCBS(getEnv(HOSTNAME));
-        log.info("Got the service information of \"" + getEnv(HOSTNAME) + "\" from CBS. The response is " + info + ".");
-
-        if (info != null){
-            JSONObject infoObj = JSONObject.fromObject(info);
-            String msbInfoTmp = infoObj.has("msb.hostname") ? infoObj.getString("msb.hostname") : null;
-            if (msbInfoTmp != null) {
-                msbServerInfo = split(msbInfoTmp);
-            } else {
-                msbServerInfo = split(getEnv(MSB_ADDR));
-            }
-        } else {
-            msbServerInfo = split(getEnv(MSB_ADDR));
-        }
-
-        return msbServerInfo;
+    public static String[] getMsbIpAndPort() {
+        return split(getEnv(MSB_ADDR));
     }
 
-    public static String[] getServiceAddrInfo() {
+    public static String[] getMicroServiceIpAndPort() {
         String[] serviceAddrInfo = null;
-        String info = getServiceAddrInfoFromCBS(getEnv(HOSTNAME));
-        log.info("Got the service information of \"" + getEnv(HOSTNAME) + "\" from CBS. The response is " + info + ".");
-        if (info != null){
+        String info = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(HOSTNAME));
+        log.info("Got the service information of \"" + getEnv(HOSTNAME) + "\" from Consul. The response is " + info + ".");
+        if (info != null && !info.isEmpty()){
             serviceAddrInfo = split(info);
         } else {
             serviceAddrInfo = split(getEnv(HOSTNAME));
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dcae/DcaeConfigurationQuery.java b/holmes-actions/src/main/java/org/onap/holmes/common/dcae/DcaeConfigurationQuery.java
index 897f689..a0bb943 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/dcae/DcaeConfigurationQuery.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/dcae/DcaeConfigurationQuery.java
@@ -19,7 +19,6 @@ import javax.ws.rs.client.Client;
 import javax.ws.rs.client.ClientBuilder;
 import javax.ws.rs.client.WebTarget;
 import org.glassfish.jersey.client.ClientConfig;
-import org.jvnet.hk2.annotations.Service;
 import org.onap.holmes.common.config.MicroServiceConfig;
 import org.onap.holmes.common.dcae.entity.DcaeConfigurations;
 import org.onap.holmes.common.dcae.utils.DcaeConfigurationParser;
@@ -29,7 +28,7 @@ public class DcaeConfigurationQuery {
 
     public static DcaeConfigurations getDcaeConfigurations(String hostname)
             throws CorrelationException {
-        String serviceAddrInfo = MicroServiceConfig.getServiceAddrInfoFromCBS(hostname);
+        String serviceAddrInfo = MicroServiceConfig.getServiceConfigInfoFromCBS(hostname);
         String response;
         try {
             response = getDcaeResponse(serviceAddrInfo);
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 1cd481e..877a824 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
@@ -18,14 +18,10 @@ package org.onap.holmes.common.utils;
 
 import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;
 
-import com.eclipsesource.jaxrs.consumer.ConsumerFactory;
-import java.io.IOException;
 import lombok.extern.slf4j.Slf4j;
 import org.jvnet.hk2.annotations.Service;
-import org.onap.holmes.common.api.entity.ServiceRegisterEntity;
 import org.onap.holmes.common.config.MicroServiceConfig;
 import org.onap.holmes.common.exception.CorrelationException;
-import org.onap.holmes.common.msb.MicroserviceBusRest;
 import org.onap.msb.sdk.discovery.common.RouteException;
 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
@@ -36,7 +32,7 @@ import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
 public class MSBRegisterUtil {
 
     public void register2Msb(MicroServiceInfo msinfo) throws CorrelationException {
-        String[] msbAddrInfo = MicroServiceConfig.getMsbAddrInfo();
+        String[] msbAddrInfo = MicroServiceConfig.getMsbIpAndPort();
         MSBServiceClient msbClient = new MSBServiceClient(msbAddrInfo[0],
                 Integer.parseInt(msbAddrInfo[1]));
 
-- 
cgit