diff options
author | GuangrongFu <fu.guangrong@zte.com.cn> | 2018-08-31 13:57:43 +0800 |
---|---|---|
committer | GuangrongFu <fu.guangrong@zte.com.cn> | 2018-08-31 14:33:25 +0800 |
commit | c7fd788a1cfc030e54047701b1c1f27e42a91d1c (patch) | |
tree | b66ad43ad00660c284eaffc57d8fb971bec44cfe | |
parent | 67ef2af341cbe8530437f67bbffdc7721f4e5587 (diff) |
Add some logs to AAI queries
Change-Id: I20c78f7fafe23f1fdcb0161e05d19c6c8c9ec1cd
Issue-ID: HOLMES-160
Signed-off-by: GuangrongFu <fu.guangrong@zte.com.cn>
5 files changed, 92 insertions, 83 deletions
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 d22eb31..2a8d3e9 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 @@ -101,8 +101,7 @@ public class AaiQuery { private String getBaseUrl(String suffixUrl) { String url = ""; try { - String[] msbUrl = MicroServiceConfig.getMsbServerAddrWithHttpPrefix().split(":"); - url = msbUrl[0] + ":" + msbUrl[1] + suffixUrl; + url = MicroServiceConfig.getMsbServerAddrWithHttpPrefix()+ suffixUrl; } catch (Exception e) { log.info("Failed to get msb address"); } diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn.java index 0e444c1..de3cbf9 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn.java @@ -28,7 +28,9 @@ import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; + import org.glassfish.jersey.client.HttpUrlConnectorProvider; + import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; @@ -61,7 +63,7 @@ public class AaiQuery4Ccvpn { * @param status * @return the ID of the logic link */ - public String getLogicLink(String networkId, String pnfName, String ifName, String status) { + public String getLogicLink(String networkId, String pnfName, String ifName, String status) throws CorrelationException { Map<String, String> params = new HashMap<>(); params.put("networkId", networkId); params.put("pnfName", pnfName); @@ -69,12 +71,7 @@ public class AaiQuery4Ccvpn { Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_LINK_QUERY, params) + (status == null ? "" : String.format("&operational-status=%s", status))); - if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { - throw new RuntimeException("Failed to connect to AAI. Cause: " - + response.getStatusInfo().getReasonPhrase()); - } - - JSONObject linkInfo = getInfo(JSONObject.toJSONString(response.getEntity()), "p-interface", "logical-link"); + JSONObject linkInfo = getInfo(response.readEntity(String.class), "p-interface", "logical-link"); return extractValueFromJsonArray(linkInfo.getJSONArray("relationship-data"), "logical-link.link-name"); } @@ -113,20 +110,12 @@ public class AaiQuery4Ccvpn { } Response response = get(getHostAddr(), getPath(serviceInstancePath)); - if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { - throw new RuntimeException("Failed to connect to AAI. Cause: " - + response.getStatusInfo().getReasonPhrase()); - } - JSONArray instances = getInstances(JSONObject.toJSONString(response.getEntity())); + JSONArray instances = getInstances(response.readEntity(String.class)); for (int i = 0; i < instances.size(); ++i) { JSONObject instance = instances.getJSONObject(i); Response res = get(getHostAddr(), serviceInstancePath + "/service-instances?service-instance-id=" + instance.getString("service-instance-id")); - if (res.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { - throw new RuntimeException("Failed to connect to AAI. Cause: " - + response.getStatusInfo().getReasonPhrase()); - } - String inputParams = JSONObject.parseObject(response.readEntity(String.class)).getString("input-parameters"); + String inputParams = JSONObject.parseObject(res.readEntity(String.class)).getString("input-parameters"); instance.put("input-parameters", inputParams); instance.put("globalSubscriberId", params[0]); instance.put("serviceType", params[1]); @@ -144,20 +133,12 @@ public class AaiQuery4Ccvpn { params.put("networkId", networkId); params.put("pnfName", pnfName); params.put("ifName", ifName); - Response response = patch(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_TP_UPDATE, params), body); - if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { - throw new CorrelationException("Failed to connecto to AAI. Cause: " - + response.getStatusInfo().getReasonPhrase()); - } + patch(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_TP_UPDATE, params), body); } public void updateLogicLinkStatus(String linkName, Map<String, Object> body) throws CorrelationException { - Response response = patch(getHostAddr(), + patch(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_TP_UPDATE, "linkName", linkName), body); - if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { - throw new CorrelationException("Failed to connecto to AAI. Cause: " - + response.getStatusInfo().getReasonPhrase()); - } } private JSONObject getVpnBindingInfo(String networkId, String pnfName, @@ -168,30 +149,18 @@ public class AaiQuery4Ccvpn { params.put("ifName", ifName); params.put("status", status); Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_VPN_ADDR, params)); - if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { - throw new CorrelationException("Failed to connecto to AAI. Cause: " - + response.getStatusInfo().getReasonPhrase()); - } - return getInfo(JSONObject.toJSONString(response.getEntity()), "p-interface", "vpn-binding"); + return getInfo(response.readEntity(String.class), "p-interface", "vpn-binding"); } private JSONObject getConnectivityInfo(String vpnId) throws CorrelationException { Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_CONN_ADDR, "vpnId", vpnId)); - if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { - throw new CorrelationException("Failed to connect to AAI. Cause: " - + response.getStatusInfo().getReasonPhrase()); - } - return getInfo(JSONObject.toJSONString(response.getEntity()), "vpn-binding", "connectivity"); + return getInfo(response.readEntity(String.class), "vpn-binding", "connectivity"); } private JSONObject getServiceInstanceByConn(String connectivityId) throws CorrelationException { Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_SERVICE_INSTANCE_ADDR_4_CCVPN, "connectivityId", connectivityId)); - if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { - throw new CorrelationException("Failed to connect to AAI. Cause: " - + response.getStatusInfo().getReasonPhrase()); - } - return getInfo(JSONObject.toJSONString(response.getEntity()), "connectivity", "service-instance"); + return getInfo(response.readEntity(String.class), "connectivity", "service-instance"); } private JSONArray getServiceInstances(String globalCustomerId, String serviceType) throws CorrelationException { @@ -199,11 +168,7 @@ public class AaiQuery4Ccvpn { params.put("global-customer-id", globalCustomerId); params.put("service-type", serviceType); Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_SERVICE_INSTANCES_ADDR_4_CCVPN, params)); - if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { - throw new CorrelationException("Failed to connect to AAI. Cause: " - + response.getStatusInfo().getReasonPhrase()); - } - return getInstances(JSONObject.toJSONString(response.getEntity())); + return getInstances(response.readEntity(String.class)); } private String getPath(String urlTemplate, Map<String, String> pathParams) { @@ -229,17 +194,42 @@ public class AaiQuery4Ccvpn { return ret; } - private Response get(String host, String path) { + private Response get(String host, String path) throws CorrelationException { Client client = ClientBuilder.newClient(); WebTarget target = client.target(host).path(path); - return target.request().headers(getAaiHeaders()).get(); + try { + Response response = target.request().headers(getAaiHeaders()).get(); + if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { + throw new CorrelationException("Failed to connect to AAI. \nCause: " + + response.getStatusInfo().getReasonPhrase() + "\nDetails: \n" + + getErrorMsg(String.format("%s%s", host, path), null, response)); + } + return response; + } catch (CorrelationException e) { + throw e; + } catch (Exception e) { + throw new CorrelationException(e.getMessage() + "More info: " + + getErrorMsg(String.format("%s%s", host, path), null, null), e); + } } - private Response patch(String host, String path, Map<String, Object> body) { + private void patch(String host, String path, Map<String, Object> body) throws CorrelationException { Client client = ClientBuilder.newClient(); WebTarget target = client.target(host).path(path); - return target.request().headers(getAaiHeaders()).build("PATCH", Entity.json(body)) - .property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true).invoke(); + try { + Response response = target.request().headers(getAaiHeaders()).build("PATCH", Entity.json(body)) + .property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true).invoke(); + if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { + throw new CorrelationException("Failed to connecto to AAI. \nCause: " + + response.getStatusInfo().getReasonPhrase() + "\nDetails: \n" + + getErrorMsg(String.format("%s%s", host, path), body, response)); + } + } catch (CorrelationException e) { + throw e; + } catch (Exception e) { + throw new CorrelationException(e.getMessage() + "More info: " + + getErrorMsg(String.format("%s%s", host, path), body, null), e); + } } private JSONObject getInfo(String response, String pField, String field) { @@ -285,10 +275,7 @@ public class AaiQuery4Ccvpn { } private String getHostAddr() { - String[] msbInfo = MicroServiceConfig.getMsbServerAddrWithHttpPrefix().split(":"); - StringBuilder sb = new StringBuilder("http://"); - sb.append(msbInfo[0]).append(msbInfo[1]); - return sb.toString(); + return MicroServiceConfig.getMsbServerAddrWithHttpPrefix(); } private String extractValueFromJsonArray(JSONArray relationshipData, String keyName) { @@ -300,4 +287,17 @@ public class AaiQuery4Ccvpn { } return null; } + + private String getErrorMsg(String url, Map<String, Object> body, Response response) { + StringBuilder sb = new StringBuilder(); + sb.append("Rerquest URL: ").append(url).append("\n"); + sb.append("Request Header: ").append(JSONObject.toJSONString(headers)).append("\n"); + if (body != null) { + sb.append("Request Body: ").append(JSONObject.toJSONString(body)).append("\n"); + } + if (response != null) { + sb.append("Request Body: ").append(response.readEntity(String.class)); + } + return sb.toString(); + } } 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 f74d1b2..c179280 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 @@ -1,12 +1,12 @@ /**
* Copyright 2017 ZTE Corporation.
- *
+ * <p>
* 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
- *
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
* 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.
@@ -17,9 +17,11 @@ package org.onap.holmes.common.config; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
+
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;
+
import lombok.extern.slf4j.Slf4j;
import org.onap.holmes.common.constant.AlarmConst;
@@ -84,7 +86,7 @@ public class MicroServiceConfig { public static String getMsbServerAddrWithHttpPrefix() {
String[] addrInfo = getMsbIpAndPort();
String ret = addrInfo[0] + ":" + addrInfo[1];
- if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)){
+ if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)) {
ret = AlarmConst.HTTP + ret;
}
return ret;
@@ -98,7 +100,7 @@ public class MicroServiceConfig { String[] serviceAddrInfo = 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()){
+ if (info != null && !info.isEmpty()) {
serviceAddrInfo = split(info);
} else {
serviceAddrInfo = split(getEnv(HOSTNAME));
@@ -109,7 +111,7 @@ public class MicroServiceConfig { private static String[] split(String addr) {
String ip;
String port = "80";
- if (addr.lastIndexOf(":") == -1){
+ 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
@@ -117,7 +119,7 @@ public class MicroServiceConfig { ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));
port = addr.substring(addr.lastIndexOf(":") + 1);
}
- return new String[] {ip, port};
+ return new String[]{ip, port};
}
}
diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQuery4CcvpnTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQuery4CcvpnTest.java index 43cb517..51c3444 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQuery4CcvpnTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQuery4CcvpnTest.java @@ -121,11 +121,12 @@ public class AaiQuery4CcvpnTest { } @Test - public void test_getLogicLink_exception() { + public void test_getLogicLink_exception() throws CorrelationException { mockGetMethod(); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2); + EasyMock.expect(response.readEntity(String.class)).andReturn("Error!"); - thrown.expect(RuntimeException.class); + thrown.expect(CorrelationException.class); PowerMock.replayAll(); @@ -138,9 +139,9 @@ public class AaiQuery4CcvpnTest { } @Test - public void test_getLogicLink() { + public void test_getLogicLink() throws CorrelationException { mockGetMethod(); - EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("logic-link")); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("logic-link").toJSONString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); PowerMock.replayAll(); @@ -156,19 +157,21 @@ public class AaiQuery4CcvpnTest { @Test public void test_getServiceInstances_exception() { mockGetMethod(); - EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("vpn-binding")); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("vpn-binding").toJSONString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockGetMethod(); - EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("connectivity")); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("connectivity").toJSONString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockGetMethod(); - EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instance-by-connectivity")); + EasyMock.expect(response.readEntity(String.class)) + .andReturn(data.getJSONObject("service-instance-by-connectivity").toJSONString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockGetMethod(); - EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type")); + EasyMock.expect(response.readEntity(String.class)) + .andReturn(data.getJSONObject("service-instances-by-service-type").toJSONString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2); mockGetMethod(); @@ -190,19 +193,21 @@ public class AaiQuery4CcvpnTest { @Test public void test_getServiceInstances() { mockGetMethod(); - EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("vpn-binding")); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("vpn-binding").toJSONString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockGetMethod(); - EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("connectivity")); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("connectivity").toJSONString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockGetMethod(); - EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instance-by-connectivity")); + EasyMock.expect(response.readEntity(String.class)) + .andReturn(data.getJSONObject("service-instance-by-connectivity").toJSONString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockGetMethod(); - EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type")); + EasyMock.expect(response.readEntity(String.class)) + .andReturn(data.getJSONObject("service-instances-by-service-type").toJSONString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockGetMethod(); @@ -232,7 +237,8 @@ public class AaiQuery4CcvpnTest { @Test public void test_getServiceInstances_1() throws Exception { mockGetMethod(); - EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type")); + EasyMock.expect(response.readEntity(String.class)) + .andReturn(data.getJSONObject("service-instances-by-service-type").toJSONString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); PowerMock.replayAll(); @@ -250,7 +256,7 @@ public class AaiQuery4CcvpnTest { @Test public void test_getServiceInstances_1_exception() throws Exception { mockGetMethod(); - EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type")); + EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to get the service instance by type."); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2); thrown.expect(CorrelationException.class); @@ -283,6 +289,7 @@ public class AaiQuery4CcvpnTest { public void test_updateTerminalPointStatus_exception() throws CorrelationException { mockPatchMethod(); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2); + EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to update the TP information."); thrown.expect(CorrelationException.class); @@ -309,6 +316,7 @@ public class AaiQuery4CcvpnTest { public void test_updateLogicLinkStatus_exception() throws CorrelationException { mockPatchMethod(); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2); + EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to update the logic link information."); thrown.expect(CorrelationException.class); diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java index 42becaa..11dba54 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java @@ -97,7 +97,7 @@ public class AaiQueryTest { headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID); headers.put("Authorization", AaiConfig.getAuthenticationCredentials()); headers.put("Accept", "application/json"); - String url = "http://10.96.33.33/api/aai-cloudInfrastructure/v11"; + String url = "http://10.96.33.33:80/api/aai-cloudInfrastructure/v11"; HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class); CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient); @@ -136,7 +136,7 @@ public class AaiQueryTest { headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID); headers.put("Authorization", AaiConfig.getAuthenticationCredentials()); headers.put("Accept", "application/json"); - String url = "http://10.96.33.33/api/aai-cloudInfrastructure/v11"; + String url = "http://10.96.33.33:80/api/aai-cloudInfrastructure/v11"; CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient); HttpGet httpGet = new HttpGet(url); @@ -293,7 +293,7 @@ public class AaiQueryTest { PowerMock.replayAll(); String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "/url"); PowerMock.verifyAll(); - assertThat(actual, equalTo("http://10.96.33.33/url")); + assertThat(actual, equalTo("http://10.96.33.33:80/url")); } @Test |