From 7f69c5ca0a6f6018166f8fee3e811edf4dee1eb8 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 20 Feb 2020 19:08:55 -0500 Subject: Change payload to Map so it's more versatile This was supposed to be two separate commits, but I goofed something. Added guard query to Operation superclass. Modified VfModuleCreate to store the VF count, pass it to the guard, and bump it once the create completes successfully. Added code to check Actors for proper plug-in to ActorService. Renamed "operation" property to "operations", to be more consistent with other parameters (e.g., TopicParameterGroup). The META-INF/services files for the actors had mixed case, which did not match the package name of the Actor class, preventing the ServiceLoader from recognizing them. Also modified the ActorService to skip any that cannot actually be loaded, for whatever reason (e.g., not in the classpath). Issue-ID: POLICY-1625 Signed-off-by: Jim Hahn Change-Id: Ifa97744543f2866cc553138ec5ec644b033de780 --- .../actor/guard/GuardActorServiceProvider.java | 3 +- .../controlloop/actor/guard/GuardOperation.java | 37 ++-------------------- ...licy.controlloop.actorServiceProvider.spi.Actor | 1 - ...licy.controlloop.actorserviceprovider.spi.Actor | 1 + .../actor/guard/GuardActorParamsTest.java | 8 +++-- .../actor/guard/GuardActorServiceProviderTest.java | 6 +++- .../actor/guard/GuardOperationTest.java | 26 ++++++++------- .../src/test/resources/makeReqDefault.json | 3 +- .../actor.guard/src/test/resources/service.yaml | 31 ++++++++++++++++++ 9 files changed, 63 insertions(+), 53 deletions(-) delete mode 100644 models-interactions/model-actors/actor.guard/src/main/resources/META-INF/services/org.onap.policy.controlloop.actorServiceProvider.spi.Actor create mode 100644 models-interactions/model-actors/actor.guard/src/main/resources/META-INF/services/org.onap.policy.controlloop.actorserviceprovider.spi.Actor create mode 100644 models-interactions/model-actors/actor.guard/src/test/resources/service.yaml (limited to 'models-interactions/model-actors/actor.guard/src') 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 index ea08d13da..e37de57bb 100644 --- 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 @@ -24,10 +24,11 @@ package org.onap.policy.controlloop.actor.guard; import org.onap.policy.controlloop.actorserviceprovider.impl.HttpActor; import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator; +import org.onap.policy.controlloop.actorserviceprovider.impl.OperationPartial; public class GuardActorServiceProvider extends HttpActor { // actor name - public static final String NAME = "GUARD"; + public static final String NAME = OperationPartial.GUARD_ACTOR_NAME; /** * Constructs the object. 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 index 453a3e377..a5459f660 100644 --- 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 @@ -20,9 +20,7 @@ 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; @@ -33,13 +31,12 @@ 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.OperationPartial; import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams; import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig; 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 @@ -57,17 +54,14 @@ import org.slf4j.LoggerFactory; * */ public class GuardOperation extends HttpOperation { - private static final Logger logger = LoggerFactory.getLogger(GuardOperation.class); // operation name - public static final String NAME = "Decision"; + public static final String NAME = OperationPartial.GUARD_OPERATION_NAME; 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. @@ -118,34 +112,9 @@ public class GuardOperation extends HttpOperation { 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 req = config.makeRequest(); - Map resource = new LinkedHashMap<>(); - - for (Entry 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.putAll(params.getPayload()); req.computeIfAbsent("requestId", key -> UUID.randomUUID().toString()); - req.put(RESOURCE, resource); return req; } 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 deleted file mode 100644 index dd4368504..000000000 --- a/models-interactions/model-actors/actor.guard/src/main/resources/META-INF/services/org.onap.policy.controlloop.actorServiceProvider.spi.Actor +++ /dev/null @@ -1 +0,0 @@ -org.onap.policy.controlloop.actor.guard.GuardActorServiceProvider 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/GuardActorParamsTest.java b/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardActorParamsTest.java index cd9f4e316..1c256090e 100644 --- a/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardActorParamsTest.java +++ b/models-interactions/model-actors/actor.guard/src/test/java/org/onap/policy/controlloop/actor/guard/GuardActorParamsTest.java @@ -32,6 +32,7 @@ import org.junit.Before; import org.junit.Test; import org.onap.policy.common.parameters.ValidationResult; import org.onap.policy.controlloop.actorserviceprovider.Util; +import org.onap.policy.controlloop.actorserviceprovider.parameters.CommonActorParams; public class GuardActorParamsTest { private static final String CONTAINER = "my-container"; @@ -68,13 +69,14 @@ public class GuardActorParamsTest { assertTrue(params.validate(CONTAINER).isValid()); // only a few fields are required - GuardActorParams sparse = Util.translate(CONTAINER, Map.of("operation", operations), GuardActorParams.class); + GuardActorParams sparse = Util.translate(CONTAINER, Map.of(CommonActorParams.OPERATIONS_FIELD, operations), + GuardActorParams.class); assertTrue(sparse.validate(CONTAINER).isValid()); assertEquals(GuardActorParams.DEFAULT_ACTION, sparse.getAction()); // check fields from superclass - testValidateField("operation", "null", params2 -> params2.setOperation(null)); + testValidateField(CommonActorParams.OPERATIONS_FIELD, "null", params2 -> params2.setOperations(null)); testValidateField("timeoutSec", "minimum", params2 -> params2.setTimeoutSec(-1)); } @@ -96,7 +98,7 @@ public class GuardActorParamsTest { GuardActorParams params2 = new GuardActorParams(); params2.setClientName(CLIENT); params2.setTimeoutSec(TIMEOUT); - params2.setOperation(operations); + params2.setOperations(operations); params2.setOnapName(ONAP_NAME); params2.setOnapComponent(ONAP_COMP); 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 index f4ab6061e..bb5d4ddf8 100644 --- 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 @@ -25,8 +25,9 @@ import static org.junit.Assert.assertEquals; import java.util.Arrays; import java.util.stream.Collectors; import org.junit.Test; +import org.onap.policy.controlloop.actor.test.BasicActor; -public class GuardActorServiceProviderTest { +public class GuardActorServiceProviderTest extends BasicActor { @Test public void test() { @@ -37,5 +38,8 @@ public class GuardActorServiceProviderTest { var actual = prov.getOperationNames().stream().sorted().collect(Collectors.toList()); assertEquals(expected.toString(), actual.toString()); + + // verify that it all plugs into the ActorService + verifyActorService(GuardActorServiceProvider.NAME, "service.yaml"); } } 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 index 406cdd05d..bd23a818b 100644 --- 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 @@ -92,14 +92,16 @@ public class GuardOperationTest extends BasicHttpOperation { verifyPayload("makeReqStd.json", makePayload()); verifyPayload("makeReqDefault.json", new TreeMap<>()); - Map payload = new TreeMap<>(); + Map 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"); + + Map resource = new TreeMap<>(); + payload.put("resource", resource); + resource.put("abc", "def"); + resource.put("ghi", "jkl"); verifyPayload("makeReq.json", payload); @@ -109,7 +111,7 @@ public class GuardOperationTest extends BasicHttpOperation { assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest()); } - private void verifyPayload(String expectedJsonFile, Map payload) throws CoderException { + private void verifyPayload(String expectedJsonFile, Map payload) throws CoderException { params.getPayload().clear(); params.getPayload().putAll(payload); @@ -150,7 +152,7 @@ public class GuardOperationTest extends BasicHttpOperation { } @Override - protected Map makePayload() { + protected Map makePayload() { DecisionRequest req = new DecisionRequest(); req.setAction("my-action"); req.setOnapComponent("my-onap-component"); @@ -158,12 +160,14 @@ public class GuardOperationTest extends BasicHttpOperation { req.setOnapName("my-onap-name"); req.setRequestId("my-request-id"); - @SuppressWarnings("unchecked") - Map map = Util.translate("", req, TreeMap.class); - // add resources - map.put(GuardOperation.RESOURCE_PREFIX + "actor", "resource-actor"); - map.put(GuardOperation.RESOURCE_PREFIX + "operation", "resource-operation"); + Map resource = new TreeMap<>(); + req.setResource(resource); + resource.put("actor", "resource-actor"); + resource.put("operation", "resource-operation"); + + @SuppressWarnings("unchecked") + Map map = Util.translate("", req, TreeMap.class); return map; } 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 index 9e9df7b22..d757eb993 100644 --- a/models-interactions/model-actors/actor.guard/src/test/resources/makeReqDefault.json +++ b/models-interactions/model-actors/actor.guard/src/test/resources/makeReqDefault.json @@ -1,5 +1,4 @@ { "action": "guard", - "requestId": "abcdefghi", - "resource": {} + "requestId": "abcdefghi" } diff --git a/models-interactions/model-actors/actor.guard/src/test/resources/service.yaml b/models-interactions/model-actors/actor.guard/src/test/resources/service.yaml new file mode 100644 index 000000000..131cf785b --- /dev/null +++ b/models-interactions/model-actors/actor.guard/src/test/resources/service.yaml @@ -0,0 +1,31 @@ +# +# ============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======================================================== +# +httpClients: +- clientName: my-client + hostname: localhost + port: 80 + basePath: base-url + managed: true +actors: + GUARD: + clientName: my-client + operations: + Decision: + path: decide \ No newline at end of file -- cgit 1.2.3-korg