From d0ba41b23a788bc557f451a0c66f0095c10dd390 Mon Sep 17 00:00:00 2001 From: Pamela Dragosh Date: Fri, 21 Feb 2020 14:57:50 -0500 Subject: [PATCH] Add SDNR Actor Actor for SDNR and necessary JUnit tests. Removed the Pair code. Issue-ID: POLICY-2382 Change-Id: I3da1d95f431cc076f12e9ad26280b92058fe51cc Signed-off-by: Pamela Dragosh --- .../model-actors/actor.sdnr/pom.xml | 116 ++++++++------- .../actor/sdnr/ModifyConfigOperation.java | 57 +++++++ .../actor/sdnr/SdnrActorServiceProvider.java | 46 +++--- .../controlloop/actor/sdnr/SdnrOperation.java | 163 +++++++++++++++++++++ .../controlloop/actor/sdnr/BasicSdnrOperation.java | 141 ++++++++++++++++++ .../actor/sdnr/ModifyConfigOperationTest.java | 94 ++++++++++++ .../controlloop/actor/sdnr/SdnrOperationTest.java | 139 ++++++++++++++++++ .../org/onap/policy/sdnr/util/StatusCodeEnum.java | 36 +++++ 8 files changed, 716 insertions(+), 76 deletions(-) create mode 100644 models-interactions/model-actors/actor.sdnr/src/main/java/org/onap/policy/controlloop/actor/sdnr/ModifyConfigOperation.java create mode 100644 models-interactions/model-actors/actor.sdnr/src/main/java/org/onap/policy/controlloop/actor/sdnr/SdnrOperation.java create mode 100644 models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/BasicSdnrOperation.java create mode 100644 models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/ModifyConfigOperationTest.java create mode 100644 models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/SdnrOperationTest.java diff --git a/models-interactions/model-actors/actor.sdnr/pom.xml b/models-interactions/model-actors/actor.sdnr/pom.xml index 37cec0dbc..b28bbd528 100644 --- a/models-interactions/model-actors/actor.sdnr/pom.xml +++ b/models-interactions/model-actors/actor.sdnr/pom.xml @@ -3,7 +3,7 @@ ============LICENSE_START======================================================= Copyright (C) 2018 Wipro Limited Intellectual Property. All rights reserved. Modifications Copyright (C) 2019-2020 Nordix Foundation. - Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + Modifications 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. @@ -18,58 +18,70 @@ limitations under the License. ============LICENSE_END========================================================= --> + + 4.0.0 - - 4.0.0 + + org.onap.policy.models.policy-models-interactions.model-actors + model-actors + 2.2.1-SNAPSHOT + - - org.onap.policy.models.policy-models-interactions.model-actors - model-actors - 2.2.1-SNAPSHOT - + actor.sdnr - actor.sdnr - - - - org.onap.policy.models.policy-models-interactions.model-actors - actorServiceProvider - ${project.version} - provided - - - org.onap.policy.models.policy-models-interactions.model-impl - sdnr - ${project.version} - provided - - - org.onap.policy.models.policy-models-interactions.model-impl - events - ${project.version} - provided - - - com.google.code.gson - gson - test - - - junit - junit - test - - - org.onap.policy.models.policy-models-interactions - simulators - ${project.version} - test - - - org.onap.policy.common - policy-endpoints - ${policy.common.version} - provided - - + + + org.onap.policy.models.policy-models-interactions.model-actors + actorServiceProvider + ${project.version} + provided + + + org.onap.policy.models.policy-models-interactions.model-impl + sdnr + ${project.version} + provided + + + org.onap.policy.models.policy-models-interactions.model-impl + events + ${project.version} + provided + + + com.google.code.gson + gson + test + + + junit + junit + test + + + org.onap.policy.models.policy-models-interactions + simulators + ${project.version} + test + + + org.onap.policy.common + policy-endpoints + ${policy.common.version} + provided + + + org.onap.policy.models.policy-models-interactions.model-actors + actor.test + ${project.version} + test + + + org.powermock + powermock-api-mockito2 + test + + 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 new file mode 100644 index 000000000..67c3d4521 --- /dev/null +++ b/models-interactions/model-actors/actor.sdnr/src/main/java/org/onap/policy/controlloop/actor/sdnr/ModifyConfigOperation.java @@ -0,0 +1,57 @@ +/*- + * ============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 java.util.concurrent.CompletableFuture; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; +import org.onap.policy.sdnr.PciRequestWrapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ModifyConfigOperation extends SdnrOperation { + private static final Logger logger = LoggerFactory.getLogger(ModifyConfigOperation.class); + + 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 PciRequestWrapper makeRequest(int attempt) { + PciRequestWrapper request = super.makeRequest(attempt); + // + // Set the recipe and action information + // + request.setRpcName(NAME.toLowerCase()); + request.getBody().setAction(NAME); + logger.info("SDNR ModifyConfig Request to be sent is {}", request); + 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 0919779a5..8c799e5cf 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 @@ -25,10 +25,13 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import java.util.Collections; import java.util.List; +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.impl.ActorImpl; +import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicActor; +import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperator; +import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicActorParams; import org.onap.policy.controlloop.policy.Policy; import org.onap.policy.controlloop.policy.PolicyResult; import org.onap.policy.sdnr.PciCommonHeader; @@ -40,27 +43,11 @@ import org.onap.policy.sdnr.PciResponseWrapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class SdnrActorServiceProvider extends ActorImpl { +public class SdnrActorServiceProvider extends BidirectionalTopicActor { private static final String NAME = "SDNR"; - public static class Pair { - public final A result; - public final B message; - - public Pair(A result, B message) { - this.result = result; - this.message = message; - } - - public A getResult() { - return this.result; - } - - public B getMessage() { - return this.message; - } - } + // TODO old code: remove lines down to **HERE** private static final Logger logger = LoggerFactory.getLogger(SdnrActorServiceProvider.class); @@ -80,10 +67,20 @@ public class SdnrActorServiceProvider extends ActorImpl { private static final ImmutableMap> payloads = new ImmutableMap.Builder>() .put(RECIPE_MODIFY, ImmutableList.of(SDNR_REQUEST_PARAMS, SDNR_CONFIG_PARAMS)).build(); + // **HERE** + + /** + * Constructor. + */ public SdnrActorServiceProvider() { - super(NAME); + super(NAME, BidirectionalTopicActorParams.class); + + addOperator(new BidirectionalTopicOperator(NAME, ModifyConfigOperation.NAME, this, SdnrOperation.SELECTOR_KEYS, + ModifyConfigOperation::new)); } + // TODO old code: remove lines down to **HERE** + @Override public String actor() { return NAME; @@ -195,7 +192,7 @@ public class SdnrActorServiceProvider extends ActorImpl { * @return an key-value pair that contains the Policy result and SDNR response * message */ - public static SdnrActorServiceProvider.Pair processResponse( + public static Pair processResponse( PciResponseWrapper dmaapResponse) { logger.info("SDNR processResponse called : {}", dmaapResponse); @@ -214,7 +211,7 @@ public class SdnrActorServiceProvider extends ActorImpl { */ if (sdnrResponse.getStatus() == null) { message = "Policy was unable to parse SDN-R response status field (it was null)."; - return new SdnrActorServiceProvider.Pair<>(PolicyResult.FAILURE_EXCEPTION, message); + return Pair.of(PolicyResult.FAILURE_EXCEPTION, message); } /* @@ -223,7 +220,7 @@ public class SdnrActorServiceProvider extends ActorImpl { String responseValue = PciResponseCode.toResponseValue(sdnrResponse.getStatus().getCode()); if (responseValue == null) { message = "Policy was unable to parse SDN-R response status code field."; - return new SdnrActorServiceProvider.Pair<>(PolicyResult.FAILURE_EXCEPTION, message); + return Pair.of(PolicyResult.FAILURE_EXCEPTION, message); } logger.info("SDNR Response Code is {}", responseValue); @@ -255,7 +252,7 @@ public class SdnrActorServiceProvider extends ActorImpl { default: result = PolicyResult.FAILURE_EXCEPTION; } - return new SdnrActorServiceProvider.Pair<>(result, message); + return Pair.of(result, message); } /** @@ -290,4 +287,5 @@ public class SdnrActorServiceProvider extends ActorImpl { return clRsp; } + // **HERE** } diff --git a/models-interactions/model-actors/actor.sdnr/src/main/java/org/onap/policy/controlloop/actor/sdnr/SdnrOperation.java b/models-interactions/model-actors/actor.sdnr/src/main/java/org/onap/policy/controlloop/actor/sdnr/SdnrOperation.java new file mode 100644 index 000000000..5faa31d7c --- /dev/null +++ b/models-interactions/model-actors/actor.sdnr/src/main/java/org/onap/policy/controlloop/actor/sdnr/SdnrOperation.java @@ -0,0 +1,163 @@ +/*- + * ============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 java.util.List; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import org.onap.policy.controlloop.VirtualControlLoopEvent; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation; +import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; +import org.onap.policy.controlloop.actorserviceprovider.topic.SelectorKey; +import org.onap.policy.controlloop.policy.PolicyResult; +import org.onap.policy.sdnr.PciCommonHeader; +import org.onap.policy.sdnr.PciRequest; +import org.onap.policy.sdnr.PciRequestWrapper; +import org.onap.policy.sdnr.PciResponse; +import org.onap.policy.sdnr.PciResponseWrapper; +import org.onap.policy.sdnr.util.StatusCodeEnum; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public abstract class SdnrOperation extends BidirectionalTopicOperation { + private static final Logger logger = LoggerFactory.getLogger(SdnrOperation.class); + + /** + * 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. + *

+ * Note: if these change, then {@link #getExpectedKeyValues(int, Request)} must be + * updated accordingly. + */ + public static final List SELECTOR_KEYS = List.of(new SelectorKey("CommonHeader", "SubRequestID")); + + public SdnrOperation(ControlLoopOperationParams params, BidirectionalTopicConfig config) { + super(params, config, PciResponseWrapper.class); + } + + /** + * Note: these values must match {@link #SELECTOR_KEYS}. + */ + @Override + protected List getExpectedKeyValues(int attempt, PciRequestWrapper request) { + return List.of(request.getBody().getCommonHeader().getSubRequestId()); + } + + @Override + protected CompletableFuture startPreprocessorAsync() { + return startGuardAsync(); + } + + @Override + protected Status detmStatus(String rawResponse, PciResponseWrapper responseWrapper) { + PciResponse response = responseWrapper.getBody(); + + if (response == null || response.getStatus() == null) { + throw new IllegalArgumentException("SDNR response is missing the response status"); + } + + StatusCodeEnum code = StatusCodeEnum.fromStatusCode(response.getStatus().getCode()); + + if (code == null) { + throw new IllegalArgumentException( + "unknown SDNR response status code: " + response.getStatus().getCode()); + } + + /* + * Response and Payload are just printed and no further action needed since + * casablanca release + */ + logger.info("SDNR Response Code {} Message is {}", code, response.getStatus().getValue()); + logger.info("SDNR Response Payload is {}", response.getPayload()); + + switch (code) { + case SUCCESS: + case PARTIAL_SUCCESS: + return Status.SUCCESS; + case FAILURE: + case PARTIAL_FAILURE: + return Status.FAILURE; + case ERROR: + case REJECT: + throw new IllegalArgumentException("SDNR request was not accepted, code=" + code); + case ACCEPTED: + default: + // awaiting a "final" response + return Status.STILL_WAITING; + } + } + + /** + * Sets the message to the status description, if available. + */ + @Override + public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result, + PciResponseWrapper responseWrapper) { + PciResponse response = responseWrapper.getBody(); + if (response.getStatus() == null || response.getStatus().getValue() == null) { + return setOutcome(outcome, result); + } + + outcome.setResult(result); + outcome.setMessage(response.getStatus().getValue()); + return outcome; + } + + @Override + protected PciRequestWrapper makeRequest(int attempt) { + VirtualControlLoopEvent onset = params.getContext().getEvent(); + String subRequestId = UUID.randomUUID().toString(); + + /* Construct an SDNR request using pci Model */ + + /* + * The actual pci request is placed in a wrapper used to send through dmaap. The + * current version is 2.0 as of R1. + */ + PciRequestWrapper dmaapRequest = new PciRequestWrapper(); + dmaapRequest.setVersion("1.0"); + dmaapRequest.setCorrelationId(onset.getRequestId() + "-" + subRequestId); + dmaapRequest.setType("request"); + + /* This is the actual request that is placed in the dmaap wrapper. */ + final PciRequest sdnrRequest = new PciRequest(); + + /* The common header is a required field for all SDNR requests. */ + PciCommonHeader requestCommonHeader = new PciCommonHeader(); + requestCommonHeader.setRequestId(onset.getRequestId()); + requestCommonHeader.setSubRequestId(subRequestId); + + sdnrRequest.setCommonHeader(requestCommonHeader); + sdnrRequest.setPayload(onset.getPayload()); + + /* + * Once the pci request is constructed, add it into the body of the dmaap + * wrapper. + */ + dmaapRequest.setBody(sdnrRequest); + logger.info("SDNR Request to be sent is {}", dmaapRequest); + + /* Return the request to be sent through dmaap. */ + return dmaapRequest; + } +} diff --git a/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/BasicSdnrOperation.java b/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/BasicSdnrOperation.java new file mode 100644 index 000000000..0aea35f37 --- /dev/null +++ b/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/BasicSdnrOperation.java @@ -0,0 +1,141 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * 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 static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import java.util.function.BiConsumer; +import org.onap.policy.common.utils.coder.StandardCoderObject; +import org.onap.policy.controlloop.actor.test.BasicBidirectionalTopicOperation; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.actorserviceprovider.Util; +import org.onap.policy.controlloop.actorserviceprovider.impl.OperationMaker; +import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig; +import org.onap.policy.controlloop.policy.PolicyResult; +import org.onap.policy.sdnr.PciResponse; +import org.onap.policy.sdnr.PciResponseWrapper; +import org.onap.policy.sdnr.Status; +import org.onap.policy.sdnr.util.StatusCodeEnum; +import org.powermock.reflect.Whitebox; + +public abstract class BasicSdnrOperation extends BasicBidirectionalTopicOperation { + + protected PciResponseWrapper response; + + /** + * Constructs the object using a default actor and operation name. + */ + public BasicSdnrOperation() { + super(); + } + + /** + * Constructs the object. + * + * @param actor actor name + * @param operation operation name + */ + public BasicSdnrOperation(String actor, String operation) { + super(actor, operation); + } + + /** + * Initializes mocks and sets up. + */ + public void setUp() throws Exception { + super.setUpBasic(); + + response = new PciResponseWrapper(); + + PciResponse body = new PciResponse(); + Status status = new Status(); + status.setCode(100); + status.setValue(StatusCodeEnum.SUCCESS.toString()); + body.setStatus(status); + response.setBody(body); + } + + /** + * Runs the operation and verifies that the response is successful. + * + * @param operation operation to run + */ + protected void verifyOperation(SdnrOperation operation) + throws InterruptedException, ExecutionException, TimeoutException { + + CompletableFuture future2 = operation.start(); + executor.runAll(100); + assertFalse(future2.isDone()); + + verify(forwarder).register(any(), listenerCaptor.capture()); + provideResponse(listenerCaptor.getValue(), StatusCodeEnum.SUCCESS.toString()); + + executor.runAll(100); + assertTrue(future2.isDone()); + + outcome = future2.get(); + assertEquals(PolicyResult.SUCCESS, outcome.getResult()); + } + + /** + * Verifies that an exception is thrown if a field is missing from the enrichment + * data. + * + * @param fieldName name of the field to be removed from the enrichment data + * @param expectedText text expected in the exception message + */ + protected void verifyMissing(String fieldName, String expectedText, + OperationMaker maker) { + + makeContext(); + enrichment.remove(fieldName); + + SdnrOperation oper = maker.apply(params, config); + + assertThatIllegalArgumentException().isThrownBy(() -> Whitebox.invokeMethod(oper, "makeRequest", 1)) + .withMessageContaining("missing").withMessageContaining(expectedText); + } + + /** + * Provides a response to the listener. + * + * @param listener listener to which to provide the response + * @param code response code + * @param description response description + */ + protected void provideResponse(BiConsumer listener, int code, String description) { + PciResponse response = new PciResponse(); + + Status status = new Status(); + response.setStatus(status); + status.setCode(code); + + provideResponse(listener, Util.translate("", response, String.class)); + } +} diff --git a/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/ModifyConfigOperationTest.java b/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/ModifyConfigOperationTest.java new file mode 100644 index 000000000..d1e192277 --- /dev/null +++ b/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/ModifyConfigOperationTest.java @@ -0,0 +1,94 @@ +/*-- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * 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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicBoolean; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext; +import org.onap.policy.controlloop.policy.PolicyResult; +import org.onap.policy.sdnr.PciRequestWrapper; + +public class ModifyConfigOperationTest extends BasicSdnrOperation { + + private ModifyConfigOperation oper; + + public ModifyConfigOperationTest() { + super(DEFAULT_ACTOR, ModifyConfigOperation.NAME); + } + + @Before + public void setUp() throws Exception { + super.setUp(); + oper = new ModifyConfigOperation(params, config); + } + + + @Test + public void testModifyConfigOperation() { + 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 { + PciRequestWrapper request = oper.makeRequest(1); + assertNotNull(request); + } +} 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 new file mode 100644 index 000000000..fc7e86a94 --- /dev/null +++ b/models-interactions/model-actors/actor.sdnr/src/test/java/org/onap/policy/controlloop/actor/sdnr/SdnrOperationTest.java @@ -0,0 +1,139 @@ +/*- + * ============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 static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; + +import java.util.Arrays; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation.Status; +import org.onap.policy.controlloop.policy.PolicyResult; +import org.onap.policy.sdnr.PciCommonHeader; +import org.onap.policy.sdnr.PciRequestWrapper; +import org.onap.policy.sdnr.util.StatusCodeEnum; + +public class SdnrOperationTest extends BasicSdnrOperation { + + private SdnrOperation operation; + + /** + * Setup. + */ + @Before + public void setUp() throws Exception { + super.setUp(); + + operation = new SdnrOperation(params, config) { + @Override + protected PciRequestWrapper makeRequest(int attempt) { + return super.makeRequest(attempt); + } + }; + } + + @Test + public void testSdnrOperation() { + assertEquals(DEFAULT_ACTOR, operation.getActorName()); + assertEquals(DEFAULT_OPERATION, operation.getName()); + } + + @Test + public void testMakeRequest() { + PciRequestWrapper request = operation.makeRequest(1); + + assertNotNull(request.getBody()); + assertEquals("1.0", request.getVersion()); + assertEquals("request", request.getType()); + + PciCommonHeader header = request.getBody().getCommonHeader(); + assertNotNull(header); + assertEquals(params.getRequestId(), header.getRequestId()); + } + + @Test + public void testGetExpectedKeyValues() { + PciRequestWrapper request = operation.makeRequest(1); + assertEquals(Arrays.asList(request.getBody().getCommonHeader().getSubRequestId()), + operation.getExpectedKeyValues(50, request)); + + } + + @Test + public void testDetmStatusStringResponse() { + final org.onap.policy.sdnr.Status status = response.getBody().getStatus(); + + // null status + response.getBody().setStatus(null); + assertThatIllegalArgumentException().isThrownBy(() -> operation.detmStatus("", response)) + .withMessage("SDNR response is missing the response status"); + response.getBody().setStatus(status); + + // invalid code + status.setCode(-45); + assertThatIllegalArgumentException().isThrownBy(() -> operation.detmStatus("", response)) + .withMessage("unknown SDNR response status code: -45"); + + + status.setValue(StatusCodeEnum.ACCEPTED.toString()); + status.setCode(StatusCodeEnum.toValue(StatusCodeEnum.ACCEPTED)); + assertEquals(Status.STILL_WAITING, operation.detmStatus("", response)); + + status.setValue(StatusCodeEnum.SUCCESS.toString()); + status.setCode(StatusCodeEnum.toValue(StatusCodeEnum.SUCCESS)); + assertEquals(Status.SUCCESS, operation.detmStatus("", response)); + + 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()); + + status.setValue(StatusCodeEnum.REJECT.toString()); + status.setCode(313); + assertThatIllegalArgumentException().isThrownBy(() -> operation.detmStatus("", response)) + .withMessage("SDNR request was not accepted, code=" + StatusCodeEnum.REJECT.toString()); + + 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()); + + status.setValue(StatusCodeEnum.FAILURE.toString()); + status.setCode(450); + assertEquals(Status.FAILURE, operation.detmStatus("", response)); + } + + @Test + public void testSetOutcome() { + final org.onap.policy.sdnr.Status status = response.getBody().getStatus(); + + // null status + response.getBody().setStatus(null); + assertSame(outcome, operation.setOutcome(outcome, PolicyResult.SUCCESS, response)); + assertEquals(PolicyResult.SUCCESS, outcome.getResult()); + assertNotNull(outcome.getMessage()); + response.getBody().setStatus(status); + + } +} diff --git a/models-interactions/model-impl/sdnr/src/main/java/org/onap/policy/sdnr/util/StatusCodeEnum.java b/models-interactions/model-impl/sdnr/src/main/java/org/onap/policy/sdnr/util/StatusCodeEnum.java index 483062d6f..09c62c23c 100644 --- a/models-interactions/model-impl/sdnr/src/main/java/org/onap/policy/sdnr/util/StatusCodeEnum.java +++ b/models-interactions/model-impl/sdnr/src/main/java/org/onap/policy/sdnr/util/StatusCodeEnum.java @@ -74,6 +74,42 @@ public enum StatusCodeEnum { return null; } + /** + * Best guess on a value, but since some of these + * codes could be any value this may be difficult to + * use during runtime. + + * @param code input StatusCodeEnum + * @return integer value + */ + public static int toValue(StatusCodeEnum code) { + switch (code) { + case ACCEPTED: + return 100; + + case SUCCESS: + return 200; + + case REJECT: + return 300; + + case ERROR: + return 400; + + case FAILURE: + return 450; + + case PARTIAL_SUCCESS: + return 500; + + case PARTIAL_FAILURE: + return 501; + + default: + return 0; + } + } + private static boolean isRejectStatusCode(final int statusCode) { return statusCode >= 300 && statusCode <= 313; } -- 2.16.6