Remove legacy actor code from models
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / impl / ActorImpl.java
index 9b9aa91..336860e 100644 (file)
 
 package org.onap.policy.controlloop.actorserviceprovider.impl;
 
-import com.google.common.collect.ImmutableMap;
-import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.function.Function;
-import org.onap.policy.common.parameters.BeanValidationResult;
 import org.onap.policy.controlloop.actorserviceprovider.Operator;
 import org.onap.policy.controlloop.actorserviceprovider.Util;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.ActorParams;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
 import org.slf4j.Logger;
@@ -46,44 +42,50 @@ public class ActorImpl extends StartConfigPartial<Map<String, Object>> implement
     /**
      * Maps a name to an operator.
      */
-    private Map<String, Operator> name2operator;
+    private final Map<String, Operator> name2operator = new ConcurrentHashMap<>();
 
     /**
      * Constructs the object.
      *
      * @param name actor name
-     * @param operators the operations supported by this actor
      */
-    public ActorImpl(String name, Operator... operators) {
+    public ActorImpl(String name) {
         super(name);
-        setOperators(Arrays.asList(operators));
     }
 
     /**
-     * Sets the operators supported by this actor, overriding any previous list.
+     * This method simply returns {@code 0}.
+     */
+    @Override
+    public int getSequenceNumber() {
+        return 0;
+    }
+
+    /**
+     * Adds an operator supported by this actor.
      *
-     * @param operators the operations supported by this actor
+     * @param operator operation to be added
      */
-    protected void setOperators(List<Operator> operators) {
+    protected synchronized void addOperator(Operator operator) {
+        /*
+         * This method is "synchronized" to prevent the state from changing while the
+         * operator is added. The map, itself, does not need synchronization as it's a
+         * concurrent map.
+         */
+
         if (isConfigured()) {
             throw new IllegalStateException("attempt to set operators on a configured actor: " + getName());
         }
 
-        Map<String, Operator> map = new HashMap<>();
-        for (Operator newOp : operators) {
-            map.compute(newOp.getName(), (opName, existingOp) -> {
-                if (existingOp == null) {
-                    return newOp;
-                }
-
-                // TODO: should this throw an exception?
-                logger.warn("duplicate names for actor operation {}.{}: {}, ignoring {}", getName(), opName,
-                                existingOp.getClass().getSimpleName(), newOp.getClass().getSimpleName());
-                return existingOp;
-            });
-        }
+        name2operator.compute(operator.getName(), (opName, existingOp) -> {
+            if (existingOp == null) {
+                return operator;
+            }
 
-        this.name2operator = ImmutableMap.copyOf(map);
+            logger.warn("duplicate names for actor operation {}.{}: {}, ignoring {}", getName(), opName,
+                            existingOp.getClass().getSimpleName(), operator.getClass().getSimpleName());
+            return existingOp;
+        });
     }
 
     @Override
@@ -95,7 +97,7 @@ public class ActorImpl extends StartConfigPartial<Map<String, Object>> implement
     public Operator getOperator(String name) {
         Operator operator = name2operator.get(name);
         if (operator == null) {
-            throw new IllegalArgumentException("unknown operation " + getName() + "." + name);
+            throw new IllegalArgumentException("unknown operator " + getName() + "." + name);
         }
 
         return operator;
@@ -120,8 +122,6 @@ public class ActorImpl extends StartConfigPartial<Map<String, Object>> implement
         final String actorName = getName();
         logger.info("configuring operations for actor {}", actorName);
 
-        BeanValidationResult valres = new BeanValidationResult(actorName, parameters);
-
         // function that creates operator-specific parameters, given the operation name
         Function<String, Map<String, Object>> opParamsMaker = makeOperatorParameters(parameters);
 
@@ -135,8 +135,8 @@ public class ActorImpl extends StartConfigPartial<Map<String, Object>> implement
                     operator.configure(subparams);
 
                 } catch (ParameterValidationRuntimeException e) {
-                    logger.warn("failed to configure operation {}.{}", actorName, operName, e);
-                    valres.addResult(e.getResult());
+                    logger.warn("failed to configure operation {}.{} because:\n{}", actorName, operName,
+                                    e.getResult().getResult(), e);
 
                 } catch (RuntimeException e) {
                     logger.warn("failed to configure operation {}.{}", actorName, operName, e);
@@ -155,7 +155,8 @@ public class ActorImpl extends StartConfigPartial<Map<String, Object>> implement
 
     /**
      * Extracts the operator parameters from the actor parameters, for a given operator.
-     * This method assumes each operation has its own set of parameters.
+     * This method translates the parameters to an {@link ActorParams} and then creates a function
+     * that will extract operator-specific parameters.
      *
      * @param actorParameters actor parameters
      * @return a function to extract the operator parameters from the actor parameters.
@@ -163,8 +164,13 @@ public class ActorImpl extends StartConfigPartial<Map<String, Object>> implement
      *         the given operation name
      */
     protected Function<String, Map<String, Object>> makeOperatorParameters(Map<String, Object> actorParameters) {
+        String actorName = getName();
 
-        return operName -> Util.translateToMap(getName() + "." + operName, actorParameters.get(operName));
+        // @formatter:off
+        return Util.translate(actorName, actorParameters, ActorParams.class)
+                        .doValidation(actorName)
+                        .makeOperationParameters(actorName);
+        // @formatter:on
     }
 
     /**
@@ -177,7 +183,7 @@ public class ActorImpl extends StartConfigPartial<Map<String, Object>> implement
 
         for (Operator oper : name2operator.values()) {
             if (oper.isConfigured()) {
-                Util.logException(oper::start, "failed to start operation {}.{}", actorName, oper.getName());
+                Util.runFunction(oper::start, "failed to start operation {}.{}", actorName, oper.getName());
 
             } else {
                 logger.warn("not starting unconfigured operation {}.{}", actorName, oper.getName());
@@ -195,7 +201,7 @@ public class ActorImpl extends StartConfigPartial<Map<String, Object>> implement
 
         // @formatter:off
         name2operator.values().forEach(
-            oper -> Util.logException(oper::stop, "failed to stop operation {}.{}", actorName, oper.getName()));
+            oper -> Util.runFunction(oper::stop, "failed to stop operation {}.{}", actorName, oper.getName()));
         // @formatter:on
     }
 
@@ -208,32 +214,8 @@ public class ActorImpl extends StartConfigPartial<Map<String, Object>> implement
         logger.info("shutting down operations for actor {}", actorName);
 
         // @formatter:off
-        name2operator.values().forEach(oper -> Util.logException(oper::shutdown,
+        name2operator.values().forEach(oper -> Util.runFunction(oper::shutdown,
                         "failed to shutdown operation {}.{}", actorName, oper.getName()));
         // @formatter:on
     }
-
-    // TODO old code: remove lines down to **HERE**
-
-    @Override
-    public String actor() {
-        return null;
-    }
-
-    @Override
-    public List<String> recipes() {
-        return Collections.emptyList();
-    }
-
-    @Override
-    public List<String> recipeTargets(String recipe) {
-        return Collections.emptyList();
-    }
-
-    @Override
-    public List<String> recipePayloads(String recipe) {
-        return Collections.emptyList();
-    }
-
-    // **HERE**
 }