Changes for checkstyle 8.32
[policy/apex-pdp.git] / core / core-engine / src / main / java / org / onap / policy / apex / core / engine / executor / StateOutput.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.core.engine.executor;
23
24 import java.util.Map;
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;
35
36 /**
37  * This class is the output of a state, and is used by the engine to decide what the next state for execution is.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
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;
46
47     /**
48      * Create a new state output from a state output definition.
49      *
50      * @param axStateOutput the state output definition
51      */
52     public StateOutput(final AxStateOutput axStateOutput) {
53         this(axStateOutput, new EnEvent(axStateOutput.getOutgingEvent()));
54     }
55
56     /**
57      * Create a new state output with the given definition and event key.
58      *
59      * @param stateOutputDefinition the state output definition
60      * @param outputEvent the output event
61      */
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");
65
66         this.stateOutputDefinition = stateOutputDefinition;
67         this.outputEvent = outputEvent;
68         outputEventDef = ModelService.getModel(AxEvents.class).get(stateOutputDefinition.getOutgingEvent());
69     }
70
71     /**
72      * Gets the next state.
73      *
74      * @return the next state
75      */
76     public AxReferenceKey getNextState() {
77         return stateOutputDefinition.getNextState();
78     }
79
80     /**
81      * Gets the state output definition.
82      *
83      * @return the state output definition
84      */
85     public AxStateOutput getStateOutputDefinition() {
86         return stateOutputDefinition;
87     }
88
89     /**
90      * Gets the output event.
91      *
92      * @return the output event
93      */
94     public EnEvent getOutputEvent() {
95         return outputEvent;
96     }
97
98     /**
99      * Transfer the fields from the incoming field map into the event.
100      *
101      * @param incomingFieldDefinitionMap definitions of the incoming fields
102      * @param eventFieldMap the event field map
103      * @throws StateMachineException on errors populating the event fields
104      */
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");
109
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());
114         }
115         for (final Entry<String, Object> incomingFieldEntry : eventFieldMap.entrySet()) {
116             final String fieldName = incomingFieldEntry.getKey();
117             final AxField fieldDef = incomingFieldDefinitionMap.get(fieldName);
118
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() + "\"");
123             }
124
125             // Set the value in the output event
126             outputEvent.put(fieldName, incomingFieldEntry.getValue());
127         }
128     }
129
130     /**
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.
133      *
134      * @param incomingEvent The incoming event to copy from
135      */
136     public void copyUnsetFields(final EnEvent incomingEvent) {
137         Assertions.argumentNotNull(incomingEvent, "incomingEvent may not be null");
138
139         for (final Entry<String, Object> incomingField : incomingEvent.entrySet()) {
140             final String fieldName = incomingField.getKey();
141
142             // Check if the field exists on the outgoing event
143             if ((!outputEventDef.getParameterMap().containsKey(fieldName))
144
145                             // Check if the field is set on the outgoing event
146                             || (outputEvent.containsKey(fieldName))
147
148                             // Now, check the fields have the same type
149                             || (!incomingEvent.getAxEvent().getParameterMap().get(fieldName)
150                                             .equals(outputEvent.getAxEvent().getParameterMap().get(fieldName)))) {
151                 continue;
152             }
153
154             // All checks done, we can copy the value
155             outputEvent.put(fieldName, incomingField.getValue());
156         }
157
158     }
159 }