Address more sonars in apex-pdp
[policy/apex-pdp.git] / services / services-engine / src / main / java / org / onap / policy / apex / service / parameters / engineservice / EngineServiceParametersJsonAdapter.java
index 902322c..dee3a15 100644 (file)
@@ -1,19 +1,21 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
+ *  Modifications Copyright (C) 2021 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.
- * 
+ *
  * SPDX-License-Identifier: Apache-2.0
  * ============LICENSE_END=========================================================
  */
@@ -27,10 +29,10 @@ import com.google.gson.JsonObject;
 import com.google.gson.JsonParseException;
 import com.google.gson.JsonSerializationContext;
 import com.google.gson.JsonSerializer;
-
 import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map.Entry;
-
 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
 import org.onap.policy.apex.context.parameters.ContextParameters;
 import org.onap.policy.apex.context.parameters.DistributorParameters;
@@ -40,8 +42,11 @@ import org.onap.policy.apex.context.parameters.SchemaHelperParameters;
 import org.onap.policy.apex.context.parameters.SchemaParameters;
 import org.onap.policy.apex.core.engine.EngineParameters;
 import org.onap.policy.apex.core.engine.ExecutorParameters;
+import org.onap.policy.apex.core.engine.TaskParameters;
 import org.onap.policy.common.parameters.ParameterGroup;
 import org.onap.policy.common.parameters.ParameterRuntimeException;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
 import org.slf4j.ext.XLogger;
 import org.slf4j.ext.XLoggerFactory;
 
@@ -66,13 +71,15 @@ public class EngineServiceParametersJsonAdapter
     private static final String EXECUTOR_PARAMETERS     = "executorParameters";
     // @formatter:on
 
+    private static StandardCoder standardCoder = new StandardCoder();
+
     /**
      * {@inheritDoc}.
      */
     @Override
     public JsonElement serialize(final EngineParameters src, final Type typeOfSrc,
                     final JsonSerializationContext context) {
-        final String returnMessage = "serialization of Apex parameters to Json is not supported";
+        final var returnMessage = "serialization of Apex parameters to Json is not supported";
         LOGGER.error(returnMessage);
         throw new ParameterRuntimeException(returnMessage);
     }
@@ -83,9 +90,9 @@ public class EngineServiceParametersJsonAdapter
     @Override
     public EngineParameters deserialize(final JsonElement json, final Type typeOfT,
                     final JsonDeserializationContext context) {
-        final JsonObject engineParametersJsonObject = json.getAsJsonObject();
+        final var engineParametersJsonObject = json.getAsJsonObject();
 
-        final EngineParameters engineParameters = new EngineParameters();
+        final var engineParameters = new EngineParameters();
 
         // Deserialise context parameters, they may be a subclass of the ContextParameters class
         engineParameters.setContextParameters(
@@ -97,9 +104,37 @@ public class EngineServiceParametersJsonAdapter
         // Executor parameter wrangling
         getExecutorParameters(engineParametersJsonObject, engineParameters, context);
 
+        // Task parameter wrangling
+        getTaskParametersList(engineParametersJsonObject, engineParameters);
         return engineParameters;
     }
 
+    /**
+     * Method to get the task parameters list for Apex.
+     *
+     * @param engineParametersJsonObject The input JSON
+     * @param engineParameters The output parameters
+     */
+    private void getTaskParametersList(JsonObject engineParametersJsonObject, EngineParameters engineParameters) {
+        final JsonElement parametersElement = engineParametersJsonObject.get("taskParameters");
+
+        // configurable parameters are optional so if the element does not exist, just return
+        if (parametersElement == null) {
+            return;
+        }
+        List<TaskParameters> parametersList = new ArrayList<>();
+        parametersElement.getAsJsonArray().forEach(taskParam -> {
+            TaskParameters parameters = null;
+            try {
+                parameters = standardCoder.decode(standardCoder.encode(taskParam), TaskParameters.class);
+            } catch (CoderException e) {
+                throw new ParameterRuntimeException("Error reading taskParameters from the config json provided.");
+            }
+            parametersList.add(parameters);
+        });
+        engineParameters.setTaskParameters(parametersList);
+    }
+
     /**
      * Get the context parameters for Apex.
      *
@@ -117,12 +152,12 @@ public class EngineServiceParametersJsonAdapter
         }
 
         // We do this because the JSON parameters may be for a subclass of ContextParameters
-        final ContextParameters contextParameters = (ContextParameters) deserializeParameters(CONTEXT_PARAMETERS,
+        final var contextParameters = (ContextParameters) deserializeParameters(CONTEXT_PARAMETERS,
                         contextParametersElement, context);
 
         // We know this will work because if the context parameters was not a Json object, the
         // previous deserializeParameters() call would not have worked
-        final JsonObject contextParametersObject = engineParametersJsonObject.get(CONTEXT_PARAMETERS).getAsJsonObject();
+        final var contextParametersObject = engineParametersJsonObject.get(CONTEXT_PARAMETERS).getAsJsonObject();
 
         // Now get the distributor, lock manager, and persistence parameters
         final JsonElement distributorParametersElement = contextParametersObject.get(DISTRIBUTOR_PARAMETERS);
@@ -170,11 +205,11 @@ public class EngineServiceParametersJsonAdapter
         }
 
         // Deserialize the executor parameters
-        final JsonObject executorParametersJsonObject = engineParametersJsonObject.get(EXECUTOR_PARAMETERS)
+        final var executorParametersJsonObject = engineParametersJsonObject.get(EXECUTOR_PARAMETERS)
                         .getAsJsonObject();
 
         for (final Entry<String, JsonElement> executorEntries : executorParametersJsonObject.entrySet()) {
-            final ExecutorParameters executorParameters = (ExecutorParameters) deserializeParameters(
+            final var executorParameters = (ExecutorParameters) deserializeParameters(
                             EXECUTOR_PARAMETERS + ':' + executorEntries.getKey(), executorEntries.getValue(), context);
             engineParameters.getExecutorParameterMap().put(executorEntries.getKey(), executorParameters);
         }
@@ -201,7 +236,7 @@ public class EngineServiceParametersJsonAdapter
         }
 
         // Deserialize the executor parameters
-        final JsonObject schemaHelperParametersJsonObject = contextParametersJsonObject.get(SCHEMA_PARAMETERS)
+        final var schemaHelperParametersJsonObject = contextParametersJsonObject.get(SCHEMA_PARAMETERS)
                         .getAsJsonObject();
 
         for (final Entry<String, JsonElement> schemaHelperEntries : schemaHelperParametersJsonObject.entrySet()) {
@@ -252,7 +287,7 @@ public class EngineServiceParametersJsonAdapter
         }
 
         // Check the parameter has a value
-        final String parameterClassName = parameterClassNameElement.getAsString();
+        final var parameterClassName = parameterClassNameElement.getAsString();
         if (parameterClassName == null || parameterClassName.trim().length() == 0) {
             final String returnMessage = "value for field \"" + PARAMETER_CLASS_NAME + "\" in \"" + parametersLabel
                             + "\" entry is not specified or is blank";
@@ -266,8 +301,8 @@ public class EngineServiceParametersJsonAdapter
             parameters = context.deserialize(parametersObject, Class.forName(parameterClassName));
         } catch (JsonParseException | ClassNotFoundException e) {
             final String returnMessage = "failed to deserialize the parameters for \"" + parametersLabel + "\" "
-                            + "to parameter class \"" + parameterClassName + "\"\n" + e.getClass().getCanonicalName()
-                            + ": " + e.getMessage();
+                            + "to parameter class \"" + parameterClassName + "\"\n" + e.getClass().getName() + ": "
+                            + e.getMessage();
             LOGGER.error(returnMessage, e);
             throw new ParameterRuntimeException(returnMessage, e);
         }