From 926646d8e5e86e680a119360f93d7bdb46c89435 Mon Sep 17 00:00:00 2001 From: "a.sreekumar" Date: Tue, 22 Jun 2021 15:55:46 +0100 Subject: Changes to support multiple outputs from a state This review addresses two main changes: 1) inputFields and outputFields are not tied to task definition anymore. Instead inputEvent and outputEvents associated to a task is populated as part of the policy state definition, as the state definition have the information anyway. - Clean up of the usage of inputFields and outputFields in task definition will happen in a future review - inputFields and outputFields defined in task definition in policies until honolulu will not make the policy invalid as the changes are done in backward compatible way. 2) Multiple output events can come out of a final state now. - Define another policy state output with the relevant eventName in the command file - In the task logic, create a map to store the fields of the relevant outputEvent, and then just call "executor.addFieldsToOutput()" These 2 steps are enough to send multiple events to relevant components as per the apex configuration. Change-Id: Id88ca402704106404f529e595e1a76f6bf167876 Issue-ID: POLICY-3336 Signed-off-by: a.sreekumar --- .../apex/model/modelapi/impl/ApexModelImpl.java | 16 ++++++ .../apex/model/modelapi/impl/PolicyFacade.java | 59 +++++++++++++++++++--- 2 files changed, 69 insertions(+), 6 deletions(-) (limited to 'model/model-api/src/main/java/org') diff --git a/model/model-api/src/main/java/org/onap/policy/apex/model/modelapi/impl/ApexModelImpl.java b/model/model-api/src/main/java/org/onap/policy/apex/model/modelapi/impl/ApexModelImpl.java index e66322801..5e8d1a5c7 100644 --- a/model/model-api/src/main/java/org/onap/policy/apex/model/modelapi/impl/ApexModelImpl.java +++ b/model/model-api/src/main/java/org/onap/policy/apex/model/modelapi/impl/ApexModelImpl.java @@ -3,6 +3,7 @@ * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019 Samsung Electronics Co., Ltd. * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2021 Bell Canada. 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. @@ -27,6 +28,8 @@ import org.onap.policy.apex.model.basicmodel.dao.DaoParameters; import org.onap.policy.apex.model.modelapi.ApexApiResult; import org.onap.policy.apex.model.modelapi.ApexModel; import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This class is an implementation of a facade on an Apex model for editors of Apex models. @@ -34,6 +37,13 @@ import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; * @author Liam Fallon (liam.fallon@ericsson.com) */ public final class ApexModelImpl implements ApexModel { + + private static final String FIELDS_DEPRECATED_WARN_MSG = + "inputFields and outputFields are deprecated from Task definition and will be removed. " + + "Instead, inputEvent and outputEvents are automatically populated to Tasks based on State definition"; + + private static final Logger LOGGER = LoggerFactory.getLogger(ApexModelImpl.class); + // The policy model being acted upon private AxPolicyModel policyModel = new AxPolicyModel(); @@ -436,6 +446,7 @@ public final class ApexModelImpl implements ApexModel { @Override public ApexApiResult createTaskInputField(final String name, final String version, final String fieldName, final String dataTypeName, final String dataTypeVersion, final boolean optional) { + LOGGER.warn(FIELDS_DEPRECATED_WARN_MSG); return taskFacade.createTaskInputField(name, version, fieldName, dataTypeName, dataTypeVersion, optional); } @@ -444,6 +455,7 @@ public final class ApexModelImpl implements ApexModel { */ @Override public ApexApiResult listTaskInputField(final String name, final String version, final String fieldName) { + LOGGER.warn(FIELDS_DEPRECATED_WARN_MSG); return taskFacade.listTaskInputField(name, version, fieldName); } @@ -452,6 +464,7 @@ public final class ApexModelImpl implements ApexModel { */ @Override public ApexApiResult deleteTaskInputField(final String name, final String version, final String fieldName) { + LOGGER.warn(FIELDS_DEPRECATED_WARN_MSG); return taskFacade.deleteTaskInputField(name, version, fieldName); } @@ -461,6 +474,7 @@ public final class ApexModelImpl implements ApexModel { @Override public ApexApiResult createTaskOutputField(final String name, final String version, final String fieldName, final String dataTypeName, final String dataTypeVersion, final boolean optional) { + LOGGER.warn(FIELDS_DEPRECATED_WARN_MSG); return taskFacade.createTaskOutputField(name, version, fieldName, dataTypeName, dataTypeVersion, optional); } @@ -469,6 +483,7 @@ public final class ApexModelImpl implements ApexModel { */ @Override public ApexApiResult listTaskOutputField(final String name, final String version, final String fieldName) { + LOGGER.warn(FIELDS_DEPRECATED_WARN_MSG); return taskFacade.listTaskOutputField(name, version, fieldName); } @@ -477,6 +492,7 @@ public final class ApexModelImpl implements ApexModel { */ @Override public ApexApiResult deleteTaskOutputField(final String name, final String version, final String fieldName) { + LOGGER.warn(FIELDS_DEPRECATED_WARN_MSG); return taskFacade.deleteTaskOutputField(name, version, fieldName); } diff --git a/model/model-api/src/main/java/org/onap/policy/apex/model/modelapi/impl/PolicyFacade.java b/model/model-api/src/main/java/org/onap/policy/apex/model/modelapi/impl/PolicyFacade.java index 0c0a51691..9480702ac 100644 --- a/model/model-api/src/main/java/org/onap/policy/apex/model/modelapi/impl/PolicyFacade.java +++ b/model/model-api/src/main/java/org/onap/policy/apex/model/modelapi/impl/PolicyFacade.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2021 Bell Canada. 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. @@ -25,6 +26,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import java.util.Set; +import java.util.TreeMap; import java.util.TreeSet; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.onap.policy.apex.model.basicmodel.concepts.AxKey; @@ -655,9 +657,11 @@ public class PolicyFacade { } final AxReferenceKey refKey = new AxReferenceKey(state.getKey(), outputName); - if (state.getStateOutputs().containsKey(refKey.getLocalName())) { + // There can be multipe state outputs only when the current state is the final state + if (nextState != null && !AxReferenceKey.getNullKey().getLocalName().equals(nextState) + && state.getStateOutputs().containsKey(refKey.getLocalName())) { return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS, - "Output concept " + refKey.getId() + ALREADY_EXISTS); + "Output concept " + refKey.getId() + ALREADY_EXISTS); } final AxEvent event = apexModel.getPolicyModel().getEvents().get(eventName, eventVersion); @@ -680,13 +684,35 @@ public class PolicyFacade { } } - state.getStateOutputs().put(refKey.getLocalName(), new AxStateOutput(refKey, event.getKey(), nextStateKey)); + populateStateOuputInfo(nextState, state, refKey, event, nextStateKey); return new ApexApiResult(); } catch (final Exception e) { return new ApexApiResult(ApexApiResult.Result.FAILED, e); } } + private void populateStateOuputInfo(final String nextState, final AxState state, final AxReferenceKey refKey, + final AxEvent event, AxReferenceKey nextStateKey) { + // nextState is null. There could be multiple events coming out of the state + if ((nextState == null || AxReferenceKey.getNullKey().getLocalName().equals(nextState)) + && state.getStateOutputs().containsKey(refKey.getLocalName())) { + AxStateOutput existingStateOutput = state.getStateOutputs().get(refKey.getLocalName()); + if (null == existingStateOutput.getOutgoingEventSet() + || existingStateOutput.getOutgoingEventSet().isEmpty()) { + Set eventSet = new TreeSet<>(); + eventSet.add(existingStateOutput.getOutgoingEvent()); + existingStateOutput.setOutgoingEventSet(eventSet); + } + existingStateOutput.getOutgoingEventSet().add(event.getKey()); + } else { + AxStateOutput axStateOutput = new AxStateOutput(refKey, event.getKey(), nextStateKey); + Set eventSet = new TreeSet<>(); + eventSet.add(axStateOutput.getOutgoingEvent()); + axStateOutput.setOutgoingEventSet(eventSet); + state.getStateOutputs().put(refKey.getLocalName(), axStateOutput); + } + } + /** * List policy state outputs. * @@ -1086,15 +1112,36 @@ public class PolicyFacade { ApexApiResult.Result.FAILED, "output type " + builder.getOutputType() + " invalid"); } - state - .getTaskReferences() - .put(task.getKey(), new AxStateTaskReference(refKey, stateTaskOutputType, outputRefKey)); + // add input and output events to the task based on state definition + if (state.getStateOutputs().containsKey(outputRefKey.getLocalName())) { + populateIoEventsToTask(state, task, outputRefKey); + } + state.getTaskReferences().put(task.getKey(), + new AxStateTaskReference(refKey, stateTaskOutputType, outputRefKey)); return new ApexApiResult(); } catch (final Exception e) { return new ApexApiResult(ApexApiResult.Result.FAILED, e); } } + private void populateIoEventsToTask(final AxState state, final AxTask task, final AxReferenceKey outputRefKey) { + AxEvent triggerEvent = apexModel.getPolicyModel().getEvents().get(state.getTrigger()); + task.setInputEvent(triggerEvent); + Map outputEvents = new TreeMap<>(); + if (state.getNextStateSet().isEmpty() + || state.getNextStateSet().contains(AxReferenceKey.getNullKey().getLocalName())) { + state.getStateOutputs().get(outputRefKey.getLocalName()).getOutgoingEventSet() + .forEach(outgoingEventKey -> outputEvents.put(outgoingEventKey.getName(), + apexModel.getPolicyModel().getEvents().get(outgoingEventKey))); + } else { + AxArtifactKey outgoingEventKey = + state.getStateOutputs().get(outputRefKey.getLocalName()).getOutgoingEvent(); + outputEvents.put(outgoingEventKey.getName(), + apexModel.getPolicyModel().getEvents().get(outgoingEventKey)); + } + task.setOutputEvents(outputEvents); + } + /** * List policy state task references. * -- cgit 1.2.3-korg