4482d2a327e22498e6612036f699c82fab1defa0
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
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  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.pnf.delegate;
22
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import org.camunda.bpm.engine.delegate.DelegateExecution;
25 import org.camunda.bpm.engine.delegate.JavaDelegate;
26 import org.onap.aai.domain.yang.Pnf;
27 import org.onap.so.bpmn.core.json.JsonUtils;
28 import org.onap.so.bpmn.infrastructure.pnf.management.PnfManagement;
29 import org.onap.so.client.exception.ExceptionBuilder;
30 import org.onap.so.db.catalog.beans.PnfResourceCustomization;
31 import org.onap.so.db.catalog.client.CatalogDbClient;
32 import org.onap.so.serviceinstancebeans.RequestDetails;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
37 import java.io.IOException;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Optional;
41 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.*;
42
43 /**
44  * This implementation of {@link JavaDelegate} is used to populate the execution object for pnf software upgrade
45  */
46 @Component
47 public class NfSoftwareUpgradeDispatcher implements JavaDelegate {
48
49     private final Logger logger = LoggerFactory.getLogger(getClass());
50     private static final String SERVICE_INSTANCE_NAME = "serviceInstanceName";
51     private static final String BPMN_REQUEST = "bpmnRequest";
52     private static final String RESOURCE_CUSTOMIZATION_UUID_PARAM = "resource_customization_uuid";
53     private static final String PNF_NAME = "pnfName";
54
55     // ERROR CODE for variable not found in the delegation Context
56     private static final int ERROR_CODE = 601;
57
58     @Autowired
59     private PnfManagement pnfManagement;
60
61     @Autowired
62     private ExceptionBuilder exceptionUtil;
63
64     @Autowired
65     private CatalogDbClient catalogDbClient;
66
67     @Autowired
68     private ObjectMapper mapper;
69
70     @Override
71     public void execute(DelegateExecution delegateExecution) throws Exception {
72         logger.debug("Running execute block for activity id:{}, name:{}", delegateExecution.getCurrentActivityId(),
73                 delegateExecution.getCurrentActivityName());
74
75         RequestDetails bpmnRequestDetails = requestVerification(delegateExecution);
76
77         final String serviceInstanceName = bpmnRequestDetails.getRequestInfo().getInstanceName();
78         final String pnfName = bpmnRequestDetails.getRequestParameters().getUserParamValue(PNF_NAME);
79         final String serviceModelUuid = bpmnRequestDetails.getModelInfo().getModelUuid();
80         final List<Map<String, Object>> userParams = bpmnRequestDetails.getRequestParameters().getUserParams();
81         final Pnf pnf = getPnfByPnfName(delegateExecution, pnfName);
82         final List<PnfResourceCustomization> pnfCustomizations =
83                 getPnfResourceCustomizations(delegateExecution, serviceModelUuid);
84         final PnfResourceCustomization pnfResourceCustomization = pnfCustomizations.get(0);
85
86         populateExecution(delegateExecution, bpmnRequestDetails, pnfResourceCustomization, pnf, serviceInstanceName,
87                 pnfName, serviceModelUuid, userParams);
88
89         logger.trace("Completed preProcessRequest PnfSoftwareUpgradeServiceRequest Request ");
90     }
91
92     private RequestDetails requestVerification(DelegateExecution delegateExecution) throws IOException {
93         RequestDetails bpmnRequestDetails = mapper.readValue(
94                 JsonUtils.getJsonValue(String.valueOf(delegateExecution.getVariable(BPMN_REQUEST)), "requestDetails"),
95                 RequestDetails.class);
96
97         throwIfNull(delegateExecution, bpmnRequestDetails.getModelInfo(), SERVICE_MODEL_INFO);
98         throwIfNull(delegateExecution, bpmnRequestDetails.getRequestInfo(), "RequestInfo");
99         throwIfNull(delegateExecution, bpmnRequestDetails.getRequestParameters(), "RequestParameters");
100         throwIfNull(delegateExecution, bpmnRequestDetails.getRequestParameters().getUserParams(), "UserParams");
101
102         return bpmnRequestDetails;
103     }
104
105     private void populateExecution(DelegateExecution delegateExecution, RequestDetails bpmnRequestDetails,
106             PnfResourceCustomization pnfResourceCustomization, Pnf pnf, String serviceInstanceName, String pnfName,
107             String serviceModelUuid, List<Map<String, Object>> userParams) {
108
109         delegateExecution.setVariable(SERVICE_MODEL_INFO, bpmnRequestDetails.getModelInfo());
110         delegateExecution.setVariable(SERVICE_INSTANCE_NAME, serviceInstanceName);
111         delegateExecution.setVariable(PNF_CORRELATION_ID, pnfName);
112         delegateExecution.setVariable(MODEL_UUID, serviceModelUuid);
113         delegateExecution.setVariable(PNF_UUID, pnf.getPnfId());
114         delegateExecution.setVariable(PRC_BLUEPRINT_NAME, pnfResourceCustomization.getBlueprintName());
115         delegateExecution.setVariable(PRC_BLUEPRINT_VERSION, pnfResourceCustomization.getBlueprintVersion());
116         delegateExecution.setVariable(PRC_CUSTOMIZATION_UUID, pnfResourceCustomization.getModelCustomizationUUID());
117         delegateExecution.setVariable(RESOURCE_CUSTOMIZATION_UUID_PARAM,
118                 pnfResourceCustomization.getModelCustomizationUUID());
119         delegateExecution.setVariable(PRC_INSTANCE_NAME, pnfResourceCustomization.getModelInstanceName());
120         delegateExecution.setVariable(PRC_CONTROLLER_ACTOR, pnfResourceCustomization.getControllerActor());
121
122         for (Map<String, Object> param : userParams) {
123             if (param.containsKey("name") && param.containsKey("value")) {
124                 delegateExecution.setVariable(param.get("name").toString(), param.get("value").toString());
125             }
126         }
127     }
128
129     private Pnf getPnfByPnfName(DelegateExecution delegateExecution, String pnfName) {
130         Optional<Pnf> pnfOptional = null;
131         try {
132             pnfOptional = pnfManagement.getEntryFor(pnfName);
133         } catch (IOException e) {
134             logger.warn(e.getMessage(), e);
135             exceptionUtil.buildAndThrowWorkflowException(delegateExecution, ERROR_CODE,
136                     "Unable to fetch from AAI" + e.getMessage());
137         }
138         if (pnfOptional == null || !pnfOptional.isPresent()) {
139             exceptionUtil.buildAndThrowWorkflowException(delegateExecution, ERROR_CODE,
140                     "AAI entry for PNF: " + pnfName + " does not exist");
141         }
142         return pnfOptional.get();
143     }
144
145     private List<PnfResourceCustomization> getPnfResourceCustomizations(DelegateExecution delegateExecution,
146             String serviceModelUuid) {
147         List<PnfResourceCustomization> pnfCustomizations =
148                 catalogDbClient.getPnfResourceCustomizationByModelUuid(serviceModelUuid);
149
150         if (pnfCustomizations == null || pnfCustomizations.isEmpty()) {
151             logger.warn("Unable to find the PNF resource customizations of model service UUID: {}", serviceModelUuid);
152             exceptionUtil.buildAndThrowWorkflowException(delegateExecution, ERROR_CODE,
153                     "Unable to find the PNF resource customizations of model service UUID:  " + serviceModelUuid);
154         }
155         return pnfCustomizations;
156     }
157
158     private void throwIfNull(DelegateExecution delegateExecution, Object obj, String param) {
159         if (obj == null) {
160             logger.warn("Unable to find the parameter: {} in the execution context", param);
161             exceptionUtil.buildAndThrowWorkflowException(delegateExecution, ERROR_CODE,
162                     "Unable to find parameter " + param);
163         }
164     }
165 }