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