From df6d6bb862fb0af49a8873479f9e3b609553deca Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 8 Apr 2020 12:37:20 -0400 Subject: [PATCH] SDNR Actor enhancements Made the following changes: - Enhanced SDNR Actor to support any operation name specified within the policy, constructing the same request, but passing a different "RPC name" and "Action" in the request. - Added ControlLoopResponse to OperationOutcome - Modified SDNR Actor to populate ControlLoopResponse Issue-ID: POLICY-2468 Change-Id: I50ee0674077d975f3cd211454656edc47d78520f Signed-off-by: Jim Hahn --- .../actor/sdnr/ModifyConfigOperation.java | 50 -------- .../actor/sdnr/SdnrActorServiceProvider.java | 17 ++- .../controlloop/actor/sdnr/SdnrOperation.java | 58 +++++++-- .../actor/sdnr/ModifyConfigOperationTest.java | 140 --------------------- .../actor/sdnr/SdnrActorServiceProviderTest.java | 13 +- .../controlloop/actor/sdnr/SdnrOperationTest.java | 98 ++++++++++++--- .../controlloop/actor/test/BasicOperation.java | 12 +- .../actorserviceprovider/OperationOutcome.java | 3 + .../actorserviceprovider/OperationOutcomeTest.java | 10 ++ 9 files changed, 179 insertions(+), 222 deletions(-) delete mode 100644 models-interactions/model-actors/actor.sdnr/src/main/java/org/onap/policy/controlloop/actor/sdnr/ModifyConfigOperation.java delete mode 100644 models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/ModifyConfigOperationTest.java diff --git a/models-interactions/model-actors/actor.sdnr/src/main/java/org/onap/policy/controlloop/actor/sdnr/ModifyConfigOperation.java b/models-interactions/model-actors/actor.sdnr/src/main/java/org/onap/policy/controlloop/actor/sdnr/ModifyConfigOperation.java deleted file mode 100644 index 2b7f644b6..000000000 --- a/models-interactions/model-actors/actor.sdnr/src/main/java/org/onap/policy/controlloop/actor/sdnr/ModifyConfigOperation.java +++ /dev/null @@ -1,50 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SdnrOperation - * ================================================================================ - * Copyright (C) 2020 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========================================================= - */ - -package org.onap.policy.controlloop.actor.sdnr; - -import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig; -import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; -import org.onap.policy.sdnr.PciMessage; - -public class ModifyConfigOperation extends SdnrOperation { - public static final String NAME = "ModifyConfig"; - - /** - * Constructs the object. - * - * @param params operation parameters - * @param config configuration for this operation - */ - public ModifyConfigOperation(ControlLoopOperationParams params, BidirectionalTopicConfig config) { - super(params, config); - } - - @Override - protected PciMessage makeRequest(int attempt) { - final PciMessage request = super.makeRequest(attempt); - // - // Set the recipe and action information - // - request.setRpcName(NAME.toLowerCase()); - request.getBody().getInput().setAction(NAME); - return request; - } -} diff --git a/models-interactions/model-actors/actor.sdnr/src/main/java/org/onap/policy/controlloop/actor/sdnr/SdnrActorServiceProvider.java b/models-interactions/model-actors/actor.sdnr/src/main/java/org/onap/policy/controlloop/actor/sdnr/SdnrActorServiceProvider.java index 8c799e5cf..5cb7af6ee 100644 --- a/models-interactions/model-actors/actor.sdnr/src/main/java/org/onap/policy/controlloop/actor/sdnr/SdnrActorServiceProvider.java +++ b/models-interactions/model-actors/actor.sdnr/src/main/java/org/onap/policy/controlloop/actor/sdnr/SdnrActorServiceProvider.java @@ -29,6 +29,7 @@ import org.apache.commons.lang3.tuple.Pair; import org.onap.policy.controlloop.ControlLoopOperation; import org.onap.policy.controlloop.ControlLoopResponse; import org.onap.policy.controlloop.VirtualControlLoopEvent; +import org.onap.policy.controlloop.actorserviceprovider.Operator; import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicActor; import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperator; import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicActorParams; @@ -43,6 +44,10 @@ import org.onap.policy.sdnr.PciResponseWrapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * SDNR is an unusual actor in that it uses a single, generic operator to initiate all + * operation types. The action taken is always the same, only the operation name changes. + */ public class SdnrActorServiceProvider extends BidirectionalTopicActor { private static final String NAME = "SDNR"; @@ -75,8 +80,16 @@ public class SdnrActorServiceProvider extends BidirectionalTopicActor { +public class SdnrOperation extends BidirectionalTopicOperation { private static final Logger logger = LoggerFactory.getLogger(SdnrOperation.class); + /** + * Operation name as it should appear within config files. + */ + public static final String NAME = "any"; + /** * Keys used to match the response with the request listener. The sub request ID is a * UUID, so it can be used to uniquely identify the response. @@ -68,27 +74,27 @@ public abstract class SdnrOperation extends BidirectionalTopicOperation startGuardAsync() { - return null; - } - }; - - outcome = oper.start().get(); - assertEquals(PolicyResult.SUCCESS, outcome.getResult()); - } - - @Test - public void testConstructor() { - assertEquals(DEFAULT_ACTOR, oper.getActorName()); - assertEquals(ModifyConfigOperation.NAME, oper.getName()); - } - - @Test - public void testStartPreprocessorAsync() throws Exception { - final CompletableFuture future2 = new CompletableFuture<>(); - context = mock(ControlLoopEventContext.class); - when(context.getEvent()).thenReturn(event); - params = params.toBuilder().context(context).build(); - - AtomicBoolean guardStarted = new AtomicBoolean(); - - oper = new ModifyConfigOperation(params, config) { - @Override - protected CompletableFuture startGuardAsync() { - guardStarted.set(true); - return super.startGuardAsync(); - } - }; - CompletableFuture future3 = oper.startPreprocessorAsync(); - - assertNotNull(future3); - assertFalse(future.isDone()); - assertTrue(guardStarted.get()); - - future2.complete(params.makeOutcome()); - assertTrue(executor.runAll(100)); - assertTrue(future3.isDone()); - assertEquals(PolicyResult.SUCCESS, future3.get().getResult()); - } - - @Test - public void testMakeRequest() throws CoderException { - oper.generateSubRequestId(1); - - PciMessage request = oper.makeRequest(1); - assertNotNull(request); - } -} diff --git a/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/SdnrActorServiceProviderTest.java b/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/SdnrActorServiceProviderTest.java index 8101b12fd..12a1666a0 100644 --- a/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/SdnrActorServiceProviderTest.java +++ b/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/SdnrActorServiceProviderTest.java @@ -22,18 +22,19 @@ package org.onap.policy.controlloop.actor.sdnr; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import java.time.Instant; import java.util.HashMap; import java.util.UUID; - import org.junit.Test; import org.onap.policy.controlloop.ControlLoopEventStatus; import org.onap.policy.controlloop.ControlLoopOperation; import org.onap.policy.controlloop.ControlLoopResponse; import org.onap.policy.controlloop.ControlLoopTargetType; import org.onap.policy.controlloop.VirtualControlLoopEvent; +import org.onap.policy.controlloop.actorserviceprovider.Operator; import org.onap.policy.controlloop.policy.Policy; import org.onap.policy.controlloop.policy.Target; import org.onap.policy.controlloop.policy.TargetType; @@ -93,6 +94,16 @@ public class SdnrActorServiceProviderTest { } + @Test + public void testGetOperator() { + SdnrActorServiceProvider sp = new SdnrActorServiceProvider(); + + // should always return the same operator regardless of the name + Operator oper = sp.getOperator("unknown"); + assertNotNull(oper); + assertSame(oper, sp.getOperator("another")); + } + @Test public void testGetControlLoopResponse() { PciRequest sdnrRequest; diff --git a/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/SdnrOperationTest.java b/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/SdnrOperationTest.java index 836b1ae57..2bb0d9469 100644 --- a/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/SdnrOperationTest.java +++ b/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/SdnrOperationTest.java @@ -20,22 +20,33 @@ package org.onap.policy.controlloop.actor.sdnr; -import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.util.Arrays; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicBoolean; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.onap.policy.controlloop.ControlLoopResponse; import org.onap.policy.controlloop.actor.test.BasicBidirectionalTopicOperation; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext; import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation.Status; +import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig; +import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams; import org.onap.policy.controlloop.policy.PolicyResult; import org.onap.policy.sdnr.PciCommonHeader; import org.onap.policy.sdnr.PciMessage; +import org.onap.policy.sdnr.PciRequest; import org.onap.policy.sdnr.util.StatusCodeEnum; public class SdnrOperationTest extends BasicSdnrOperation { @@ -60,7 +71,7 @@ public class SdnrOperationTest extends BasicSdnrOperation { public void setUp() throws Exception { super.setUp(); - operation = new SdnrOperation(params, config) {}; + operation = new SdnrOperation(params, config); } @After @@ -84,8 +95,13 @@ public class SdnrOperationTest extends BasicSdnrOperation { assertNotNull(request.getBody()); assertEquals("1.0", request.getVersion()); assertEquals("request", request.getType()); + assertEquals(DEFAULT_OPERATION.toLowerCase(), request.getRpcName()); - PciCommonHeader header = request.getBody().getInput().getCommonHeader(); + PciRequest input = request.getBody().getInput(); + assertNotNull(input); + assertEquals(DEFAULT_OPERATION, input.getAction()); + + PciCommonHeader header = input.getCommonHeader(); assertNotNull(header); assertEquals(params.getRequestId(), header.getRequestId()); } @@ -99,9 +115,66 @@ public class SdnrOperationTest extends BasicSdnrOperation { operation.getExpectedKeyValues(50, request)); } + /** + * Tests "success" case with simulator. + */ + @Test + public void testSuccess() throws Exception { + BidirectionalTopicParams opParams = + BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SOURCE).build(); + config = new BidirectionalTopicConfig(blockingExecutor, opParams, topicMgr, SdnrOperation.SELECTOR_KEYS); + + params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build(); + + operation = new SdnrOperation(params, config) { + @Override + protected CompletableFuture startGuardAsync() { + return null; + } + }; + + outcome = operation.start().get(); + assertEquals(PolicyResult.SUCCESS, outcome.getResult()); + + ControlLoopResponse clresp = outcome.getControlLoopResponse(); + assertNotNull(clresp); + + assertEquals(DEFAULT_ACTOR, clresp.getFrom()); + assertEquals("DCAE", clresp.getTarget()); + assertEquals(CL_NAME, clresp.getClosedLoopControlName()); + assertEquals(EVENT_POLICY_NAME, clresp.getPolicyName()); + assertEquals(EVENT_POLICY_VERSION, clresp.getPolicyVersion()); + assertEquals(EVENT_VERSION, clresp.getVersion()); + assertEquals(REQ_ID, clresp.getRequestId()); + assertNotNull(clresp.getPayload()); + } + @Test - public void testStartPreprocessorAsync() { - assertNotNull(operation.startPreprocessorAsync()); + public void testStartPreprocessorAsync() throws Exception { + final CompletableFuture future2 = new CompletableFuture<>(); + context = mock(ControlLoopEventContext.class); + when(context.getEvent()).thenReturn(event); + params = params.toBuilder().context(context).build(); + + AtomicBoolean guardStarted = new AtomicBoolean(); + + operation = new SdnrOperation(params, config) { + @Override + protected CompletableFuture startGuardAsync() { + guardStarted.set(true); + return super.startGuardAsync(); + } + }; + CompletableFuture future3 = operation.startPreprocessorAsync(); + + assertNotNull(future3); + assertFalse(future.isDone()); + assertTrue(guardStarted.get()); + + future2.complete(params.makeOutcome()); + assertTrue(executor.runAll(100)); + assertTrue(future3.isDone()); + assertEquals(PolicyResult.SUCCESS, future3.get().getResult()); } @Test @@ -110,14 +183,12 @@ public class SdnrOperationTest extends BasicSdnrOperation { // null status response.getBody().getOutput().setStatus(null); - assertThatIllegalArgumentException().isThrownBy(() -> operation.detmStatus("", response)) - .withMessage("SDNR response is missing the response status"); + assertEquals(Status.FAILURE, operation.detmStatus("", response)); response.getBody().getOutput().setStatus(status); // invalid code status.setCode(-45); - assertThatIllegalArgumentException().isThrownBy(() -> operation.detmStatus("", response)) - .withMessage("unknown SDNR response status code: -45"); + assertEquals(Status.FAILURE, operation.detmStatus("", response)); status.setValue(StatusCodeEnum.ACCEPTED.toString()); @@ -130,18 +201,15 @@ public class SdnrOperationTest extends BasicSdnrOperation { status.setValue(StatusCodeEnum.REJECT.toString()); status.setCode(StatusCodeEnum.toValue(StatusCodeEnum.REJECT)); - assertThatIllegalArgumentException().isThrownBy(() -> operation.detmStatus("", response)) - .withMessage("SDNR request was not accepted, code=" + StatusCodeEnum.REJECT.toString()); + assertEquals(Status.FAILURE, operation.detmStatus("", response)); status.setValue(StatusCodeEnum.REJECT.toString()); status.setCode(313); - assertThatIllegalArgumentException().isThrownBy(() -> operation.detmStatus("", response)) - .withMessage("SDNR request was not accepted, code=" + StatusCodeEnum.REJECT.toString()); + assertEquals(Status.FAILURE, operation.detmStatus("", response)); status.setValue(StatusCodeEnum.ERROR.toString()); status.setCode(StatusCodeEnum.toValue(StatusCodeEnum.ERROR)); - assertThatIllegalArgumentException().isThrownBy(() -> operation.detmStatus("", response)) - .withMessage("SDNR request was not accepted, code=" + StatusCodeEnum.ERROR.toString()); + assertEquals(Status.FAILURE, operation.detmStatus("", response)); status.setValue(StatusCodeEnum.FAILURE.toString()); status.setCode(450); diff --git a/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicOperation.java b/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicOperation.java index 3b1871f20..c0ea44715 100644 --- a/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicOperation.java +++ b/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicOperation.java @@ -56,9 +56,13 @@ import org.onap.policy.controlloop.policy.PolicyResult; public class BasicOperation { protected static final UUID REQ_ID = UUID.randomUUID(); protected static final String SUB_REQ_ID = "my-sub-request-id"; - protected static final String DEFAULT_ACTOR = "default-actor"; - protected static final String DEFAULT_OPERATION = "default-operation"; + protected static final String DEFAULT_ACTOR = "default-Actor"; + protected static final String DEFAULT_OPERATION = "default-Operation"; protected static final String TARGET_ENTITY = "my-target"; + protected static final String CL_NAME = "my-closed-loop"; + protected static final String EVENT_POLICY_NAME = "my-event-policy-name"; + protected static final String EVENT_POLICY_VERSION = "my-event-policy-version"; + protected static final String EVENT_VERSION = "my-event-version"; protected static final Executor blockingExecutor = command -> { Thread thread = new Thread(command); @@ -158,6 +162,10 @@ public class BasicOperation { event = new VirtualControlLoopEvent(); event.setRequestId(REQ_ID); event.setAai(enrichment); + event.setClosedLoopControlName(CL_NAME); + event.setPolicyName(EVENT_POLICY_NAME); + event.setPolicyVersion(EVENT_POLICY_VERSION); + event.setVersion(EVENT_VERSION); context = new ControlLoopEventContext(event); diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/OperationOutcome.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/OperationOutcome.java index d8db70619..e2d94b3b3 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/OperationOutcome.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/OperationOutcome.java @@ -25,6 +25,7 @@ import lombok.Data; import lombok.NoArgsConstructor; import lombok.NonNull; import org.onap.policy.controlloop.ControlLoopOperation; +import org.onap.policy.controlloop.ControlLoopResponse; import org.onap.policy.controlloop.policy.PolicyResult; /** @@ -42,6 +43,7 @@ public class OperationOutcome { private PolicyResult result = PolicyResult.SUCCESS; private String message; private boolean finalOutcome; + private ControlLoopResponse controlLoopResponse; /** * Copy constructor. @@ -58,6 +60,7 @@ public class OperationOutcome { this.result = source.result; this.message = source.message; this.finalOutcome = source.finalOutcome; + this.controlLoopResponse = source.controlLoopResponse; } /** diff --git a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/OperationOutcomeTest.java b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/OperationOutcomeTest.java index 4e9728336..77d2253e1 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/OperationOutcomeTest.java +++ b/models-interactions/model-actors/actorServiceProvider/src/test/java/org/onap/policy/controlloop/actorserviceprovider/OperationOutcomeTest.java @@ -23,12 +23,14 @@ package org.onap.policy.controlloop.actorserviceprovider; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import java.time.Instant; import org.junit.Before; import org.junit.Test; import org.onap.policy.controlloop.ControlLoopOperation; +import org.onap.policy.controlloop.ControlLoopResponse; import org.onap.policy.controlloop.policy.PolicyResult; public class OperationOutcomeTest { @@ -41,10 +43,16 @@ public class OperationOutcomeTest { private static final PolicyResult RESULT = PolicyResult.FAILURE_GUARD; private static final String MESSAGE = "my-message"; + private ControlLoopResponse response; private OperationOutcome outcome; + /** + * Sets up. + */ @Before public void setUp() { + response = new ControlLoopResponse(); + outcome = new OperationOutcome(); } @@ -62,6 +70,7 @@ public class OperationOutcomeTest { assertEquals(SUB_REQ_ID, outcome2.getSubRequestId()); assertEquals(RESULT, outcome2.getResult()); assertEquals(MESSAGE, outcome2.getMessage()); + assertSame(response, outcome2.getControlLoopResponse()); } @Test @@ -133,5 +142,6 @@ public class OperationOutcomeTest { outcome.setStart(START); outcome.setSubRequestId(SUB_REQ_ID); outcome.setTarget(TARGET); + outcome.setControlLoopResponse(response); } } -- 2.16.6