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.buildingblock;
22 import com.google.common.base.Strings;
23 import java.util.Optional;
24 import org.onap.logging.filter.base.ONAPComponents;
25 import org.onap.so.bpmn.common.BuildingBlockExecution;
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.bpmn.infrastructure.decisionpoint.impl.AbstractControllerExecution;
29 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
30 import org.onap.so.db.catalog.beans.PnfResourceCustomization;
31 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
32 import org.onap.so.db.catalog.client.CatalogDbClient;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
37 * This class is used for {@ref BuildingBlockExecution} API based execution.
39 * it decides which controller implementation to use based on the parameters, like actor, scope, action.
41 * The following parameters are expected in the {@ref BuildingBlockExecution} context,
43 * <li>action: action to be executed</li>
44 * <li>scope: type of the resource, i.e, pnf, vnf, vf</li>
45 * <li>resource_customization_uuid: resource customization UUID</li>
46 * <li>resource_type: resource type, optional. It's used to find the controller from controller_selection_reference
47 * table. Same as VNF_TYPE in the table</li>
51 public class ControllerExecutionBB extends AbstractControllerExecution<BuildingBlockExecution> {
54 protected CatalogDbClient catalogDbClient;
56 public void execute(final BuildingBlockExecution execution) {
57 ControllerContext<BuildingBlockExecution> controllerContext = buildControllerContext(execution);
58 controllerExecute(controllerContext);
62 protected String getParameterFromExecution(BuildingBlockExecution execution, String parameterName) {
63 Object object = execution.getVariable(parameterName);
65 String paramValue = String.valueOf(object);
66 logger.debug("parameterName: {}, parameterValue: {}", parameterName, paramValue);
73 * this method is used to get the controller actor, there could be few places to get the actor(ordered by priority),
76 * <li>Execution Context, i.e, BuildingBlockExecution</li>
77 * <li>Resource customization table, pnf_resource_customization for PNF or vnf_resource_customization for VNF</li>
78 * <li>controller_selection_reference, resource_type and action will be used to fetch from this table</li>
81 * @param execution BuildingBlockExecution instance
82 * @param controllerScope controller scope, e.g, pnf, vnf, vfModule
83 * @param resourceCustomizationUuid resource customization UUID, e.g, pnfCustomizationUuid, vnfCustomizationUuid
84 * @param controllerAction controller action, e.g, configAssign, configDeploy
85 * @return controller actor name
88 protected String getControllerActor(BuildingBlockExecution execution, String controllerScope,
89 String resourceCustomizationUuid, String controllerAction) {
92 * Firstly, check the execution context for actor parameter.
94 String controllerActor = getParameterFromExecution(execution, CONTROLLER_ACTOR_PARAM);
97 * If no meaningful controller actor value found in the execution context and the value is not SO-REF-DATA.
99 if (Strings.isNullOrEmpty(controllerActor) && !isSoRefControllerActor(controllerActor)) {
102 * For BuildingBlockExecution, we should try to get the resource information from Cached metadata.
104 * As the current cached metadata doesn't support controller actor, we use the
105 * {@link org.onap.so.db.catalog.client.CatalogDbClient} to fetch information. Once the change is done in
106 * cached metadata, this part should be refactored as well.
108 if (isPnfResourceScope(controllerScope)) {
109 PnfResourceCustomization pnfResourceCustomization =
110 catalogDbClient.getPnfResourceCustomizationByModelCustomizationUUID(resourceCustomizationUuid);
111 controllerActor = pnfResourceCustomization.getControllerActor();
112 } else if (isServiceResourceScope(controllerScope)) {
113 return controllerActor;
114 } else if (isVnfResourceScope(controllerScope)) {
115 VnfResourceCustomization vnfResourceCustomization =
116 catalogDbClient.getVnfResourceCustomizationByModelCustomizationUUID(resourceCustomizationUuid);
117 controllerActor = vnfResourceCustomization.getControllerActor();
119 logger.warn("Unrecognized scope: {}", controllerScope);
124 * Lastly, can NOT find the controller actor information from resource customization table or the return value
127 if (Strings.isNullOrEmpty(controllerActor) || isSoRefControllerActor(controllerActor)) {
128 String resourceType = getParameterFromExecution(execution, RESOURCE_TYPE_PARAM);
129 ControllerSelectionReference reference = catalogDbClient
130 .getControllerSelectionReferenceByVnfTypeAndActionCategory(resourceType, controllerAction);
132 controllerActor = reference.getControllerName();
134 return controllerActor;
138 public void controllerExecute(ControllerContext<BuildingBlockExecution> controllerContext) {
139 Optional<ControllerRunnable> optional = getController(controllerContext);
140 if (optional.isPresent()) {
141 ControllerRunnable controller = optional.get();
142 if (controller.ready(controllerContext)) {
143 controller.prepare(controllerContext);
144 controller.run(controllerContext);
146 exceptionBuilder.buildAndThrowWorkflowException(controllerContext.getExecution(), 9001,
147 "Controller is NOT Ready for the action", ONAPComponents.SO);
150 exceptionBuilder.buildAndThrowWorkflowException(controllerContext.getExecution(), 9000,
151 "Unable to find the controller implementation", ONAPComponents.SO);