diff options
Diffstat (limited to 'controlloop/common/actors/actor.vfc')
2 files changed, 189 insertions, 168 deletions
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 c2de78c2c..839b07e2b 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2017 Intel Corp. All rights reserved. + * Copyright (C) 2017-2018 Intel Corp. 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. @@ -18,120 +18,133 @@ package org.onap.policy.controlloop.actor.vfc; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; + import java.util.Collections; import java.util.List; import java.util.UUID; -import org.onap.policy.controlloop.VirtualControlLoopEvent; -import org.onap.policy.vfc.VFCRequest; -import org.onap.policy.vfc.VFCHealRequest; -import org.onap.policy.vfc.VFCHealAdditionalParams; -import org.onap.policy.vfc.VFCHealActionVmInfo; + +import org.onap.policy.aai.AAIGETVnfResponse; +import org.onap.policy.aai.AAIManager; import org.onap.policy.controlloop.ControlLoopOperation; +import org.onap.policy.controlloop.VirtualControlLoopEvent; +import org.onap.policy.controlloop.actorserviceprovider.spi.Actor; import org.onap.policy.controlloop.policy.Policy; import org.onap.policy.drools.system.PolicyEngine; import org.onap.policy.rest.RESTManager; -import org.onap.policy.controlloop.actorserviceprovider.spi.Actor; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import org.onap.policy.aai.AAIManager; -import org.onap.policy.aai.AAIGETVnfResponse; +import org.onap.policy.vfc.VFCHealActionVmInfo; +import org.onap.policy.vfc.VFCHealAdditionalParams; +import org.onap.policy.vfc.VFCHealRequest; +import org.onap.policy.vfc.VFCRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class VFCActorServiceProvider implements Actor { - private static final Logger logger = LoggerFactory.getLogger(VFCActorServiceProvider.class); - - // Strings for VFC Actor - private static final String VFC_ACTOR = "VFC"; - - // Strings for targets - private static final String TARGET_VM = "VM"; - - // Strings for recipes - private static final String RECIPE_RESTART = "Restart"; - - private static final ImmutableList<String> recipes = ImmutableList.of(RECIPE_RESTART); - private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>() - .put(RECIPE_RESTART, ImmutableList.of(TARGET_VM)).build(); - - @Override - public String actor() { - return VFC_ACTOR; - } - - @Override - public List<String> recipes() { - return ImmutableList.copyOf(recipes); - } - - @Override - public List<String> recipeTargets(String recipe) { - return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList())); - } - - @Override - public List<String> recipePayloads(String recipe) { - return Collections.emptyList(); - } - - public static VFCRequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation, - Policy policy, AAIGETVnfResponse vnfResponse) { - - // Construct an VFC request - VFCRequest request = new VFCRequest(); - String serviceInstance = onset.getAAI().get("service-instance.service-instance-id"); - if (serviceInstance == null || "".equals(serviceInstance)) - { - AAIGETVnfResponse tempVnfResp = vnfResponse; - if(tempVnfResp == null) //if the response is null, we haven't queried - { - tempVnfResp = getAAIServiceInstance(onset); //This does the AAI query since we haven't already - if (tempVnfResp == null) - return null; - } - serviceInstance = tempVnfResp.getServiceId(); - } - request.setNSInstanceId(serviceInstance); - request.setRequestId(onset.getRequestID()); - request.setHealRequest(new VFCHealRequest()); - request.getHealRequest().setVnfInstanceId(onset.getAAI().get("generic-vnf.vnf-id")); - request.getHealRequest().setCause(operation.getMessage()); - request.getHealRequest().setAdditionalParams(new VFCHealAdditionalParams()); - - if (policy.getRecipe().toLowerCase().equalsIgnoreCase(RECIPE_RESTART)) { - request.getHealRequest().getAdditionalParams().setAction("restartvm"); - request.getHealRequest().getAdditionalParams().setActionInfo(new VFCHealActionVmInfo()); - request.getHealRequest().getAdditionalParams().getActionInfo().setVmid(onset.getAAI().get("vserver.vserver-id")); - request.getHealRequest().getAdditionalParams().getActionInfo().setVmname(onset.getAAI().get("vserver.vserver-name")); - } - else { - return null; - } - return request; - } - - private static AAIGETVnfResponse getAAIServiceInstance(VirtualControlLoopEvent event) { - AAIGETVnfResponse response = null; - UUID requestID = event.getRequestID(); - String vnfName = event.getAAI().get("generic-vnf.vnf-name"); - String vnfID = event.getAAI().get("generic-vnf.vnf-id"); - 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 = aaiUrl + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name="; - response = new AAIManager(new RESTManager()).getQueryByVnfName(url, aaiUsername, aaiPassword, requestID, vnfName); - } else if (vnfID != null) { - String url = aaiUrl + "/aai/v11/network/generic-vnfs/generic-vnf/"; - response = new AAIManager(new RESTManager()).getQueryByVnfID(url, aaiUsername, aaiPassword, requestID, vnfID); - } else { - logger.error("getAAIServiceInstance failed"); - } - } catch (Exception e) { - logger.error("getAAIServiceInstance exception: ", e); - } - return response; - } + private static final Logger logger = LoggerFactory.getLogger(VFCActorServiceProvider.class); + + // Strings for VFC Actor + private static final String VFC_ACTOR = "VFC"; + + // Strings for targets + private static final String TARGET_VM = "VM"; + + // Strings for recipes + private static final String RECIPE_RESTART = "Restart"; + + private static final ImmutableList<String> recipes = ImmutableList.of(RECIPE_RESTART); + private static final ImmutableMap<String, List<String>> targets = + new ImmutableMap.Builder<String, List<String>>().put(RECIPE_RESTART, ImmutableList.of(TARGET_VM)).build(); + + @Override + public String actor() { + return VFC_ACTOR; + } + + @Override + public List<String> recipes() { + return ImmutableList.copyOf(recipes); + } + + @Override + public List<String> recipeTargets(String recipe) { + return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList())); + } + + @Override + public List<String> recipePayloads(String recipe) { + return Collections.emptyList(); + } + + /** + * Construct a request. + * + * @param onset the onset event + * @param operation the control loop operation + * @param policy the policy + * @param vnfResponse the VNF response + * @return the constructed request + */ + public static VFCRequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation, + Policy policy, AAIGETVnfResponse vnfResponse) { + + // Construct an VFC request + VFCRequest request = new VFCRequest(); + String serviceInstance = onset.getAAI().get("service-instance.service-instance-id"); + if (serviceInstance == null || "".equals(serviceInstance)) { + AAIGETVnfResponse tempVnfResp = vnfResponse; + if (tempVnfResp == null) { // if the response is null, we haven't queried + // This does the AAI query since we haven't already + tempVnfResp = getAaiServiceInstance(onset); + if (tempVnfResp == null) { + return null; + } + } + serviceInstance = tempVnfResp.getServiceId(); + } + request.setNSInstanceId(serviceInstance); + request.setRequestId(onset.getRequestID()); + request.setHealRequest(new VFCHealRequest()); + request.getHealRequest().setVnfInstanceId(onset.getAAI().get("generic-vnf.vnf-id")); + request.getHealRequest().setCause(operation.getMessage()); + request.getHealRequest().setAdditionalParams(new VFCHealAdditionalParams()); + + if (policy.getRecipe().toLowerCase().equalsIgnoreCase(RECIPE_RESTART)) { + request.getHealRequest().getAdditionalParams().setAction("restartvm"); + request.getHealRequest().getAdditionalParams().setActionInfo(new VFCHealActionVmInfo()); + request.getHealRequest().getAdditionalParams().getActionInfo() + .setVmid(onset.getAAI().get("vserver.vserver-id")); + request.getHealRequest().getAdditionalParams().getActionInfo() + .setVmname(onset.getAAI().get("vserver.vserver-name")); + } else { + return null; + } + return request; + } + + private static AAIGETVnfResponse getAaiServiceInstance(VirtualControlLoopEvent event) { + AAIGETVnfResponse response = null; + UUID requestId = event.getRequestID(); + String vnfName = event.getAAI().get("generic-vnf.vnf-name"); + String vnfId = event.getAAI().get("generic-vnf.vnf-id"); + 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 = aaiUrl + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name="; + response = new AAIManager(new RESTManager()).getQueryByVnfName(url, aaiUsername, aaiPassword, requestId, + vnfName); + } else if (vnfId != null) { + String url = aaiUrl + "/aai/v11/network/generic-vnfs/generic-vnf/"; + response = new AAIManager(new RESTManager()).getQueryByVnfID(url, aaiUsername, aaiPassword, requestId, + vnfId); + } else { + logger.error("getAAIServiceInstance failed"); + } + } catch (Exception e) { + logger.error("getAAIServiceInstance exception: ", e); + } + return response; + } } diff --git a/controlloop/common/actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/TestVFCActorServiceProvider.java b/controlloop/common/actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/TestVFCActorServiceProvider.java index 7919af446..39be472ae 100644 --- a/controlloop/common/actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/TestVFCActorServiceProvider.java +++ b/controlloop/common/actors/actor.vfc/src/test/java/org/onap/policy/controlloop/actor/vfc/TestVFCActorServiceProvider.java @@ -20,7 +20,10 @@ package org.onap.policy.controlloop.actor.vfc; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; import java.util.UUID; @@ -37,72 +40,77 @@ import org.onap.policy.simulators.Util; import org.onap.policy.vfc.VFCRequest; public class TestVFCActorServiceProvider { - @BeforeClass - public static void setUpSimulator() { - try { - Util.buildAaiSim(); - } catch (Exception e) { - fail(e.getMessage()); - } - } - - @AfterClass - public static void tearDownSimulator() { - HttpServletServer.factory.destroy(); - } - - @Test - public void testConstructRequest() { - VirtualControlLoopEvent onset = new VirtualControlLoopEvent(); - ControlLoopOperation operation = new ControlLoopOperation(); - - Policy policy = new Policy(); - policy.setRecipe("GoToOz"); - - assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null)); - - onset.getAAI().put("generic-vnf.vnf-id", "dorothy.gale.1939"); - assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null)); - - PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666"); + + /** + * Set up for test class. + */ + @BeforeClass + public static void setUpSimulator() { + try { + Util.buildAaiSim(); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @AfterClass + public static void tearDownSimulator() { + HttpServletServer.factory.destroy(); + } + + @Test + public void testConstructRequest() { + VirtualControlLoopEvent onset = new VirtualControlLoopEvent(); + ControlLoopOperation operation = new ControlLoopOperation(); + + Policy policy = new Policy(); + policy.setRecipe("GoToOz"); + + assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null)); + + onset.getAAI().put("generic-vnf.vnf-id", "dorothy.gale.1939"); + assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null)); + + PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666"); PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI"); PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI"); - assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null)); - - UUID requestID = UUID.randomUUID(); - onset.setRequestID(requestID); - assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null)); - - onset.getAAI().put("generic-vnf.vnf-name", "Dorothy"); + assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null)); + + UUID requestId = UUID.randomUUID(); + onset.setRequestID(requestId); + assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null)); + + onset.getAAI().put("generic-vnf.vnf-name", "Dorothy"); PolicyEngine.manager.getEnvironment().remove("aai.password"); - assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null)); - + assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null)); + PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI"); - assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null)); - - onset.getAAI().put("service-instance.service-instance-id", ""); - assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null)); - - assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, new AAIGETVnfResponse())); - - policy.setRecipe("Restart"); - assertNotNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, new AAIGETVnfResponse())); - - VFCRequest request = VFCActorServiceProvider.constructRequest(onset, operation, policy, new AAIGETVnfResponse()); - - assertEquals(requestID, request.getRequestId()); - assertEquals("dorothy.gale.1939", request.getHealRequest().getVnfInstanceId()); - assertEquals("restartvm", request.getHealRequest().getAdditionalParams().getAction()); + assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null)); + + onset.getAAI().put("service-instance.service-instance-id", ""); + assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null)); + + assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, new AAIGETVnfResponse())); + + policy.setRecipe("Restart"); + assertNotNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, new AAIGETVnfResponse())); + + VFCRequest request = + VFCActorServiceProvider.constructRequest(onset, operation, policy, new AAIGETVnfResponse()); + + assertEquals(requestId, request.getRequestId()); + assertEquals("dorothy.gale.1939", request.getHealRequest().getVnfInstanceId()); + assertEquals("restartvm", request.getHealRequest().getAdditionalParams().getAction()); } - @Test - public void testMethods() { - VFCActorServiceProvider sp = new VFCActorServiceProvider(); + @Test + public void testMethods() { + VFCActorServiceProvider sp = new VFCActorServiceProvider(); - assertEquals("VFC", sp.actor()); - assertEquals(1, sp.recipes().size()); - assertEquals("Restart", sp.recipes().get(0)); - assertEquals("VM", sp.recipeTargets("Restart").get(0)); - assertEquals(0, sp.recipePayloads("Restart").size()); - } + assertEquals("VFC", sp.actor()); + assertEquals(1, sp.recipes().size()); + assertEquals("Restart", sp.recipes().get(0)); + assertEquals("VM", sp.recipeTargets("Restart").get(0)); + assertEquals(0, sp.recipePayloads("Restart").size()); + } } |