aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/decisionpoint/impl/buildingblock/ControllerExecutionBB.java
blob: 92be82469194c4873058f47e019c61e93d609d3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.onap.so.bpmn.infrastructure.decisionpoint.impl.buildingblock;

import com.google.common.base.Strings;
import java.util.Optional;
import org.onap.logging.filter.base.ONAPComponents;
import org.onap.so.bpmn.common.BuildingBlockExecution;
import org.onap.so.bpmn.infrastructure.decisionpoint.api.ControllerContext;
import org.onap.so.bpmn.infrastructure.decisionpoint.api.ControllerRunnable;
import org.onap.so.bpmn.infrastructure.decisionpoint.impl.AbstractControllerExecution;
import org.onap.so.db.catalog.beans.ControllerSelectionReference;
import org.onap.so.db.catalog.beans.PnfResourceCustomization;
import org.onap.so.db.catalog.beans.VnfResourceCustomization;
import org.onap.so.db.catalog.client.CatalogDbClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * This class is used for {@ref BuildingBlockExecution} API based execution.
 *
 * it decides which controller implementation to use based on the parameters, like actor, scope, action.
 *
 * The following parameters are expected in the {@ref BuildingBlockExecution} context,
 * <ul>
 * <li>action: action to be executed</li>
 * <li>scope: type of the resource, i.e, pnf, vnf, vf</li>
 * <li>resource_customization_uuid: resource customization UUID</li>
 * <li>resource_type: resource type, optional. It's used to find the controller from controller_selection_reference
 * table. Same as VNF_TYPE in the table</li>
 * </ul>
 */
@Component
public class ControllerExecutionBB extends AbstractControllerExecution<BuildingBlockExecution> {

    @Autowired
    protected CatalogDbClient catalogDbClient;

    public void execute(final BuildingBlockExecution execution) {
        ControllerContext<BuildingBlockExecution> controllerContext = buildControllerContext(execution);
        controllerExecute(controllerContext);
    }

    @Override
    protected String getParameterFromExecution(BuildingBlockExecution execution, String parameterName) {
        Object object = execution.getVariable(parameterName);
        if (object != null) {
            String paramValue = String.valueOf(object);
            logger.debug("parameterName: {}, parameterValue: {}", parameterName, paramValue);
            return paramValue;
        }
        return "";
    }

    /**
     * this method is used to get the controller actor, there could be few places to get the actor(ordered by priority),
     *
     * <ol>
     * <li>Execution Context, i.e, BuildingBlockExecution</li>
     * <li>Resource customization table, pnf_resource_customization for PNF or vnf_resource_customization for VNF</li>
     * <li>controller_selection_reference, resource_type and action will be used to fetch from this table</li>
     * </ol>
     *
     * @param execution BuildingBlockExecution instance
     * @param controllerScope controller scope, e.g, pnf, vnf, vfModule
     * @param resourceCustomizationUuid resource customization UUID, e.g, pnfCustomizationUuid, vnfCustomizationUuid
     * @param controllerAction controller action, e.g, configAssign, configDeploy
     * @return controller actor name
     */
    @Override
    protected String getControllerActor(BuildingBlockExecution execution, String controllerScope,
            String resourceCustomizationUuid, String controllerAction) {

        /**
         * Firstly, check the execution context for actor parameter.
         */
        String controllerActor = getParameterFromExecution(execution, CONTROLLER_ACTOR_PARAM);

        /**
         * If no meaningful controller actor value found in the execution context and the value is not SO-REF-DATA.
         */
        if (Strings.isNullOrEmpty(controllerActor) && !isSoRefControllerActor(controllerActor)) {

            /**
             * For BuildingBlockExecution, we should try to get the resource information from Cached metadata.
             *
             * As the current cached metadata doesn't support controller actor, we use the
             * {@link org.onap.so.db.catalog.client.CatalogDbClient} to fetch information. Once the change is done in
             * cached metadata, this part should be refactored as well.
             */
            if (isPnfResourceScope(controllerScope)) {
                PnfResourceCustomization pnfResourceCustomization =
                        catalogDbClient.getPnfResourceCustomizationByModelCustomizationUUID(resourceCustomizationUuid);
                controllerActor = pnfResourceCustomization.getControllerActor();
            } else if (isServiceResourceScope(controllerScope)) {
                return controllerActor;
            } else if (isVnfResourceScope(controllerScope)) {
                VnfResourceCustomization vnfResourceCustomization =
                        catalogDbClient.getVnfResourceCustomizationByModelCustomizationUUID(resourceCustomizationUuid);
                controllerActor = vnfResourceCustomization.getControllerActor();
            } else {
                logger.warn("Unrecognized scope: {}", controllerScope);
            }
        }

        /**
         * Lastly, can NOT find the controller actor information from resource customization table or the return value
         * is SO-REF-DATA.
         */
        if (Strings.isNullOrEmpty(controllerActor) || isSoRefControllerActor(controllerActor)) {
            String resourceType = getParameterFromExecution(execution, RESOURCE_TYPE_PARAM);
            ControllerSelectionReference reference = catalogDbClient
                    .getControllerSelectionReferenceByVnfTypeAndActionCategory(resourceType, controllerAction);

            controllerActor = reference.getControllerName();
        }
        return controllerActor;
    }

    @Override
    public void controllerExecute(ControllerContext<BuildingBlockExecution> controllerContext) {
        Optional<ControllerRunnable> optional = getController(controllerContext);
        if (optional.isPresent()) {
            ControllerRunnable controller = optional.get();
            if (controller.ready(controllerContext)) {
                controller.prepare(controllerContext);
                controller.run(controllerContext);
            } else {
                exceptionBuilder.buildAndThrowWorkflowException(controllerContext.getExecution(), 9001,
                        "Controller is NOT Ready for the action", ONAPComponents.SO);
            }
        } else {
            exceptionBuilder.buildAndThrowWorkflowException(controllerContext.getExecution(), 9000,
                    "Unable to find the controller implementation", ONAPComponents.SO);
        }
    }
}