diff options
author | Jorge Hernandez <jorge.hernandez-herrero@att.com> | 2020-02-20 16:50:04 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-02-20 16:50:04 +0000 |
commit | f6da7772d9dc01ce4ddd21a55b0f1c5fb7ad814f (patch) | |
tree | 915a876d464ca12d909965d322e23c20081e6f0a /models-interactions | |
parent | 7babb8153d36b09e536720258f403535b79f9243 (diff) | |
parent | 2e2eae531ad0d6e0e09a1cc4824fc5bb74432679 (diff) |
Merge "Add Guard Actor"
Diffstat (limited to 'models-interactions')
11 files changed, 534 insertions, 2 deletions
diff --git a/models-interactions/model-actors/actor.guard/pom.xml b/models-interactions/model-actors/actor.guard/pom.xml new file mode 100644 index 000000000..a70c84f31 --- /dev/null +++ b/models-interactions/model-actors/actor.guard/pom.xml @@ -0,0 +1,83 @@ +<?xml version="1.0"?> +<!-- + ============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========================================================= + --> + +<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"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.onap.policy.models.policy-models-interactions.model-actors</groupId> + <artifactId>model-actors</artifactId> + <version>2.2.1-SNAPSHOT</version> + </parent> + + <artifactId>actor.guard</artifactId> + + <dependencies> + <dependency> + <groupId>org.onap.policy.models.policy-models-interactions.model-actors</groupId> + <artifactId>actorServiceProvider</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.onap.policy.models.policy-models-interactions.model-impl</groupId> + <artifactId>aai</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.onap.policy.models.policy-models-interactions.model-actors</groupId> + <artifactId>actor.aai</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.onap.policy.models</groupId> + <artifactId>policy-models-decisions</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.onap.policy.common</groupId> + <artifactId>policy-endpoints</artifactId> + <version>${policy.common.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.onap.policy.models.policy-models-interactions.model-actors</groupId> + <artifactId>actor.test</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.powermock</groupId> + <artifactId>powermock-api-mockito2</artifactId> + <scope>test</scope> + </dependency> + </dependencies> +</project> diff --git a/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardActorServiceProvider.java b/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardActorServiceProvider.java new file mode 100644 index 000000000..104c3830c --- /dev/null +++ b/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardActorServiceProvider.java @@ -0,0 +1,41 @@ +/*- + * ============LICENSE_START======================================================= + * SdncActorServiceProvider + * ================================================================================ + * Copyright (C) 2018-2019 Huawei Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. + * ================================================================================ + * 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.guard; + +import org.onap.policy.controlloop.actorserviceprovider.impl.HttpActor; +import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator; + +public class GuardActorServiceProvider extends HttpActor { + // actor name + public static final String NAME = "GUARD"; + + /** + * Constructs the object. + */ + public GuardActorServiceProvider() { + super(NAME); + + addOperator(HttpOperator.makeOperator(NAME, GuardOperation.NAME, + GuardOperation::new)); + } +} diff --git a/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardOperation.java b/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardOperation.java new file mode 100644 index 000000000..941838f00 --- /dev/null +++ b/models-interactions/model-actors/actor.guard/src/main/java/org/onap/policy/controlloop/actor/guard/GuardOperation.java @@ -0,0 +1,174 @@ +/*- + * ============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.guard; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; +import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.actorserviceprovider.Util; +import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation; +import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator; +import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; +import org.onap.policy.controlloop.policy.PolicyResult; +import org.onap.policy.models.decisions.concepts.DecisionRequest; +import org.onap.policy.models.decisions.concepts.DecisionResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Guard Operation. The outcome message is set to the guard response. If the guard is + * permitted or indeterminate, then the outcome is set to SUCCESS. + * <p/> + * The input to the request is taken from the payload, where properties are mapped to the + * field names in the {@link DecisionRequest} object. Properties whose names begin with + * "resource." are placed into the "resource" field of the {@link DecisionRequest}. The + * following will be provided, if not specified in the payload: + * <dl> + * <dt>action</dt> + * <dd>"guard"</dd> + * <dt>request ID</dt> + * <dd>generated</dd> + * </dl> + */ +public class GuardOperation extends HttpOperation<DecisionResponse> { + private static final Logger logger = LoggerFactory.getLogger(GuardOperation.class); + + // operation name + public static final String NAME = "Decision"; + + public static final String PERMIT = "Permit"; + public static final String DENY = "Deny"; + public static final String INDETERMINATE = "Indeterminate"; + + private static final String RESOURCE = "resource"; + + /** + * Prefix for properties in the payload that should be copied to the "resource" field + * of the request. + */ + public static final String RESOURCE_PREFIX = "resource."; + + + /** + * Constructs the object. + * + * @param params operation parameters + * @param operator operator that created this operation + */ + public GuardOperation(ControlLoopOperationParams params, HttpOperator operator) { + super(params, operator, DecisionResponse.class); + } + + @Override + protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) { + + DecisionRequest request = Util.translate(getName(), makeRequest(), DecisionRequest.class); + + Entity<DecisionRequest> entity = Entity.entity(request, MediaType.APPLICATION_JSON); + + Map<String, Object> headers = makeHeaders(); + + headers.put("Accept", MediaType.APPLICATION_JSON); + String url = makeUrl(); + + logMessage(EventType.OUT, CommInfrastructure.REST, url, request); + + // @formatter:off + return handleResponse(outcome, url, + callback -> getOperator().getClient().post(callback, makePath(), entity, headers)); + // @formatter:on + } + + /** + * Makes a request from the payload. + * + * @return a new request map + */ + protected Map<String, Object> makeRequest() { + if (params.getPayload() == null) { + throw new IllegalArgumentException("missing payload"); + } + + /* + * This code could be easily modified to allow the context and/or resource to be + * an encoded JSON string, that is decoded into a Map and stuffed into the + * appropriate field. + */ + + Map<String, Object> req = new LinkedHashMap<>(); + Map<String, Object> resource = new LinkedHashMap<>(); + + for (Entry<String, String> ent : params.getPayload().entrySet()) { + String key = ent.getKey(); + + if (key.startsWith(RESOURCE_PREFIX)) { + // it's a resource property - put into the resource map + key = key.substring(RESOURCE_PREFIX.length()); + resource.put(key, ent.getValue()); + + } else if (key.indexOf('.') < 0) { + // it's a normal property - put into the request map + req.put(key, ent.getValue()); + + } else { + logger.warn("{}: unused key {} in payload for {}", getFullName(), key, params.getRequestId()); + } + } + + req.putIfAbsent("action", "guard"); + req.computeIfAbsent("requestId", key -> UUID.randomUUID().toString()); + req.put(RESOURCE, resource); + + return req; + } + + @Override + protected CompletableFuture<OperationOutcome> postProcessResponse(OperationOutcome outcome, String url, + Response rawResponse, DecisionResponse response) { + + // determine the result + String status = response.getStatus(); + if (status == null) { + outcome.setResult(PolicyResult.FAILURE); + outcome.setMessage("response contains no status"); + return CompletableFuture.completedFuture(outcome); + } + + if (PERMIT.equalsIgnoreCase(status) || INDETERMINATE.equalsIgnoreCase(status)) { + outcome.setResult(PolicyResult.SUCCESS); + } else { + outcome.setResult(PolicyResult.FAILURE); + } + + // set the message + outcome.setMessage(response.getStatus()); + + return CompletableFuture.completedFuture(outcome); + } +} diff --git a/models-interactions/model-actors/actor.guard/src/main/resources/META-INF/services/org.onap.policy.controlloop.actorServiceProvider.spi.Actor b/models-interactions/model-actors/actor.guard/src/main/resources/META-INF/services/org.onap.policy.controlloop.actorServiceProvider.spi.Actor new file mode 100644 index 000000000..dd4368504 --- /dev/null +++ b/models-interactions/model-actors/actor.guard/src/main/resources/META-INF/services/org.onap.policy.controlloop.actorServiceProvider.spi.Actor @@ -0,0 +1 @@ +org.onap.policy.controlloop.actor.guard.GuardActorServiceProvider diff --git a/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardActorServiceProviderTest.java b/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardActorServiceProviderTest.java new file mode 100644 index 000000000..f4ab6061e --- /dev/null +++ b/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardActorServiceProviderTest.java @@ -0,0 +1,41 @@ +/*- + * ============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.guard; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.stream.Collectors; +import org.junit.Test; + +public class GuardActorServiceProviderTest { + + @Test + public void test() { + final GuardActorServiceProvider prov = new GuardActorServiceProvider(); + + // verify that it has the operators we expect + var expected = Arrays.asList(GuardOperation.NAME).stream().sorted().collect(Collectors.toList()); + var actual = prov.getOperationNames().stream().sorted().collect(Collectors.toList()); + + assertEquals(expected.toString(), actual.toString()); + } +} diff --git a/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardOperationTest.java b/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardOperationTest.java new file mode 100644 index 000000000..505106041 --- /dev/null +++ b/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardOperationTest.java @@ -0,0 +1,167 @@ +/*- + * ============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.guard; + +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 static org.mockito.Mockito.when; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.TreeMap; +import java.util.concurrent.CompletableFuture; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.controlloop.actor.test.BasicHttpOperation; +import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome; +import org.onap.policy.controlloop.actorserviceprovider.Util; +import org.onap.policy.controlloop.policy.PolicyResult; +import org.onap.policy.models.decisions.concepts.DecisionRequest; +import org.onap.policy.models.decisions.concepts.DecisionResponse; + +public class GuardOperationTest extends BasicHttpOperation<DecisionRequest> { + + private GuardOperation oper; + + /** + * Sets up. + */ + @Before + public void setUp() throws Exception { + super.setUp(); + + oper = new GuardOperation(params, operator); + } + + @Test + public void testConstructor() { + assertEquals(DEFAULT_ACTOR, oper.getActorName()); + assertEquals(DEFAULT_OPERATION, oper.getName()); + } + + @Test + public void testStartOperationAsync() throws Exception { + CompletableFuture<OperationOutcome> future2 = oper.start(); + executor.runAll(100); + assertFalse(future2.isDone()); + + DecisionResponse resp = new DecisionResponse(); + resp.setStatus(GuardOperation.PERMIT); + when(rawResponse.readEntity(String.class)).thenReturn(Util.translate("", resp, String.class)); + + verify(client).post(callbackCaptor.capture(), any(), requestCaptor.capture(), any()); + callbackCaptor.getValue().completed(rawResponse); + + executor.runAll(100); + assertTrue(future2.isDone()); + + DecisionRequest request = requestCaptor.getValue().getEntity(); + verifyRequest("makeReqStd.json", request, "requestId"); + + assertEquals(PolicyResult.SUCCESS, future2.get().getResult()); + } + + @Test + public void testMakeRequest() throws CoderException { + verifyPayload("makeReqStd.json", makePayload()); + verifyPayload("makeReqDefault.json", new TreeMap<>()); + + Map<String, String> payload = new TreeMap<>(); + payload.put("action", "some action"); + payload.put("hello", "world"); + payload.put("r u there?", "yes"); + payload.put("requestId", "some request id"); + payload.put("resource.abc", "def"); + payload.put("resource.ghi", "jkl"); + payload.put("some.other", "unused"); + + verifyPayload("makeReq.json", payload); + + // null payload - start with fresh parameters and operation + params = params.toBuilder().payload(null).build(); + oper = new GuardOperation(params, operator); + assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest()); + } + + private void verifyPayload(String expectedJsonFile, Map<String, String> payload) throws CoderException { + params.getPayload().clear(); + params.getPayload().putAll(payload); + + Map<String, Object> requestMap = oper.makeRequest(); + + verifyRequest(expectedJsonFile, requestMap, "requestId"); + } + + @Test + public void testPostProcessResponse() { + DecisionResponse response = new DecisionResponse(); + + // null status + response.setStatus(null); + verifyOutcome(response, PolicyResult.FAILURE, "response contains no status"); + + // permit, mixed case + response.setStatus("peRmit"); + verifyOutcome(response, PolicyResult.SUCCESS, "peRmit"); + + // indeterminate, mixed case + response.setStatus("inDETerminate"); + verifyOutcome(response, PolicyResult.SUCCESS, "inDETerminate"); + + // deny, mixed case + response.setStatus("deNY"); + verifyOutcome(response, PolicyResult.FAILURE, "deNY"); + + // unknown status + response.setStatus("unknown"); + verifyOutcome(response, PolicyResult.FAILURE, "unknown"); + } + + private void verifyOutcome(DecisionResponse response, PolicyResult expectedResult, String expectedMessage) { + oper.postProcessResponse(outcome, BASE_URI, rawResponse, response); + assertEquals(expectedResult, outcome.getResult()); + assertEquals(expectedMessage, outcome.getMessage()); + } + + @Override + protected Map<String, String> makePayload() { + DecisionRequest req = new DecisionRequest(); + req.setAction("my-action"); + req.setOnapComponent("my-onap-component"); + req.setOnapInstance("my-onap-instance"); + req.setOnapName("my-onap-name"); + req.setRequestId("my-request-id"); + + @SuppressWarnings("unchecked") + Map<String, String> map = Util.translate("", req, LinkedHashMap.class); + + // add resources + map.put(GuardOperation.RESOURCE_PREFIX + "actor", "resource-actor"); + map.put(GuardOperation.RESOURCE_PREFIX + "operation", "resource-operation"); + + return map; + } +} diff --git a/models-interactions/model-actors/actor.guard/src/test/resources/makeReq.json b/models-interactions/model-actors/actor.guard/src/test/resources/makeReq.json new file mode 100644 index 000000000..2716d030e --- /dev/null +++ b/models-interactions/model-actors/actor.guard/src/test/resources/makeReq.json @@ -0,0 +1,10 @@ +{ + "action": "some action", + "hello": "world", + "r u there?": "yes", + "requestId": "abcdefghi", + "resource": { + "abc": "def", + "ghi": "jkl" + } +} diff --git a/models-interactions/model-actors/actor.guard/src/test/resources/makeReqDefault.json b/models-interactions/model-actors/actor.guard/src/test/resources/makeReqDefault.json new file mode 100644 index 000000000..9e9df7b22 --- /dev/null +++ b/models-interactions/model-actors/actor.guard/src/test/resources/makeReqDefault.json @@ -0,0 +1,5 @@ +{ + "action": "guard", + "requestId": "abcdefghi", + "resource": {} +} diff --git a/models-interactions/model-actors/actor.guard/src/test/resources/makeReqStd.json b/models-interactions/model-actors/actor.guard/src/test/resources/makeReqStd.json new file mode 100644 index 000000000..5437ae896 --- /dev/null +++ b/models-interactions/model-actors/actor.guard/src/test/resources/makeReqStd.json @@ -0,0 +1,11 @@ +{ + "ONAPName": "my-onap-name", + "ONAPComponent": "my-onap-component", + "ONAPInstance": "my-onap-instance", + "requestId": "abcdefghi", + "action": "my-action", + "resource": { + "actor": "resource-actor", + "operation": "resource-operation" + } +}
\ No newline at end of file diff --git a/models-interactions/model-actors/actor.test/pom.xml b/models-interactions/model-actors/actor.test/pom.xml index 9bddbd9dd..3df35c355 100644 --- a/models-interactions/model-actors/actor.test/pom.xml +++ b/models-interactions/model-actors/actor.test/pom.xml @@ -36,13 +36,11 @@ <groupId>org.onap.policy.models.policy-models-interactions.model-impl</groupId> <artifactId>events</artifactId> <version>${project.version}</version> - <scope>provided</scope> </dependency> <dependency> <groupId>org.onap.policy.models.policy-models-interactions.model-actors</groupId> <artifactId>actorServiceProvider</artifactId> <version>${project.version}</version> - <scope>provided</scope> </dependency> <dependency> <groupId>org.onap.policy.common</groupId> diff --git a/models-interactions/model-actors/pom.xml b/models-interactions/model-actors/pom.xml index 30891142d..987788c51 100644 --- a/models-interactions/model-actors/pom.xml +++ b/models-interactions/model-actors/pom.xml @@ -38,6 +38,7 @@ <module>actorServiceProvider</module> <module>actor.test</module> <module>actor.aai</module> + <module>actor.guard</module> <module>actor.appc</module> <module>actor.vfc</module> <module>actor.sdnc</module> |