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/so/SoActorServiceProvider.java | 6 +-- .../policy/controlloop/actor/so/SoConstants.java | 29 ++++++++++++ .../policy/controlloop/actor/so/SoOperation.java | 55 +++++++++++++++++++--- .../controlloop/actor/so/VfModuleCreate.java | 53 +++++++++++++++++++-- ...licy.controlloop.actorServiceProvider.spi.Actor | 1 - ...licy.controlloop.actorserviceprovider.spi.Actor | 1 + 6 files changed, 132 insertions(+), 13 deletions(-) create mode 100644 models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoConstants.java delete mode 100644 models-interactions/model-actors/actor.so/src/main/resources/META-INF/services/org.onap.policy.controlloop.actorServiceProvider.spi.Actor create mode 100644 models-interactions/model-actors/actor.so/src/main/resources/META-INF/services/org.onap.policy.controlloop.actorserviceprovider.spi.Actor (limited to 'models-interactions/model-actors/actor.so/src/main') diff --git a/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoActorServiceProvider.java b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoActorServiceProvider.java index 1dbad623b..9c9e6dc62 100644 --- a/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoActorServiceProvider.java +++ b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoActorServiceProvider.java @@ -35,7 +35,7 @@ import org.onap.aai.domain.yang.Tenant; import org.onap.policy.aai.AaiCqResponse; import org.onap.policy.controlloop.ControlLoopOperation; import org.onap.policy.controlloop.VirtualControlLoopEvent; -import org.onap.policy.controlloop.actorserviceprovider.impl.ActorImpl; +import org.onap.policy.controlloop.actorserviceprovider.impl.HttpActor; import org.onap.policy.controlloop.policy.Policy; import org.onap.policy.so.SoCloudConfiguration; import org.onap.policy.so.SoManager; @@ -51,7 +51,7 @@ import org.onap.policy.so.util.Serialization; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class SoActorServiceProvider extends ActorImpl { +public class SoActorServiceProvider extends HttpActor { private static final Logger logger = LoggerFactory.getLogger(SoActorServiceProvider.class); public static final String NAME = "SO"; @@ -96,7 +96,7 @@ public class SoActorServiceProvider extends ActorImpl { * Constructs the object. */ public SoActorServiceProvider() { - super(NAME); + super(NAME, SoActorParams.class); addOperator(new SoOperator(NAME, VfModuleCreate.NAME, VfModuleCreate::new)); } diff --git a/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoConstants.java b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoConstants.java new file mode 100644 index 000000000..faafb43a6 --- /dev/null +++ b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoConstants.java @@ -0,0 +1,29 @@ +/*- + * ============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.so; + +public class SoConstants { + public static final String CONTEXT_KEY_VF_COUNT = "SO.VFCount"; + + private SoConstants() { + // do nothing + } +} diff --git a/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoOperation.java b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoOperation.java index d8d960e54..53fb973a5 100644 --- a/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoOperation.java +++ b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SoOperation.java @@ -96,6 +96,23 @@ public abstract class SoOperation extends HttpOperation { getCount = 0; } + /** + * Validates that the parameters contain the required target information to extract + * the VF count from the custom query. + */ + protected void validateTarget() { + verifyNotNull("Target information", params.getTarget()); + verifyNotNull("model-customization-id", params.getTarget().getModelCustomizationId()); + verifyNotNull("model-invariant-id", params.getTarget().getModelInvariantId()); + verifyNotNull("model-version-id", params.getTarget().getModelVersionId()); + } + + private void verifyNotNull(String type, Object value) { + if (value == null) { + throw new IllegalArgumentException("missing " + type + " for guard payload"); + } + } + /** * Starts the GUARD. */ @@ -104,6 +121,24 @@ public abstract class SoOperation extends HttpOperation { return startGuardAsync(); } + /** + * Stores the VF count and then runs the guard. + * + * @return a future to cancel or await the guard response + */ + protected CompletableFuture storeVfCountRunGuard() { + String custId = params.getTarget().getModelCustomizationId(); + String invId = params.getTarget().getModelInvariantId(); + String verId = params.getTarget().getModelVersionId(); + + AaiCqResponse cq = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY); + int vfcount = cq.getVfModuleCount(custId, invId, verId); + + params.getContext().setProperty(SoConstants.CONTEXT_KEY_VF_COUNT, vfcount); + + return startGuardAsync(); + } + /** * If the response does not indicate that the request has been completed, then sleep a * bit and issue a "get". @@ -116,6 +151,7 @@ public abstract class SoOperation extends HttpOperation { if (rawResponse.getStatus() == 200) { String requestState = getRequestState(response); if (COMPLETE.equalsIgnoreCase(requestState)) { + successfulCompletion(); return CompletableFuture .completedFuture(setOutcome(outcome, PolicyResult.SUCCESS, rawResponse, response)); } @@ -146,6 +182,13 @@ public abstract class SoOperation extends HttpOperation { return sleep(getWaitMsGet(), TimeUnit.MILLISECONDS).thenComposeAsync(doGet); } + /** + * Invoked when a request completes successfully. + */ + protected void successfulCompletion() { + // do nothing + } + /** * Issues a "get" request to see if the original request is complete yet. * @@ -251,13 +294,13 @@ public abstract class SoOperation extends HttpOperation { return null; } - String json = params.getPayload().get(REQ_PARAM_NM); - if (json == null) { + Object data = params.getPayload().get(REQ_PARAM_NM); + if (data == null) { return null; } try { - return coder.decode(json, SoRequestParameters.class); + return coder.decode(data.toString(), SoRequestParameters.class); } catch (CoderException e) { throw new IllegalArgumentException("invalid payload value: " + REQ_PARAM_NM); } @@ -271,14 +314,14 @@ public abstract class SoOperation extends HttpOperation { return null; } - String json = params.getPayload().get(CONFIG_PARAM_NM); - if (json == null) { + Object data = params.getPayload().get(CONFIG_PARAM_NM); + if (data == null) { return null; } try { @SuppressWarnings("unchecked") - List> result = coder.decode(json, ArrayList.class); + List> result = coder.decode(data.toString(), ArrayList.class); return result; } catch (CoderException | RuntimeException e) { throw new IllegalArgumentException("invalid payload value: " + CONFIG_PARAM_NM); diff --git a/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/VfModuleCreate.java b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/VfModuleCreate.java index c17d25211..e88a10cff 100644 --- a/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/VfModuleCreate.java +++ b/models-interactions/model-actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/VfModuleCreate.java @@ -20,6 +20,7 @@ package org.onap.policy.controlloop.actor.so; +import java.util.Map; import java.util.concurrent.CompletableFuture; import javax.ws.rs.client.Entity; import javax.ws.rs.core.MediaType; @@ -45,24 +46,61 @@ import org.onap.policy.so.SoRequest; import org.onap.policy.so.SoRequestDetails; import org.onap.policy.so.SoRequestParameters; +/** + * Operation to create a VF Module. This gets the VF count from the A&AI Custom Query + * response and stores it in the context. It also passes the count+1 to the guard. Once + * the "create" completes successfully, it bumps the VF count that's stored in the + * context. + *

+ * Note: currently, this only supports storing the count for a single target VF. + */ public class VfModuleCreate extends SoOperation { public static final String NAME = "VF Module Create"; + public static final String PAYLOAD_KEY_VF_COUNT = "vfCount"; + + /** + * Constructs the object. + * + * @param params operation parameters + * @param config configuration for this operation + */ public VfModuleCreate(ControlLoopOperationParams params, HttpConfig config) { super(params, config); + + // ensure we have the necessary parameters + validateTarget(); } /** - * Ensures that A&AI customer query has been performed, and then runs the guard query. + * Ensures that A&AI customer query has been performed, and then runs the guard. */ @Override @SuppressWarnings("unchecked") protected CompletableFuture startPreprocessorAsync() { + if (params.getContext().contains(SoConstants.CONTEXT_KEY_VF_COUNT)) { + return startGuardAsync(); + } + + // need the VF count ControlLoopOperationParams cqParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME) .operation(AaiCustomQueryOperation.NAME).payload(null).retry(null).timeoutSec(null).build(); - // run Custom Query and Guard, in parallel - return allOf(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams), this::startGuardAsync); + // run Custom Query, extract the VF count, and then run the Guard + return sequence(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams), + this::storeVfCountRunGuard); + } + + @Override + protected Map makeGuardPayload() { + Map payload = super.makeGuardPayload(); + + int vfcount = params.getContext().getProperty(SoConstants.CONTEXT_KEY_VF_COUNT); + + // run guard with the proposed vf count + payload.put(PAYLOAD_KEY_VF_COUNT, vfcount + 1); + + return payload; } @Override @@ -85,6 +123,15 @@ public class VfModuleCreate extends SoOperation { return handleResponse(outcome, url, callback -> getClient().post(callback, path, entity, null)); } + /** + * Increments the VF count that's stored in the context. + */ + @Override + protected void successfulCompletion() { + int vfcount = params.getContext().getProperty(SoConstants.CONTEXT_KEY_VF_COUNT); + params.getContext().setProperty(SoConstants.CONTEXT_KEY_VF_COUNT, vfcount + 1); + } + /** * Makes a request. * diff --git a/models-interactions/model-actors/actor.so/src/main/resources/META-INF/services/org.onap.policy.controlloop.actorServiceProvider.spi.Actor b/models-interactions/model-actors/actor.so/src/main/resources/META-INF/services/org.onap.policy.controlloop.actorServiceProvider.spi.Actor deleted file mode 100644 index a955eb71c..000000000 --- a/models-interactions/model-actors/actor.so/src/main/resources/META-INF/services/org.onap.policy.controlloop.actorServiceProvider.spi.Actor +++ /dev/null @@ -1 +0,0 @@ -org.onap.policy.controlloop.actor.so.SoActorServiceProvider \ No newline at end of file diff --git a/models-interactions/model-actors/actor.so/src/main/resources/META-INF/services/org.onap.policy.controlloop.actorserviceprovider.spi.Actor b/models-interactions/model-actors/actor.so/src/main/resources/META-INF/services/org.onap.policy.controlloop.actorserviceprovider.spi.Actor new file mode 100644 index 000000000..a955eb71c --- /dev/null +++ b/models-interactions/model-actors/actor.so/src/main/resources/META-INF/services/org.onap.policy.controlloop.actorserviceprovider.spi.Actor @@ -0,0 +1 @@ +org.onap.policy.controlloop.actor.so.SoActorServiceProvider \ No newline at end of file -- cgit 1.2.3-korg