diff options
14 files changed, 498 insertions, 38 deletions
diff --git a/controlloop/common/actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SOActorServiceProvider.java b/controlloop/common/actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SOActorServiceProvider.java index a014c7a80..8c1b23af9 100644 --- a/controlloop/common/actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SOActorServiceProvider.java +++ b/controlloop/common/actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SOActorServiceProvider.java @@ -136,10 +136,10 @@ public class SOActorServiceProvider implements Actor { } - /** + /** * Constructs and sends an AAI vserver Named Query * - * @param eventRequestID + * @param onset * @returns the response to the AAI Named Query */ private AAINQResponseWrapper AaiNamedQueryRequest(VirtualControlLoopEvent onset) { @@ -239,9 +239,9 @@ public class SOActorServiceProvider implements Actor { int nonBaseIndex = -1; List<AAINQInventoryResponseItem> inventoryItems = namedQueryResponseWrapper.aainqresponse.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems; for (AAINQInventoryResponseItem m : inventoryItems) { - if (m.vfModule != null && m.vfModule.isBaseVfModule == true) { + if (m.vfModule != null && m.vfModule.isBaseVfModule) { baseIndex = inventoryItems.indexOf(m); - } else if (m.vfModule != null && m.vfModule.isBaseVfModule == false && m.vfModule.orchestrationStatus == null) { + } else if (m.vfModule != null && m.vfModule.orchestrationStatus == null) { nonBaseIndex = inventoryItems.indexOf(m); } // @@ -302,7 +302,7 @@ public class SOActorServiceProvider implements Actor { } // Extracted fields should not be null - if (checkExtractedFields() == false) { + if (!checkExtractedFields()) { System.err.println("some fields are missing from AAI response."); return; } @@ -338,7 +338,7 @@ public class SOActorServiceProvider implements Actor { */ public SORequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy) { - if (policy.getActor().equals("SO") && policy.getRecipe().equals("VF Module Create")) { + if ("SO".equals(policy.getActor()) && "VF Module Create".equals(policy.getRecipe())) { // perform named query request and handle response AaiNamedQueryRequest(onset); } else { @@ -348,7 +348,7 @@ public class SOActorServiceProvider implements Actor { // check if the fields extracted from named query response are // not null so we can proceed with SO request - if (checkExtractedFields() == false) { + if (!checkExtractedFields()) { System.err.println("AAI response is missing some required fields. Cannot proceed with SO Request construction."); return null; @@ -553,7 +553,7 @@ public class SOActorServiceProvider implements Actor { } /** - * @param serviceItemModeInvariantlId the serviceItemModelInvariantId to set + * @param serviceItemModelInvariantId the serviceItemModelInvariantId to set */ private void setServiceItemModelInvariantId(String serviceItemModelInvariantId) { this.serviceItemModelInvariantId = serviceItemModelInvariantId; diff --git a/controlloop/common/actors/actor.vfc/pom.xml b/controlloop/common/actors/actor.vfc/pom.xml index c52b6aed7..223be4bda 100644 --- a/controlloop/common/actors/actor.vfc/pom.xml +++ b/controlloop/common/actors/actor.vfc/pom.xml @@ -34,6 +34,11 @@ <version>1.1.0-SNAPSHOT</version> <scope>provided</scope> </dependency> - + <dependency> + <groupId>org.onap.policy.drools-pdp</groupId> + <artifactId>policy-management</artifactId> + <version>1.1.0-SNAPSHOT</version> + <scope>provided</scope> + </dependency> </dependencies> </project> diff --git a/controlloop/common/actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VFCActorServiceProvider.java b/controlloop/common/actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VFCActorServiceProvider.java index 1ea65faa5..469faa46c 100644 --- a/controlloop/common/actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VFCActorServiceProvider.java +++ b/controlloop/common/actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VFCActorServiceProvider.java @@ -28,6 +28,7 @@ import org.onap.policy.vfc.VFCHealAdditionalParams; import org.onap.policy.vfc.VFCHealActionVmInfo; import org.onap.policy.controlloop.ControlLoopOperation; import org.onap.policy.controlloop.policy.Policy; +import org.onap.policy.drools.system.PolicyEngine; import org.onap.policy.controlloop.actorServiceProvider.spi.Actor; import com.google.common.collect.ImmutableList; @@ -66,53 +67,50 @@ public class VFCActorServiceProvider implements Actor { public static VFCRequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy) { - // Construct an VFC request VFCRequest request = new VFCRequest(); - // TODO: Verify service-instance-id is part of onset event - request.nsInstanceId = getAAIServiceInstance(onset); // onset.AAI.get("service-instance.service-instance-id"); - request.requestId = onset.requestID; + request.nsInstanceId = getAAIServiceInstance(onset); + request.requestId = onset.requestID; request.healRequest = new VFCHealRequest(); request.healRequest.vnfInstanceId = onset.AAI.get("generic-vnf.vnf-id"); request.healRequest.cause = operation.message; request.healRequest.additionalParams = new VFCHealAdditionalParams(); - switch (policy.getRecipe()) { - case "Restart": - // TODO: check target?? - request.healRequest.additionalParams.action = "restartvm"; - request.healRequest.additionalParams.actionInfo = new VFCHealActionVmInfo(); - // TODO: Verify vserver-id and vserver-name is part of onset event - request.healRequest.additionalParams.actionInfo.vmid = onset.AAI.get("vserver.vserver-id"); - request.healRequest.additionalParams.actionInfo.vmname = onset.AAI.get("vserver.vserver-name"); - break; - default: - // TODO: default - break; + if ("Restart".equalsIgnoreCase(policy.getRecipe())) { + request.healRequest.additionalParams.action = "restartvm"; + request.healRequest.additionalParams.actionInfo = new VFCHealActionVmInfo(); + request.healRequest.additionalParams.actionInfo.vmid = onset.AAI.get("vserver.vserver-id"); + request.healRequest.additionalParams.actionInfo.vmname = onset.AAI.get("vserver.vserver-name"); + } else { + request = null; } return request; } private static String getAAIServiceInstance(VirtualControlLoopEvent event) { - AAIGETVnfResponse response = null; + AAIGETVnfResponse response; UUID requestID = event.requestID; String serviceInstance = event.AAI.get("service-instance.service-instance-id"); String vnfName = event.AAI.get("generic-vnf.vnf-name"); String vnfID = event.AAI.get("generic-vnf.vnf-id"); - String urlBase = "http://localhost:6666"; - String username = "testUser"; - String password = "testPass"; if (serviceInstance == null) { + String aaiUrl = PolicyEngine.manager.getEnvironmentProperty("aai.url"); + String aaiUsername = PolicyEngine.manager.getEnvironmentProperty("aai.username"); + String aaiPassword = PolicyEngine.manager.getEnvironmentProperty("aai.password"); try { if (vnfName != null) { - String url = urlBase + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name="; - response = AAIManager.getQueryByVnfName(url, username, password, requestID, vnfName); - serviceInstance = response.serviceId; + String url = aaiUrl + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name="; + response = AAIManager.getQueryByVnfName(url, aaiUsername, aaiPassword, requestID, vnfName); + if (response != null) { + serviceInstance = response.serviceId; + } } else if (vnfID != null) { - String url = urlBase + "/aai/v11/network/generic-vnfs/generic-vnf/"; - response = AAIManager.getQueryByVnfID(url, username, password, requestID, vnfID); - serviceInstance = response.serviceId; + String url = aaiUrl + "/aai/v11/network/generic-vnfs/generic-vnf/"; + response = AAIManager.getQueryByVnfID(url, aaiUsername, aaiPassword, requestID, vnfID); + if (response != null) { + serviceInstance = response.serviceId; + } } else { logger.error("getAAIServiceInstance failed"); diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java index add981c65..e13352ac0 100644 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java +++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java @@ -375,11 +375,12 @@ public class ControlLoopOperationManager implements Serializable { } else if (response instanceof VFCResponse) { VFCResponse vfcResponse = (VFCResponse) response; + Integer operationAttempt = this.attempts; if (vfcResponse.responseDescriptor.getStatus().equalsIgnoreCase("finished")) { // // Consider it as success // - this.completeOperation(new Integer(1), " Success", PolicyResult.SUCCESS); + this.completeOperation(operationAttempt, " Success", PolicyResult.SUCCESS); if (this.policyResult != null && this.policyResult.equals(PolicyResult.FAILURE_TIMEOUT)) { return null; } @@ -388,10 +389,12 @@ public class ControlLoopOperationManager implements Serializable { // // Consider it as failure // - this.completeOperation(new Integer(1), " Failed", PolicyResult.FAILURE); + this.completeOperation(operationAttempt, " Failed", PolicyResult.FAILURE); if (this.policyResult != null && this.policyResult.equals(PolicyResult.FAILURE_TIMEOUT)) { return null; } + // increment operation attempts for retries + this.attempts += 1; return PolicyResult.FAILURE; } } diff --git a/controlloop/common/guard/src/main/java/org/onap/policy/guard/CallGuardTask.java b/controlloop/common/guard/src/main/java/org/onap/policy/guard/CallGuardTask.java index dde18ed31..d81aaa40c 100644 --- a/controlloop/common/guard/src/main/java/org/onap/policy/guard/CallGuardTask.java +++ b/controlloop/common/guard/src/main/java/org/onap/policy/guard/CallGuardTask.java @@ -53,6 +53,8 @@ public class CallGuardTask implements Runnable { requestId = reqId; target = tar; } + + @Override public void run() { long startTime = System.nanoTime(); com.att.research.xacml.api.Request request = null; diff --git a/controlloop/common/model-impl/sdc/src/main/java/org/onap/policy/sdc/ResourceType.java b/controlloop/common/model-impl/sdc/src/main/java/org/onap/policy/sdc/ResourceType.java index ee2fc89fd..c4dbfffa4 100644 --- a/controlloop/common/model-impl/sdc/src/main/java/org/onap/policy/sdc/ResourceType.java +++ b/controlloop/common/model-impl/sdc/src/main/java/org/onap/policy/sdc/ResourceType.java @@ -33,7 +33,8 @@ public enum ResourceType { this.type = type; } - public String toString() { + @Override + public String toString() { return this.type; } diff --git a/controlloop/common/msb/pom.xml b/controlloop/common/msb/pom.xml new file mode 100644 index 000000000..9b66a1159 --- /dev/null +++ b/controlloop/common/msb/pom.xml @@ -0,0 +1,51 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>model-impl</artifactId> + <groupId>org.onap.policy.drools-applications</groupId> + <version>1.1.0-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>msb</artifactId> + <dependencies> + <dependency> + <groupId>org.onap.msb.java-sdk</groupId> + <artifactId>msb-java-sdk</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.12</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.powermock</groupId> + <artifactId>powermock-module-junit4</artifactId> + <version>1.6.5</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.powermock</groupId> + <artifactId>powermock-api-mockito</artifactId> + <version>1.6.5</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-classic</artifactId> + <version>1.2.3</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-core</artifactId> + <version>1.2.3</version> + <scope>test</scope> + </dependency> + </dependencies> + +</project>
\ No newline at end of file diff --git a/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceException.java b/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceException.java new file mode 100644 index 000000000..f5bbdb5f2 --- /dev/null +++ b/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceException.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Copyright 2017 ZTE, Inc. and others. + * + * 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.policy.msb.client; + +public class MSBServiceException extends Exception { + + public MSBServiceException() { + super(); + } + + public MSBServiceException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + + public MSBServiceException(String message, Throwable cause) { + super(message, cause); + } + + public MSBServiceException(String message) { + super(message); + } + + public MSBServiceException(Throwable cause) { + super(cause); + } + +} diff --git a/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceFactory.java b/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceFactory.java new file mode 100644 index 000000000..cc3ff71e3 --- /dev/null +++ b/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceFactory.java @@ -0,0 +1,107 @@ +/*******************************************************************************
+ * Copyright 2017 ZTE, Inc. and others.
+ *
+ * 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.policy.msb.client;
+
+import org.onap.msb.sdk.discovery.common.RouteException;
+import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
+import org.onap.msb.sdk.discovery.entity.NodeInfo;
+import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Iterator;
+import java.util.Properties;
+
+
+public class MSBServiceFactory {
+ private static final Logger logger = LoggerFactory.getLogger(MSBServiceFactory.class);
+ private static final String msbPropertyFile = "msb.policy.properties";
+ private static final String MSB_IP = "msb.ip";
+ private static final String MSB_PORT = "msb.port";
+ private MSBServiceClient msbClient;
+ private Properties properties;
+
+ public MSBServiceFactory() throws MSBServiceException,IOException{
+ this.init();
+ this.msbClient = new MSBServiceClient(properties.getProperty(MSB_IP), Integer.parseInt(properties.getProperty(MSB_PORT)));
+ }
+ public MSBServiceFactory (MSBServiceClient msbClient) {
+ this.msbClient = msbClient;
+ }
+
+ private void init() throws MSBServiceException,IOException {
+ properties = new Properties();
+ Path file = Paths.get(System.getProperty(msbPropertyFile));
+ if (file == null) {
+ throw new MSBServiceException("No msb.policy.properties specified.");
+ }
+ if (Files.notExists(file)) {
+ throw new MSBServiceException("No msb.policy.properties specified.");
+ }
+
+ if (Files.isReadable(file) == false) {
+ throw new MSBServiceException ("Repository is NOT readable: " + file.toAbsolutePath());
+ }
+ try(InputStream is = new FileInputStream(file.toFile())){
+ properties.load(is);
+ }
+ }
+
+
+ public Node getNode(String serviceName,String version){
+ return this.build(serviceName,version);
+ }
+
+ public Node getNode(String actor){
+ Node node = null;
+ switch (actor) {
+ case "AAI":
+ node = this.build("aai-search","v11");
+ return node;
+ case "SO":
+ node = this.build("so","v2");
+ return node;
+ case "VFC":
+ node = this.build("nfvo-nslcm","v1");
+ return node;
+ default:
+ logger.info("MSBServiceManager: policy has an unknown actor.");
+ }
+ return node;
+ }
+
+ private Node build(String serviceName,String version){
+ Node node = new Node();
+ node.setName(serviceName);
+ try {
+ MicroServiceFullInfo serviceInfo = msbClient.queryMicroServiceInfo(serviceName,version);
+ Iterator iterator = serviceInfo.getNodes().iterator();
+ while(iterator.hasNext()) {
+ NodeInfo nodeInfo = (NodeInfo)iterator.next();
+ node.setIp(nodeInfo.getIp());
+ node.setPort(nodeInfo.getPort());
+ }
+ } catch (RouteException e) {
+ logger.info("MSBServiceManager:",e);
+ }
+ return node;
+ }
+}
diff --git a/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceManager.java b/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceManager.java new file mode 100644 index 000000000..cbff8d88a --- /dev/null +++ b/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceManager.java @@ -0,0 +1,58 @@ +/*******************************************************************************
+ * Copyright 2017 ZTE, Inc. and others.
+ *
+ * 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.policy.msb.client;
+
+import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.Serializable;
+
+public class MSBServiceManager implements Serializable {
+ private static final Logger logger = LoggerFactory.getLogger(MSBServiceManager.class);
+ private static final long serialVersionUID = -2517971308551895215L;
+ private MSBServiceFactory factory;
+
+ public MSBServiceManager() throws MSBServiceException,IOException {
+ this.factory = new MSBServiceFactory();
+ }
+
+ public MSBServiceManager(MSBServiceClient msbClient){
+
+ this.factory = new MSBServiceFactory(msbClient);
+ }
+
+ /**
+ * Get the IP and port of the components registered in the MSB
+ * @param actor AAI or SO or VFC
+ * @return
+ */
+ public Node getNode(String actor){
+
+ return factory.getNode(actor);
+ }
+
+ /**
+ * Get the IP and port of the components registered in the MSB
+ * @param serviceName the service name registered in the MSB
+ * @param version the service version registered in the MSB
+ * @return
+ */
+ public Node getNode(String serviceName,String version){
+
+ return factory.getNode(serviceName,version);
+ }
+
+}
diff --git a/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/Node.java b/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/Node.java new file mode 100644 index 000000000..5c06939bf --- /dev/null +++ b/controlloop/common/msb/src/main/java/org/onap/policy/msb/client/Node.java @@ -0,0 +1,56 @@ +/*******************************************************************************
+ * Copyright 2017 ZTE, Inc. and others.
+ *
+ * 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.policy.msb.client;
+
+import java.io.Serializable;
+
+public class Node implements Serializable {
+ private static final long serialVersionUID = -5028618045561310837L;
+ private String name;
+ private String ip;
+ private String port;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getIp() {
+ return ip;
+ }
+
+ public void setIp(String ip) {
+ this.ip = ip;
+ }
+
+ public String getPort() {
+ return port;
+ }
+
+ public void setPort(String port) {
+ this.port = port;
+ }
+
+ @Override
+ public String toString() {
+ return "Node{" +
+ "name='" + name + '\'' +
+ ", ip='" + ip + '\'' +
+ ", port='" + port + '\'' +
+ '}';
+ }
+}
diff --git a/controlloop/common/msb/src/main/resources/msb.policy.properties b/controlloop/common/msb/src/main/resources/msb.policy.properties new file mode 100644 index 000000000..bb372ec85 --- /dev/null +++ b/controlloop/common/msb/src/main/resources/msb.policy.properties @@ -0,0 +1,22 @@ +###
+# ============LICENSE_START=======================================================
+# ONAP-PDP
+# ================================================================================
+# Copyright (C) 2017 AT&T Intellectual Property. 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.
+# 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.
+# ============LICENSE_END=========================================================
+###
+
+msb.ip=127.0.0.1
+msb.port=10081
\ No newline at end of file diff --git a/controlloop/common/msb/src/test/java/org/onap/policy/msb/client/MSBServiceManagerTest.java b/controlloop/common/msb/src/test/java/org/onap/policy/msb/client/MSBServiceManagerTest.java new file mode 100644 index 000000000..9ab54f7ea --- /dev/null +++ b/controlloop/common/msb/src/test/java/org/onap/policy/msb/client/MSBServiceManagerTest.java @@ -0,0 +1,116 @@ +/*******************************************************************************
+ * Copyright 2017 ZTE, Inc. and others.
+ *
+ * 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.policy.msb.client;
+
+import org.junit.*;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.onap.msb.sdk.discovery.common.RouteException;
+import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
+import org.onap.msb.sdk.discovery.entity.NodeInfo;
+import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
+import org.onap.policy.msb.client.MSBServiceManager;
+import org.onap.policy.msb.client.Node;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.when;
+
+public class MSBServiceManagerTest {
+ @Mock
+ private MSBServiceClient msbClient;
+
+ private MSBServiceManager msbManager;
+
+ public MSBServiceManagerTest(){}
+
+ @BeforeClass
+ public static void setUpClass(){}
+
+ @AfterClass
+ public static void tearDownClass(){}
+
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ msbManager = new MSBServiceManager(msbClient);
+ }
+
+ @After
+ public void tearDown() {
+ }
+
+ @Test
+ public void testByActor () throws RouteException,UnknownHostException {
+ MicroServiceFullInfo serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8843");
+ when(msbClient.queryMicroServiceInfo("aai-search","v11")).thenReturn(serviceInfo);
+ Node node = msbManager.getNode("AAI");
+ assertNotNull(node);
+ serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8840");
+ when(msbClient.queryMicroServiceInfo("so","v2")).thenReturn(serviceInfo);
+ node = msbManager.getNode("SO");
+ assertNotNull(node);
+
+ serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8082");
+ when(msbClient.queryMicroServiceInfo("nfvo-nslcm","v1")).thenReturn(serviceInfo);
+ node = msbManager.getNode("VFC");
+ assertNotNull(node);
+
+ }
+
+ @Test
+ public void testByActor_when_actorNotExist_returnNull () throws RouteException,UnknownHostException {
+ MicroServiceFullInfo serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8843");
+ when(msbClient.queryMicroServiceInfo("aai-search","v11")).thenReturn(serviceInfo);
+ Node node = msbManager.getNode("DDD");
+ assertNull(node);
+ }
+
+ @Test
+ public void testByServiceNameAndVersion () throws RouteException,UnknownHostException {
+ MicroServiceFullInfo serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8843");
+ when(msbClient.queryMicroServiceInfo("aai-search","v11")).thenReturn(serviceInfo);
+ Node node = msbManager.getNode("aai-search","v11");
+ assertNotNull(node);
+ }
+
+ @Test
+ public void testByServiceNameAndVersion_when_serice_notRegistedToMSB () throws RouteException,UnknownHostException {
+ MicroServiceFullInfo serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8843");
+ when(msbClient.queryMicroServiceInfo("aai-search","v11")).thenThrow(new RouteException());
+ Node node = msbManager.getNode("aai-search","v11");
+ assertNotNull(node);
+ assertTrue(node.getName() == "aai-search");
+ assertTrue(node.getIp() == null);
+ assertTrue(node.getPort() == null);
+ }
+
+ public static MicroServiceFullInfo build(String ip,String port){
+ MicroServiceFullInfo serviceInfo = new MicroServiceFullInfo();
+ Set<NodeInfo> nodes = new HashSet<NodeInfo>();
+ NodeInfo node= new NodeInfo();
+ node.setPort(port);
+ node.setIp(ip);
+ nodes.add(node);
+ serviceInfo.setNodes(nodes);
+ return serviceInfo;
+ }
+
+}
diff --git a/controlloop/common/pom.xml b/controlloop/common/pom.xml index 39f1b7038..8725d7956 100644 --- a/controlloop/common/pom.xml +++ b/controlloop/common/pom.xml @@ -40,6 +40,7 @@ <module>policy-yaml</module> <module>simulators</module> <module>feature-controlloop-utils</module> + <module>msb</module> </modules> |