Containerization feature of SO
[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 public class DelegateExecutionImpl implements BuildingBlockExecution, Serializable {
35
36         private final Map<String, Serializable> seedMap;
37         private transient DelegateExecution execution;
38         private static final String INVALID_INPUT_MISSING = "Expected variable of \"%s\" not found in execution";
39         private static final String INVALID_INPUT_CLASS_CAST = "Expected variable of \"%s\" was the wrong object type in the execution";
40
41         private static final String MISSING_MSG = "Execution variable \"gBBInput\" cannot be null when executing building blocks";
42         private static final String MALFORMED_MSG = "Execution variable \"gBBInput\" must contain an element of type GeneralBuildingBlock";
43         
44         public DelegateExecutionImpl(Map<String, Serializable> seedMap) {
45                 this.seedMap = seedMap;
46         }
47         
48         public DelegateExecutionImpl(DelegateExecution execution) {
49                 this.seedMap = new HashMap<>();
50                 execution.getVariables().forEach((key, value) -> {
51                         if (value instanceof Serializable) {
52                                 seedMap.put(key, (Serializable)value);
53                         }
54                 });
55                 /* must occur for junit tests to work */
56                 this.execution = execution;
57         }
58         @Override
59         public GeneralBuildingBlock getGeneralBuildingBlock() {
60                 try {
61                         GeneralBuildingBlock generalBuildingBlock =  (GeneralBuildingBlock) execution.getVariable("gBBInput");
62                         
63                         if (generalBuildingBlock == null) {
64                                 throw new MissingBuildingBlockInputException(MISSING_MSG);
65                         }
66                         
67                         return generalBuildingBlock;
68                 } catch (ClassCastException e) {
69                         throw new MalformedBuildingBlockInputException(MALFORMED_MSG, e);
70                 }
71         }
72
73         @Override
74         public <T> T getVariable(String key) {
75                 return this.get(key);
76         }
77
78         @Override
79         public <T> T getRequiredVariable(String key) throws RequiredExecutionVariableExeception {
80                 final T result;
81         
82                 result = this.get(key);
83                 if (result == null) {
84                         throw new RequiredExecutionVariableExeception(String.format(INVALID_INPUT_MISSING, key));
85
86                 }
87                 return result;
88         }
89
90         @Override
91         public void setVariable(String key, Serializable value) {
92                 this.execution.setVariable(key, value);
93         }
94         
95         @Override
96         public Map<ResourceKey, String> getLookupMap() {
97                 return this.get("lookupKeyMap");
98         }
99         
100         public DelegateExecution getDelegateExecution() {
101                 return this.execution;
102         }
103         
104         public void setDelegateExecution(DelegateExecution execution) {
105                 this.execution = execution;
106                 this.seedMap.forEach((key, value) -> {
107                         if (!execution.hasVariable(key)) {
108                                 execution.setVariable(key, value);
109                         }
110                 });
111         }
112         
113         protected <T> T get(String key) {
114                 final Object value = this.execution.getVariable(key);
115         
116                 return (T)value;
117         }
118 }