summaryrefslogtreecommitdiffstats
path: root/holmes-actions/src
diff options
context:
space:
mode:
authorShiwei Tian <tian.shiwei@zte.com.cn>2017-09-27 10:19:15 +0800
committerShiwei Tian <tian.shiwei@zte.com.cn>2017-09-27 15:30:49 +0800
commitc9ed775685b01f5622618216748eeac3000285c4 (patch)
treeae11132542e4266daee864cc6fe5c318ab58288d /holmes-actions/src
parent4eaf0290dd2572f40526da9cfd09a1ccee4da76d (diff)
modify unit test and aai query
Issue-ID: HOLMES-44 Change-Id: I9d67fc26681dfb540ae4d011cde4d0cdc5f3d2a3 Signed-off-by: Shiwei Tian <tian.shiwei@zte.com.cn>
Diffstat (limited to 'holmes-actions/src')
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java63
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiResponseUtil.java97
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/aai/CorrelationUtil.java23
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/aai/config/AaiConfig.java4
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/api/stat/VesAlarm.java1
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java1
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java29
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java128
-rw-r--r--holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java60
-rw-r--r--holmes-actions/src/test/java/org/onap/holmes/common/aai/CorrelationUtilTest.java5
-rw-r--r--holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java30
11 files changed, 276 insertions, 165 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 36479d7..aa840ba 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
@@ -15,6 +15,10 @@ package org.onap.holmes.common.aai;
import java.util.HashMap;
import java.util.Map;
+import java.util.stream.Stream;
+import javax.inject.Inject;
+import lombok.extern.slf4j.Slf4j;
+import org.jvnet.hk2.annotations.Service;
import org.onap.holmes.common.aai.config.AaiConfig;
import org.onap.holmes.common.aai.entity.VmEntity;
import org.onap.holmes.common.aai.entity.VnfEntity;
@@ -22,8 +26,11 @@ import org.onap.holmes.common.config.MicroServiceConfig;
import org.onap.holmes.common.exception.CorrelationException;
import org.onap.holmes.common.utils.HttpsUtils;
+@Service
+@Slf4j
public class AaiQuery {
+ @Inject
private AaiResponseUtil aaiResponseUtil;
public VnfEntity getAaiVnfData(String vnfId, String vnfName) throws CorrelationException {
@@ -36,7 +43,7 @@ public class AaiQuery {
}
public VmEntity getAaiVmData(String vserverId, String vserverName) throws CorrelationException {
- String url = MicroServiceConfig.getMsbServerAddr() + getVmResourceLinks(vserverId, vserverName);
+ String url = getVmUrl(vserverId, vserverName);
String response = getResponse(url);
try {
return aaiResponseUtil.convertJsonToVmEntity(response);
@@ -45,6 +52,18 @@ public class AaiQuery {
}
}
+ private String getVmUrl(String vserverId, String vserverName) throws CorrelationException {
+ String url = "";
+ String resourceLinkUrl = getVmResourceLinks(vserverId, vserverName);
+ String baseUrl = getBaseUrl("");
+ if (baseUrl.startsWith("http")) {
+ url = baseUrl + getMsbSuffixAddr(resourceLinkUrl);
+ } else {
+ url = baseUrl + resourceLinkUrl;
+ }
+ return url;
+ }
+
private String getVmResourceLinks(String vserverId, String vserverName) throws CorrelationException {
String response = getResourceLinksResponse(vserverId, vserverName);
try {
@@ -55,29 +74,55 @@ public class AaiQuery {
}
private String getResourceLinksResponse(String vserverId, String vserverName) throws CorrelationException {
- String url =
- MicroServiceConfig.getMsbServerAddr() + AaiConfig.VM_ADDR + "vserver-id:EQUALS:"
- + vserverId;
+ String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VNF_ADDR) + "vserver-id:EQUALS:" + vserverId);
String response = getResponse(url);
if (response.equals("")) {
- url = MicroServiceConfig.getMsbServerAddr() + AaiConfig.VM_ADDR
- + "vserver-name:EQUALS:" + vserverName;
+ url = getBaseUrl(AaiConfig.AAI_VM_ADDR + "vserver-name:EQUALS:" + vserverName);
response = getResponse(url);
}
return response;
}
private String getVnfDataResponse(String vnfId, String vnfName) throws CorrelationException {
- String url = MicroServiceConfig.getMsbServerAddr() + AaiConfig.VNF_ADDR + "vnf-id=" + vnfId;
+ String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VM_ADDR)+ "vnf-id=" + vnfId);
String response = getResponse(url);
if (response.equals("")) {
- url = MicroServiceConfig.getMsbServerAddr() + AaiConfig.VNF_ADDR + "vnf-name="
- + vnfName;
+ url = getBaseUrl(AaiConfig.AAI_VNF_ADDR + "vnf-name=" + vnfName);
response = getResponse(url);
}
return response;
}
+ private String getBaseUrl(String suffixUrl) {
+ String url = "";
+ try {
+ url = MicroServiceConfig.getMsbServerAddr() + suffixUrl;
+ } catch (Exception e) {
+ log.info("Failed to get msb address");
+ }
+ if (url.equals("")) {
+ try {
+ url = "https:\\\\" + MicroServiceConfig.getServiceAddrInfoFromCBS("aai_config")
+ + suffixUrl;
+ } catch (Exception e) {
+ log.info("Failed to get aai address");
+ }
+ }
+ return url;
+ }
+
+ private String getMsbSuffixAddr(String suffixUrl) {
+ String[] addrSplits = suffixUrl.substring(1).split("/");
+ String ret = addrSplits[1];
+ addrSplits[1] = addrSplits[2];
+ addrSplits[2] = ret;
+ StringBuffer stringBuffer = new StringBuffer();
+ for (String split : addrSplits) {
+ stringBuffer.append("/" + split);
+ }
+ return stringBuffer.toString();
+ }
+
private String getResponse(String url) throws CorrelationException {
String response = "";
try {
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 3869522..ccfb4fd 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
@@ -30,6 +30,7 @@ import org.onap.holmes.common.aai.entity.VmEntity;
import org.onap.holmes.common.aai.entity.VmResourceLink;
import org.onap.holmes.common.aai.entity.VnfEntity;
+@Service
public class AaiResponseUtil {
public List<VmResourceLink> convertJsonToVmResourceLink(String responseJson) throws IOException {
@@ -106,73 +107,65 @@ public class AaiResponseUtil {
private List<VmResourceLink> convertResultDataList(JsonNode resultData) {
List<VmResourceLink> vmResourceLinkList = new ArrayList<>();
- if (resultData.isArray()) {
- resultData.forEach(node ->{
- if (node.has("resource-link") && node.has("resource-type")) {
- VmResourceLink vmResourceLink = new VmResourceLink();
- vmResourceLink.setResourceLink(getTextElementByNode(node, "resource-link"));
- vmResourceLink.setResourceType(getTextElementByNode(node, "resource-type"));
- vmResourceLinkList.add(vmResourceLink);
- }
- });
- }
+ resultData.forEach(node ->{
+ if (node.has("resource-link") && node.has("resource-type")) {
+ VmResourceLink vmResourceLink = new VmResourceLink();
+ vmResourceLink.setResourceLink(getTextElementByNode(node, "resource-link"));
+ vmResourceLink.setResourceType(getTextElementByNode(node, "resource-type"));
+ vmResourceLinkList.add(vmResourceLink);
+ }
+ });
return vmResourceLinkList;
}
private List<Relationship> convertRelationships(JsonNode relationshipNode) {
List<Relationship> relationshipList = new ArrayList<>();
- if (relationshipNode.isArray()) {
- relationshipNode.forEach(node ->{
- Relationship relationship = new Relationship();
- relationship.setRelatedLink(getTextElementByNode(node, "related-link"));
- relationship.setRelatedTo(getTextElementByNode(node, "related-to"));
- if (node.has("related-to-property")) {
- JsonNode relatedToPropertyNode = node.get("related-to-property");
- relationship.setRelatedToPropertyList(
- convertRelatedToProperty(relatedToPropertyNode));
- } else {
- relationship.setRelatedToPropertyList(Collections.emptyList());
- }
- if (node.has("relationship-data")) {
- JsonNode relationshipDataNode = node.get("relationship-data");
- relationship
- .setRelationshipDataList(convertRelationshipDate(relationshipDataNode));
- } else {
- relationship.setRelationshipDataList(Collections.emptyList());
- }
- relationshipList.add(relationship);
- });
- }
+ relationshipNode.forEach(node ->{
+ Relationship relationship = new Relationship();
+ relationship.setRelatedLink(getTextElementByNode(node, "related-link"));
+ relationship.setRelatedTo(getTextElementByNode(node, "related-to"));
+ if (node.has("related-to-property")) {
+ JsonNode relatedToPropertyNode = node.get("related-to-property");
+ relationship.setRelatedToPropertyList(
+ convertRelatedToProperty(relatedToPropertyNode));
+ } else {
+ relationship.setRelatedToPropertyList(Collections.emptyList());
+ }
+ if (node.has("relationship-data")) {
+ JsonNode relationshipDataNode = node.get("relationship-data");
+ relationship
+ .setRelationshipDataList(convertRelationshipDate(relationshipDataNode));
+ } else {
+ relationship.setRelationshipDataList(Collections.emptyList());
+ }
+ relationshipList.add(relationship);
+ });
return relationshipList;
}
private List<RelationshipData> convertRelationshipDate(JsonNode relationshipDataNode) {
List<RelationshipData> relationshipDataList = new ArrayList<>();
- if (relationshipDataNode.isArray()) {
- relationshipDataNode.forEach(node ->{
- RelationshipData relationshipData = new RelationshipData();
- relationshipData.setRelationshipKey(
- getTextElementByNode(node,"relationship-key"));
- relationshipData.setRelationshipValue(
- getTextElementByNode(node,"relationship-value"));
- relationshipDataList.add(relationshipData);
- });
- }
+ relationshipDataNode.forEach(node ->{
+ RelationshipData relationshipData = new RelationshipData();
+ relationshipData.setRelationshipKey(
+ getTextElementByNode(node,"relationship-key"));
+ relationshipData.setRelationshipValue(
+ getTextElementByNode(node,"relationship-value"));
+ relationshipDataList.add(relationshipData);
+ });
return relationshipDataList;
}
private List<RelatedToProperty> convertRelatedToProperty(JsonNode relatedToPropertyNode) {
List<RelatedToProperty> relatedToPropertyList = new ArrayList<>();
- if (relatedToPropertyNode.isArray()) {
- relatedToPropertyNode.forEach(node ->{
- RelatedToProperty relatedToProperty = new RelatedToProperty();
- relatedToProperty
- .setPropertyKey(getTextElementByNode(node, "property-key"));
- relatedToProperty.setPropertyValue(
- getTextElementByNode(node, "property-value"));
- relatedToPropertyList.add(relatedToProperty);
- });
- }
+ relatedToPropertyNode.forEach(node ->{
+ RelatedToProperty relatedToProperty = new RelatedToProperty();
+ relatedToProperty
+ .setPropertyKey(getTextElementByNode(node, "property-key"));
+ relatedToProperty.setPropertyValue(
+ getTextElementByNode(node, "property-value"));
+ relatedToPropertyList.add(relatedToProperty);
+ });
return relatedToPropertyList;
}
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/CorrelationUtil.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/CorrelationUtil.java
index 894b6f1..9295f53 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/CorrelationUtil.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/CorrelationUtil.java
@@ -18,12 +18,13 @@ import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.onap.holmes.common.aai.entity.RelationshipList.Relationship;
import org.onap.holmes.common.aai.entity.VmEntity;
+import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
import org.onap.holmes.common.exception.CorrelationException;
@Slf4j
public class CorrelationUtil {
- private AaiQuery aaiQuery;
+ private static AaiQuery aaiQuery;
private static class LazyHolder {
private static final CorrelationUtil INSTANCE = new CorrelationUtil();
@@ -31,31 +32,33 @@ public class CorrelationUtil {
private CorrelationUtil (){}
public static final CorrelationUtil getInstance() {
+ if (aaiQuery == null) {
+ aaiQuery = ServiceLocatorHolder.getLocator().getService(AaiQuery.class);
+ }
return LazyHolder.INSTANCE;
}
- public boolean isTopologicallyRelated(String eventId, String sourceId, String sourceName) {
-
- return Optional.ofNullable(getVmEntity(sourceId, sourceName)).map(vmEntity ->
- getIsRelated(eventId, vmEntity)).orElse(false);
+ public boolean isTopologicallyRelated(String sourceId, String rootSourceId, String rootSourceName) {
+ return Optional.ofNullable(getVmEntity(rootSourceId, rootSourceName)).map(vmEntity ->
+ getIsRelated(sourceId, vmEntity)).orElse(false);
}
- private boolean getIsRelated(String eventId, VmEntity vmEntity) {
+ private boolean getIsRelated(String sourceId, VmEntity vmEntity) {
List<Relationship> relationships = vmEntity.getRelationshipList().getRelationships();
for (Relationship relationship : relationships) {
boolean isRelated = relationship.getRelationshipDataList().stream().anyMatch(
- relationshipData -> relationshipData.getRelationshipValue().equals(eventId));
+ relationshipData -> relationshipData.getRelationshipValue().equals(sourceId));
if (isRelated) {
return true;
}
}
- return false;
+ return false;
}
- private VmEntity getVmEntity(String sourceId, String sourceName) {
+ private VmEntity getVmEntity(String rootSourceId, String rootSourceName) {
VmEntity vmEntity = null;
try {
- vmEntity = aaiQuery.getAaiVmData(sourceId, sourceName);
+ vmEntity = aaiQuery.getAaiVmData(rootSourceId, rootSourceName);
} catch (CorrelationException e) {
log.error("Failed to get vm data", e.getMessage());
}
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/config/AaiConfig.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/config/AaiConfig.java
index e3bacc1..0161eb0 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/config/AaiConfig.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/config/AaiConfig.java
@@ -15,9 +15,9 @@ package org.onap.holmes.common.aai.config;
public class AaiConfig {
- public static String VNF_ADDR = "/aai/v11/network/generic-vnfs/generic-vnf?";
+ public static String AAI_VNF_ADDR = "/aai/v11/network/generic-vnfs/generic-vnf?";
- public static String VM_ADDR = "/aai/v11/search/nodes-query?search-node-type=vserver&filter=";
+ public static String AAI_VM_ADDR = "/aai/v11/search/nodes-query?search-node-type=vserver&filter=";
public static String X_TRANSACTION_ID = "9999";
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/VesAlarm.java b/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/VesAlarm.java
index 127903f..918ed6c 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/VesAlarm.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/VesAlarm.java
@@ -51,6 +51,7 @@ public class VesAlarm implements Cloneable, Serializable{
private Long faultFieldsVersion;
private String specificProblem;
private String vfStatus;
+ private String parentId;
@Override
public int hashCode() {
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 bb78288..01a5f2e 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
@@ -22,7 +22,6 @@ import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.glassfish.jersey.client.ClientConfig;
-import org.onap.holmes.common.api.stat.Alarm;
import org.onap.holmes.common.constant.AlarmConst;
@Slf4j
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 9c3d19b..79c861b 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
@@ -15,10 +15,13 @@
*/
package org.onap.holmes.common.dmaap;
+import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.List;
import java.util.Optional;
+import javax.inject.Inject;
import lombok.AllArgsConstructor;
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.RelationshipData;
import org.onap.holmes.common.aai.entity.VmEntity;
@@ -27,29 +30,35 @@ import org.onap.holmes.common.api.stat.VesAlarm;
import org.onap.holmes.common.dmaap.entity.PolicyMsg;
import org.onap.holmes.common.dmaap.entity.PolicyMsg.EVENT_STATUS;
import org.onap.holmes.common.exception.CorrelationException;
+import org.onap.holmes.common.utils.JacksonUtil;
@Slf4j
-@AllArgsConstructor
+@Service
public class DmaapService {
- private static AaiQuery aaiQuery;
- private static Publisher publisher;
+ @Inject
+ private AaiQuery aaiQuery;
+ @Inject
+ private Publisher publisher;
- public static void publishPolicyMsg(PolicyMsg policyMsg) {
+ public void publishPolicyMsg(PolicyMsg policyMsg) {
try {
publisher.publish(policyMsg);
+ log.info("send policyMsg: " + JacksonUtil.beanToJson(policyMsg));
} catch (CorrelationException e) {
log.error("Failed to publish policyMsg to dmaap", e.getMessage());
+ } catch (JsonProcessingException e) {
+ log.info("Failed to convert policyMsg to json");
}
}
- public static PolicyMsg getPolicyMsg(VesAlarm vesAlarm) {
+ public PolicyMsg getPolicyMsg(VesAlarm vesAlarm) {
return Optional.ofNullable(getVmEntity(vesAlarm.getSourceId(), vesAlarm.getSourceName()))
.map(vmEntity -> getEnrichedPolicyMsg(vmEntity, vesAlarm))
.orElse(getDefaultPolicyMsg(vesAlarm.getSourceName()));
}
- private static String getVserverInstanceId(VnfEntity vnfEntity) {
+ private String getVserverInstanceId(VnfEntity vnfEntity) {
String vserverInstanceId = "";
if (vnfEntity != null) {
List<RelationshipData> relationshipDataList = vnfEntity.getRelationshipList()
@@ -65,7 +74,7 @@ public class DmaapService {
return vserverInstanceId;
}
- private static VnfEntity getVnfEntity(String vnfId, String vnfName) {
+ private VnfEntity getVnfEntity(String vnfId, String vnfName) {
VnfEntity vnfEntity = null;
try {
vnfEntity = aaiQuery.getAaiVnfData(vnfId, vnfName);
@@ -75,7 +84,7 @@ public class DmaapService {
return vnfEntity;
}
- private static VmEntity getVmEntity(String sourceId, String sourceName) {
+ private VmEntity getVmEntity(String sourceId, String sourceName) {
VmEntity vmEntity = null;
try {
vmEntity = aaiQuery.getAaiVmData(sourceId, sourceName);
@@ -85,7 +94,7 @@ public class DmaapService {
return vmEntity;
}
- private static PolicyMsg getEnrichedPolicyMsg(VmEntity vmEntity, VesAlarm vesAlarm) {
+ private PolicyMsg getEnrichedPolicyMsg(VmEntity vmEntity, VesAlarm vesAlarm) {
VnfEntity vnfEntity = getVnfEntity(vesAlarm.getEventId(), vesAlarm.getEventName());
String vserverInstatnceId = getVserverInstanceId(vnfEntity);
PolicyMsg policyMsg = new PolicyMsg();
@@ -118,7 +127,7 @@ public class DmaapService {
return policyMsg;
}
- private static PolicyMsg getDefaultPolicyMsg(String sourceName) {
+ private PolicyMsg getDefaultPolicyMsg(String sourceName) {
PolicyMsg policyMsg = new PolicyMsg();
policyMsg.setTarget("vserver.vserver-name");
policyMsg.setTargetType("VM");
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java
index 3ef1201..441261f 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java
@@ -32,6 +32,7 @@ import org.apache.http.ParseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
@@ -79,73 +80,88 @@ public class HttpsUtils {
public static String post(String url, Map<String, String> header, Map<String, String> param,
HttpEntity entity) throws Exception {
- String result = "";
- CloseableHttpClient httpClient = null;
+ HttpResponse httpResponse = null;
try {
- httpClient = getHttpClient();
- HttpPost httpPost = new HttpPost(url);
- if (!header.isEmpty()) {
- for (Map.Entry<String, String> entry : header.entrySet()) {
- httpPost.addHeader(entry.getKey(), entry.getValue());
- }
+ CloseableHttpClient httpClient = getHttpClient();
+ HttpPost httpPost = getHttpPost(url, header, param, entity);
+ httpResponse = getHttpResponse(httpClient, httpPost);
+ } catch (Exception e) {
+ throw new CorrelationException("Failed to use post method query data from server");
+ }
+
+ return getResponseEntity(httpResponse);
+ }
+
+ public static String get(String url, Map<String, String> header) throws Exception {
+ HttpResponse httpResponse = null;
+ try {
+ CloseableHttpClient httpClient = getHttpClient();
+ HttpGet httpGet = getHttpGet(url, header);
+ httpResponse = getHttpResponse(httpClient, httpGet);
+ } catch (Exception e) {
+ throw new CorrelationException("Failed to use get method query data from server");
+ }
+ return getResponseEntity(httpResponse);
+ }
+
+ private static HttpPost getHttpPost(String url, Map<String, String> header,
+ Map<String, String> param, HttpEntity entity) {
+ HttpPost httpPost = new HttpPost(url);
+ if (!header.isEmpty()) {
+ for (Map.Entry<String, String> entry : header.entrySet()) {
+ httpPost.addHeader(entry.getKey(), entry.getValue());
}
- if (!param.isEmpty()) {
- List<NameValuePair> formparams = new ArrayList<>();
- for (Map.Entry<String, String> entry : param.entrySet()) {
- formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
- }
- UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formparams,
- Consts.UTF_8);
- httpPost.setEntity(urlEncodedFormEntity);
+ }
+ if (!param.isEmpty()) {
+ List<NameValuePair> formparams = new ArrayList<>();
+ for (Map.Entry<String, String> entry : param.entrySet()) {
+ formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
- if (entity != null) {
- httpPost.setEntity(entity);
+ UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formparams,
+ Consts.UTF_8);
+ httpPost.setEntity(urlEncodedFormEntity);
+ }
+ if (entity != null) {
+ httpPost.setEntity(entity);
+ }
+ return httpPost;
+ }
+
+ private static HttpGet getHttpGet(String url, Map<String, String> header) {
+ HttpGet httpGet = new HttpGet(url);
+ if (!header.isEmpty()) {
+ for (Map.Entry<String, String> entry : header.entrySet()) {
+ httpGet.addHeader(entry.getKey(), entry.getValue());
}
- HttpResponse httpResponse = httpClient.execute(httpPost);
+ }
+ return httpGet;
+ }
+
+ private static String getResponseEntity(HttpResponse httpResponse) throws IOException {
+ String result = "";
+ if (httpResponse != null) {
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity resEntity = httpResponse.getEntity();
result = EntityUtils.toString(resEntity);
- } else {
- readHttpResponse(httpResponse);
- }
- } catch (Exception e) {
- throw new CorrelationException("Failed to use post method to get data from server");
- } finally {
- if (httpClient != null) {
- httpClient.close();
}
}
return result;
}
- public static String get(String url, Map<String, String> header) throws Exception {
- String result = "";
- CloseableHttpClient httpClient = null;
+ private static HttpResponse getHttpResponse(CloseableHttpClient httpClient, HttpRequestBase httpRequest)
+ throws Exception {
+ HttpResponse httpResponse = null;
try {
- httpClient = getHttpClient();
- HttpGet httpGet = new HttpGet(url);
- if (!header.isEmpty()) {
- for (Map.Entry<String, String> entry : header.entrySet()) {
- httpGet.addHeader(entry.getKey(), entry.getValue());
- }
- }
- HttpResponse httpResponse = httpClient.execute(httpGet);
- int statusCode = httpResponse.getStatusLine().getStatusCode();
- if (statusCode == HttpStatus.SC_OK) {
- HttpEntity resEntity = httpResponse.getEntity();
- result = EntityUtils.toString(resEntity);
- } else {
- readHttpResponse(httpResponse);
- }
+ httpResponse = httpClient.execute(httpRequest);
} catch (Exception e) {
- throw new CorrelationException("Failed to use get method get data from server");
+ throw new CorrelationException("Failed to get data from server");
} finally {
if (httpClient != null) {
httpClient.close();
}
}
- return result;
+ return httpResponse;
}
private static CloseableHttpClient getHttpClient() throws Exception {
@@ -156,22 +172,4 @@ public class HttpsUtils {
.build();
return httpClient;
}
-
- private static String readHttpResponse(HttpResponse httpResponse)
- throws ParseException, IOException {
- StringBuilder builder = new StringBuilder();
- HttpEntity entity = httpResponse.getEntity();
- builder.append("status:" + httpResponse.getStatusLine());
- builder.append("headers:");
- HeaderIterator iterator = httpResponse.headerIterator();
- while (iterator.hasNext()) {
- builder.append("\t" + iterator.next());
- }
- if (entity != null) {
- String responseString = EntityUtils.toString(entity);
- builder.append("response length:" + responseString.length());
- builder.append("response content:" + responseString.replace("\r\n", ""));
- }
- return builder.toString();
- }
}
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 f089881..59f8848 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
@@ -301,4 +301,64 @@ public class AaiQueryTest {
assertThat(actual.get("Authorization"), equalTo("Basic QUFJOkFBSQ=="));
assertThat(actual.get("Accept"), equalTo("application/json"));
}
+
+ @Test
+ public void testAaiQuery_getBaseUrl_msb() throws Exception {
+ PowerMock.resetAll();
+ aaiQuery = new AaiQuery();
+
+ PowerMockito.mockStatic(MicroServiceConfig.class);
+ when(MicroServiceConfig.getMsbServerAddr()).thenReturn("msb");
+ when(MicroServiceConfig.getServiceAddrInfoFromCBS("nihao")).thenReturn("");
+
+ PowerMock.replayAll();
+ String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
+ PowerMock.verifyAll();
+ assertThat(actual, equalTo("msburl"));
+ }
+
+ @Test
+ public void testAaiQuery_getBaseUrl_aaiurl() throws Exception {
+ PowerMock.resetAll();
+ aaiQuery = new AaiQuery();
+
+ PowerMockito.mockStatic(MicroServiceConfig.class);
+ when(MicroServiceConfig.getMsbServerAddr()).thenThrow(new NullPointerException());
+ when(MicroServiceConfig.getServiceAddrInfoFromCBS("aai_config")).thenReturn("aai");
+
+ PowerMock.replayAll();
+ String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
+ System.out.println(actual);
+ PowerMock.verifyAll();
+ assertThat(actual, equalTo("https:\\\\aaiurl"));
+ }
+
+ @Test
+ public void testAaiQuery_getBaseUrl_exception() throws Exception {
+ PowerMock.resetAll();
+ aaiQuery = new AaiQuery();
+
+ PowerMockito.mockStatic(MicroServiceConfig.class);
+ when(MicroServiceConfig.getMsbServerAddr()).thenThrow(new NullPointerException());
+ when(MicroServiceConfig.getServiceAddrInfoFromCBS("aai_config"))
+ .thenThrow(new NullPointerException());
+
+ PowerMock.replayAll();
+ String actual = Whitebox.invokeMethod(aaiQuery,"getBaseUrl", "url");
+ System.out.println(actual);
+ PowerMock.verifyAll();
+ assertThat(actual, equalTo(""));
+ }
+
+ @Test
+ public void testAaiQuery_getMsbSuffixAddr_Ok() throws Exception {
+ PowerMock.resetAll();
+ String url = "/aai/v11/network/generic-vnfs/generic-vnf?";
+ String expect = "/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/aai/CorrelationUtilTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/aai/CorrelationUtilTest.java
index f5da5df..baf5205 100644
--- a/holmes-actions/src/test/java/org/onap/holmes/common/aai/CorrelationUtilTest.java
+++ b/holmes-actions/src/test/java/org/onap/holmes/common/aai/CorrelationUtilTest.java
@@ -46,9 +46,10 @@ public class CorrelationUtilTest {
@Before
public void testCorrelationUtil() {
- correlationUtil = CorrelationUtil.getInstance();
aaiQuery = PowerMock.createMock(AaiQuery.class);
- Whitebox.setInternalState(correlationUtil, "aaiQuery", aaiQuery);
+ Whitebox.setInternalState(CorrelationUtil.class, "aaiQuery", aaiQuery);
+ correlationUtil = CorrelationUtil.getInstance();
+ PowerMock.replayAll();
}
@Test
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 b6f57ce..7198ec8 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
@@ -51,13 +51,15 @@ public class DmaapServiceTest {
private AaiQuery aaiQuery;
+ private DmaapService dmaapService;
+
@Before
public void setUp() {
+ dmaapService = new DmaapService();
publisher = PowerMock.createMock(Publisher.class);
- Whitebox.setInternalState(DmaapService.class, "publisher", publisher);
+ Whitebox.setInternalState(dmaapService, "publisher", publisher);
aaiQuery = PowerMock.createMock(AaiQuery.class);
- Whitebox.setInternalState(DmaapService.class, "aaiQuery", aaiQuery);
- PowerMock.replayAll();
+ Whitebox.setInternalState(dmaapService, "aaiQuery", aaiQuery);
}
@Test
@@ -67,7 +69,7 @@ public class DmaapServiceTest {
PowerMock.expectPrivate(publisher, "publish", anyObject(PolicyMsg.class)).andReturn(true)
.anyTimes();
PowerMock.replayAll();
- Whitebox.invokeMethod(DmaapService.class, "publishPolicyMsg", policyMsg);
+ Whitebox.invokeMethod(dmaapService, "publishPolicyMsg", policyMsg);
PowerMock.verifyAll();
}
@@ -78,7 +80,7 @@ public class DmaapServiceTest {
PowerMock.expectPrivate(publisher, "publish", policyMsg)
.andThrow(new CorrelationException("")).anyTimes();
PowerMock.replayAll();
- Whitebox.invokeMethod(DmaapService.class, "publishPolicyMsg", policyMsg);
+ Whitebox.invokeMethod(dmaapService, "publishPolicyMsg", policyMsg);
PowerMock.verifyAll();
}
@@ -88,7 +90,7 @@ public class DmaapServiceTest {
PowerMock.replayAll();
PolicyMsg policyMsg = Whitebox
- .invokeMethod(DmaapService.class, "getDefaultPolicyMsg", "tetss");
+ .invokeMethod(dmaapService, "getDefaultPolicyMsg", "tetss");
PowerMock.verifyAll();
assertThat(policyMsg.getTarget(), equalTo("vserver.vserver-name"));
@@ -105,7 +107,7 @@ public class DmaapServiceTest {
anyObject(String.class)).andReturn(expect).anyTimes();
PowerMock.replayAll();
VnfEntity actual = Whitebox
- .invokeMethod(DmaapService.class, "getVnfEntity", "tset", "test");
+ .invokeMethod(dmaapService, "getVnfEntity", "tset", "test");
PowerMock.verifyAll();
assertThat(actual.getVnfName(), equalTo("test"));
@@ -117,7 +119,7 @@ public class DmaapServiceTest {
PowerMock.expectPrivate(aaiQuery, "getAaiVnfData", anyObject(String.class),
anyObject(String.class)).andThrow(new CorrelationException("")).anyTimes();
PowerMock.replayAll();
- VnfEntity actual = Whitebox.invokeMethod(DmaapService.class, "getVnfEntity", "tset", "test");
+ VnfEntity actual = Whitebox.invokeMethod(dmaapService, "getVnfEntity", "tset", "test");
PowerMock.verifyAll();
assertThat(actual == null, equalTo(true));
@@ -132,7 +134,7 @@ public class DmaapServiceTest {
anyObject(String.class)).andReturn(expect).anyTimes();
PowerMock.replayAll();
VmEntity actual = Whitebox
- .invokeMethod(DmaapService.class, "getVmEntity", "tset", "test");
+ .invokeMethod(dmaapService, "getVmEntity", "tset", "test");
PowerMock.verifyAll();
assertThat(actual.getVserverId(), equalTo("11111"));
@@ -144,7 +146,7 @@ public class DmaapServiceTest {
PowerMock.expectPrivate(aaiQuery, "getAaiVmData", anyObject(String.class),
anyObject(String.class)).andThrow(new CorrelationException("")).anyTimes();
PowerMock.replayAll();
- VnfEntity actual = Whitebox.invokeMethod(DmaapService.class, "getVmEntity", "tset", "test");
+ VnfEntity actual = Whitebox.invokeMethod(dmaapService, "getVmEntity", "tset", "test");
PowerMock.verifyAll();
assertThat(actual == null, equalTo(true));
@@ -170,7 +172,7 @@ public class DmaapServiceTest {
vnfEntity.getRelationshipList().setRelationships(relationships);
PowerMock.replayAll();
- String actual = Whitebox.invokeMethod(DmaapService.class, "getVserverInstanceId", vnfEntity);
+ String actual = Whitebox.invokeMethod(dmaapService, "getVserverInstanceId", vnfEntity);
PowerMock.verifyAll();
assertThat(actual, equalTo("USUCP0PCOIL0110UJZZ01"));
@@ -182,7 +184,7 @@ public class DmaapServiceTest {
VnfEntity vnfEntity = null;
PowerMock.replayAll();
- String actual = Whitebox.invokeMethod(DmaapService.class, "getVserverInstanceId", vnfEntity);
+ String actual = Whitebox.invokeMethod(dmaapService, "getVserverInstanceId", vnfEntity);
PowerMock.verifyAll();
assertThat(actual, equalTo(""));
@@ -200,12 +202,12 @@ public class DmaapServiceTest {
vesAlarm.setEventId("11111");
vesAlarm.setEventName("3333");
- PowerMock.expectPrivate(DmaapService.class, "getVnfEntity", anyObject(String.class),
+ PowerMock.expectPrivate(dmaapService, "getVnfEntity", anyObject(String.class),
anyObject(String.class)).andReturn(null).anyTimes();
PowerMock.replayAll();
PolicyMsg actual = Whitebox
- .invokeMethod(DmaapService.class, "getEnrichedPolicyMsg", vmEntity, vesAlarm);
+ .invokeMethod(dmaapService, "getEnrichedPolicyMsg", vmEntity, vesAlarm);
PowerMock.verifyAll();
assertThat(actual.getPolicyName(), equalTo("vLoadBalancer"));