From deed677c3dc8751209d50e7d35132c929fe6800d Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Tue, 4 Aug 2020 16:27:18 -0400 Subject: Modify Actors to use properties when provided Modified the Actors to use properties when the application provides them instead of going to the event context for the data. This sometimes entailed moving code out of the Operation subclass constructor that used or validated the context data. Combined some property names and renamed others. Changed VF Count from AtomicInteger to Integer. Issue-ID: POLICY-2746 Change-Id: Ib8730538309bb77d2f4f6161e9a20a49362d8972 Signed-off-by: Jim Hahn --- .../controlloop/actor/cds/GrpcOperation.java | 100 ++++++++++++---- .../controlloop/actor/cds/GrpcOperationTest.java | 132 ++++++++++++++++++++- 2 files changed, 207 insertions(+), 25 deletions(-) (limited to 'models-interactions/model-actors/actor.cds/src') diff --git a/models-interactions/model-actors/actor.cds/src/main/java/org/onap/policy/controlloop/actor/cds/GrpcOperation.java b/models-interactions/model-actors/actor.cds/src/main/java/org/onap/policy/controlloop/actor/cds/GrpcOperation.java index ec8f2ac12..d3842441d 100644 --- a/models-interactions/model-actors/actor.cds/src/main/java/org/onap/policy/controlloop/actor/cds/GrpcOperation.java +++ b/models-interactions/model-actors/actor.cds/src/main/java/org/onap/policy/controlloop/actor/cds/GrpcOperation.java @@ -44,7 +44,6 @@ import org.onap.policy.aai.AaiConstants; import org.onap.policy.aai.AaiCqResponse; import org.onap.policy.cds.client.CdsProcessorGrpcClient; import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoderObject; import org.onap.policy.controlloop.actor.aai.AaiCustomQueryOperation; import org.onap.policy.controlloop.actor.aai.AaiGetPnfOperation; import org.onap.policy.controlloop.actor.cds.constants.CdsActorConstants; @@ -91,13 +90,15 @@ public class GrpcOperation extends OperationPartial { // @formatter:off private static final List PNF_PROPERTY_NAMES = List.of( OperationProperties.AAI_PNF, - OperationProperties.EVENT_ADDITIONAL_PARAMS); + OperationProperties.EVENT_ADDITIONAL_PARAMS, + OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES); private static final List VNF_PROPERTY_NAMES = List.of( - OperationProperties.AAI_MODEL_INVARIANT_GENERIC_VNF, - OperationProperties.AAI_RESOURCE_SERVICE_INSTANCE, - OperationProperties.EVENT_ADDITIONAL_PARAMS); + OperationProperties.AAI_RESOURCE_VNF, + OperationProperties.AAI_SERVICE, + OperationProperties.EVENT_ADDITIONAL_PARAMS, + OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES); // @formatter:on /** @@ -177,11 +178,15 @@ public class GrpcOperation extends OperationPartial { * @return a map of the PNF data */ private Map convertPnfToAaiProperties() { + Map result = this.getProperty(OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES); + if (result != null) { + return result; + } + // convert PNF data to a Map - StandardCoderObject pnf = params.getContext().getProperty(AaiGetPnfOperation.getKey(params.getTargetEntity())); - Map source = Util.translateToMap(getFullName(), pnf); + Map source = Util.translateToMap(getFullName(), getPnfData()); - Map result = new LinkedHashMap<>(); + result = new LinkedHashMap<>(); for (Entry ent : source.entrySet()) { result.put(AAI_PNF_PREFIX + ent.getKey(), ent.getValue().toString()); @@ -190,6 +195,26 @@ public class GrpcOperation extends OperationPartial { return result; } + /** + * Gets the PNF from the operation properties, if it exists, or from the context + * properties otherwise. + * + * @return the PNF item + */ + protected Object getPnfData() { + Object pnf = getProperty(OperationProperties.AAI_PNF); + if (pnf != null) { + return pnf; + } + + pnf = params.getContext().getProperty(AaiGetPnfOperation.getKey(params.getTargetEntity())); + if (pnf == null) { + throw new IllegalArgumentException("missing PNF data"); + } + + return pnf; + } + /** * Converts the A&AI Custom Query data to a map suitable for passing via the * "aaiProperties" field in the CDS request. @@ -197,24 +222,49 @@ public class GrpcOperation extends OperationPartial { * @return a map of the custom query data */ private Map convertCqToAaiProperties() { - AaiCqResponse aaicq = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY); + Map result = this.getProperty(OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES); + if (result != null) { + return result; + } - Map result = new LinkedHashMap<>(); + result = new LinkedHashMap<>(); + + result.put(AAI_SERVICE_INSTANCE_ID_KEY, getServiceInstanceId()); + result.put(AAI_VNF_ID_KEY, getVnfId()); + + return result; + } - ServiceInstance serviceInstance = aaicq.getServiceInstance(); + protected String getServiceInstanceId() { + ServiceInstance serviceInstance = getProperty(OperationProperties.AAI_SERVICE); + if (serviceInstance != null) { + return serviceInstance.getServiceInstanceId(); + } + + AaiCqResponse aaicq = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY); + + serviceInstance = aaicq.getServiceInstance(); if (serviceInstance == null) { throw new IllegalArgumentException("Target service instance could not be found"); } - GenericVnf genericVnf = aaicq.getGenericVnfByModelInvariantId(params.getTarget().getResourceID()); + return serviceInstance.getServiceInstanceId(); + } + + protected String getVnfId() { + GenericVnf genericVnf = getProperty(OperationProperties.AAI_RESOURCE_VNF); + if (genericVnf != null) { + return genericVnf.getVnfId(); + } + + AaiCqResponse aaicq = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY); + + genericVnf = aaicq.getGenericVnfByModelInvariantId(params.getTarget().getResourceID()); if (genericVnf == null) { throw new IllegalArgumentException("Target generic vnf could not be found"); } - result.put(AAI_SERVICE_INSTANCE_ID_KEY, serviceInstance.getServiceInstanceId()); - result.put(AAI_VNF_ID_KEY, genericVnf.getVnfId()); - - return result; + return genericVnf.getVnfId(); } @Override @@ -232,7 +282,7 @@ public class GrpcOperation extends OperationPartial { * construct the request first so that we don't have to clean up the "client" if * an exception is thrown */ - ExecutionServiceInput request = constructRequest(params); + ExecutionServiceInput request = constructRequest(); CompletableFuture future = new CompletableFuture<>(); @@ -255,10 +305,9 @@ public class GrpcOperation extends OperationPartial { * enriched parameters. TO-DO: Avoid leaking Exceptions to the Kie Session thread. TBD * item for Frankfurt release. * - * @param params the control loop parameters specifying the onset, payload, etc. * @return an ExecutionServiceInput instance. */ - public ExecutionServiceInput constructRequest(ControlLoopOperationParams params) { + public ExecutionServiceInput constructRequest() { // For the current operational TOSCA policy model (yaml) CBA name and version are // embedded in the payload @@ -295,8 +344,9 @@ public class GrpcOperation extends OperationPartial { request.setAaiProperties(aaiConverter.get()); // Inject any additional event parameters that may be present in the onset event - if (params.getContext().getEvent().getAdditionalEventParams() != null) { - request.setAdditionalEventParams(params.getContext().getEvent().getAdditionalEventParams()); + Map additionalParams = getAdditionalEventParams(); + if (additionalParams != null) { + request.setAdditionalEventParams(additionalParams); } Builder struct = Struct.newBuilder(); @@ -324,6 +374,14 @@ public class GrpcOperation extends OperationPartial { .setPayload(struct.build()).build(); } + protected Map getAdditionalEventParams() { + if (containsProperty(OperationProperties.EVENT_ADDITIONAL_PARAMS)) { + return getProperty(OperationProperties.EVENT_ADDITIONAL_PARAMS); + } + + return params.getContext().getEvent().getAdditionalEventParams(); + } + private Map convertPayloadMap(Map payload) { Map convertedPayload = new HashMap<>(); for (Entry entry : payload.entrySet()) { diff --git a/models-interactions/model-actors/actor.cds/src/test/java/org/onap/policy/controlloop/actor/cds/GrpcOperationTest.java b/models-interactions/model-actors/actor.cds/src/test/java/org/onap/policy/controlloop/actor/cds/GrpcOperationTest.java index 06f239b63..2e9935f82 100644 --- a/models-interactions/model-actors/actor.cds/src/test/java/org/onap/policy/controlloop/actor/cds/GrpcOperationTest.java +++ b/models-interactions/model-actors/actor.cds/src/test/java/org/onap/policy/controlloop/actor/cds/GrpcOperationTest.java @@ -24,6 +24,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; @@ -31,6 +32,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -49,6 +51,7 @@ import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.onap.aai.domain.yang.GenericVnf; +import org.onap.aai.domain.yang.Pnf; import org.onap.aai.domain.yang.ServiceInstance; import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput; import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput; @@ -187,6 +190,37 @@ public class GrpcOperationTest { assertTrue(outcome.getResponse() instanceof ExecutionServiceOutput); } + /** + * Tests "success" case with simulator using properties. + */ + @Test + public void testSuccessViaProperties() throws Exception { + ControlLoopEventContext context = new ControlLoopEventContext(onset); + loadCqData(context); + + Map payload = Map.of("artifact_name", "my_artifact", "artifact_version", "1.0"); + + params = ControlLoopOperationParams.builder() + .actor(CdsActorConstants.CDS_ACTOR).operation("subscribe").context(context) + .actorService(new ActorService()).targetEntity(TARGET_ENTITY).target(target).retry(0) + .timeoutSec(5).executor(blockingExecutor).payload(payload).preprocessed(true).build(); + + cdsProps.setHost("localhost"); + cdsProps.setPort(sim.getPort()); + cdsProps.setTimeout(3); + + GrpcConfig config = new GrpcConfig(blockingExecutor, cdsProps); + + operation = new GrpcOperation(params, config); + + // set the properties + operation.setProperty(OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES, Collections.emptyMap()); + + OperationOutcome outcome = operation.start().get(); + assertEquals(PolicyResult.SUCCESS, outcome.getResult()); + assertTrue(outcome.getResponse() instanceof ExecutionServiceOutput); + } + @Test public void testGetPropertyNames() { @@ -198,9 +232,10 @@ public class GrpcOperationTest { // @formatter:off assertThat(operation.getPropertyNames()).isEqualTo( List.of( - OperationProperties.AAI_MODEL_INVARIANT_GENERIC_VNF, - OperationProperties.AAI_RESOURCE_SERVICE_INSTANCE, - OperationProperties.EVENT_ADDITIONAL_PARAMS)); + OperationProperties.AAI_RESOURCE_VNF, + OperationProperties.AAI_SERVICE, + OperationProperties.EVENT_ADDITIONAL_PARAMS, + OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES)); // @formatter:on /* @@ -213,10 +248,76 @@ public class GrpcOperationTest { assertThat(operation.getPropertyNames()).isEqualTo( List.of( OperationProperties.AAI_PNF, - OperationProperties.EVENT_ADDITIONAL_PARAMS)); + OperationProperties.EVENT_ADDITIONAL_PARAMS, + OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES)); // @formatter:on } + @Test + public void testGetPnf() { + ControlLoopEventContext context = new ControlLoopEventContext(onset); + params = params.toBuilder().context(context).build(); + operation = new GrpcOperation(params, config); + + // in neither property nor context + assertThatIllegalArgumentException().isThrownBy(() -> operation.getPnfData()) + .withMessage("missing PNF data"); + + // only in context + Pnf pnf = new Pnf(); + params.getContext().setProperty(AaiGetPnfOperation.getKey(params.getTargetEntity()), pnf); + assertSame(pnf, operation.getPnfData()); + + // both - should choose the property + Pnf pnf2 = new Pnf(); + operation.setProperty(OperationProperties.AAI_PNF, pnf2); + assertSame(pnf2, operation.getPnfData()); + } + + @Test + public void testGetServiceInstanceId() { + ControlLoopEventContext context = new ControlLoopEventContext(onset); + params = params.toBuilder().context(context).build(); + operation = new GrpcOperation(params, config); + + // in neither property nor custom query + context.setProperty(AaiCqResponse.CONTEXT_KEY, mock(AaiCqResponse.class)); + assertThatIllegalArgumentException().isThrownBy(() -> operation.getServiceInstanceId()) + .withMessage("Target service instance could not be found"); + + // only in custom query + loadCqData(params.getContext()); + assertEquals(MY_SVC_ID, operation.getServiceInstanceId()); + + // both - should choose the property + ServiceInstance serviceInstance = new ServiceInstance(); + serviceInstance.setServiceInstanceId("another-service-id"); + operation.setProperty(OperationProperties.AAI_SERVICE, serviceInstance); + assertEquals("another-service-id", operation.getServiceInstanceId()); + } + + @Test + public void testGetVnfId() { + ControlLoopEventContext context = new ControlLoopEventContext(onset); + params = params.toBuilder().context(context).build(); + operation = new GrpcOperation(params, config); + + // in neither property nor custom query + context.setProperty(AaiCqResponse.CONTEXT_KEY, mock(AaiCqResponse.class)); + assertThatIllegalArgumentException().isThrownBy(() -> operation.getVnfId()) + .withMessage("Target generic vnf could not be found"); + + // only in custom query + loadCqData(params.getContext()); + assertEquals(MY_VNF, operation.getVnfId()); + + // both - should choose the property + GenericVnf vnf = new GenericVnf(); + vnf.setVnfId("another-vnf-id"); + operation.setProperty(OperationProperties.AAI_RESOURCE_VNF, vnf); + assertEquals("another-vnf-id", operation.getVnfId()); + } + @Test public void testStartPreprocessorAsync() throws InterruptedException, ExecutionException, TimeoutException { AtomicBoolean guardStarted = new AtomicBoolean(); @@ -317,6 +418,29 @@ public class GrpcOperationTest { assertThatIllegalArgumentException().isThrownBy(() -> operation.startOperationAsync(1, params.makeOutcome())); } + @Test + public void testGetAdditionalEventParams() { + operation = new GrpcOperation(params, config); + + // in neither property nor context + assertNull(operation.getAdditionalEventParams()); + + final Map eventParams = Collections.emptyMap(); + + // only in context + onset.setAdditionalEventParams(eventParams); + assertSame(eventParams, operation.getAdditionalEventParams()); + + // both - should choose the property, even if it's null + operation.setProperty(OperationProperties.EVENT_ADDITIONAL_PARAMS, null); + assertNull(operation.getAdditionalEventParams()); + + // both - should choose the property + final Map propParams = Collections.emptyMap(); + operation.setProperty(OperationProperties.EVENT_ADDITIONAL_PARAMS, propParams); + assertSame(propParams, operation.getAdditionalEventParams()); + } + private void verifyOperation(ControlLoopEventContext context) { Map payloadMap = Map.of(CdsActorConstants.KEY_CBA_NAME, CDS_BLUEPRINT_NAME, -- cgit 1.2.3-korg