dfe77cac5fbe7d40c50c0bb07039d523ff776dde
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix
4  *  Modifications Copyright (C) 2020 Huawei Technologies Co., Ltd.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
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.decisionpoint.impl.camunda.controller.sdnc;
22
23 import java.util.List;
24 import java.util.UUID;
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import org.springframework.stereotype.Component;
28 import org.camunda.bpm.engine.delegate.DelegateExecution;
29 import org.onap.so.bpmn.infrastructure.decisionpoint.api.ControllerContext;
30 import org.onap.so.bpmn.infrastructure.decisionpoint.impl.camunda.controller.common.SoPropertyConstants;
31 import org.onap.so.bpmn.infrastructure.decisionpoint.impl.camunda.controller.LcmControllerDE;
32 import org.onap.so.client.exception.BadResponseException;
33 import org.onap.so.client.exception.PayloadGenerationException;
34 import org.onap.so.client.sdnc.common.SDNCConstants;
35 import org.onap.so.client.sdnc.lcm.*;
36 import org.onap.so.client.sdnc.lcm.beans.*;
37 import org.onap.so.client.sdnc.lcm.beans.payload.*;
38 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.REQUEST_ID;
39 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PNF_CORRELATION_ID;
40 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.REQUEST_PAYLOAD;
41
42 @Component
43 public class SdncControllerDE extends LcmControllerDE {
44
45     private static final int SDNC_DELEGATE_EXECUTION_ERROR_CODE = 1103;
46     private static final ObjectMapper mapper = new ObjectMapper();
47
48     @Override
49     public Boolean understand(ControllerContext<DelegateExecution> context) {
50         return context.getControllerActor().equalsIgnoreCase("sdnc");
51     }
52
53     @Override
54     public Boolean ready(ControllerContext<DelegateExecution> context) {
55         return true;
56     }
57
58     @Override
59     protected int callLcmClient(ControllerContext<DelegateExecution> context) {
60         DelegateExecution execution = context.getExecution();
61
62         logger.debug("Running activity for id: {}, name: {}", execution.getCurrentActivityId(),
63                 execution.getCurrentActivityName());
64
65         try {
66             LcmInput lcmInput = buildLcmInput(execution);
67             sendLcmRequest(execution, lcmInput);
68
69             execution.setVariable(SoPropertyConstants.CONTROLLER_STATUS, "Success");
70         } catch (Exception e) {
71             execution.setVariable(SoPropertyConstants.CONTROLLER_STATUS, "Failure");
72
73             exceptionUtil.buildAndThrowWorkflowException(execution, SDNC_DELEGATE_EXECUTION_ERROR_CODE, e);
74         }
75
76         logger.debug("Finish activity for id: {}, name: {}", execution.getCurrentActivityId(),
77                 execution.getCurrentActivityName());
78
79         return 0;
80     }
81
82     @Override
83     protected int getErrorCode() {
84         return SDNC_DELEGATE_EXECUTION_ERROR_CODE;
85     }
86
87     private LcmOutput sendSyncRequest(String operation, LcmInput lcmInput) throws BadResponseException {
88         SDNCLcmClientBuilder sdncLcmClientBuilder = new SDNCLcmClientBuilder();
89         SDNCLcmRestClient sdncLcmRestClient;
90         try {
91             sdncLcmRestClient = sdncLcmClientBuilder.newSDNCLcmRestClient(operation);
92         } catch (SDNCLcmClientBuilderException e) {
93             logger.error("Create SDNCLcmRestClient error: ", e);
94             throw new BadResponseException("Can not send request to SDNC.");
95         }
96
97         LcmOutput lcmOutput;
98         try {
99             lcmOutput = sdncLcmRestClient.sendRequest(lcmInput);
100         } catch (Exception e) {
101             logger.error("SDNCLcmRestClient sends request failure: ", e);
102             throw new BadResponseException("Send request to SDNC failure.");
103         }
104         return lcmOutput;
105     }
106
107     private LcmOutput selectLcmOutputFromDmaapResponses(List<LcmDmaapResponse> lcmDmaapResponses, LcmInput lcmInput) {
108         String requestId = lcmInput.getCommonHeader().getRequestId();
109         String subRequestId = lcmInput.getCommonHeader().getSubRequestId();
110
111         for (LcmDmaapResponse lcmDmaapResponse : lcmDmaapResponses) {
112             LcmOutput lcmOutput = lcmDmaapResponse.getBody().getOutput();
113             if (requestId.equals(lcmOutput.getCommonHeader().getRequestId())
114                     && subRequestId.equals(lcmOutput.getCommonHeader().getSubRequestId())) {
115                 return lcmOutput;
116             }
117         }
118
119         return null;
120     }
121
122     private LcmOutput sendAsyncRequest(String operation, LcmInput lcmInput) throws BadResponseException {
123         SDNCLcmClientBuilder sdncLcmClientBuilder = new SDNCLcmClientBuilder();
124         SDNCLcmDmaapClient sdncLcmDmaapClient;
125         try {
126             sdncLcmDmaapClient = sdncLcmClientBuilder.newSDNCLcmDmaapClient();
127         } catch (SDNCLcmClientBuilderException e) {
128             logger.error("Create SDNCLcmDmaapClient error: ", e);
129             throw new BadResponseException("Can not send request to SDNC.");
130         }
131
132         LcmDmaapRequest lcmDmaapRequest = SDNCLcmMessageBuilder.buildLcmDmaapRequest(operation, lcmInput);
133         try {
134             sdncLcmDmaapClient.sendRequest(lcmDmaapRequest);
135         } catch (Exception e) {
136             logger.error("SDNCLcmDmaapClient sends request failure: ", e);
137             throw new BadResponseException("Send request to SDNC failure.");
138         }
139
140         long timeout = sdncLcmClientBuilder.getSDNCLcmProperties().getActionTimeout();
141         long startTime = System.currentTimeMillis();
142         while (true) {
143             List<LcmDmaapResponse> lcmDmaapResponses = sdncLcmDmaapClient.getResponse();
144             if (lcmDmaapResponses.size() > 0) {
145                 LcmOutput lcmOutput = selectLcmOutputFromDmaapResponses(lcmDmaapResponses, lcmInput);
146                 if (lcmOutput != null) {
147                     return lcmOutput;
148                 }
149             }
150
151             long stopTime = System.currentTimeMillis();
152             if ((stopTime - startTime) > timeout) {
153                 String msg = "Timeout for SDNC LCM action " + lcmInput.getAction();
154                 logger.error(msg);
155                 throw new BadResponseException(msg);
156             }
157         }
158     }
159
160     public static String toLowerHyphen(String lcmAction) {
161         String regex = "([a-z0-9A-Z])(?=[A-Z])";
162         String replacement = "$1-";
163         return lcmAction.replaceAll(regex, replacement).toLowerCase();
164     }
165
166     private String convertToSting(Object msgObject) throws PayloadGenerationException {
167         try {
168             return SDNCLcmPayloadBuilder.convertToSting(msgObject);
169         } catch (JsonProcessingException e) {
170             throw new PayloadGenerationException(e.getMessage());
171         }
172     }
173
174     private LcmInput buildLcmInput(DelegateExecution execution) throws PayloadGenerationException {
175         String requestId = String.valueOf(execution.getVariable(REQUEST_ID));
176         String requestAction = String.valueOf(execution.getVariable(SoPropertyConstants.SO_ACTION));
177         String pnfName = String.valueOf(execution.getVariable(PNF_CORRELATION_ID));
178         logger.debug(String.format("requestId: %s, action: %s, pnfName: %s", requestId, requestAction, pnfName));
179
180         String requestPayload = String.valueOf(execution.getVariable(REQUEST_PAYLOAD));
181         logger.debug("SO request payload: " + requestPayload);
182
183         String lcmAction;
184         String lcmPayload;
185
186         switch (requestAction) {
187             case SoPropertyConstants.ACTION_PRE_CHECK:
188                 lcmAction = SDNCLcmActionConstants.UPGRADE_PRE_CHECK;
189
190                 UpgradePreCheckPayload upgradePreCheckPayload;
191                 upgradePreCheckPayload = SDNCLcmPayloadBuilder.buildUpgradePreCheckPayload(execution);
192                 lcmPayload = convertToSting(upgradePreCheckPayload);
193                 break;
194             case SoPropertyConstants.ACTION_DOWNLOAD_N_E_SW:
195                 lcmAction = SDNCLcmActionConstants.DOWNLOAD_N_E_SW;
196
197                 DownloadNESwPayload downloadNESwPayload;
198                 downloadNESwPayload = SDNCLcmPayloadBuilder.buildDownloadNESwPayload(execution);
199                 lcmPayload = convertToSting(downloadNESwPayload);
200                 break;
201             case SoPropertyConstants.ACTION_ACTIVATE_N_E_SW:
202                 lcmAction = SDNCLcmActionConstants.ACTIVATE_N_E_SW;
203
204                 ActivateNESwPayload activateNESwPayload;
205                 activateNESwPayload = SDNCLcmPayloadBuilder.buildActivateNESwPayload(execution);
206                 lcmPayload = convertToSting(activateNESwPayload);
207                 break;
208             case SoPropertyConstants.ACTION_POST_CHECK:
209                 lcmAction = SDNCLcmActionConstants.UPGRADE_POST_CHECK;
210
211                 UpgradePostCheckPayload upgradePostCheckPayload;
212                 upgradePostCheckPayload = SDNCLcmPayloadBuilder.buildUpgradePostCheckPayload(execution);
213                 lcmPayload = convertToSting(upgradePostCheckPayload);
214                 break;
215             default:
216                 String msg = "Unsupported SO Action: " + requestAction;
217                 logger.error(msg);
218                 throw new PayloadGenerationException(msg);
219         }
220
221         String subRequestId = UUID.randomUUID().toString();
222         LcmInput lcmInput =
223                 SDNCLcmMessageBuilder.buildLcmInputForPnf(requestId, subRequestId, pnfName, lcmAction, lcmPayload);
224
225         try {
226             String lcmInputMsg = mapper.writeValueAsString(lcmInput);
227             logger.debug("SDNC input message for {}: {}", lcmAction, lcmInputMsg);
228         } catch (JsonProcessingException e) {
229             throw new PayloadGenerationException(e.getMessage());
230         }
231
232         return lcmInput;
233     }
234
235     private void parseLcmOutput(LcmOutput lcmOutput, String lcmAction) throws BadResponseException {
236         LcmStatus lcmStatus = lcmOutput.getStatus();
237         int lcmStatusCode = lcmStatus.getCode();
238         String outputPayload = lcmOutput.getPayload();
239
240         if (lcmStatusCode == SDNCConstants.LCM_OUTPUT_SUCCESS_CODE) {
241             logger.debug("Call SDNC LCM API for {} success, code: {}, message: {}, payload: {}", lcmAction,
242                     lcmStatusCode, lcmStatus.getMessage(), outputPayload);
243         } else {
244             String msg = String.format("Call SDNC LCM API for %s failure, code: %d, message: %s, payload: %s",
245                     lcmAction, lcmStatusCode, lcmStatus.getMessage(), outputPayload);
246             logger.error(msg);
247             throw new BadResponseException(msg);
248         }
249     }
250
251     private void sendLcmRequest(DelegateExecution execution, LcmInput lcmInput) throws BadResponseException {
252         String actionMode = String.valueOf(execution.getVariable(SoPropertyConstants.SO_ACTION_MODE));
253         String lcmOperation = toLowerHyphen(lcmInput.getAction());
254
255         LcmOutput lcmOutput;
256         if ("async".equals(actionMode)) {
257             lcmOutput = sendAsyncRequest(lcmOperation, lcmInput);
258         } else {
259             lcmOutput = sendSyncRequest(lcmOperation, lcmInput);
260         }
261
262         parseLcmOutput(lcmOutput, lcmInput.getAction());
263     }
264 }