diff options
author | a.sreekumar <ajith.sreekumar@est.tech> | 2020-02-13 11:17:54 +0000 |
---|---|---|
committer | a.sreekumar <ajith.sreekumar@est.tech> | 2020-02-20 17:03:05 +0000 |
commit | 8f8df7ee1ddb20b03cedb17dccdc79fc291cc678 (patch) | |
tree | 6ed8b1ebb110992c5e7985100478ab5b216d185d /core/core-engine/src/main/java | |
parent | 8d22852d50a9b76dd05f68fef2717bb37f3b5bc6 (diff) |
Passing taskParameters from ApexConfig to policy logic
TaskParameters can be used to pass parameters from ApexConfig to the policy logic. In the config, these are optional.
Usage as below:
{
"engineParameters": {
"taskParameters": [
{
"key": "ParameterKey1",
"value": "ParameterValue1"
},
{
"taskId": "TaskIdVal",
"key": "ParameterKey2",
"value": "ParameterValue2"
}
]
}
}
In the taskLogic, taskParameters can be accessed as below:
eg: executor.parameters.get("ParameterKey1"))
If taskId is provided in ApexConfig for an entry, then that
parameter is updated only for that particular task. Otherwise, the task
parameter is added to all tasks.
Change-Id: I9e1b3d3697428309e7d86db40b63ffe822935b69
Issue-ID: POLICY-2364
Signed-off-by: a.sreekumar <ajith.sreekumar@est.tech>
Diffstat (limited to 'core/core-engine/src/main/java')
5 files changed, 143 insertions, 65 deletions
diff --git a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/EngineParameters.java b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/EngineParameters.java index d76d8e589..3ca74153e 100644 --- a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/EngineParameters.java +++ b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/EngineParameters.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,13 +21,17 @@ package org.onap.policy.apex.core.engine; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; - +import lombok.Getter; +import lombok.Setter; import org.onap.policy.apex.context.parameters.ContextParameters; import org.onap.policy.common.parameters.GroupValidationResult; import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.ValidationResult; /** * This class holds the parameters for a single Apex engine. This parameter class holds parameters for context schemas @@ -44,6 +49,8 @@ import org.onap.policy.common.parameters.ParameterGroup; * * @author Liam Fallon (liam.fallon@ericsson.com) */ +@Getter +@Setter public class EngineParameters implements ParameterGroup { private ContextParameters contextParameters = new ContextParameters(); @@ -53,6 +60,9 @@ public class EngineParameters implements ParameterGroup { // A map of parameters for executors of various logic types private Map<String, ExecutorParameters> executorParameterMap = new TreeMap<>(); + // A list of parameters to be passed to the task, so that they can be used in the logic + private List<TaskParameters> taskParameters = new ArrayList<>(); + /** * Constructor to create an engine parameters instance and register the instance with the parameter service. */ @@ -63,52 +73,6 @@ public class EngineParameters implements ParameterGroup { this.name = EngineParameterConstants.MAIN_GROUP_NAME; } - /** - * Gets the parameters for context schema and album handling. - * - * @return the parameters for context schema and album handling - */ - public ContextParameters getContextParameters() { - return contextParameters; - } - - /** - * Sets the parameters for context schema and album handling. - * - * @param contextParameters the parameters for context schema and album handling - */ - public void setContextParameters(final ContextParameters contextParameters) { - this.contextParameters = contextParameters; - } - - /** - * Gets the executor parameter map of the engine. - * - * @return the executor parameter map of the engine - */ - public Map<String, ExecutorParameters> getExecutorParameterMap() { - return executorParameterMap; - } - - /** - * Sets the executor parameter map of the engine. - * - * @param executorParameterMap the executor parameter map of the engine - */ - public void setExecutorParameterMap(final Map<String, ExecutorParameters> executorParameterMap) { - this.executorParameterMap = executorParameterMap; - } - - @Override - public String getName() { - return name; - } - - @Override - public void setName(final String name) { - this.name = name; - } - @Override public GroupValidationResult validate() { final GroupValidationResult result = new GroupValidationResult(this); @@ -118,6 +82,13 @@ public class EngineParameters implements ParameterGroup { for (Entry<String, ExecutorParameters> executorParEntry : executorParameterMap.entrySet()) { result.setResult("executorParameterMap", executorParEntry.getKey(), executorParEntry.getValue().validate()); } + for (TaskParameters taskParam : taskParameters) { + ValidationResult taskParamValidationResult = taskParam.validate("taskParameters"); + result.setResult(taskParamValidationResult.getName(), taskParamValidationResult.getStatus(), + taskParamValidationResult.getResult()); + } return result; } + + } diff --git a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/TaskParameters.java b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/TaskParameters.java new file mode 100644 index 000000000..8a0f6db9c --- /dev/null +++ b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/TaskParameters.java @@ -0,0 +1,71 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. + * ================================================================================ + * 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========================================================= + */ + +package org.onap.policy.apex.core.engine; + +import lombok.Getter; +import lombok.Setter; +import org.onap.policy.common.parameters.BeanValidator; +import org.onap.policy.common.parameters.ValidationResult; +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; + +/** + * This class provides the configurable parameters for Apex Tasks. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +@Getter +@Setter +public class TaskParameters { + private String name; + + // If taskId is not specified, then the taskParameter is added to all tasks in the engine. + private String taskId; + + @NotNull + @NotBlank + private String key; + @NotNull + @NotBlank + private String value; + + public TaskParameters() { + this.name = "taskParameters"; + } + + public TaskParameters(String key, String value, String taskId) { + this(); + this.key = key; + this.value = value; + this.taskId = taskId; + } + + /** + * Validates the parameters. + * + * @param resultName name of the result + * + * @return the validation result + */ + public ValidationResult validate(String resultName) { + return new BeanValidator().validateTop(resultName, this); + } +} diff --git a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/TaskExecutor.java b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/TaskExecutor.java index 50abeea6d..0150b65b2 100644 --- a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/TaskExecutor.java +++ b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/TaskExecutor.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,24 +23,28 @@ package org.onap.policy.apex.core.engine.executor; import static org.onap.policy.common.utils.validation.Assertions.argumentOfClassNotNull; +import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; - +import lombok.Getter; import lombok.NonNull; - import org.onap.policy.apex.context.ContextException; import org.onap.policy.apex.core.engine.ExecutorParameters; +import org.onap.policy.apex.core.engine.TaskParameters; import org.onap.policy.apex.core.engine.context.ApexInternalContext; import org.onap.policy.apex.core.engine.executor.context.TaskExecutionContext; import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; import org.onap.policy.apex.model.eventmodel.concepts.AxInputField; import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField; import org.onap.policy.apex.model.policymodel.concepts.AxTask; +import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter; import org.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; @@ -71,18 +75,10 @@ public abstract class TaskExecutor // The task execution context; contains the facades for events and context to be used by tasks // executed by this task // executor + @Getter private TaskExecutionContext executionContext = null; /** - * Gets the execution internalContext. - * - * @return the execution context - */ - protected TaskExecutionContext getExecutionContext() { - return executionContext; - } - - /** * {@inheritDoc}. */ @Override @@ -239,6 +235,28 @@ public abstract class TaskExecutor } /** + * If taskParameters are provided in ApexConfig, then they will be updated in the Tasks. + * If taskId is empty, the task parameter is added/updated to all available tasks + * Otherwise, the task parameter is added/updated to the corresponding task only. + * + * @param taskParametersFromConfig the list of task parameters provided in ApexConfig during deployment + */ + public void updateTaskParameters(List<TaskParameters> taskParametersFromConfig) { + Map<String, AxTaskParameter> taskParameters = getSubject().getTaskParameters(); + if (null == taskParameters) { + taskParameters = new HashMap<>(); + } + for (TaskParameters taskParameterFromConfig : taskParametersFromConfig) { + if (null == taskParameterFromConfig.getTaskId() + || getSubject().getId().equals(taskParameterFromConfig.getTaskId())) { + taskParameters.put(taskParameterFromConfig.getKey(), + new AxTaskParameter(new AxReferenceKey(), taskParameterFromConfig.getValue())); + } + } + getSubject().setTaskParameters(taskParameters); + } + + /** * {@inheritDoc}. */ @Override diff --git a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContext.java b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContext.java index b322cf402..6c670b9d8 100644 --- a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContext.java +++ b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/context/TaskExecutionContext.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,14 +23,13 @@ package org.onap.policy.apex.core.engine.executor.context; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.TreeMap; - import lombok.Getter; import lombok.Setter; - import org.onap.policy.apex.context.ContextAlbum; import org.onap.policy.apex.context.ContextRuntimeException; import org.onap.policy.apex.core.engine.context.ApexInternalContext; @@ -38,6 +38,7 @@ import org.onap.policy.apex.core.engine.executor.TaskExecutor; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; import org.onap.policy.apex.model.policymodel.concepts.AxTask; +import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter; import org.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; @@ -103,6 +104,10 @@ public class TaskExecutionContext { @Getter private Properties executionProperties; + // Parameters associated to a task + @Getter + private Map<String, String> parameters = new HashMap<>(); + /** * Instantiates a new task execution context. * @@ -120,6 +125,9 @@ public class TaskExecutionContext { // The subject is the task definition subject = new AxTaskFacade(axTask); + // Populate parameters to be accessed in the task logic from the task parameters. + populateParameters(axTask.getTaskParameters()); + // Execution ID is the current policy execution instance this.executionId = executionId; this.executionProperties = executionProperties; @@ -151,6 +159,16 @@ public class TaskExecutionContext { } /** + * Populate parameters to be accessed in the task logic. + * + * @param taskParameters The task parameters + */ + private void populateParameters(Map<String, AxTaskParameter> taskParameters) { + taskParameters.entrySet().forEach(taskParamEntry -> parameters.put(taskParamEntry.getKey(), + taskParamEntry.getValue().getTaskParameterValue())); + } + + /** * Return a context album if it exists in the context definition of this task. * * @param contextAlbumName The context album name diff --git a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/impl/ExecutorFactoryImpl.java b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/impl/ExecutorFactoryImpl.java index ca80db938..231f6bc39 100644 --- a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/impl/ExecutorFactoryImpl.java +++ b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/impl/ExecutorFactoryImpl.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,7 +24,6 @@ package org.onap.policy.apex.core.engine.executor.impl; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; - import org.onap.policy.apex.core.engine.EngineParameterConstants; import org.onap.policy.apex.core.engine.EngineParameters; import org.onap.policy.apex.core.engine.ExecutorParameters; @@ -54,6 +53,7 @@ public class ExecutorFactoryImpl implements ExecutorFactory { // Get a reference to the logger private static final XLogger LOGGER = XLoggerFactory.getXLogger(ExecutorFactoryImpl.class); + private final EngineParameters engineParameters; // A map of logic flavours mapped to executor classes for plugins to executors for those logic flavours private Map<String, Class<Executor<?, ?, ?, ?>>> taskExecutorPluginClassMap = new TreeMap<>(); private Map<String, Class<Executor<?, ?, ?, ?>>> taskSelectionExecutorPluginClassMap = new TreeMap<>(); @@ -68,7 +68,7 @@ public class ExecutorFactoryImpl implements ExecutorFactory { * @throws StateMachineException on plugin creation errors */ public ExecutorFactoryImpl() throws StateMachineException { - final EngineParameters engineParameters = ParameterService.get(EngineParameterConstants.MAIN_GROUP_NAME); + engineParameters = ParameterService.get(EngineParameterConstants.MAIN_GROUP_NAME); Assertions.argumentOfClassNotNull(engineParameters, StateMachineException.class, "Parameter \"engineParameters\" may not be null"); @@ -121,7 +121,7 @@ public class ExecutorFactoryImpl implements ExecutorFactory { taskExecutorPluginClassMap.get(task.getTaskLogic().getLogicFlavour()), TaskExecutor.class); taskExecutor.setParameters(implementationParameterMap.get(task.getTaskLogic().getLogicFlavour())); taskExecutor.setContext(parentExecutor, task, context); - + taskExecutor.updateTaskParameters(engineParameters.getTaskParameters()); return taskExecutor; } |