summaryrefslogtreecommitdiffstats
path: root/holmes-actions/src/main/java
diff options
context:
space:
mode:
authorGuangrongFu <fu.guangrong@zte.com.cn>2021-06-08 18:48:01 +0800
committerGuangrongFu <fu.guangrong@zte.com.cn>2021-06-08 18:48:01 +0800
commitbb55dbd5674c5a5165592387de13a2a211f90ec9 (patch)
tree581d3fc0f8fd00521661552e25364c7362a3cee3 /holmes-actions/src/main/java
parentbe5ca2b68e80688677d75e5f688199511745570b (diff)
Refactored AaiJsonParserUtil
Change-Id: I7c5458e945d860ef75e5defd803600f56ec718e9 Issue-ID: HOLMES-453 Signed-off-by: GuangrongFu <fu.guangrong@zte.com.cn>
Diffstat (limited to 'holmes-actions/src/main/java')
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiJsonParserUtil.java50
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn2.java52
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/utils/JerseyClient.java21
3 files changed, 42 insertions, 81 deletions
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiJsonParserUtil.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiJsonParserUtil.java
index 8bc006a..ba66628 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiJsonParserUtil.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiJsonParserUtil.java
@@ -20,7 +20,6 @@
package org.onap.holmes.common.aai;
-import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
@@ -28,15 +27,9 @@ import lombok.extern.slf4j.Slf4j;
import org.jvnet.hk2.annotations.Service;
import org.onap.holmes.common.aai.config.AaiConfig;
import org.onap.holmes.common.config.MicroServiceConfig;
-import org.onap.holmes.common.exception.CorrelationException;
-import javax.ws.rs.client.Client;
-import javax.ws.rs.client.ClientBuilder;
-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 java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -60,25 +53,6 @@ public class AaiJsonParserUtil {
return ret;
}
- public static Response get(String host, String path) throws CorrelationException {
- Client client = ClientBuilder.newClient();
- WebTarget target = client.target(host).path(path);
- 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);
- }
- }
-
public static JsonObject getInfo(String response, String field) {
JsonObject jObject = JsonParser.parseString(response).getAsJsonObject();
JsonObject relationshipList = extractJsonObject(jObject, "relationship-list");
@@ -111,28 +85,4 @@ public class AaiJsonParserUtil {
public static String getHostAddr() {
return MicroServiceConfig.getMsbServerAddrWithHttpPrefix();
}
-
- public static String getErrorMsg(String url, Map<String, Object> body, Response response) {
- Gson gson = new Gson();
- StringBuilder sb = new StringBuilder();
- sb.append("Rerquest URL: ").append(url).append("\n");
- sb.append("Request Header: ").append(gson.toJson(getAaiHeaders())).append("\n");
- if (body != null) {
- sb.append("Request Body: ").append(gson.toJson(body)).append("\n");
- }
- if (response != null) {
- sb.append("Request Body: ").append(response.readEntity(String.class));
- }
- return sb.toString();
- }
-
- public static MultivaluedMap getAaiHeaders() {
- MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
- headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
- headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
- headers.add("Authorization", AaiConfig.getAuthenticationCredentials());
- headers.add("Accept", "application/json");
- headers.add("Content-Type", "application/json");
- return headers;
- }
}
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn2.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn2.java
index cf39910..41eb2c0 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn2.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn2.java
@@ -27,10 +27,10 @@ import lombok.extern.slf4j.Slf4j;
import org.jvnet.hk2.annotations.Service;
import org.onap.holmes.common.aai.config.AaiConfig;
import org.onap.holmes.common.exception.CorrelationException;
+import org.onap.holmes.common.utils.JerseyClient;
-import javax.ws.rs.core.MultivaluedHashMap;
-import javax.ws.rs.core.MultivaluedMap;
-import javax.ws.rs.core.Response;
+import java.util.HashMap;
+import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -40,25 +40,27 @@ import static org.onap.holmes.common.aai.AaiJsonParserUtil.*;
@Slf4j
public class AaiQuery4Ccvpn2 {
- private MultivaluedMap<String, Object> headers;
+ private Map<String, Object> headers;
static public AaiQuery4Ccvpn2 newInstance() {
return new AaiQuery4Ccvpn2();
}
private AaiQuery4Ccvpn2() {
- headers = new MultivaluedHashMap<>();
- headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
- headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
- headers.add("Authorization", AaiConfig.getAuthenticationCredentials());
- headers.add("Accept", "application/json");
- headers.add("Content-Type", "application/json");
+ headers = new HashMap<>();
+ headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
+ headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
+ headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
+ headers.put("Accept", "application/json");
+ headers.put("Content-Type", "application/json");
}
- private String getSiteVNFId(String siteService) throws CorrelationException {
- Response response = get(getHostAddr(), AaiConfig.MsbConsts.AAI_SITE_RESOURCES_QUERY);
- String resStr = response.readEntity(String.class);
- JsonObject resObj = JsonParser.parseString(resStr).getAsJsonObject();
+ private String getSiteVNFId(String siteService) {
+ String response = JerseyClient.newInstance()
+ .headers(headers)
+ .path(AaiConfig.MsbConsts.AAI_SITE_RESOURCES_QUERY)
+ .get(getHostAddr());
+ JsonObject resObj = JsonParser.parseString(response).getAsJsonObject();
JsonArray siteResources = extractJsonArray(resObj, "site-resource");
if (siteResources != null) {
for (int i = 0; i < siteResources.size(); i++) {
@@ -81,27 +83,31 @@ public class AaiQuery4Ccvpn2 {
return null;
}
- private JsonObject getServiceInstanceByVnfId(String vnfId) throws CorrelationException {
- Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_SITE_VNF_QUERY,
- "vnfId", vnfId));
- String resStr = response.readEntity(String.class);
+ private JsonObject getServiceInstanceByVnfId(String vnfId) {
+ String resStr = JerseyClient.newInstance()
+ .headers(headers)
+ .path(getPath(AaiConfig.MsbConsts.AAI_SITE_VNF_QUERY,
+ "vnfId", vnfId))
+ .get(getHostAddr());
return getInfo(resStr, "service-instance");
}
- public JsonObject getSiteServiceInstance(String siteService) throws CorrelationException {
+ public JsonObject getSiteServiceInstance(String siteService) {
String vnfId = getSiteVNFId(siteService);
if (vnfId == null) {
return null;
}
JsonObject serviceInstanceInfo = getServiceInstanceByVnfId(vnfId);
String serviceInstancePath = serviceInstanceInfo.get("related-link").getAsString();
- Response response = get(getHostAddr(), getPath(serviceInstancePath));
- String res = response.readEntity(String.class);
+ String res = JerseyClient.newInstance()
+ .headers(headers)
+ .path(getPath(serviceInstancePath))
+ .get(getHostAddr());
JsonObject instance = JsonParser.parseString(res).getAsJsonObject();
String[] params = new String[2];
Pattern pattern = Pattern.compile("/aai/v\\d+/business/customers/customer/(.+)" +
- "/service-subscriptions/service-subscription/(.+)" +
- "/service-instances/service-instance/(.+)");
+ "/service-subscriptions/service-subscription/(.+)" +
+ "/service-instances/service-instance/(.+)");
Matcher matcher = pattern.matcher(serviceInstancePath);
if (matcher.find()) {
params[0] = matcher.group(1);
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/JerseyClient.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/JerseyClient.java
index cd87ebe..871ffad 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/utils/JerseyClient.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/JerseyClient.java
@@ -122,7 +122,7 @@ public class JerseyClient {
Response response = builder.get();
- if (isSuccessful(response)) {
+ if (isSuccessful(response, url)) {
return response2Target(response, clazz);
}
@@ -147,7 +147,7 @@ public class JerseyClient {
Response response = builder.post(entity);
- if (isSuccessful(response)) {
+ if (isSuccessful(response, url)) {
return response2Target(response, clazz);
}
@@ -167,7 +167,7 @@ public class JerseyClient {
Response response = builder.put(entity);
- if (isSuccessful(response)) {
+ if (isSuccessful(response, url)) {
return response2Target(response, clazz);
}
@@ -187,17 +187,22 @@ public class JerseyClient {
Response response = builder.delete();
- if (isSuccessful(response)) {
+ if (isSuccessful(response, url)) {
return response2Target(response, clazz);
}
return null;
}
- private boolean isSuccessful(Response response) {
- if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
- throw new HttpException(response.getStatus(), String.format("Failed to get response from the server. Info: %s",
- response.readEntity(String.class)));
+ private boolean isSuccessful(Response response, String url) {
+ Response.StatusType statusInfo = response.getStatusInfo();
+ if (statusInfo.getFamily() != Response.Status.Family.SUCCESSFUL) {
+ throw new HttpException(statusInfo.getStatusCode(),
+ String.format("Failed to get response from the server. " +
+ "\nURL: %s\nCause: %s\nResponse body: %s",
+ url,
+ statusInfo.getReasonPhrase(),
+ response.readEntity(String.class)));
}
return true;
}