92be82469194c4873058f47e019c61e93d609d3c
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / decisionpoint / impl / buildingblock / ControllerExecutionBB.java
1 /*-
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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.so.bpmn.infrastructure.decisionpoint.impl.buildingblock;
21
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;
35
36 /**
37  * This class is used for {@ref BuildingBlockExecution} API based execution.
38  *
39  * it decides which controller implementation to use based on the parameters, like actor, scope, action.
40  *
41  * The following parameters are expected in the {@ref BuildingBlockExecution} context,
42  * <ul>
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>
48  * </ul>
49  */
50 @Component
51 public class ControllerExecutionBB extends AbstractControllerExecution<BuildingBlockExecution> {
52
53     @Autowired
54     protected CatalogDbClient catalogDbClient;
55
56     public void execute(final BuildingBlockExecution execution) {
57         ControllerContext<BuildingBlockExecution> controllerContext = buildControllerContext(execution);
58         controllerExecute(controllerContext);
59     }
60
61     @Override
62     protected String getParameterFromExecution(BuildingBlockExecution execution, String parameterName) {
63         Object object = execution.getVariable(parameterName);
64         if (object != null) {
65             String paramValue = String.valueOf(object);
66             logger.debug("parameterName: {}, parameterValue: {}", parameterName, paramValue);
67             return paramValue;
68         }
69         return "";
70     }
71
72     /**
73      * this method is used to get the controller actor, there could be few places to get the actor(ordered by priority),
74      *
75      * <ol>
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>
79      * </ol>
80      *
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
86      */
87     @Override
88     protected String getControllerActor(BuildingBlockExecution execution, String controllerScope,
89             String resourceCustomizationUuid, String controllerAction) {
90
91         /**
92          * Firstly, check the execution context for actor parameter.
93          */
94         String controllerActor = getParameterFromExecution(execution, CONTROLLER_ACTOR_PARAM);
95
96         /**
97          * If no meaningful controller actor value found in the execution context and the value is not SO-REF-DATA.
98          */
99         if (Strings.isNullOrEmpty(controllerActor) && !isSoRefControllerActor(controllerActor)) {
100
101             /**
102              * For BuildingBlockExecution, we should try to get the resource information from Cached metadata.
103              *
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.
107              */
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();
118             } else {
119                 logger.warn("Unrecognized scope: {}", controllerScope);
120             }
121         }
122
123         /**
124          * Lastly, can NOT find the controller actor information from resource customization table or the return value
125          * is SO-REF-DATA.
126          */
127         if (Strings.isNullOrEmpty(controllerActor) || isSoRefControllerActor(controllerActor)) {
128             String resourceType = getParameterFromExecution(execution, RESOURCE_TYPE_PARAM);
129             ControllerSelectionReference reference = catalogDbClient
130                     .getControllerSelectionReferenceByVnfTypeAndActionCategory(resourceType, controllerAction);
131
132             controllerActor = reference.getControllerName();
133         }
134         return controllerActor;
135     }
136
137     @Override
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);
145             } else {
146                 exceptionBuilder.buildAndThrowWorkflowException(controllerContext.getExecution(), 9001,
147                         "Controller is NOT Ready for the action", ONAPComponents.SO);
148             }
149         } else {
150             exceptionBuilder.buildAndThrowWorkflowException(controllerContext.getExecution(), 9000,
151                     "Unable to find the controller implementation", ONAPComponents.SO);
152         }
153     }
154 }