2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.core.engine.executor;
25 import java.util.Map.Entry;
26 import org.onap.policy.apex.core.engine.event.EnEvent;
27 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
29 import org.onap.policy.apex.model.basicmodel.service.ModelService;
30 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
31 import org.onap.policy.apex.model.eventmodel.concepts.AxEvents;
32 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
33 import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput;
34 import org.onap.policy.common.utils.validation.Assertions;
37 * This class is the output of a state, and is used by the engine to decide what the next state for execution is.
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public class StateOutput {
42 // The state output has a state and an event
43 private final AxStateOutput stateOutputDefinition;
44 private final AxEvent outputEventDef;
45 private final EnEvent outputEvent;
48 * Create a new state output from a state output definition.
50 * @param axStateOutput the state output definition
52 public StateOutput(final AxStateOutput axStateOutput) {
53 this(axStateOutput, new EnEvent(axStateOutput.getOutgingEvent()));
57 * Create a new state output with the given definition and event key.
59 * @param stateOutputDefinition the state output definition
60 * @param outputEvent the output event
62 public StateOutput(final AxStateOutput stateOutputDefinition, final EnEvent outputEvent) {
63 Assertions.argumentNotNull(stateOutputDefinition, "stateOutputDefinition may not be null");
64 Assertions.argumentNotNull(outputEvent, "outputEvent may not be null");
66 this.stateOutputDefinition = stateOutputDefinition;
67 this.outputEvent = outputEvent;
68 outputEventDef = ModelService.getModel(AxEvents.class).get(stateOutputDefinition.getOutgingEvent());
72 * Gets the next state.
74 * @return the next state
76 public AxReferenceKey getNextState() {
77 return stateOutputDefinition.getNextState();
81 * Gets the state output definition.
83 * @return the state output definition
85 public AxStateOutput getStateOutputDefinition() {
86 return stateOutputDefinition;
90 * Gets the output event.
92 * @return the output event
94 public EnEvent getOutputEvent() {
99 * Transfer the fields from the incoming field map into the event.
101 * @param incomingFieldDefinitionMap definitions of the incoming fields
102 * @param eventFieldMap the event field map
103 * @throws StateMachineException on errors populating the event fields
105 public void setEventFields(final Map<String, AxField> incomingFieldDefinitionMap,
106 final Map<String, Object> eventFieldMap) throws StateMachineException {
107 Assertions.argumentNotNull(incomingFieldDefinitionMap, "incomingFieldDefinitionMap may not be null");
108 Assertions.argumentNotNull(eventFieldMap, "eventFieldMap may not be null");
110 if (!incomingFieldDefinitionMap.keySet().equals(eventFieldMap.keySet())) {
111 throw new StateMachineException(
112 "field definitions and values do not match for event " + outputEventDef.getId() + '\n'
113 + incomingFieldDefinitionMap.keySet() + '\n' + eventFieldMap.keySet());
115 for (final Entry<String, Object> incomingFieldEntry : eventFieldMap.entrySet()) {
116 final String fieldName = incomingFieldEntry.getKey();
117 final AxField fieldDef = incomingFieldDefinitionMap.get(fieldName);
119 // Check if this field is a field in the event
120 if (!outputEventDef.getFields().contains(fieldDef)) {
121 throw new StateMachineException("field \"" + fieldName + "\" does not exist on event \""
122 + outputEventDef.getId() + "\"");
125 // Set the value in the output event
126 outputEvent.put(fieldName, incomingFieldEntry.getValue());
131 * This method copies any fields that exist on the input event that also exist on the output event if they are not
132 * set on the output event.
134 * @param incomingEvent The incoming event to copy from
136 public void copyUnsetFields(final EnEvent incomingEvent) {
137 Assertions.argumentNotNull(incomingEvent, "incomingEvent may not be null");
139 for (final Entry<String, Object> incomingField : incomingEvent.entrySet()) {
140 final String fieldName = incomingField.getKey();
142 // Check if the field exists on the outgoing event
143 if ((!outputEventDef.getParameterMap().containsKey(fieldName))
145 // Check if the field is set on the outgoing event
146 || (outputEvent.containsKey(fieldName))
148 // Now, check the fields have the same type
149 || (!incomingEvent.getAxEvent().getParameterMap().get(fieldName)
150 .equals(outputEvent.getAxEvent().getParameterMap().get(fieldName)))) {
154 // All checks done, we can copy the value
155 outputEvent.put(fieldName, incomingField.getValue());