diff options
20 files changed, 524 insertions, 374 deletions
diff --git a/holmes-actions/pom.xml b/holmes-actions/pom.xml index ebec435..9a516b6 100644 --- a/holmes-actions/pom.xml +++ b/holmes-actions/pom.xml @@ -85,6 +85,10 @@ <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
+ <exclusion>
+ <groupId>org.yaml</groupId>
+ <artifactId>snakeyaml</artifactId>
+ </exclusion>
</exclusions>
</dependency>
<dependency>
@@ -145,6 +149,16 @@ <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
+ <exclusions>
+ <exclusion>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
@@ -157,14 +171,13 @@ <version>2.8.2</version>
</dependency>
<dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.2.49</version>
- </dependency>
- <dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
+ <dependency>
+ <groupId>com.google.code.gson</groupId>
+ <artifactId>gson</artifactId>
+ </dependency>
</dependencies>
<build>
<resources>
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 e6cd207..8bc006a 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * org.onap.holmes.common.aai * ================================================================================ - * Copyright (C) 2018-2019 Huawei. All rights reserved. + * Copyright (C) 2018-2020 Huawei. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,8 +20,10 @@ package org.onap.holmes.common.aai; -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import lombok.extern.slf4j.Slf4j; import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.aai.config.AaiConfig; @@ -77,14 +79,14 @@ public class AaiJsonParserUtil { } } - public static JSONObject getInfo(String response, String field) { - JSONObject jObject = JSONObject.parseObject(response); - JSONObject relationshipList = extractJsonObject(jObject, "relationship-list"); - JSONArray relationShip = extractJsonArray(relationshipList, "relationship"); + public static JsonObject getInfo(String response, String field) { + JsonObject jObject = JsonParser.parseString(response).getAsJsonObject(); + JsonObject relationshipList = extractJsonObject(jObject, "relationship-list"); + JsonArray relationShip = extractJsonArray(relationshipList, "relationship"); if (relationShip != null) { for (int i = 0; i < relationShip.size(); ++i) { - final JSONObject object = relationShip.getJSONObject(i); - if (object.getString("related-to").equals(field)) { + final JsonObject object = relationShip.get(i).getAsJsonObject(); + if (object.get("related-to").getAsString().equals(field)) { return object; } } @@ -92,16 +94,16 @@ public class AaiJsonParserUtil { return null; } - public static JSONObject extractJsonObject(JSONObject obj, String key) { - if (obj != null && key != null && obj.containsKey(key)) { - return obj.getJSONObject(key); + public static JsonObject extractJsonObject(JsonObject obj, String key) { + if (obj != null && key != null && obj.has(key)) { + return obj.get(key).getAsJsonObject(); } return null; } - public static JSONArray extractJsonArray(JSONObject obj, String key) { - if (obj != null && key != null && obj.containsKey(key)) { - return obj.getJSONArray(key); + public static JsonArray extractJsonArray(JsonObject obj, String key) { + if (obj != null && key != null && obj.has(key)) { + return obj.get(key).getAsJsonArray(); } return null; } @@ -111,11 +113,12 @@ public class AaiJsonParserUtil { } 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(JSONObject.toJSONString(getAaiHeaders())).append("\n"); + sb.append("Request Header: ").append(gson.toJson(getAaiHeaders())).append("\n"); if (body != null) { - sb.append("Request Body: ").append(JSONObject.toJSONString(body)).append("\n"); + sb.append("Request Body: ").append(gson.toJson(body)).append("\n"); } if (response != null) { sb.append("Request Body: ").append(response.readEntity(String.class)); 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 b11cafc..f409c27 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 @@ -13,12 +13,6 @@ */ package org.onap.holmes.common.aai; -import java.io.IOException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import javax.inject.Inject; - import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; @@ -30,6 +24,12 @@ import org.onap.holmes.common.aai.entity.VnfEntity; import org.onap.holmes.common.exception.CorrelationException; import org.onap.holmes.common.utils.HttpsUtils; +import javax.inject.Inject; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + @Service @Slf4j public class AaiQuery { @@ -94,30 +94,6 @@ public class AaiQuery { return "https://aai.onap:8443" + suffixUrl; } - private String getMsbSuffixAddr(String suffixUrl) { - if (suffixUrl.length() <= 0) { - return ""; - } - String[] addrSplits = suffixUrl.substring(1).split("/"); - String[] conv = addrSplits[2].split("-"); - addrSplits[2] = conv[0]; - if (conv.length > 1) { - for (int i = 1; i < conv.length; i++) { - addrSplits[2] = addrSplits[2] + conv[i].substring(0, 1).toUpperCase() + conv[i] - .substring(1); - } - } - String ret = addrSplits[1]; - addrSplits[1] = addrSplits[0] + "-" + addrSplits[2]; - addrSplits[2] = ret; - addrSplits[0] = "api"; - StringBuilder stringBuffer = new StringBuilder(); - for (String split : addrSplits) { - stringBuffer.append("/" + split); - } - return stringBuffer.toString(); - } - private String getResponse(String url) throws CorrelationException { String response; CloseableHttpClient httpClient = null; 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 4cf2aba..9feba18 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 @@ -1,5 +1,5 @@ /** - * Copyright 2018 ZTE Corporation. + * Copyright 2018-2020 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 @@ -14,13 +14,16 @@ package org.onap.holmes.common.aai; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import org.glassfish.jersey.client.HttpUrlConnectorProvider; import org.onap.holmes.common.aai.config.AaiConfig; import org.onap.holmes.common.config.MicroServiceConfig; import org.onap.holmes.common.exception.CorrelationException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; @@ -34,11 +37,10 @@ import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; -import lombok.extern.slf4j.Slf4j; - -@Slf4j public class AaiQuery4Ccvpn { + private final Logger log = LoggerFactory.getLogger(AaiQuery4Ccvpn.class); + private MultivaluedMap<String, Object> headers; public static AaiQuery4Ccvpn newInstance() { @@ -47,7 +49,7 @@ public class AaiQuery4Ccvpn { private static final String EMPTY_STR = ""; - private static final JSONObject EMPTY_JSON = new JSONObject(); + private static final JsonObject EMPTY_JSON = new JsonObject(); private AaiQuery4Ccvpn() { headers = new MultivaluedHashMap<>(); @@ -77,13 +79,13 @@ public class AaiQuery4Ccvpn { Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_LINK_QUERY, params) + (status == null ? "" : String.format("&operational-status=%s", status))); - JSONObject linkInfo = getInfo(response.readEntity(String.class), "p-interface", "logical-link"); + JsonObject linkInfo = getInfo(response.readEntity(String.class), "p-interface", "logical-link"); if (linkInfo == null) { log.warn(String.format("Link information is missing from AAI. Method: [getLogicLink], " + "params: [networkId - %s, pnfName - %s, ifName - %s].", networkId, pnfName, ifName)); return EMPTY_STR; } - return extractValueFromJsonArray(linkInfo.getJSONArray("relationship-data"), "logical-link.link-name"); + return extractValueFromJsonArray(linkInfo.get("relationship-data").getAsJsonArray(), "logical-link.link-name"); } /** @@ -99,37 +101,37 @@ public class AaiQuery4Ccvpn { * @param status * @return service instances in JSONObject format */ - public JSONObject getServiceInstance(String networkId, String pnfName, String ifName, String status) { + public JsonObject getServiceInstance(String networkId, String pnfName, String ifName, String status) { try { - JSONObject vpnBindingInfo = getVpnBindingInfo(networkId, pnfName, ifName, status); + JsonObject vpnBindingInfo = getVpnBindingInfo(networkId, pnfName, ifName, status); if (vpnBindingInfo == null) { log.warn(String.format("VPN binding information is missing from AAI. " + "Method: [getServiceInstance], params: [networkId - %s, pnfName - %s, " + "ifName - %s, status - %s].", networkId, pnfName, ifName, status)); return EMPTY_JSON; } - String vpnBindingId = extractValueFromJsonArray(vpnBindingInfo.getJSONArray("relationship-data"), + String vpnBindingId = extractValueFromJsonArray(vpnBindingInfo.get("relationship-data").getAsJsonArray(), "vpn-binding.vpn-id"); - JSONObject connectivityInfo = getConnectivityInfo(vpnBindingId); + JsonObject connectivityInfo = getConnectivityInfo(vpnBindingId); if (connectivityInfo == null) { log.warn(String.format("Connectivity information is missing from AAI. " + "Method: [getServiceInstance], params: [networkId - %s, pnfName - %s, " + "ifName - %s, status - %s].", networkId, pnfName, ifName, status)); return EMPTY_JSON; } - String connectivityId = extractValueFromJsonArray(connectivityInfo.getJSONArray("relationship-data"), + String connectivityId = extractValueFromJsonArray(connectivityInfo.get("relationship-data").getAsJsonArray(), "connectivity.connectivity-id"); - JSONObject serviceInstanceInfo = getServiceInstanceByConn(connectivityId); + JsonObject serviceInstanceInfo = getServiceInstanceByConn(connectivityId); if (serviceInstanceInfo == null) { log.warn(String.format("Service instance information is missing from AAI. " + "Method: [getServiceInstance], params: [networkId - %s, pnfName - %s, " + "ifName - %s, status - %s].", networkId, pnfName, ifName, status)); return EMPTY_JSON; } - String serviceInstancePath = serviceInstanceInfo.getString("related-link"); + String serviceInstancePath = serviceInstanceInfo.get("related-link").getAsString(); Response response = get(getHostAddr(), getPath(serviceInstancePath)); - JSONObject instance = JSON.parseObject(response.readEntity(String.class)); + JsonObject instance = JsonParser.parseString(response.readEntity(String.class)).getAsJsonObject(); String[] params = new String[2]; Pattern pattern = Pattern.compile("/aai/v\\d+/business/customers/customer/(.+)" + @@ -140,8 +142,8 @@ public class AaiQuery4Ccvpn { params[0] = matcher.group(1); params[1] = matcher.group(2); } - instance.put("globalSubscriberId", params[0]); - instance.put("serviceType", params[1]); + instance.addProperty("globalSubscriberId", params[0]); + instance.addProperty("serviceType", params[1]); return instance; } catch (CorrelationException e) { throw new RuntimeException(e.getMessage(), e); @@ -155,7 +157,7 @@ public class AaiQuery4Ccvpn { params.put("pnfName", pnfName); params.put("ifName", ifName); Response r = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_TP_UPDATE, params)); - JSONObject jsonObject = JSONObject.parseObject(r.readEntity(String.class)); + JsonObject jsonObject = JsonParser.parseString(r.readEntity(String.class)).getAsJsonObject(); body.put("resource-version", jsonObject.get("resource-version").toString()); put(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_TP_UPDATE, params), body); @@ -163,13 +165,13 @@ public class AaiQuery4Ccvpn { public void updateLogicLinkStatus(String linkName, Map<String, Object> body) throws CorrelationException { Response r = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_LINK_UPDATE, "linkName", linkName)); - JSONObject jsonObject = JSONObject.parseObject(r.readEntity(String.class)); + JsonObject jsonObject = JsonParser.parseString(r.readEntity(String.class)).getAsJsonObject(); body.put("resource-version", jsonObject.get("resource-version").toString()); body.put("link-type", jsonObject.get("link-type").toString()); put(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_LINK_UPDATE, "linkName", linkName), body); } - private JSONObject getVpnBindingInfo(String networkId, String pnfName, + private JsonObject getVpnBindingInfo(String networkId, String pnfName, String ifName, String status) throws CorrelationException { Map<String, String> params = new HashMap(); params.put("networkId", networkId); @@ -180,23 +182,23 @@ public class AaiQuery4Ccvpn { return getInfo(response.readEntity(String.class), "p-interface", "vpn-binding"); } - private JSONObject getConnectivityInfo(String vpnId) throws CorrelationException { + private JsonObject getConnectivityInfo(String vpnId) throws CorrelationException { Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_CONN_ADDR, "vpnId", vpnId)); return getInfo(response.readEntity(String.class), "vpn-binding", "connectivity"); } - private JSONObject getServiceInstanceByConn(String connectivityId) throws CorrelationException { + private JsonObject getServiceInstanceByConn(String connectivityId) throws CorrelationException { Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_SERVICE_INSTANCE_ADDR_4_CCVPN, "connectivityId", connectivityId)); return getInfo(response.readEntity(String.class), "connectivity", "service-instance"); } - private JSONObject getServiceInstance(String globalCustomerId, String serviceType) throws CorrelationException { + private JsonObject getServiceInstance(String globalCustomerId, String serviceType) throws CorrelationException { Map<String, String> params = new HashMap(); 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)); - return JSON.parseObject(response.readEntity(String.class)); + return JsonParser.parseString(response.readEntity(String.class)).getAsJsonObject(); } private String getPath(String urlTemplate, Map<String, String> pathParams) { @@ -260,18 +262,18 @@ public class AaiQuery4Ccvpn { } } - private JSONObject getInfo(String response, String pField, String field) { - JSONObject jObject = JSONObject.parseObject(response); - JSONObject pInterface = extractJsonObject(jObject, pField); + private JsonObject getInfo(String response, String pField, String field) { + JsonObject jObject = JsonParser.parseString(response).getAsJsonObject(); + JsonObject pInterface = extractJsonObject(jObject, pField); if (pInterface == null) { pInterface = jObject; } - JSONObject relationshipList = extractJsonObject(pInterface, "relationship-list"); - JSONArray relationShip = extractJsonArray(relationshipList, "relationship"); + JsonObject relationshipList = extractJsonObject(pInterface, "relationship-list"); + JsonArray relationShip = extractJsonArray(relationshipList, "relationship"); if (relationShip != null) { for (int i = 0; i < relationShip.size(); ++i) { - final JSONObject object = relationShip.getJSONObject(i); - if (object.getString("related-to").equals(field)) { + final JsonObject object = relationShip.get(i).getAsJsonObject(); + if (object.get("related-to").getAsString().equals(field)) { return object; } } @@ -279,16 +281,16 @@ public class AaiQuery4Ccvpn { return null; } - private JSONObject extractJsonObject(JSONObject obj, String key) { - if (obj != null && key != null && obj.containsKey(key)) { - return obj.getJSONObject(key); + private JsonObject extractJsonObject(JsonObject obj, String key) { + if (obj != null && key != null && obj.has(key)) { + return obj.get(key).getAsJsonObject(); } return null; } - private JSONArray extractJsonArray(JSONObject obj, String key) { - if (obj != null && key != null && obj.containsKey(key)) { - return obj.getJSONArray(key); + private JsonArray extractJsonArray(JsonObject obj, String key) { + if (obj != null && key != null && obj.has(key)) { + return obj.get(key).getAsJsonArray(); } return null; } @@ -301,12 +303,12 @@ public class AaiQuery4Ccvpn { return MicroServiceConfig.getMsbServerAddrWithHttpPrefix(); } - private String extractValueFromJsonArray(JSONArray relationshipData, String keyName) { + private String extractValueFromJsonArray(JsonArray relationshipData, String keyName) { if (relationshipData != null) { for (int i = 0; i < relationshipData.size(); ++i) { - JSONObject item = relationshipData.getJSONObject(i); - if (item.getString("relationship-key").equals(keyName)) { - return item.getString("relationship-value"); + JsonObject item = relationshipData.get(i).getAsJsonObject(); + if (item.get("relationship-key").getAsString().equals(keyName)) { + return item.get("relationship-value").getAsString(); } } } @@ -314,11 +316,12 @@ public class AaiQuery4Ccvpn { } private 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(JSONObject.toJSONString(headers)).append("\n"); + sb.append("Request Header: ").append(gson.toJson(headers)).append("\n"); if (body != null) { - sb.append("Request Body: ").append(JSONObject.toJSONString(body)).append("\n"); + sb.append("Request Body: ").append(gson.toJson(body)).append("\n"); } if (response != null) { sb.append("Request Body: ").append(response.readEntity(String.class)); 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 aff064b..cf39910 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * org.onap.holmes.common.aai * ================================================================================ - * Copyright (C) 2018-2019 Huawei. All rights reserved. + * Copyright (C) 2018-2020 Huawei. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,9 +20,9 @@ package org.onap.holmes.common.aai; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import lombok.extern.slf4j.Slf4j; import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.aai.config.AaiConfig; @@ -34,11 +34,7 @@ import javax.ws.rs.core.Response; import java.util.regex.Matcher; import java.util.regex.Pattern; -import static org.onap.holmes.common.aai.AaiJsonParserUtil.extractJsonArray; -import static org.onap.holmes.common.aai.AaiJsonParserUtil.get; -import static org.onap.holmes.common.aai.AaiJsonParserUtil.getHostAddr; -import static org.onap.holmes.common.aai.AaiJsonParserUtil.getInfo; -import static org.onap.holmes.common.aai.AaiJsonParserUtil.getPath; +import static org.onap.holmes.common.aai.AaiJsonParserUtil.*; @Service @Slf4j @@ -62,14 +58,14 @@ public class AaiQuery4Ccvpn2 { 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 = JSON.parseObject(resStr); - JSONArray siteResources = extractJsonArray(resObj, "site-resource"); + JsonObject resObj = JsonParser.parseString(resStr).getAsJsonObject(); + JsonArray siteResources = extractJsonArray(resObj, "site-resource"); if (siteResources != null) { for (int i = 0; i < siteResources.size(); i++) { - final JSONObject object = siteResources.getJSONObject(i); - if (siteService.equals(object.getString("site-resource-name"))) { - JSONObject vnfInfo = getInfo(object.toJSONString(), "generic-vnf"); - String vnfPath = vnfInfo.getString("related-link"); + final JsonObject object = siteResources.get(i).getAsJsonObject(); + if (siteService.equals(object.get("site-resource-name").getAsString())) { + JsonObject vnfInfo = getInfo(object.toString(), "generic-vnf"); + String vnfPath = vnfInfo.get("related-link").getAsString(); String vnfId = null; Pattern pattern = Pattern.compile("/aai/v\\d+/network/generic-vnfs/generic-vnf/(.+)"); @@ -85,23 +81,23 @@ public class AaiQuery4Ccvpn2 { return null; } - private JSONObject getServiceInstanceByVnfId(String vnfId) throws CorrelationException { + 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); return getInfo(resStr, "service-instance"); } - public JSONObject getSiteServiceInstance(String siteService) throws CorrelationException { + public JsonObject getSiteServiceInstance(String siteService) throws CorrelationException { String vnfId = getSiteVNFId(siteService); if (vnfId == null) { return null; } - JSONObject serviceInstanceInfo = getServiceInstanceByVnfId(vnfId); - String serviceInstancePath = serviceInstanceInfo.getString("related-link"); + JsonObject serviceInstanceInfo = getServiceInstanceByVnfId(vnfId); + String serviceInstancePath = serviceInstanceInfo.get("related-link").getAsString(); Response response = get(getHostAddr(), getPath(serviceInstancePath)); String res = response.readEntity(String.class); - JSONObject instance = JSON.parseObject(res); + 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/(.+)" + @@ -111,9 +107,9 @@ public class AaiQuery4Ccvpn2 { params[0] = matcher.group(1); params[1] = matcher.group(2); } - instance.put("globalSubscriberId", params[0]); - instance.put("serviceType", params[1]); - instance.put("vnfId", vnfId); + instance.addProperty("globalSubscriberId", params[0]); + instance.addProperty("serviceType", params[1]); + instance.addProperty("vnfId", vnfId); return instance; } } diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiResponseUtil.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiResponseUtil.java index 5df05ca..5ad4984 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiResponseUtil.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiResponseUtil.java @@ -1,5 +1,5 @@ /** - * Copyright 2017 ZTE Corporation. + * Copyright 2017-2020 ZTE Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,13 +15,9 @@ */ package org.onap.holmes.common.aai; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.aai.entity.RelationshipList; import org.onap.holmes.common.aai.entity.RelationshipList.RelatedToProperty; @@ -31,6 +27,10 @@ import org.onap.holmes.common.aai.entity.VmEntity; import org.onap.holmes.common.aai.entity.VmResourceLink; import org.onap.holmes.common.aai.entity.VnfEntity; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + @Service public class AaiResponseUtil { @@ -40,17 +40,17 @@ public class AaiResponseUtil { { List<VmResourceLink> vmResourceLinkList = new ArrayList<>(); String resultDataKey = "result-data"; - JSONObject jsonNode = JSON.parseObject(responseJson); + JsonObject jsonNode = JsonParser.parseString(responseJson).getAsJsonObject(); if (jsonNode != null && jsonNode.get(resultDataKey) != null) { - JSONArray resultData = jsonNode.getJSONArray(resultDataKey); + JsonArray resultData = jsonNode.getAsJsonArray(resultDataKey); vmResourceLinkList = convertResultDataList(resultData); } return vmResourceLinkList; } public VmEntity convertJsonToVmEntity(String responseJson) { - JSONObject jsonObject = JSON.parseObject(responseJson); - if (jsonObject == null ||jsonObject.isEmpty()) { + JsonObject jsonObject = JsonParser.parseString(responseJson).getAsJsonObject(); + if (jsonObject == null ||jsonObject.size() == 0) { return null; } VmEntity vmEntity = new VmEntity(); @@ -72,9 +72,9 @@ public class AaiResponseUtil { } public VnfEntity convertJsonToVnfEntity(String responseJson) { - JSONObject jsonObject = JSON.parseObject(responseJson); + JsonObject jsonObject = JsonParser.parseString(responseJson).getAsJsonObject(); - if (jsonObject.isEmpty()) { + if (jsonObject.size() == 0) { return null; } @@ -97,24 +97,24 @@ public class AaiResponseUtil { return vnfEntity; } - private void setRelationShips(JSONObject jsonObject, RelationshipList relationshipList) { + private void setRelationShips(JsonObject jsonObject, RelationshipList relationshipList) { if (jsonObject.get(RELATIONSHIP_LIST) != null) { - JSONObject relationshipListNode = jsonObject.getJSONObject(RELATIONSHIP_LIST); + JsonObject relationshipListNode = jsonObject.getAsJsonObject(RELATIONSHIP_LIST); String relationship = "relationship"; if (relationshipListNode.get(relationship) != null) { - JSONArray relationshipNode = relationshipListNode.getJSONArray(relationship); + JsonArray relationshipNode = relationshipListNode.getAsJsonArray(relationship); relationshipList .setRelationships(convertRelationships(relationshipNode)); } } } - private List<VmResourceLink> convertResultDataList(JSONArray resultData) { + private List<VmResourceLink> convertResultDataList(JsonArray resultData) { List<VmResourceLink> vmResourceLinkList = new ArrayList<>(); String resourceLink = "resource-link"; String resourceType = "resource-type"; for (int i = 0; i < resultData.size(); i++) { - JSONObject jsonObject = resultData.getJSONObject(i); + JsonObject jsonObject = resultData.get(i).getAsJsonObject(); if (jsonObject.get(resourceLink) != null && jsonObject.get(resourceType) != null) { VmResourceLink vmResourceLink = new VmResourceLink(); @@ -126,23 +126,23 @@ public class AaiResponseUtil { return vmResourceLinkList; } - private List<Relationship> convertRelationships(JSONArray relationshipNode) { + private List<Relationship> convertRelationships(JsonArray relationshipNode) { List<Relationship> relationshipList = new ArrayList<>(); for (int i = 0; i < relationshipNode.size(); i++) { Relationship relationship = new Relationship(); - JSONObject jsonObject = relationshipNode.getJSONObject(i); + JsonObject jsonObject = relationshipNode.get(i).getAsJsonObject(); relationship.setRelatedLink(getTextElementByNode(jsonObject, "related-link")); relationship.setRelatedTo(getTextElementByNode(jsonObject, "related-to")); if (jsonObject.get("related-to-property") != null) { - JSONArray relatedToPropertyNode = jsonObject.getJSONArray("related-to-property"); + JsonArray relatedToPropertyNode = jsonObject.getAsJsonArray("related-to-property"); relationship.setRelatedToPropertyList( convertRelatedToProperty(relatedToPropertyNode)); } else { relationship.setRelatedToPropertyList(Collections.emptyList()); } if (jsonObject.get("relationship-data") != null) { - JSONArray relationshipDataNode = jsonObject.getJSONArray("relationship-data"); + JsonArray relationshipDataNode = jsonObject.getAsJsonArray("relationship-data"); relationship .setRelationshipDataList(convertRelationshipDate(relationshipDataNode)); } else { @@ -154,10 +154,10 @@ public class AaiResponseUtil { return relationshipList; } - private List<RelationshipData> convertRelationshipDate(JSONArray relationshipDataNode) { + private List<RelationshipData> convertRelationshipDate(JsonArray relationshipDataNode) { List<RelationshipData> relationshipDataList = new ArrayList<>(); for (int i = 0; i < relationshipDataNode.size(); i++) { - JSONObject jsonObject = relationshipDataNode.getJSONObject(i); + JsonObject jsonObject = relationshipDataNode.get(i).getAsJsonObject(); RelationshipData relationshipData = new RelationshipData(); relationshipData.setRelationshipKey( getTextElementByNode(jsonObject, "relationship-key")); @@ -170,10 +170,10 @@ public class AaiResponseUtil { return relationshipDataList; } - private List<RelatedToProperty> convertRelatedToProperty(JSONArray relatedToPropertyNode) { + private List<RelatedToProperty> convertRelatedToProperty(JsonArray relatedToPropertyNode) { List<RelatedToProperty> relatedToPropertyList = new ArrayList<>(); for (int i = 0; i < relatedToPropertyNode.size(); i++) { - JSONObject jsonObject = relatedToPropertyNode.getJSONObject(i); + JsonObject jsonObject = relatedToPropertyNode.get(i).getAsJsonObject(); RelatedToProperty relatedToProperty = new RelatedToProperty(); relatedToProperty .setPropertyKey(getTextElementByNode(jsonObject, "property-key")); @@ -184,16 +184,16 @@ public class AaiResponseUtil { return relatedToPropertyList; } - private String getTextElementByNode(JSONObject jsonNode, String name) { + private String getTextElementByNode(JsonObject jsonNode, String name) { if (jsonNode.get(name) != null) { - return jsonNode.getString(name); + return jsonNode.get(name).getAsString(); } return null; } - private Boolean getBooleanElementByNode(JSONObject jsonNode, String name) { + private Boolean getBooleanElementByNode(JsonObject jsonNode, String name) { if (jsonNode.get(name) != null) { - return jsonNode.getBoolean(name); + return jsonNode.get(name).getAsBoolean(); } return null; } diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/Alarm.java b/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/Alarm.java index 6d9d602..5a61cc0 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/Alarm.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/Alarm.java @@ -16,7 +16,7 @@ package org.onap.holmes.common.api.stat;
-import com.alibaba.fastjson.JSONObject;
+import com.google.gson.Gson;
import lombok.Getter;
import lombok.Setter;
@@ -90,7 +90,7 @@ public class Alarm implements AplusData, Cloneable, Serializable { @Override
public String toString() {
- return JSONObject.toJSONString(this);
+ return new Gson().toJson(this);
}
@Override
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 3d94325..33bd1d2 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,5 +1,5 @@ /**
- * Copyright 2017 ZTE Corporation.
+ * Copyright 2017-2020 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.
@@ -15,19 +15,17 @@ */
package org.onap.holmes.common.config;
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONObject;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import org.onap.holmes.common.constant.AlarmConst;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
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;
-
import java.util.regex.Pattern;
-@Slf4j
public class MicroServiceConfig {
final static public String CONSUL_ADDR_SUF = ":8500/v1/catalog/service/";
@@ -40,6 +38,8 @@ public class MicroServiceConfig { final static public Pattern IP_REG = Pattern.compile("(http(s)?://)?(\\d+\\.\\d+\\.\\d+\\.\\d+)(:(\\d+))?");
final static public String AAI_HOSTNAME = "aai.onap";
+ final static public Logger log = LoggerFactory.getLogger(MicroServiceConfig.class);
+
public static String getEnv(String name) {
String value = System.getenv(name);
if (value == null) {
@@ -57,11 +57,14 @@ public class MicroServiceConfig { String queryString = getConsulAddrInfo() + hostname;
log.info("Query the " + hostname + " address using the URL: " + queryString);
try {
- JSONObject addrJson = (JSONObject) JSON.parseArray(execQuery(queryString)).get(0);
+ JsonObject addrJson = JsonParser.parseString(execQuery(queryString))
+ .getAsJsonArray()
+ .get(0)
+ .getAsJsonObject();
if (addrJson != null && addrJson.get("ServiceAddress") != null
&& addrJson.get("ServicePort") != null) {
- ret = "http://" + addrJson.getString("ServiceAddress") + ":" + addrJson
- .getString("ServicePort");
+ ret = "http://" + addrJson.get("ServiceAddress").getAsString() + ":" + addrJson
+ .get("ServicePort").getAsString();
}
} catch (Exception e) {
log.warn(e.getMessage(), e);
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParser.java b/holmes-actions/src/main/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParser.java index 2c878b0..c1eede7 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParser.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParser.java @@ -1,5 +1,5 @@ /*
- * Copyright 2017 ZTE Corporation.
+ * Copyright 2017-2020 ZTE Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,19 +15,21 @@ */
package org.onap.holmes.common.dcae.utils;
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONObject;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map.Entry;
-import java.util.Set;
-import java.util.stream.Stream;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
import org.apache.commons.lang3.StringUtils;
import org.onap.holmes.common.dcae.entity.DcaeConfigurations;
import org.onap.holmes.common.dcae.entity.Rule;
import org.onap.holmes.common.dcae.entity.SecurityInfo;
import org.onap.holmes.common.exception.CorrelationException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.stream.Stream;
+
public class DcaeConfigurationParser {
private static final String RULE_CONTENT_SPLIT = "\\$\\$\\$";
@@ -43,9 +45,9 @@ public class DcaeConfigurationParser { DcaeConfigurations ret = new DcaeConfigurations();
- JSONObject jsonObject = null;
+ JsonObject jsonObject = null;
try {
- jsonObject = JSON.parseObject(jsonStr);
+ jsonObject = JsonParser.parseString(jsonStr).getAsJsonObject();
} catch (Exception e) {
throw new CorrelationException(e.getMessage(), e);
}
@@ -54,73 +56,73 @@ public class DcaeConfigurationParser { fillInPublishesInfo(ret, jsonObject);
fillInSubscribesInfo(ret, jsonObject);
- JSONObject finalJsonObject = jsonObject;
+ JsonObject finalJsonObject = jsonObject;
Stream.of(jsonObject.keySet().toArray(new String[0]))
.filter(key -> !OBJECT_ATTRS.contains(key))
- .forEach(key -> ret.put(key, finalJsonObject.getString(String.valueOf(key))));
+ .forEach(key -> ret.put(key, finalJsonObject.get(String.valueOf(key)).getAsString()));
return ret;
}
- private static void fillInPublishesInfo(DcaeConfigurations ret, JSONObject jsonObject) {
- if (jsonObject.containsKey("streams_publishes")) {
- JSONObject publishes = jsonObject.getJSONObject("streams_publishes");
+ private static void fillInPublishesInfo(DcaeConfigurations ret, JsonObject jsonObject) {
+ if (jsonObject.has("streams_publishes")) {
+ JsonObject publishes = jsonObject.get("streams_publishes").getAsJsonObject();
for (Object key : publishes.keySet()) {
ret.addPubSecInfo((String) key,
- createSecurityInfo((String) key, publishes.getJSONObject((String) key)));
+ createSecurityInfo((String) key, publishes.get((String) key).getAsJsonObject()));
}
}
}
- private static void fillInSubscribesInfo(DcaeConfigurations ret, JSONObject jsonObject) {
- if (jsonObject.containsKey("streams_subscribes")) {
- JSONObject subscribes = jsonObject.getJSONObject("streams_subscribes");
+ private static void fillInSubscribesInfo(DcaeConfigurations ret, JsonObject jsonObject) {
+ if (jsonObject.has("streams_subscribes")) {
+ JsonObject subscribes = jsonObject.get("streams_subscribes").getAsJsonObject();
for (Object key : subscribes.keySet()) {
ret.addSubSecInfo((String) key,
- createSecurityInfo((String) key, subscribes.getJSONObject((String) key)));
+ createSecurityInfo((String) key, subscribes.get((String) key).getAsJsonObject()));
}
}
}
- private static SecurityInfo createSecurityInfo(String key, JSONObject entity) {
+ private static SecurityInfo createSecurityInfo(String key, JsonObject entity) {
SecurityInfo securityInfo = new SecurityInfo();
- if (entity.containsKey("type")) {
- securityInfo.setType(entity.getString("type"));
+ if (entity.has("type") && !entity.get("type").isJsonNull()) {
+ securityInfo.setType(entity.get("type").getAsString());
}
- if (entity.containsKey("aaf_password")) {
- securityInfo.setAafPassword(entity.getString("aaf_password"));
+ if (entity.has("aaf_password") && !entity.get("aaf_password").isJsonNull()) {
+ securityInfo.setAafPassword(entity.get("aaf_password").getAsString());
}
- if (entity.containsKey("aaf_username")) {
- securityInfo.setAafUsername(entity.getString("aaf_username"));
+ if (entity.has("aaf_username") && !entity.get("aaf_username").isJsonNull()) {
+ securityInfo.setAafUsername(entity.get("aaf_username").getAsString());
}
securityInfo.setSecureTopic(!key.endsWith("unsecure"));
- fillInDmaapInfo(securityInfo, entity.getJSONObject("dmaap_info"));
+ fillInDmaapInfo(securityInfo, entity.get("dmaap_info").getAsJsonObject());
return securityInfo;
}
- private static void fillInDmaapInfo(SecurityInfo securityInfo, JSONObject jsonDmaapInfo) {
+ private static void fillInDmaapInfo(SecurityInfo securityInfo, JsonObject jsonDmaapInfo) {
SecurityInfo.DmaapInfo dmaapInfo = securityInfo.getDmaapInfo();
- if (jsonDmaapInfo.containsKey("location")){
- dmaapInfo.setLocation(jsonDmaapInfo.getString("location"));
+ if (jsonDmaapInfo.has("location") && !jsonDmaapInfo.get("location").isJsonNull()){
+ dmaapInfo.setLocation(jsonDmaapInfo.get("location").getAsString());
}
- if (jsonDmaapInfo.containsKey("topic_url")) {
- dmaapInfo.setTopicUrl(jsonDmaapInfo.getString("topic_url"));
+ if (jsonDmaapInfo.has("topic_url") && !jsonDmaapInfo.get("topic_url").isJsonNull()) {
+ dmaapInfo.setTopicUrl(jsonDmaapInfo.get("topic_url").getAsString());
}
- if (jsonDmaapInfo.containsKey("client_id")) {
- dmaapInfo.setClientId(jsonDmaapInfo.getString("client_id"));
+ if (jsonDmaapInfo.has("client_id") && !jsonDmaapInfo.get("client_id").isJsonNull()) {
+ dmaapInfo.setClientId(jsonDmaapInfo.get("client_id").getAsString());
}
- if (jsonDmaapInfo.containsKey("client_role")) {
- dmaapInfo.setClientRole(jsonDmaapInfo.getString("client_role"));
+ if (jsonDmaapInfo.has("client_role") && !jsonDmaapInfo.get("client_role").isJsonNull()) {
+ dmaapInfo.setClientRole(jsonDmaapInfo.get("client_role").getAsString());
}
- if (jsonDmaapInfo.containsKey("type")) {
- dmaapInfo.setType(jsonDmaapInfo.getString("type"));
+ if (jsonDmaapInfo.has("type") && !jsonDmaapInfo.get("type").isJsonNull()) {
+ dmaapInfo.setType(jsonDmaapInfo.get("type").getAsString());
}
}
- private static void fillInRules(DcaeConfigurations ret, JSONObject jsonObject) {
- Set<Entry<String, Object>> entries = jsonObject.entrySet();
- for (Entry<String, Object> entry : entries) {
+ private static void fillInRules(DcaeConfigurations ret, JsonObject jsonObject) {
+ Set<Entry<String, JsonElement>> entries = jsonObject.entrySet();
+ for (Entry<String, JsonElement> entry : entries) {
if (entry.getKey().startsWith("holmes.default.rule")) {
- String value = (String) entry.getValue();
+ String value = entry.getValue().getAsString();
String[] contents = value.split(RULE_CONTENT_SPLIT);
ret.addDefaultRule(new Rule(entry.getKey(), contents[0], contents[1], 1));
}
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java index ba52262..f29f1d9 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 ZTE Corporation. + * Copyright 2017-2020 ZTE Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,12 +15,6 @@ */ package org.onap.holmes.common.dmaap; -import java.util.Map.Entry; -import java.util.Optional; -import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; -import javax.inject.Inject; -import lombok.extern.slf4j.Slf4j; import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.aai.AaiQuery; import org.onap.holmes.common.aai.entity.RelationshipList.Relationship; @@ -30,17 +24,41 @@ import org.onap.holmes.common.api.stat.VesAlarm; import org.onap.holmes.common.dcae.DcaeConfigurationsCache; import org.onap.holmes.common.dmaap.entity.PolicyMsg; import org.onap.holmes.common.dmaap.entity.PolicyMsg.EVENT_STATUS; +import org.onap.holmes.common.dmaap.store.ClosedLoopControlNameCache; +import org.onap.holmes.common.dmaap.store.UniqueRequestIdCache; import org.onap.holmes.common.exception.CorrelationException; import org.onap.holmes.common.utils.GsonUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.inject.Inject; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.UUID; -@Slf4j @Service public class DmaapService { - @Inject + private static final Logger log = LoggerFactory.getLogger(DmaapService.class); + private AaiQuery aaiQuery; - public static final ConcurrentHashMap<String, String> loopControlNames = new ConcurrentHashMap<>(); - public static final ConcurrentHashMap<String, String> alarmUniqueRequestID = new ConcurrentHashMap<>(); + private ClosedLoopControlNameCache closedLoopControlNameCache; + private UniqueRequestIdCache uniqueRequestIdCache; + + @Inject + public void setAaiQuery(AaiQuery aaiQuery) { + this.aaiQuery = aaiQuery; + } + + @Inject + public void setClosedLoopControlNameCache(ClosedLoopControlNameCache closedLoopControlNameCache) { + this.closedLoopControlNameCache = closedLoopControlNameCache; + } + + @Inject + public void setUniqueRequestIdCache(UniqueRequestIdCache uniqueRequestIdCache) { + this.uniqueRequestIdCache = uniqueRequestIdCache; + } public void publishPolicyMsg(PolicyMsg policyMsg, String dmaapConfigKey) { try { @@ -48,7 +66,7 @@ public class DmaapService { publisher.setUrl(DcaeConfigurationsCache.getPubSecInfo(dmaapConfigKey).getDmaapInfo() .getTopicUrl()); publisher.publish(policyMsg); - deleteRequestId(policyMsg); + deleteRequestIdIfNecessary(policyMsg); log.info("send policyMsg: " + GsonUtil.beanToJson(policyMsg)); } catch (CorrelationException e) { log.error("Failed to publish the control loop event to DMaaP", e); @@ -79,7 +97,7 @@ public class DmaapService { policyMsg.setClosedLoopAlarmEnd(rootAlarm.getLastEpochMicrosec()); policyMsg.setClosedLoopEventStatus(EVENT_STATUS.ABATED); } - policyMsg.setClosedLoopControlName(loopControlNames.get(packageName)); + policyMsg.setClosedLoopControlName(closedLoopControlNameCache.get(packageName)); policyMsg.setClosedLoopAlarmStart(rootAlarm.getStartEpochMicrosec()); policyMsg.getAai().put("vserver.vserver-id", vmEntity.getVserverId()); policyMsg.getAai().put("vserver.vserver-name", vmEntity.getVserverName()); @@ -92,7 +110,7 @@ public class DmaapService { private PolicyMsg getDefaultPolicyMsg(VesAlarm rootAlarm, String packageName) { PolicyMsg policyMsg = new PolicyMsg(); policyMsg.setRequestID(getUniqueRequestId(rootAlarm)); - policyMsg.setClosedLoopControlName(loopControlNames.get(packageName)); + policyMsg.setClosedLoopControlName(closedLoopControlNameCache.get(packageName)); policyMsg.setClosedLoopAlarmStart(rootAlarm.getStartEpochMicrosec()); policyMsg.setTarget("vserver.vserver-name"); policyMsg.setTargetType("VM"); @@ -112,11 +130,11 @@ public class DmaapService { } else { alarmUniqueKey = rootAlarm.getSourceId() + ":" + rootAlarm.getEventName(); } - if (alarmUniqueRequestID.containsKey(alarmUniqueKey)) { - return alarmUniqueRequestID.get(alarmUniqueKey); + if (uniqueRequestIdCache.containsKey(alarmUniqueKey)) { + return uniqueRequestIdCache.get(alarmUniqueKey); } else { String requestID = UUID.randomUUID().toString(); - alarmUniqueRequestID.put(alarmUniqueKey, requestID); + uniqueRequestIdCache.put(alarmUniqueKey, requestID); return requestID; } } @@ -176,13 +194,13 @@ public class DmaapService { return vmEntity; } - private void deleteRequestId(PolicyMsg policyMsg){ + private void deleteRequestIdIfNecessary(PolicyMsg policyMsg){ EVENT_STATUS status = policyMsg.getClosedLoopEventStatus(); if(EVENT_STATUS.ABATED.equals(status)) { String requestId = policyMsg.getRequestID(); - for(Entry<String, String> kv: alarmUniqueRequestID.entrySet()) { + for(Entry<String, String> kv: uniqueRequestIdCache.entrySet()) { if(kv.getValue().equals(requestId)) { - alarmUniqueRequestID.remove(kv.getKey()); + uniqueRequestIdCache.remove(kv.getKey()); break; } } diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/store/ClosedLoopControlNameCache.java b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/store/ClosedLoopControlNameCache.java new file mode 100644 index 0000000..8a692b2 --- /dev/null +++ b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/store/ClosedLoopControlNameCache.java @@ -0,0 +1,41 @@ +/* + * Copyright 2020 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.store; + +import org.jvnet.hk2.annotations.Service; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.inject.Singleton; +import java.util.concurrent.ConcurrentHashMap; + +@Service +@Singleton +public class ClosedLoopControlNameCache extends ConcurrentHashMap<String, String> { + + private static final Logger log = LoggerFactory.getLogger(ClosedLoopControlNameCache.class); + + @Override + public String put(String packageName, String controlLoopName) { + log.info(String.format("<%s:%s> was added into ClosedLoopControlNameCache.", packageName, controlLoopName)); + return super.put(packageName, controlLoopName); + } + + public String remove(String packageName) { + log.info(String.format("<%s> was removed into ClosedLoopControlNameCache.", packageName)); + return super.remove(packageName); + } +} diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/store/UniqueRequestIdCache.java b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/store/UniqueRequestIdCache.java new file mode 100644 index 0000000..070159e --- /dev/null +++ b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/store/UniqueRequestIdCache.java @@ -0,0 +1,41 @@ +/* + * Copyright 2020 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.store; + +import org.jvnet.hk2.annotations.Service; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.inject.Singleton; +import java.util.concurrent.ConcurrentHashMap; + +@Service +@Singleton +public class UniqueRequestIdCache extends ConcurrentHashMap<String, String> { + + private static final Logger log = LoggerFactory.getLogger(UniqueRequestIdCache.class); + + @Override + public String put(String alarmId, String requestId) { + log.info(String.format("<%s:%s> was added into UniqueRequestIdCache.", alarmId, requestId)); + return super.put(alarmId, requestId); + } + + public String remove(String alarmId) { + log.info(String.format("<%s> was removed into UniqueRequestIdCache.", alarmId)); + return super.remove(alarmId); + } +} diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn2Test.java b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn2Test.java index cd1d505..804df1f 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn2Test.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn2Test.java @@ -20,13 +20,10 @@ package org.onap.holmes.common.aai; -import com.alibaba.fastjson.JSONObject; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import org.easymock.EasyMock; -import org.junit.After; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; +import org.junit.*; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.onap.holmes.common.aai.config.AaiConfig; @@ -43,11 +40,7 @@ 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.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; +import java.io.*; import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR; @@ -59,7 +52,7 @@ public class AaiQuery4Ccvpn2Test { @Rule public ExpectedException thrown = ExpectedException.none(); - private static JSONObject data; + private static JsonObject data; private static AaiQuery4Ccvpn2 aai = AaiQuery4Ccvpn2.newInstance(); @@ -79,7 +72,7 @@ public class AaiQuery4Ccvpn2Test { reader = new BufferedReader(new FileReader(file)); StringBuilder sb = new StringBuilder(); reader.lines().forEach(l -> sb.append(l)); - data = JSONObject.parseObject(sb.toString()); + data = JsonParser.parseString(sb.toString()).getAsJsonObject(); } catch (FileNotFoundException e) { // Do nothing } catch (IOException e) { @@ -119,18 +112,15 @@ public class AaiQuery4Ccvpn2Test { @Test public void test_getServiceInstances_exception() throws CorrelationException { mockGetMethod(); - EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("site-resources").toJSONString - ()); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("site-resources").toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockGetMethod(); - EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("499hkg9933NNN").toJSONString - ()); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("499hkg9933NNN").toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockGetMethod(); - EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("499hkg9933NNN").toJSONString - ()); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("499hkg9933NNN").toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); PowerMock.replayAll(); @@ -143,18 +133,15 @@ public class AaiQuery4Ccvpn2Test { @Test public void test_getServiceInstancesNull_exception() throws CorrelationException { mockGetMethod(); - EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("site-resources1").toJSONString - ()); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("site-resources1").toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockGetMethod(); - EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("499hkg9933NNN").toJSONString - ()); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("499hkg9933NNN").toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockGetMethod(); - EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("499hkg9933NNN").toJSONString - ()); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("499hkg9933NNN").toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); PowerMock.replayAll(); 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 b75e61d..f1855f9 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 @@ -1,5 +1,5 @@ /** - * Copyright 2018 ZTE Corporation. + * Copyright 2018-2020 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 @@ -14,14 +14,11 @@ package org.onap.holmes.common.aai; -import com.alibaba.fastjson.JSONObject; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import org.easymock.EasyMock; import org.glassfish.jersey.client.HttpUrlConnectorProvider; -import org.junit.After; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; +import org.junit.*; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.onap.holmes.common.aai.config.AaiConfig; @@ -31,20 +28,12 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; -import javax.ws.rs.client.Client; -import javax.ws.rs.client.ClientBuilder; -import javax.ws.rs.client.Entity; -import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.*; import javax.ws.rs.client.Invocation.Builder; -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.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; +import java.io.*; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; @@ -61,7 +50,7 @@ public class AaiQuery4CcvpnTest { @Rule public ExpectedException thrown = ExpectedException.none(); - private static JSONObject data; + private static JsonObject data; private static AaiQuery4Ccvpn aai = AaiQuery4Ccvpn.newInstance(); @@ -81,7 +70,7 @@ public class AaiQuery4CcvpnTest { reader = new BufferedReader(new FileReader(file)); StringBuilder sb = new StringBuilder(); reader.lines().forEach(l -> sb.append(l)); - data = JSONObject.parseObject(sb.toString()); + data = JsonParser.parseString(sb.toString()).getAsJsonObject(); } catch (FileNotFoundException e) { // Do nothing } catch (IOException e) { @@ -152,7 +141,7 @@ public class AaiQuery4CcvpnTest { @Test public void test_getLogicLink() throws CorrelationException { mockGetMethod(); - EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("logic-link").toJSONString()); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("logic-link").toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); PowerMock.replayAll(); @@ -168,32 +157,32 @@ public class AaiQuery4CcvpnTest { @Test public void test_getServiceInstances_exception() { mockGetMethod(); - EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("vpn-binding").toJSONString()); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("vpn-binding").toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockGetMethod(); - EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("connectivity").toJSONString()); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("connectivity").toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockGetMethod(); EasyMock.expect(response.readEntity(String.class)) - .andReturn(data.getJSONObject("service-instance-by-connectivity").toJSONString()); + .andReturn(data.get("service-instance-by-connectivity").toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockGetMethod(); EasyMock.expect(response.readEntity(String.class)) - .andReturn(data.getJSONObject("service-instances-by-service-type").toJSONString()); + .andReturn(data.get("service-instances-by-service-type").toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2); mockGetMethod(); - EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("service-instance").toString()); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("service-instance").toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2); thrown.expect(RuntimeException.class); PowerMock.replayAll(); - JSONObject instance = aai.getServiceInstance("network-1", "pnf-1", "interface-1", "DOWN"); + JsonObject instance = aai.getServiceInstance("network-1", "pnf-1", "interface-1", "DOWN"); PowerMock.verifyAll(); @@ -204,49 +193,49 @@ public class AaiQuery4CcvpnTest { @Test public void test_getServiceInstance() { mockGetMethod(); - EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("vpn-binding").toJSONString()); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("vpn-binding").toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockGetMethod(); - EasyMock.expect(response.readEntity(String.class)).andReturn(data.getJSONObject("connectivity").toJSONString()); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("connectivity").toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockGetMethod(); EasyMock.expect(response.readEntity(String.class)) - .andReturn(data.getJSONObject("service-instance-by-connectivity").toJSONString()); + .andReturn(data.get("service-instance-by-connectivity").toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockGetMethod(); EasyMock.expect(response.readEntity(String.class)) - .andReturn(data.getJSONObject("service-instances-by-service-type").toJSONString()); + .andReturn(data.get("service-instances-by-service-type").toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); PowerMock.replayAll(); - JSONObject instance = aai.getServiceInstance("network-1", "pnf-1", "interface-1", "DOWN"); + JsonObject instance = aai.getServiceInstance("network-1", "pnf-1", "interface-1", "DOWN"); PowerMock.verifyAll(); - assertThat(instance.getString("service-instance-id"), equalTo("some id 1")); - assertThat(instance.getString("globalSubscriberId"), equalTo("e151059a-d924-4629-845f-264db19e50b4")); - assertThat(instance.getString("serviceType"), equalTo("volte")); + assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 1")); + assertThat(instance.get("globalSubscriberId").getAsString(), equalTo("e151059a-d924-4629-845f-264db19e50b4")); + assertThat(instance.get("serviceType").getAsString(), equalTo("volte")); } @Test public void test_getServiceInstance_1() throws Exception { mockGetMethod(); EasyMock.expect(response.readEntity(String.class)) - .andReturn(data.getJSONObject("service-instances-by-service-type").toJSONString()); + .andReturn(data.get("service-instances-by-service-type").toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); PowerMock.replayAll(); - JSONObject instance = Whitebox.invokeMethod(aai, "getServiceInstance", + JsonObject instance = Whitebox.invokeMethod(aai, "getServiceInstance", "custom-1", "service-type-1"); PowerMock.verifyAll(); - assertThat(instance.getString("service-instance-id"), equalTo("some id 1")); + assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 1")); } @Test @@ -259,20 +248,20 @@ public class AaiQuery4CcvpnTest { PowerMock.replayAll(); - JSONObject instance = Whitebox.invokeMethod(aai, "getServiceInstance", + JsonObject instance = Whitebox.invokeMethod(aai, "getServiceInstance", "custom-1", "service-type-1"); PowerMock.verifyAll(); - assertThat(instance.getString("service-instance-id"), equalTo("some id 1")); - assertThat(instance.getString("service-instance-id"), equalTo("some id 2")); - assertThat(instance.getString("service-instance-id"), equalTo("some id 3")); + assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 1")); + assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 2")); + assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 3")); } @Test public void test_updateTerminalPointStatus() throws CorrelationException { mockGetMethod(); - EasyMock.expect(response.readEntity(String.class)).andReturn(data.toJSONString()); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockPatchMethod(); @@ -288,7 +277,7 @@ public class AaiQuery4CcvpnTest { @Test public void test_updateTerminalPointStatus_exception() throws CorrelationException { mockGetMethod(); - EasyMock.expect(response.readEntity(String.class)).andReturn(data.toJSONString()); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockPatchMethod(); @@ -307,7 +296,7 @@ public class AaiQuery4CcvpnTest { @Test public void test_updateLogicLinkStatus() throws CorrelationException { mockGetMethod(); - EasyMock.expect(response.readEntity(String.class)).andReturn(data.toJSONString()); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockPatchMethod(); @@ -323,7 +312,7 @@ public class AaiQuery4CcvpnTest { @Test public void test_updateLogicLinkStatus_exception() throws CorrelationException { mockGetMethod(); - EasyMock.expect(response.readEntity(String.class)).andReturn(data.toJSONString()); + EasyMock.expect(response.readEntity(String.class)).andReturn(data.toString()); EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK); mockPatchMethod(); 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 15904d5..0340e04 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 @@ -295,16 +295,4 @@ public class AaiQueryTest { assertThat(actual, equalTo("https://aai.onap:8443/url")); } - - @Test - public void testAaiQuery_getMsbSuffixAddr_Ok() throws Exception { - PowerMock.resetAll(); - String url = "/aai/v11/network/generic-vnfs/generic-vnf?"; - String expect = "/api/aai-network/v11/generic-vnfs/generic-vnf?"; - aaiQuery = new AaiQuery(); - PowerMock.replayAll(); - String actual = Whitebox.invokeMethod(aaiQuery, "getMsbSuffixAddr", url); - PowerMock.verifyAll(); - assertThat(actual, equalTo(expect)); - } } diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/api/stat/AlarmTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/api/stat/AlarmTest.java index 6cf62d6..0063b08 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/api/stat/AlarmTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/api/stat/AlarmTest.java @@ -16,24 +16,16 @@ package org.onap.holmes.common.api.stat; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; - -import java.util.Date; -import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -import java.util.HashMap; -import java.util.Map; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.onap.holmes.common.api.entity.ServiceNode; + +import java.util.Date; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.*; public class AlarmTest { @@ -43,39 +35,39 @@ public class AlarmTest { private Alarm alarm; @Before - public void before() throws Exception { + public void before() { alarm = new Alarm(); } @After - public void after() throws Exception { + public void after() { } @Test - public void testContainNode_NoContainLink() throws Exception { + public void testContainNode_NoContainLink() { alarm.addLinkIdNodeIdx(1, 2); assertThat(false, equalTo(alarm.containNode(2, 2))); } @Test - public void testContainNode_ContainLinkNoIdx() throws Exception { + public void testContainNode_ContainLinkNoIdx() { alarm.addLinkIdNodeIdx(1, 2); assertFalse(alarm.containNode(1, 3)); } @Test - public void testContainNode_ContainLinkAndIdx() throws Exception { + public void testContainNode_ContainLinkAndIdx() { alarm.addLinkIdNodeIdx(1, 2); assertTrue(alarm.containNode(1, 2)); } @Test - public void testGetDataType() throws Exception { + public void testGetDataType() { assertThat(Alarm.APLUS_EVENT, equalTo(alarm.getDataType())); } @Test - public void testToString() throws Exception { + public void testToString() { Alarm alarmTempA = new Alarm(); Alarm alarmTempB = new Alarm(); Date date = new Date(); @@ -89,7 +81,7 @@ public class AlarmTest { } @Test - public void testHashCode() throws Exception { + public void testHashCode() { final Alarm alarmTemp = new Alarm(); final String alarmKey = "alarmKey"; alarm.setAlarmKey(alarmKey); @@ -98,7 +90,7 @@ public class AlarmTest { } @Test - public void testEqualsAnd_NotNull() throws Exception { + public void testEqualsAnd_NotNull() { final Alarm alarmTemp = new Alarm(); final String alarmKey = "alarmKey"; alarm.setAlarmKey(alarmKey); @@ -107,12 +99,12 @@ public class AlarmTest { } @Test - public void testEqualsAndH_isNull() throws Exception { + public void testEqualsAndH_isNull() { assertFalse(alarm.equals(null)); } @Test - public void testClone() throws Exception { + public void testClone() throws CloneNotSupportedException { alarm.setAlarmKey("alarmKey"); Alarm alarmTemp = (Alarm) alarm.clone(); assertTrue(alarm.equals(alarmTemp)); @@ -120,40 +112,40 @@ public class AlarmTest { } @Test - public void testGetObjectId() throws Exception { + public void testGetObjectId() { alarm.setId(11); assertThat("11", equalTo(alarm.getObjectId())); } @Test - public void testAddLinkIds() throws Exception { + public void testAddLinkIds() { final int linkId = 11; alarm.addLinkIds(linkId); assertTrue(alarm.getLinkIds().contains(linkId)); } @Test - public void testContainsPriority_true() throws Exception { + public void testContainsPriority_true() { String ruleId = "ruleId"; alarm.getPriorityMap().put(ruleId, 2); assertTrue(alarm.containsPriority(ruleId)); } @Test - public void testContainsPriority_false() throws Exception { + public void testContainsPriority_false() { final String ruleId = "ruleId"; assertFalse(alarm.containsPriority(ruleId)); } @Test - public void testGetPriority_isNull() throws Exception { + public void testGetPriority_isNull() { final String ruleId = "ruleId"; alarm.getPriorityMap().put(ruleId, null); assertThat(0, equalTo(alarm.getPriority(ruleId))); } @Test - public void testGetPriority_notNull() throws Exception { + public void testGetPriority_notNull() { final String ruleId = "ruleId"; final int priority = 2; alarm.getPriorityMap().put(ruleId, priority); @@ -161,14 +153,14 @@ public class AlarmTest { } @Test - public void testGetAlarmTypeRuleId_isNull() throws Exception { + public void testGetAlarmTypeRuleId_isNull() { final String ruleId = "ruleId"; alarm.getRootAlarmTypeMap().put(ruleId, null); assertThat(-1, equalTo(alarm.getRootAlarmType(ruleId))); } @Test - public void testGetAlarmTypeRuleId_notNull() throws Exception { + public void testGetAlarmTypeRuleId_notNull() { final String ruleId = "ruleId"; final int rootAlarmType = 2; alarm.getRootAlarmTypeMap().put(ruleId, rootAlarmType); @@ -176,21 +168,9 @@ public class AlarmTest { } @Test - public void getterAndSetter4CenterType() throws Exception { + public void getterAndSetter4CenterType() { final int centerType = 1; alarm.setCenterType(centerType); assertThat(centerType, equalTo(alarm.getCenterType())); } - @Test - public void TestJson(){ - ServiceNode serviceNode = new ServiceNode(); - serviceNode.setIp("111"); - String jsonString = "{\"uid\":\"189024\", \"region\":\"SouthChina\", \"order\":123}"; - String COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}"; - - JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR); - JSONArray jsonArray = jsonObject.getJSONArray("students"); - System.out.printf("jsonObject:"+jsonArray); -// System.out.println("uid:" + retMap.get("uid") + ", " + "region:" + retMap.get("region") + ", " + "order:" + retMap.get("order")); - } } diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java index 6494cd8..76c4a45 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2017 ZTE Corporation. + * Copyright 2017-2020 ZTE Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,20 +15,6 @@ */ package org.onap.holmes.common.dmaap; -import static org.easymock.EasyMock.anyObject; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; - -import java.io.*; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.util.ArrayList; -import java.util.List; - import org.easymock.EasyMock; import org.junit.Before; import org.junit.Rule; @@ -45,12 +31,26 @@ import org.onap.holmes.common.dcae.DcaeConfigurationsCache; import org.onap.holmes.common.dcae.utils.DcaeConfigurationParser; import org.onap.holmes.common.dmaap.entity.PolicyMsg; import org.onap.holmes.common.dmaap.entity.PolicyMsg.EVENT_STATUS; +import org.onap.holmes.common.dmaap.store.ClosedLoopControlNameCache; +import org.onap.holmes.common.dmaap.store.UniqueRequestIdCache; import org.onap.holmes.common.exception.CorrelationException; import org.powermock.api.easymock.PowerMock; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; +import java.io.*; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import static org.easymock.EasyMock.anyObject; +import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + @PrepareForTest({DmaapService.class, Publisher.class, AaiQuery.class}) @RunWith(PowerMockRunner.class) public class DmaapServiceTest { @@ -62,17 +62,24 @@ public class DmaapServiceTest { private DmaapService dmaapService; + private ClosedLoopControlNameCache closedLoopControlNameCache = new ClosedLoopControlNameCache(); + + private UniqueRequestIdCache uniqueRequestIdCache = new UniqueRequestIdCache(); + @Before public void setUp() { - dmaapService = new DmaapService(); aaiQuery = PowerMock.createMock(AaiQuery.class); - Whitebox.setInternalState(dmaapService, "aaiQuery", aaiQuery); + + dmaapService = new DmaapService(); + dmaapService.setClosedLoopControlNameCache(closedLoopControlNameCache); + dmaapService.setUniqueRequestIdCache(uniqueRequestIdCache); + dmaapService.setAaiQuery(aaiQuery); } @Test public void testDmaapService_getDefaultPolicyMsg_ok() throws Exception { String packageName = "org.onap.holmes.rule"; - DmaapService.loopControlNames.put(packageName, "Control-loop-VoLTE"); + closedLoopControlNameCache.put(packageName, "Control-loop-VoLTE"); long startTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis() + 1000000; VesAlarm vesAlarm = new VesAlarm(); @@ -274,7 +281,7 @@ public class DmaapServiceTest { PolicyMsg policyMsg = new PolicyMsg(); policyMsg.setClosedLoopEventStatus(EVENT_STATUS.ABATED); policyMsg.setRequestID("testRequestid"); - DmaapService.alarmUniqueRequestID.put("testAlarmId", "testRequestid"); + uniqueRequestIdCache.put("testAlarmId", "testRequestid"); PowerMock.expectNew(Publisher.class).andReturn(publisher); EasyMock.expect(publisher.publish(policyMsg)).andReturn(true); diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/store/ClosedLoopControlNameCacheTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/store/ClosedLoopControlNameCacheTest.java new file mode 100644 index 0000000..038f634 --- /dev/null +++ b/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/store/ClosedLoopControlNameCacheTest.java @@ -0,0 +1,47 @@ +/* + * Copyright 2020 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.store; + +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + +public class ClosedLoopControlNameCacheTest { + + private ClosedLoopControlNameCache cache = new ClosedLoopControlNameCache(); + + @Before + public void setUp() { + cache.clear(); + } + + @Test + public void put() { + cache.put("org.onap.holmes.test", "control-loop-1"); + assertThat(cache.get("org.onap.holmes.test"), equalTo("control-loop-1")); + } + + @Test + public void remove() { + cache.put("org.onap.holmes.test", "control-loop-1"); + assertThat(cache.get("org.onap.holmes.test"), equalTo("control-loop-1")); + cache.remove("org.onap.holmes.test"); + assertThat(cache.get("org.onap.holmes.test"), nullValue()); + } +}
\ No newline at end of file diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/store/UniqueRequestIdCacheTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/store/UniqueRequestIdCacheTest.java new file mode 100644 index 0000000..c341d7a --- /dev/null +++ b/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/store/UniqueRequestIdCacheTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2020 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.store; + +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + +public class UniqueRequestIdCacheTest { + private UniqueRequestIdCache cache = new UniqueRequestIdCache(); + + @Before + public void setUp() { + cache.clear(); + } + + @Test + public void put() { + cache.put("alarmId-1", "requestId-1"); + assertThat(cache.get("alarmId-1"), equalTo("requestId-1")); + } + + @Test + public void remove() { + cache.put("alarmId-1", "requestId-1"); + assertThat(cache.get("alarmId-1"), equalTo("requestId-1")); + cache.remove("alarmId-1"); + assertThat(cache.get("alarmId-1"), nullValue()); + } +}
\ No newline at end of file @@ -166,6 +166,16 @@ <artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
+ <dependency>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
+ <version>1.14</version>
+ </dependency>
+ <dependency>
+ <groupId>com.google.code.gson</groupId>
+ <artifactId>gson</artifactId>
+ <version>2.8.6</version>
+ </dependency>
</dependencies>
</dependencyManagement>
|