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;
26 import org.camunda.bpm.engine.delegate.DelegateExecution;
27 import org.onap.so.bpmn.common.exceptions.MalformedBuildingBlockInputException;
28 import org.onap.so.bpmn.common.exceptions.MissingBuildingBlockInputException;
29 import org.onap.so.bpmn.common.exceptions.RequiredExecutionVariableExeception;
30 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
31 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
32 import com.fasterxml.jackson.annotation.JsonIgnore;
33 import com.fasterxml.jackson.annotation.JsonProperty;
35 public class DelegateExecutionImpl implements BuildingBlockExecution, Serializable {
37 private static final long serialVersionUID = 5559067662634919395L;
40 private final Map<String, Serializable> seedMap;
42 private transient DelegateExecution execution;
43 private static final String INVALID_INPUT_MISSING = "Expected variable of \"%s\" not found in execution";
45 private static final String MISSING_MSG =
46 "Execution variable \"gBBInput\" cannot be null when executing building blocks";
47 private static final String MALFORMED_MSG =
48 "Execution variable \"gBBInput\" must contain an element of type GeneralBuildingBlock";
50 public DelegateExecutionImpl(final Map<String, Serializable> seedMap) {
51 this.seedMap = seedMap;
54 public DelegateExecutionImpl(final DelegateExecution execution) {
55 this.seedMap = new HashMap<>();
56 execution.getVariables().forEach((key, value) -> {
57 if (value instanceof Serializable) {
58 seedMap.put(key, (Serializable) value);
61 /* must occur for junit tests to work */
62 this.execution = execution;
67 public GeneralBuildingBlock getGeneralBuildingBlock() {
69 final GeneralBuildingBlock generalBuildingBlock = (GeneralBuildingBlock) execution.getVariable("gBBInput");
71 if (generalBuildingBlock == null) {
72 throw new MissingBuildingBlockInputException(MISSING_MSG);
75 return generalBuildingBlock;
76 } catch (final ClassCastException e) {
77 throw new MalformedBuildingBlockInputException(MALFORMED_MSG, e);
82 public <T> T getVariable(final String key) {
87 public <T> T getRequiredVariable(final String key) throws RequiredExecutionVariableExeception {
90 result = this.get(key);
92 throw new RequiredExecutionVariableExeception(String.format(INVALID_INPUT_MISSING, key));
99 public void setVariable(final String key, final Serializable value) {
100 this.execution.setVariable(key, value);
105 public Map<ResourceKey, String> getLookupMap() {
106 return this.get("lookupKeyMap");
111 public String getFlowToBeCalled() {
112 return this.get("flowToBeCalled");
116 public DelegateExecution getDelegateExecution() {
117 return this.execution;
120 public void setDelegateExecution(final DelegateExecution execution) {
121 this.execution = execution;
122 this.seedMap.forEach((key, value) -> {
123 if (!execution.hasVariable(key)) {
124 execution.setVariable(key, value);
129 @SuppressWarnings("unchecked")
130 protected <T> T get(final String key) {
131 final Object value = this.execution.getVariable(key);