/*-
* ============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=========================================================
*/
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;
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;
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);
}
@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(
// 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.
*
}
// 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);
}
// 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);
}
}
// 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()) {
}
// 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";
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);
}