2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.so.bpmn.infrastructure.decisionpoint.impl;
22 import com.google.common.base.Strings;
23 import java.util.List;
24 import java.util.Optional;
25 import java.util.stream.Collectors;
26 import org.onap.so.bpmn.infrastructure.decisionpoint.api.ControllerContext;
27 import org.onap.so.bpmn.infrastructure.decisionpoint.api.ControllerRunnable;
28 import org.onap.so.client.exception.ExceptionBuilder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
34 * Abstract class of the controller execution, it should be extended for the controller execution task.
36 * @param <T> execution context type, e.g., BuildingBlockExecution or DelegateExecution
38 public abstract class AbstractControllerExecution<T> {
41 * parameters from the execution context.
43 protected static final String CONTROLLER_ACTOR_PARAM = "actor";
44 protected static final String CONTROLLER_ACTION_PARAM = "action";
45 protected static final String CONTROLLER_SCOPE_PARAM = "scope";
46 protected static final String RESOURCE_CUSTOMIZATION_UUID_PARAM = "resource_customization_uuid";
47 protected static final String RESOURCE_TYPE_PARAM = "resource_type";
49 protected final Logger logger = LoggerFactory.getLogger(this.getClass());
52 protected List<ControllerRunnable<T>> availableControllerRunnables;
55 protected ExceptionBuilder exceptionBuilder;
58 * Find the {@ref ControllerRunnable} instances understanding the {@ref ControllerContext}.
60 * Only one instance should be able to understand the context. if more than one or none instances found, return
63 * @param context controller context
64 * @return Optional of ControllerRunnable instance
66 protected Optional<ControllerRunnable> getController(ControllerContext<T> context) {
67 List<ControllerRunnable<T>> satisfiedControllers = availableControllerRunnables.stream()
68 .filter(controllerRunnable -> controllerRunnable.understand(context)).collect(Collectors.toList());
70 if (logger.isDebugEnabled()) {
71 for (ControllerRunnable<T> satisfiedController : satisfiedControllers) {
72 logger.debug("{} controllerRunnable understands the context", satisfiedController.getClass().getName());
77 * Make sure only one {@ref ControllerRunnable} instance understands the context or else return Null.
79 if (satisfiedControllers.size() == 1) {
80 return Optional.of(satisfiedControllers.get(0));
81 } else if (satisfiedControllers.isEmpty()) {
82 logger.warn("Can NOT find any ControllerRunnable for context: {}", context);
83 } else if (satisfiedControllers.size() > 1) {
85 "Found {} instances understanding the context: {}, please make sure only 1 instance understands it",
86 satisfiedControllers.size(), context);
89 return Optional.empty();
93 * Build the {@ref ControllerContext} context based on execution context.
95 * @param t execution context instance, e.g., BuildingBlockExecution or DelegateExecution instance
96 * @return controller context instance of the execution context type
98 protected ControllerContext<T> buildControllerContext(final T t) {
99 String controllerAction = getParameterFromExecution(t, CONTROLLER_ACTION_PARAM);
100 String controllerScope = getParameterFromExecution(t, CONTROLLER_SCOPE_PARAM);
101 String resourceCustomizationUuid = getParameterFromExecution(t, RESOURCE_CUSTOMIZATION_UUID_PARAM);
102 String controllerActor = getControllerActor(t, controllerScope, resourceCustomizationUuid, controllerAction);
103 ControllerContext<T> controllerContext = new ControllerContext<>();
104 controllerContext.setExecution(t);
105 controllerContext.setControllerAction(controllerAction);
106 controllerContext.setControllerActor(controllerActor);
107 controllerContext.setControllerScope(controllerScope);
108 return controllerContext;
112 * Retrieve the controller actor.
114 * @param t execution context instance, e.g., BuildingBlockExecution or DelegateExecution instance
115 * @param controllerScope controller scope, e.g, pnf, vnf, vfModule
116 * @param resourceCustomizationUuid resource customization UUID, e.g, pnfCustomizationUuid, vnfCustomizationUuid
117 * @param controllerAction controller action, e.g, configAssign, configDeploy
118 * @return controller actor
120 protected abstract String getControllerActor(T t, String controllerScope, String resourceCustomizationUuid,
121 String controllerAction);
124 * Controller execution based on the Controller Context.
126 * @param controllerContext ControllerContext object
128 public abstract void controllerExecute(final ControllerContext<T> controllerContext);
131 * Retrieve the parameter value as String from the execution context.
133 * @param t execution context instance, e.g., BuildingBlockExecution or DelegateExecution instance
134 * @param parameterName parameter name to be retrieved
135 * @return String value of the parameter
137 protected abstract String getParameterFromExecution(final T t, String parameterName);
140 * Check whether the controller actor value is SO ref value, i.e, equals to SO-REF-DATA.
142 * @param controllerActor controller actor, e.g, SO-REF-DATA, SDNC, CDS
143 * @return true if the controller actor is SO-REF-DATA, else return false
145 protected boolean isSoRefControllerActor(final String controllerActor) {
146 return !Strings.isNullOrEmpty(controllerActor) && controllerActor.equalsIgnoreCase("SO-REF-DATA");
150 * Check whether the controller scope is PNF resource related.
152 * @param controllerScope controller scope, e.g, pnf, vnf, vfModule
153 * @return true if the controller scope is pnf, else return false
155 protected boolean isPnfResourceScope(final String controllerScope) {
156 return ("pnf").equalsIgnoreCase(controllerScope);
160 * Check whether the controller scope is SERVICE
162 * @param controllerScope controller scope, e.g, pnf, vnf, vfModule, service
163 * @return true if the controller scope is service, else return false
165 protected boolean isServiceResourceScope(final String controllerScope) {
166 return "service".equalsIgnoreCase(controllerScope);
170 * Check whether the controller scope is VNF resource related.
172 * @param controllerScope controller scope, e.g, pnf, vnf, vfModule
173 * @return true if the controller scope is vnf or vfModule, else return false
175 protected boolean isVnfResourceScope(final String controllerScope) {
176 if (Strings.isNullOrEmpty(controllerScope)) {
179 if (controllerScope.toLowerCase().startsWith("vnf") || controllerScope.equalsIgnoreCase("vfmodule")) {