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 import com.fasterxml.jackson.annotation.JsonIgnore;
35 import com.fasterxml.jackson.annotation.JsonProperty;
37 public class DelegateExecutionImpl implements BuildingBlockExecution, Serializable {
39 private static final long serialVersionUID = 5559067662634919395L;
42 private final Map<String, Serializable> seedMap;
44 private transient DelegateExecution execution;
45 private static final String INVALID_INPUT_MISSING = "Expected variable of \"%s\" not found in execution";
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";
52 public DelegateExecutionImpl(final Map<String, Serializable> seedMap) {
53 this.seedMap = seedMap;
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);
63 /* must occur for junit tests to work */
64 this.execution = execution;
69 public GeneralBuildingBlock getGeneralBuildingBlock() {
71 final GeneralBuildingBlock generalBuildingBlock = (GeneralBuildingBlock) execution.getVariable("gBBInput");
73 if (generalBuildingBlock == null) {
74 throw new MissingBuildingBlockInputException(MISSING_MSG);
77 return generalBuildingBlock;
78 } catch (final ClassCastException e) {
79 throw new MalformedBuildingBlockInputException(MALFORMED_MSG, e);
84 public <T> T getVariable(final String key) {
89 public <T> T getRequiredVariable(final String key) throws RequiredExecutionVariableExeception {
92 result = this.get(key);
94 throw new RequiredExecutionVariableExeception(String.format(INVALID_INPUT_MISSING, key));
101 public void setVariable(final String key, final Serializable value) {
102 this.execution.setVariable(key, value);
107 public Map<ResourceKey, String> getLookupMap() {
108 return this.get("lookupKeyMap");
113 public String getFlowToBeCalled() {
114 return this.get("flowToBeCalled");
118 public DelegateExecution getDelegateExecution() {
119 return this.execution;
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);
131 @SuppressWarnings("unchecked")
132 protected <T> T get(final String key) {
133 final Object value = this.execution.getVariable(key);