Enable ControllerExecutionBB for service scope
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / decisionpoint / impl / AbstractControllerExecution.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;
21
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;
32
33 /**
34  * Abstract class of the controller execution, it should be extended for the controller execution task.
35  * 
36  * @param <T> execution context type, e.g., BuildingBlockExecution or DelegateExecution
37  */
38 public abstract class AbstractControllerExecution<T> {
39
40     /**
41      * parameters from the execution context.
42      */
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";
48
49     protected final Logger logger = LoggerFactory.getLogger(this.getClass());
50
51     @Autowired
52     protected List<ControllerRunnable<T>> availableControllerRunnables;
53
54     @Autowired
55     protected ExceptionBuilder exceptionBuilder;
56
57     /**
58      * Find the {@ref ControllerRunnable} instances understanding the {@ref ControllerContext}.
59      *
60      * Only one instance should be able to understand the context. if more than one or none instances found, return
61      * null.
62      *
63      * @param context controller context
64      * @return Optional of ControllerRunnable instance
65      */
66     protected Optional<ControllerRunnable> getController(ControllerContext<T> context) {
67         List<ControllerRunnable<T>> satisfiedControllers = availableControllerRunnables.stream()
68                 .filter(controllerRunnable -> controllerRunnable.understand(context)).collect(Collectors.toList());
69
70         if (logger.isDebugEnabled()) {
71             for (ControllerRunnable<T> satisfiedController : satisfiedControllers) {
72                 logger.debug("{} controllerRunnable understands the context", satisfiedController.getClass().getName());
73             }
74         }
75
76         /**
77          * Make sure only one {@ref ControllerRunnable} instance understands the context or else return Null.
78          */
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) {
84             logger.warn(
85                     "Found {} instances understanding the context: {}, please make sure only 1 instance understands it",
86                     satisfiedControllers.size(), context);
87         }
88
89         return Optional.empty();
90     }
91
92     /**
93      * Build the {@ref ControllerContext} context based on execution context.
94      * 
95      * @param t execution context instance, e.g., BuildingBlockExecution or DelegateExecution instance
96      * @return controller context instance of the execution context type
97      */
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;
109     }
110
111     /**
112      * Retrieve the controller actor.
113      * 
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
119      */
120     protected abstract String getControllerActor(T t, String controllerScope, String resourceCustomizationUuid,
121             String controllerAction);
122
123     /**
124      * Controller execution based on the Controller Context.
125      * 
126      * @param controllerContext ControllerContext object
127      */
128     public abstract void controllerExecute(final ControllerContext<T> controllerContext);
129
130     /**
131      * Retrieve the parameter value as String from the execution context.
132      * 
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
136      */
137     protected abstract String getParameterFromExecution(final T t, String parameterName);
138
139     /**
140      * Check whether the controller actor value is SO ref value, i.e, equals to SO-REF-DATA.
141      * 
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
144      */
145     protected boolean isSoRefControllerActor(final String controllerActor) {
146         return !Strings.isNullOrEmpty(controllerActor) && controllerActor.equalsIgnoreCase("SO-REF-DATA");
147     }
148
149     /**
150      * Check whether the controller scope is PNF resource related.
151      * 
152      * @param controllerScope controller scope, e.g, pnf, vnf, vfModule
153      * @return true if the controller scope is pnf, else return false
154      */
155     protected boolean isPnfResourceScope(final String controllerScope) {
156         return ("pnf").equalsIgnoreCase(controllerScope);
157     }
158
159     /**
160      * Check whether the controller scope is SERVICE
161      *
162      * @param controllerScope controller scope, e.g, pnf, vnf, vfModule, service
163      * @return true if the controller scope is service, else return false
164      */
165     protected boolean isServiceResourceScope(final String controllerScope) {
166         return "service".equalsIgnoreCase(controllerScope);
167     }
168
169     /**
170      * Check whether the controller scope is VNF resource related.
171      * 
172      * @param controllerScope controller scope, e.g, pnf, vnf, vfModule
173      * @return true if the controller scope is vnf or vfModule, else return false
174      */
175     protected boolean isVnfResourceScope(final String controllerScope) {
176         if (Strings.isNullOrEmpty(controllerScope)) {
177             return false;
178         }
179         if (controllerScope.toLowerCase().startsWith("vnf") || controllerScope.equalsIgnoreCase("vfmodule")) {
180             return true;
181         }
182         return false;
183     }
184 }