b29009ff1d3b29c2113646eb84a8edc849d66e91
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020 Huawei Technologies Co., Ltd. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.decisionpoint.impl.camunda.controller.sdnc;
22
23 import java.util.Arrays;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.camunda.bpm.engine.delegate.DelegateExecution;
29 import org.onap.so.bpmn.core.json.JsonUtils;
30 import org.onap.so.client.sdnc.lcm.beans.payload.*;
31 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.REQUEST_PAYLOAD;
32 import org.onap.so.bpmn.infrastructure.decisionpoint.impl.camunda.controller.common.SoPropertyConstants;
33
34 public class SDNCLcmPayloadBuilder {
35
36     private static Logger logger = LoggerFactory.getLogger(SDNCLcmPayloadBuilder.class);
37
38     public static ActivateNESwPayload buildActivateNESwPayload(DelegateExecution execution) {
39         String requestPayload = String.valueOf(execution.getVariable(REQUEST_PAYLOAD));
40
41         ActivateNESwPayload activateNESwPayload = new ActivateNESwPayload();
42
43         String ipaddressV4Oam = JsonUtils.getJsonValue(requestPayload, "ipaddressV4Oam");
44         activateNESwPayload.setIpaddressV4Oam(ipaddressV4Oam);
45
46         String targetSwVersion = String.valueOf(execution.getVariable(SoPropertyConstants.TARGET_SOFTWARE_VERSION));
47         if (targetSwVersion == null) {
48             targetSwVersion = JsonUtils.getJsonValue(requestPayload, "targetSwVersion");
49         }
50         activateNESwPayload.setSwVersionToBeActivated(targetSwVersion);
51
52         String playbookName = JsonUtils.getJsonValue(requestPayload, "activateNESwPlaybook");
53         if (playbookName != null) {
54             activateNESwPayload.setPlaybookName(playbookName);
55         }
56
57         return activateNESwPayload;
58     }
59
60     public static DownloadNESwPayload buildDownloadNESwPayload(DelegateExecution execution) {
61         String requestPayload = String.valueOf(execution.getVariable(REQUEST_PAYLOAD));
62
63         DownloadNESwPayload downloadNESwPayload = new DownloadNESwPayload();
64
65         String ipaddressV4Oam = JsonUtils.getJsonValue(requestPayload, "ipaddressV4Oam");
66         downloadNESwPayload.setIpaddressV4Oam(ipaddressV4Oam);
67
68         String swToBeDownloadedString = JsonUtils.getJsonValue(requestPayload, "swToBeDownloaded");
69         SwToBeDownloadedElement[] swToBeDownloadedArray;
70
71         ObjectMapper mapper = new ObjectMapper();
72         try {
73             swToBeDownloadedArray = mapper.readValue(swToBeDownloadedString, SwToBeDownloadedElement[].class);
74             downloadNESwPayload.setSwToBeDownloaded(Arrays.asList(swToBeDownloadedArray));
75         } catch (Exception e) {
76             logger.error("Parse SwToBeDownloaded error: ", e);
77         }
78
79         String playbookName = JsonUtils.getJsonValue(requestPayload, "downloadNESwPlaybook");
80         if (playbookName != null) {
81             downloadNESwPayload.setPlaybookName(playbookName);
82         }
83
84         return downloadNESwPayload;
85     }
86
87     public static UpgradePostCheckPayload buildUpgradePostCheckPayload(DelegateExecution execution) {
88         String requestPayload = String.valueOf(execution.getVariable(REQUEST_PAYLOAD));
89
90         UpgradePostCheckPayload upgradePostCheckPayload = new UpgradePostCheckPayload();
91
92         String ipaddressV4Oam = JsonUtils.getJsonValue(requestPayload, "ipaddressV4Oam");
93         upgradePostCheckPayload.setIpaddressV4Oam(ipaddressV4Oam);
94
95         String oldSwVersion = JsonUtils.getJsonValue(requestPayload, "oldSwVersion");
96         upgradePostCheckPayload.setOldSwVersion(oldSwVersion);
97
98         String targetSwVersion = String.valueOf(execution.getVariable(SoPropertyConstants.TARGET_SOFTWARE_VERSION));
99         if (targetSwVersion == null) {
100             targetSwVersion = JsonUtils.getJsonValue(requestPayload, "targetSwVersion");
101         }
102         upgradePostCheckPayload.setTargetSwVersion(targetSwVersion);
103
104         String ruleName = JsonUtils.getJsonValue(requestPayload, "postCheckRuleName");
105         upgradePostCheckPayload.setRuleName(ruleName);
106
107         String additionalData = JsonUtils.getJsonValue(requestPayload, "postCheckAdditionalData");
108         upgradePostCheckPayload.setAdditionalData(additionalData);
109
110         String playbookName = JsonUtils.getJsonValue(requestPayload, "postCheckPlaybook");
111         if (playbookName != null) {
112             upgradePostCheckPayload.setPlaybookName(playbookName);
113         }
114
115         return upgradePostCheckPayload;
116     }
117
118     public static UpgradePreCheckPayload buildUpgradePreCheckPayload(DelegateExecution execution) {
119         String requestPayload = String.valueOf(execution.getVariable(REQUEST_PAYLOAD));
120
121         UpgradePreCheckPayload upgradePreCheckPayload = new UpgradePreCheckPayload();
122
123         String ipaddressV4Oam = JsonUtils.getJsonValue(requestPayload, "ipaddressV4Oam");
124         upgradePreCheckPayload.setIpaddressV4Oam(ipaddressV4Oam);
125
126         String oldSwVersion = JsonUtils.getJsonValue(requestPayload, "oldSwVersion");
127         upgradePreCheckPayload.setOldSwVersion(oldSwVersion);
128
129         String targetSwVersion = String.valueOf(execution.getVariable(SoPropertyConstants.TARGET_SOFTWARE_VERSION));
130         if (targetSwVersion == null) {
131             targetSwVersion = JsonUtils.getJsonValue(requestPayload, "targetSwVersion");
132         }
133         upgradePreCheckPayload.setTargetSwVersion(targetSwVersion);
134
135         String ruleName = JsonUtils.getJsonValue(requestPayload, "preCheckRuleName");
136         upgradePreCheckPayload.setRuleName(ruleName);
137
138         String additionalData = JsonUtils.getJsonValue(requestPayload, "preCheckAdditionalData");
139         upgradePreCheckPayload.setAdditionalData(additionalData);
140
141         String playbookName = JsonUtils.getJsonValue(requestPayload, "preCheckPlaybook");
142         if (playbookName != null) {
143             upgradePreCheckPayload.setPlaybookName(playbookName);
144         }
145
146         return upgradePreCheckPayload;
147     }
148
149     public static String convertToSting(Object msgObject) throws JsonProcessingException {
150         ObjectMapper mapper = new ObjectMapper();
151         return mapper.writeValueAsString(msgObject);
152     }
153 }