X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-interactions%2Fmodel-actors%2FactorServiceProvider%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fcontrolloop%2Factorserviceprovider%2Fimpl%2FOperationPartial.java;h=9ce53aa7ad6aca3df04af11a78385ee02b125213;hb=364ef26929f06637bca03dd7bfb5e8ac69b611f8;hp=cdbdc2af568262ad51360d13e30a20bb0572cafb;hpb=b498abeab3c840ce9cfd09025892c8e93fa9a75e;p=policy%2Fmodels.git diff --git a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartial.java b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartial.java index cdbdc2af5..9ce53aa7a 100644 --- a/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartial.java +++ b/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/OperationPartial.java @@ -23,11 +23,13 @@ package org.onap.policy.controlloop.actorserviceprovider.impl; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Queue; +import java.util.UUID; import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; @@ -38,7 +40,9 @@ import java.util.function.BiConsumer; import java.util.function.Function; import java.util.function.Supplier; import java.util.function.UnaryOperator; +import lombok.AccessLevel; import lombok.Getter; +import lombok.Setter; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.utils.NetLoggerUtil; import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType; @@ -96,17 +100,31 @@ public abstract class OperationPartial implements Operation { @Getter private final String fullName; + @Getter + @Setter(AccessLevel.PROTECTED) + private String subRequestId; + + @Getter + private final List propertyNames; + + /** + * Values for the properties identified by {@link #getPropertyNames()}. + */ + private final Map properties = new HashMap<>(); + /** * Constructs the object. * * @param params operation parameters * @param config configuration for this operation + * @param propertyNames names of properties required by this operation */ - public OperationPartial(ControlLoopOperationParams params, OperatorConfig config) { + public OperationPartial(ControlLoopOperationParams params, OperatorConfig config, List propertyNames) { this.params = params; this.config = config; this.fullName = params.getActor() + "." + params.getOperation(); + this.propertyNames = propertyNames; } public Executor getBlockingExecutor() { @@ -121,6 +139,27 @@ public abstract class OperationPartial implements Operation { return params.getOperation(); } + /** + * Sets a property. + * + * @param name property name + * @param value new value + */ + public void setProperty(String name, Object value) { + properties.put(name, value); + } + + /** + * Gets a property's value. + * + * @param name name of the property of interest + * @return the property's value, or {@code null} if it has no value + */ + @SuppressWarnings("unchecked") + public T getProperty(String name) { + return (T) properties.get(name); + } + @Override public CompletableFuture start() { // allocate a controller for the entire operation @@ -229,15 +268,12 @@ public abstract class OperationPartial implements Operation { * {@code null} if this operation has no guard */ protected CompletableFuture startGuardAsync() { - // get the guard payload - Map guardPayload = makeGuardPayload(); - - // wrap it in a "resource" - Map resource = new LinkedHashMap<>(); - resource.put("guard", guardPayload); + if (params.isPreprocessed()) { + return null; + } - Map payload = new LinkedHashMap<>(); - payload.put("resource", resource); + // get the guard payload + Map payload = makeGuardPayload(); /* * Note: can't use constants from actor.guard, because that would create a @@ -253,9 +289,10 @@ public abstract class OperationPartial implements Operation { * @return a new guard payload */ protected Map makeGuardPayload() { + // TODO delete this once preprocessing is done by the application Map guard = new LinkedHashMap<>(); guard.put("actor", params.getActor()); - guard.put("recipe", params.getOperation()); + guard.put("operation", params.getOperation()); guard.put("target", params.getTargetEntity()); guard.put("requestId", params.getRequestId()); @@ -278,6 +315,8 @@ public abstract class OperationPartial implements Operation { private CompletableFuture startOperationAttempt( PipelineControllerFuture controller, int attempt) { + generateSubRequestId(attempt); + // propagate "stop" to the operation attempt controller.wrap(startAttemptWithoutRetries(attempt)).thenCompose(retryOnFailure(controller, attempt)) .whenCompleteAsync(controller.delayedComplete(), params.getExecutor()); @@ -285,6 +324,16 @@ public abstract class OperationPartial implements Operation { return controller; } + /** + * Generates and sets {@link #subRequestId} to a new subrequest ID. + * @param attempt attempt number, typically starting with 1 + */ + public void generateSubRequestId(int attempt) { + // Note: this should be "protected", but that makes junits much messier + + setSubRequestId(UUID.randomUUID().toString()); + } + /** * Starts the operation attempt, without doing any retries. * @@ -896,6 +945,7 @@ public abstract class OperationPartial implements Operation { return (outcome, thrown) -> { if (callbacks.canStart()) { + outcome.setSubRequestId(getSubRequestId()); outcome.setStart(callbacks.getStartTime()); outcome.setEnd(null); @@ -924,6 +974,7 @@ public abstract class OperationPartial implements Operation { return (outcome, thrown) -> { if (callbacks.canEnd()) { + outcome.setSubRequestId(getSubRequestId()); outcome.setStart(callbacks.getStartTime()); outcome.setEnd(callbacks.getEndTime()); @@ -975,30 +1026,24 @@ public abstract class OperationPartial implements Operation { } /** - * Logs a response. If the response is not of type, String, then it attempts to + * Logs a message. If the message is not of type, String, then it attempts to * pretty-print it into JSON before logging. * * @param direction IN or OUT * @param infra communication infrastructure on which it was published * @param source source name (e.g., the URL or Topic name) - * @param response response to be logged + * @param message message to be logged * @return the JSON text that was logged */ - public String logMessage(EventType direction, CommInfrastructure infra, String source, T response) { + public String logMessage(EventType direction, CommInfrastructure infra, String source, T message) { String json; try { - if (response == null) { - json = null; - } else if (response instanceof String) { - json = response.toString(); - } else { - json = makeCoder().encode(response, true); - } + json = prettyPrint(message); - } catch (CoderException e) { + } catch (IllegalArgumentException e) { String type = (direction == EventType.IN ? "response" : "request"); logger.warn("cannot pretty-print {}", type, e); - json = response.toString(); + json = message.toString(); } logger.info("[{}|{}|{}|]{}{}", direction, infra, source, NetLoggerUtil.SYSTEM_LS, json); @@ -1006,6 +1051,28 @@ public abstract class OperationPartial implements Operation { return json; } + /** + * Converts a message to a "pretty-printed" String using the operation's normal + * serialization provider (i.e., it's coder). + * + * @param message response to be logged + * @return the JSON text that was logged + * @throws IllegalArgumentException if the message cannot be converted + */ + public String prettyPrint(T message) { + if (message == null) { + return null; + } else if (message instanceof String) { + return message.toString(); + } else { + try { + return getCoder().encode(message, true); + } catch (CoderException e) { + throw new IllegalArgumentException("cannot encode message", e); + } + } + } + // these may be overridden by subclasses or junit tests /** @@ -1041,7 +1108,7 @@ public abstract class OperationPartial implements Operation { // these may be overridden by junit tests - protected Coder makeCoder() { + protected Coder getCoder() { return coder; } }