39a209efa88331cc1905568411007930d5bf6cce
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / bpmn / common / DelegateExecutionImpl.java
1 /*-
2 * ============LICENSE_START=======================================================
3 * ONAP - SO
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10
11  *      http://www.apache.org/licenses/LICENSE-2.0
12
13  * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20
21 package org.onap.so.bpmn.common;
22
23 import java.io.Serializable;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.camunda.bpm.engine.delegate.DelegateExecution;
28 import org.onap.so.bpmn.common.exceptions.MalformedBuildingBlockInputException;
29 import org.onap.so.bpmn.common.exceptions.MissingBuildingBlockInputException;
30 import org.onap.so.bpmn.common.exceptions.RequiredExecutionVariableExeception;
31 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
32 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
33
34 import com.fasterxml.jackson.annotation.JsonIgnore;
35 import com.fasterxml.jackson.annotation.JsonProperty;
36
37 public class DelegateExecutionImpl implements BuildingBlockExecution, Serializable {
38
39     private static final long serialVersionUID = 5559067662634919395L;
40
41     @JsonProperty
42     private final Map<String, Serializable> seedMap;
43
44     private transient DelegateExecution execution;
45     private static final String INVALID_INPUT_MISSING = "Expected variable of \"%s\" not found in execution";
46
47     private static final String MISSING_MSG =
48             "Execution variable \"gBBInput\" cannot be null when executing building blocks";
49     private static final String MALFORMED_MSG =
50             "Execution variable \"gBBInput\" must contain an element of type GeneralBuildingBlock";
51
52     public DelegateExecutionImpl(final Map<String, Serializable> seedMap) {
53         this.seedMap = seedMap;
54     }
55
56     public DelegateExecutionImpl(final DelegateExecution execution) {
57         this.seedMap = new HashMap<>();
58         execution.getVariables().forEach((key, value) -> {
59             if (value instanceof Serializable) {
60                 seedMap.put(key, (Serializable) value);
61             }
62         });
63         /* must occur for junit tests to work */
64         this.execution = execution;
65     }
66
67     @JsonIgnore
68     @Override
69     public GeneralBuildingBlock getGeneralBuildingBlock() {
70         try {
71             final GeneralBuildingBlock generalBuildingBlock = (GeneralBuildingBlock) execution.getVariable("gBBInput");
72
73             if (generalBuildingBlock == null) {
74                 throw new MissingBuildingBlockInputException(MISSING_MSG);
75             }
76
77             return generalBuildingBlock;
78         } catch (final ClassCastException e) {
79             throw new MalformedBuildingBlockInputException(MALFORMED_MSG, e);
80         }
81     }
82
83     @Override
84     public <T> T getVariable(final String key) {
85         return this.get(key);
86     }
87
88     @Override
89     public <T> T getRequiredVariable(final String key) throws RequiredExecutionVariableExeception {
90         final T result;
91
92         result = this.get(key);
93         if (result == null) {
94             throw new RequiredExecutionVariableExeception(String.format(INVALID_INPUT_MISSING, key));
95
96         }
97         return result;
98     }
99
100     @Override
101     public void setVariable(final String key, final Serializable value) {
102         this.execution.setVariable(key, value);
103     }
104
105     @JsonIgnore
106     @Override
107     public Map<ResourceKey, String> getLookupMap() {
108         return this.get("lookupKeyMap");
109     }
110
111     @JsonIgnore
112     @Override
113     public String getFlowToBeCalled() {
114         return this.get("flowToBeCalled");
115     }
116
117     @JsonIgnore
118     public DelegateExecution getDelegateExecution() {
119         return this.execution;
120     }
121
122     public void setDelegateExecution(final DelegateExecution execution) {
123         this.execution = execution;
124         this.seedMap.forEach((key, value) -> {
125             if (!execution.hasVariable(key)) {
126                 execution.setVariable(key, value);
127             }
128         });
129     }
130
131     @SuppressWarnings("unchecked")
132     protected <T> T get(final String key) {
133         final Object value = this.execution.getVariable(key);
134         return (T) value;
135     }
136
137 }