2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.common;
23 import java.io.Serializable;
24 import java.util.HashMap;
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;
34 public class DelegateExecutionImpl implements BuildingBlockExecution, Serializable {
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";
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";
44 public DelegateExecutionImpl(Map<String, Serializable> seedMap) {
45 this.seedMap = seedMap;
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);
55 /* must occur for junit tests to work */
56 this.execution = execution;
59 public GeneralBuildingBlock getGeneralBuildingBlock() {
61 GeneralBuildingBlock generalBuildingBlock = (GeneralBuildingBlock) execution.getVariable("gBBInput");
63 if (generalBuildingBlock == null) {
64 throw new MissingBuildingBlockInputException(MISSING_MSG);
67 return generalBuildingBlock;
68 } catch (ClassCastException e) {
69 throw new MalformedBuildingBlockInputException(MALFORMED_MSG, e);
74 public <T> T getVariable(String key) {
79 public <T> T getRequiredVariable(String key) throws RequiredExecutionVariableExeception {
82 result = this.get(key);
84 throw new RequiredExecutionVariableExeception(String.format(INVALID_INPUT_MISSING, key));
91 public void setVariable(String key, Serializable value) {
92 this.execution.setVariable(key, value);
96 public Map<ResourceKey, String> getLookupMap() {
97 return this.get("lookupKeyMap");
100 public DelegateExecution getDelegateExecution() {
101 return this.execution;
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);
113 protected <T> T get(String key) {
114 final Object value = this.execution.getVariable(key);