Implementation of TN NSSMF WF on SO
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / TnNssmfUtils.groovy
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.scripts
22
23
24 import org.camunda.bpm.engine.delegate.DelegateExecution
25 import org.onap.so.bpmn.common.scripts.ExceptionUtil
26 import org.onap.so.bpmn.common.scripts.MsoUtils
27 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
28 import org.onap.so.bpmn.core.RollbackData
29 import org.onap.so.bpmn.core.UrnPropertiesReader
30 import org.onap.so.bpmn.core.WorkflowException
31 import org.onap.so.bpmn.core.json.JsonUtils
32 import org.slf4j.Logger
33 import org.slf4j.LoggerFactory
34
35 import static org.apache.commons.lang3.StringUtils.isBlank
36
37 class TnNssmfUtils {
38     private static final Logger logger = LoggerFactory.getLogger(TnNssmfUtils.class);
39
40
41     ExceptionUtil exceptionUtil = new ExceptionUtil()
42     JsonUtils jsonUtil = new JsonUtils()
43     MsoUtils msoUtils = new MsoUtils()
44     SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
45
46     TnNssmfUtils() {
47     }
48
49
50     void setSdncCallbackUrl(DelegateExecution execution, boolean exceptionOnErr) {
51         setSdncCallbackUrl(execution, "sdncCallbackUrl", exceptionOnErr)
52     }
53
54     void setSdncCallbackUrl(DelegateExecution execution, String variableName, boolean exceptionOnErr) {
55         String sdncCallbackUrl = UrnPropertiesReader.getVariable('mso.workflow.sdncadapter.callback', execution)
56
57         if (isBlank(sdncCallbackUrl) && exceptionOnErr) {
58             String msg = "mso.workflow.sdncadapter.callback is null"
59             logger.debug(msg)
60             exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
61         } else {
62             execution.setVariable(variableName, sdncCallbackUrl)
63         }
64     }
65
66     String buildSDNCRequest(DelegateExecution execution, String svcInstId, String action) {
67
68         String uuid = execution.getVariable('testReqId') // for junits
69         if (uuid == null) {
70             uuid = execution.getVariable("msoRequestId") + "-" + System.currentTimeMillis()
71         }
72
73         def callbackURL = execution.getVariable("sdncCallbackUrl")
74         def requestId = execution.getVariable("msoRequestId")
75         def serviceId = execution.getVariable("sliceServiceInstanceId")
76         def vnfType = execution.getVariable("serviceType")
77         def vnfName = execution.getVariable("sliceServiceInstanceName")
78         def tenantId = execution.getVariable("sliceServiceInstanceId")
79         def source = execution.getVariable("sliceServiceInstanceId")
80         def vnfId = execution.getVariable("sliceServiceInstanceId")
81         def cloudSiteId = execution.getVariable("sliceServiceInstanceId")
82         def serviceModelInfo = execution.getVariable("serviceModelInfo")
83         def vnfModelInfo = execution.getVariable("serviceModelInfo")
84         def globalSubscriberId = execution.getVariable("globalSubscriberId")
85
86         String vnfNameString = """<vnf-name>${MsoUtils.xmlEscape(vnfName)}</vnf-name>"""
87         String serviceEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(serviceModelInfo)
88         String vnfEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(vnfModelInfo)
89
90         String sdncVNFParamsXml = ""
91
92         if (execution.getVariable("vnfParamsExistFlag") == true) {
93             sdncVNFParamsXml = buildSDNCParamsXml(execution)
94         } else {
95             sdncVNFParamsXml = ""
96         }
97
98         String sdncRequest =
99                 """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.onap/so/request/types/v1"
100                                                                                                         xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
101                                                                                                         xmlns:sdncadapter="http://org.onap/workflow/sdnc/adapter/schema/v1">
102            <sdncadapter:RequestHeader>
103                                 <sdncadapter:RequestId>${MsoUtils.xmlEscape(uuid)}</sdncadapter:RequestId>
104                                 <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(svcInstId)}</sdncadapter:SvcInstanceId>
105                                 <sdncadapter:SvcAction>${MsoUtils.xmlEscape(action)}</sdncadapter:SvcAction>
106                                 <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
107                                 <sdncadapter:CallbackUrl>${MsoUtils.xmlEscape(callbackURL)}</sdncadapter:CallbackUrl>
108                                 <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>
109                 </sdncadapter:RequestHeader>
110         <sdncadapterworkflow:SDNCRequestData>
111                 <request-information>
112                         <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
113                         <request-action>AllocateTnNssi</request-action>
114                         <source>${MsoUtils.xmlEscape(source)}</source>
115                         <notification-url/>
116                         <order-number/>
117                         <order-version/>
118                 </request-information>
119                 <service-information>
120                         <service-id>${MsoUtils.xmlEscape(serviceId)}</service-id>
121                         <subscription-service-type>${MsoUtils.xmlEscape(serviceId)}</subscription-service-type>
122                         ${serviceEcompModelInformation}
123                         <service-instance-id>${MsoUtils.xmlEscape(svcInstId)}</service-instance-id>
124                         <global-customer-id>${MsoUtils.xmlEscape(globalSubscriberId)}</global-customer-id>
125                 </service-information>
126                 <vnf-information>
127                         <vnf-id>${MsoUtils.xmlEscape(vnfId)}</vnf-id>
128                         <vnf-type>${MsoUtils.xmlEscape(vnfType)}</vnf-type>
129                         ${vnfEcompModelInformation}
130                 </vnf-information>
131                 <vnf-request-input>
132                         ${vnfNameString}
133                         <tenant>${MsoUtils.xmlEscape(tenantId)}</tenant>
134                         <aic-cloud-region>${MsoUtils.xmlEscape(cloudSiteId)}</aic-cloud-region>
135                         ${sdncVNFParamsXml}
136                 </vnf-request-input>
137         </sdncadapterworkflow:SDNCRequestData>
138         </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
139
140         logger.debug("sdncRequest:  " + sdncRequest)
141         return sdncRequest
142     }
143
144     String buildSDNCParamsXml(DelegateExecution execution) {
145         String params = ""
146         StringBuilder sb = new StringBuilder()
147         Map<String, String> paramsMap = execution.getVariable("TNNSSMF_vnfParamsMap")
148
149         for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
150             String paramsXml
151             String key = entry.getKey();
152             String value = entry.getValue()
153             paramsXml = """<${key}>$value</$key>"""
154             params = sb.append(paramsXml)
155         }
156         return params
157     }
158
159     void validateSDNCResponse(DelegateExecution execution, String response, String method) {
160         validateSDNCResponse(execution, response, method, true)
161     }
162
163     void validateSDNCResponse(DelegateExecution execution, String response, String method, boolean exceptionOnErr) {
164         logger.debug("STARTED ValidateSDNCResponse Process")
165
166         String msg
167
168         String prefix = execution.setVariable("prefix")
169         if (isBlank(prefix)) {
170             if (exceptionOnErr) {
171                 msg = "validateSDNCResponse: prefix is null"
172                 logger.error(msg)
173                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
174             }
175             return
176         }
177
178         WorkflowException workflowException = execution.getVariable("WorkflowException")
179         boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
180
181         logger.debug("workflowException: " + workflowException)
182
183         sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
184
185         String sdncResponse = response
186         if (execution.getVariable(prefix + 'sdncResponseSuccess') == true) {
187             logger.debug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call.  Response is: \n" + sdncResponse)
188             RollbackData rollbackData = execution.getVariable("rollbackData")
189
190             if (method.equals("allocate")) {
191                 rollbackData.put("VNFMODULE", "rollbackSDNCRequestAllocate", "true")
192             } else if (method.equals("deallocate")) {
193                 rollbackData.put("VNFMODULE", "rollbackSDNCRequestDeallocate", "true")
194             } else if (method.equals("activate")) {
195                 rollbackData.put("VNFMODULE", "rollbackSDNCRequestActivate", "true")
196             } else if (method.equals("deactivate")) {
197                 rollbackData.put("VNFMODULE", "rollbackSDNCRequestDeactivate", "true")
198             } else if (method.equals("modify")) {
199                 rollbackData.put("VNFMODULE", "rollbackSDNCRequestModify", "true")
200             }
201             execution.setVariable("rollbackData", rollbackData)
202         } else {
203             if (exceptionOnErr) {
204                 msg = "validateSDNCResponse: bad Response from SDNC Adapter for " + method + " SDNC Call."
205                 logger.error(msg)
206                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
207             }
208         }
209
210         logger.debug("COMPLETED ValidateSDNCResponse Process")
211     }
212
213     String getExecutionInputParams(DelegateExecution execution) {
214         String res = "msoRequestId=" + execution.getVariable("msoRequestId") +
215                 ", modelInvariantUuid=" + execution.getVariable("modelInvariantUuid") +
216                 ", modelUuid=" + execution.getVariable("modelUuid") +
217                 ", serviceInstanceID=" + execution.getVariable("serviceInstanceID") +
218                 ", operationType=" + execution.getVariable("operationType") +
219                 ", globalSubscriberId=" + execution.getVariable("globalSubscriberId") +
220                 ", dummyServiceId=" + execution.getVariable("dummyServiceId") +
221                 ", nsiId=" + execution.getVariable("nsiId") +
222                 ", networkType=" + execution.getVariable("networkType") +
223                 ", subscriptionServiceType=" + execution.getVariable("subscriptionServiceType") +
224                 ", jobId=" + execution.getVariable("jobId") +
225                 ", sliceParams=" + execution.getVariable("sliceParams") +
226                 ", servicename=" + execution.getVariable("servicename")
227
228         return res
229     }
230
231     String getFirstSnssaiFromSliceProfile(String sliceProfileStr) {
232         String snssaiListStr = jsonUtil.getJsonValue(sliceProfileStr, "snssaiList")
233         String snssai = jsonUtil.StringArrayToList(snssaiListStr).get(0)
234
235         return snssai
236     }
237
238     String getFirstPlmnIdFromSliceProfile(String sliceProfileStr) {
239         String plmnListStr = jsonUtil.getJsonValue(sliceProfileStr, "plmnIdList")
240         String res = jsonUtil.StringArrayToList(plmnListStr).get(0)
241
242         return res
243     }
244 }