Bug fixes for modify TN NSSI operation
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCreateTnNssiInstance.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 import org.camunda.bpm.engine.delegate.BpmnError
24 import org.camunda.bpm.engine.delegate.DelegateExecution
25 import org.onap.aai.domain.yang.NetworkPolicy
26 import org.onap.aai.domain.yang.SliceProfile
27 import org.onap.aaiclient.client.aai.AAIResourcesClient
28 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
29 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
30 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
31 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
32 import org.onap.so.bpmn.common.scripts.ExceptionUtil
33 import org.onap.so.bpmn.core.json.JsonUtils
34 import org.slf4j.Logger
35 import org.slf4j.LoggerFactory
36
37 import static org.apache.commons.lang3.StringUtils.isBlank
38 import static org.apache.commons.lang3.StringUtils.isNotBlank
39
40 class DoCreateTnNssiInstance extends AbstractServiceTaskProcessor {
41
42     private static final Logger logger = LoggerFactory.getLogger(DoCreateTnNssiInstance.class);
43     JsonUtils jsonUtil = new JsonUtils()
44     TnNssmfUtils tnNssmfUtils = new TnNssmfUtils()
45     ExceptionUtil exceptionUtil = new ExceptionUtil()
46     String Prefix = "DCTN_"
47
48     void preProcessRequest(DelegateExecution execution) {
49         String msg = ""
50         logger.trace("Enter preProcessRequest()")
51
52         execution.setVariable("prefix", Prefix)
53
54         String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
55         String modelUuid = execution.getVariable("modelUuid")
56         //here modelVersion is not set, we use modelUuid to decompose the service.
57         def isDebugLogEnabled = true
58         execution.setVariable("isDebugLogEnabled", isDebugLogEnabled)
59         String serviceModelInfo = """{
60             "modelInvariantUuid":"${modelInvariantUuid}",
61             "modelUuid":"${modelUuid}",
62             "modelVersion":""
63              }"""
64         execution.setVariable("serviceModelInfo", serviceModelInfo)
65
66         if (isBlank(execution.getVariable("enableSdnc"))) {
67             tnNssmfUtils.setEnableSdncConfig(execution)
68         }
69
70         logger.trace("Exit preProcessRequest")
71     }
72
73
74     void createSliceProfile(DelegateExecution execution) {
75
76         String ssInstanceId = execution.getVariable("sliceServiceInstanceId")
77         String sliceProfileStr = execution.getVariable("sliceProfile")
78         String sliceProfileId = UUID.randomUUID().toString()
79         SliceProfile sliceProfile = new SliceProfile();
80         sliceProfile.setProfileId(sliceProfileId)
81         sliceProfile.setLatency(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "latency")))
82         sliceProfile.setResourceSharingLevel(jsonUtil.getJsonValue(sliceProfileStr, "resourceSharingLevel"))
83         //sliceProfile.setSNssai(tnNssmfUtils.getFirstSnssaiFromSliceProfile(sliceProfileStr))
84
85         sliceProfile.setMaxBandwidth(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "maxBandwidth")))
86
87         //sliceProfile.setReliability(new Object())
88         try {
89             AAIResourcesClient client = getAAIClient()
90             AAIResourceUri uri =
91                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
92                             .customer(execution.getVariable("globalSubscriberId"))
93                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
94                             .serviceInstance(ssInstanceId)
95                             .sliceProfile(sliceProfileId))
96             client.create(uri, sliceProfile)
97
98         } catch (BpmnError e) {
99             throw e
100         } catch (Exception ex) {
101             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
102             logger.info(msg)
103             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
104         }
105     }
106
107
108     void createServiceInstance(DelegateExecution execution) {
109
110         String serviceRole = "nssi"
111         String serviceType = execution.getVariable("subscriptionServiceType")
112         String ssInstanceId = execution.getVariable("sliceServiceInstanceId")
113         String sliceProfileStr = execution.getVariable("sliceProfile")
114         try {
115             if (sliceProfileStr == null || sliceProfileStr.isEmpty()) {
116                 String msg = "ERROR: createServiceInstance: sliceProfile is null"
117                 logger.error(msg)
118                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
119             }
120
121             org.onap.aai.domain.yang.ServiceInstance ss = new org.onap.aai.domain.yang.ServiceInstance()
122             ss.setServiceInstanceId(ssInstanceId)
123             String sliceInstanceName = execution.getVariable("sliceServiceInstanceName")
124             if (isBlank(sliceInstanceName)) {
125                 logger.error("ERROR: createServiceInstance: sliceInstanceName is null")
126                 sliceInstanceName = ssInstanceId
127             }
128             ss.setServiceInstanceName(sliceInstanceName)
129             ss.setServiceType(serviceType)
130             String serviceStatus = "deactivated"
131             ss.setOrchestrationStatus(serviceStatus)
132             String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
133             String modelUuid = execution.getVariable("modelUuid")
134             ss.setModelInvariantId(modelInvariantUuid)
135             ss.setModelVersionId(modelUuid)
136             String serviceInstanceLocationId = tnNssmfUtils.getFirstPlmnIdFromSliceProfile(sliceProfileStr)
137             ss.setServiceInstanceLocationId(serviceInstanceLocationId)
138             String snssai = tnNssmfUtils.getFirstSnssaiFromSliceProfile(sliceProfileStr)
139             //ss.setEnvironmentContext(snssai)
140             ss.setEnvironmentContext("tn")
141             ss.setServiceRole(serviceRole)
142
143             String domainTypeStr = jsonUtil.getJsonValue(sliceProfileStr, "domainType")
144             if (isNotBlank(domainTypeStr)) {
145                 ss.setWorkloadContext(domainTypeStr)
146             }
147
148             AAIResourcesClient client = getAAIClient()
149             AAIResourceUri uri =
150                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
151                             .customer(execution.getVariable("globalSubscriberId"))
152                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
153                             .serviceInstance(ssInstanceId))
154             client.create(uri, ss)
155         } catch (BpmnError e) {
156             throw e
157         } catch (Exception ex) {
158             String msg = "Exception in DoCreateTnNssiInstance.createServiceInstance: " + ex.getMessage()
159             logger.error(msg)
160             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
161         }
162     }
163
164
165     void createAllottedResource(DelegateExecution execution) {
166         String ssInstanceId = execution.getVariable('sliceServiceInstanceId')
167
168         try {
169             List<String> networkStrList = jsonUtil.StringArrayToList(execution.getVariable("transportSliceNetworks"))
170
171             for (String networkStr : networkStrList) {
172                 String allottedResourceId = UUID.randomUUID().toString()
173                 AAIResourceUri allottedResourceUri =
174                         AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
175                                 .customer(execution.getVariable("globalSubscriberId"))
176                                 .serviceSubscription(execution.getVariable("subscriptionServiceType"))
177                                 .serviceInstance(execution.getVariable("sliceServiceInstanceId"))
178                                 .allottedResource(allottedResourceId))
179                 execution.setVariable("allottedResourceUri", allottedResourceUri)
180                 String modelInvariantId = execution.getVariable("modelInvariantUuid")
181                 String modelVersionId = execution.getVariable("modelUuid")
182
183                 org.onap.aai.domain.yang.AllottedResource resource = new org.onap.aai.domain.yang.AllottedResource()
184                 resource.setId(allottedResourceId)
185                 resource.setType("TsciNetwork")
186                 resource.setAllottedResourceName("network_" + execution.getVariable("sliceServiceInstanceName"))
187                 getAAIClient().create(allottedResourceUri, resource)
188
189                 createNetworkPolicyForAllocatedResource(execution, ssInstanceId, allottedResourceId)
190
191                 String linkArrayStr = jsonUtil.getJsonValue(networkStr, "connectionLinks")
192                 createLogicalLinksForAllocatedResource(execution, linkArrayStr, ssInstanceId, allottedResourceId)
193             }
194         } catch (BpmnError e) {
195             throw e
196         } catch (Exception ex) {
197             String msg = "Exception in DoCreateTnNssiInstance.createAllottedResource: " + ex.getMessage()
198             logger.error(msg)
199             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
200         }
201     }
202
203     void createNetworkPolicy(DelegateExecution execution, String ssInstanceId, String networkPolicyId) {
204         try {
205
206             String sliceProfileStr = execution.getVariable("sliceProfile")
207             if (sliceProfileStr == null || sliceProfileStr.isEmpty()) {
208                 String msg = "ERROR: createNetworkPolicy: sliceProfile is null"
209                 logger.error(msg)
210                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
211             }
212
213             NetworkPolicy networkPolicy = new NetworkPolicy();
214             networkPolicy.setNetworkPolicyId(networkPolicyId)
215             networkPolicy.setName("TSCi policy")
216             networkPolicy.setType("SLA")
217             networkPolicy.setNetworkPolicyFqdn(ssInstanceId)
218
219             String latencyStr = jsonUtil.getJsonValue(sliceProfileStr, "latency")
220             if (latencyStr != null && !latencyStr.isEmpty()) {
221                 networkPolicy.setLatency(Integer.parseInt(latencyStr))
222             }
223
224             String bwStr = jsonUtil.getJsonValue(sliceProfileStr, "maxBandwidth")
225             if (bwStr != null && !bwStr.isEmpty()) {
226                 networkPolicy.setMaxBandwidth(Integer.parseInt(bwStr))
227             } else {
228                 logger.debug("ERROR: createNetworkPolicy: maxBandwidth is null")
229             }
230
231             //networkPolicy.setReliability(new Object())
232
233             AAIResourceUri networkPolicyUri =
234                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().networkPolicy(networkPolicyId))
235             getAAIClient().create(networkPolicyUri, networkPolicy)
236
237         } catch (BpmnError e) {
238             throw e
239         } catch (Exception ex) {
240             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
241             logger.error(msg)
242             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
243         }
244     }
245
246     void createNetworkPolicyForAllocatedResource(DelegateExecution execution,
247                                                  String ssInstanceId,
248                                                  String allottedResourceId) {
249         try {
250             AAIResourceUri allottedResourceUri =
251                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
252                             .customer(execution.getVariable("globalSubscriberId"))
253                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
254                             .serviceInstance(ssInstanceId)
255                             .allottedResource(allottedResourceId))
256
257             if (!getAAIClient().exists(allottedResourceUri)) {
258                 logger.info("ERROR: createLogicalLinksForAllocatedResource: allottedResource not exist: uri={}",
259                         allottedResourceUri)
260                 return
261             }
262
263             String networkPolicyId = UUID.randomUUID().toString()
264             createNetworkPolicy(execution, ssInstanceId, networkPolicyId)
265
266             tnNssmfUtils.attachNetworkPolicyToAllottedResource(execution, tnNssmfUtils.AAI_VERSION,
267                     allottedResourceUri,
268                     networkPolicyId);
269
270         } catch (BpmnError e) {
271             throw e
272         } catch (Exception ex) {
273             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
274             logger.error(msg)
275             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
276         }
277     }
278
279     void createLogicalLinksForAllocatedResource(DelegateExecution execution,
280                                                 String linkArrayStr, String ssInstanceId,
281                                                 String allottedResourceId) {
282         try {
283             AAIResourceUri allottedResourceUri =
284                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
285                             .customer(execution.getVariable("globalSubscriberId"))
286                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
287                             .serviceInstance(ssInstanceId)
288                             .allottedResource(allottedResourceId))
289
290             if (!getAAIClient().exists(allottedResourceUri)) {
291                 logger.info("ERROR: createLogicalLinksForAllocatedResource: allottedResource not exist: uri={}",
292                         allottedResourceUri)
293                 return
294             }
295
296             List<String> linkStrList = jsonUtil.StringArrayToList(linkArrayStr)
297
298             for (String linkStr : linkStrList) {
299                 String linkId = jsonUtil.getJsonValue(linkStr, "id")
300                 if (isBlank(linkId)) {
301                     linkId = "tn-nssmf-" + UUID.randomUUID().toString()
302                 }
303                 logger.debug("createLogicalLinksForAllocatedResource: linkId=" + linkId)
304
305                 String epA = jsonUtil.getJsonValue(linkStr, "transportEndpointA")
306                 String epB = jsonUtil.getJsonValue(linkStr, "transportEndpointB")
307                 String modelInvariantId = execution.getVariable("modelInvariantUuid")
308                 String modelVersionId = execution.getVariable("modelUuid")
309
310                 org.onap.aai.domain.yang.LogicalLink resource = new org.onap.aai.domain.yang.LogicalLink()
311                 resource.setLinkId(linkId)
312                 resource.setLinkName(epA)
313                 resource.setLinkName2(epB)
314                 resource.setLinkType("TsciConnectionLink")
315                 resource.setInMaint(false)
316
317                 //epA is link-name
318                 AAIResourceUri logicalLinkUri =
319                         AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().logicalLink(epA))
320                 getAAIClient().create(logicalLinkUri, resource)
321
322                 tnNssmfUtils.attachLogicalLinkToAllottedResource(execution, tnNssmfUtils.AAI_VERSION,
323                         allottedResourceUri, epA);
324             }
325         } catch (BpmnError e) {
326             throw e
327         } catch (Exception ex) {
328             String msg = "Exception in DoCreateTnNssiInstance.createLogicalLinksForAllocatedResource: " + ex.getMessage()
329             logger.error(msg)
330             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
331         }
332     }
333
334     void preprocessSdncAllocateTnNssiRequest(DelegateExecution execution) {
335         def method = getClass().getSimpleName() + '.preProcessSDNCActivateRequest(' +
336                 'execution=' + execution.getId() +
337                 ')'
338         def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
339         logger.trace('Entered ' + method)
340
341         logger.trace("STARTED preProcessSDNCActivateRequest Process")
342         try {
343             String serviceInstanceId = execution.getVariable("sliceServiceInstanceId")
344
345             String createSDNCRequest = tnNssmfUtils.buildSDNCRequest(execution, serviceInstanceId, "create")
346
347             execution.setVariable("TNNSSMF_SDNCRequest", createSDNCRequest)
348             logger.debug("Outgoing SDNCRequest is: \n" + createSDNCRequest)
349
350         } catch (Exception e) {
351             logger.debug("Exception Occured Processing preProcessSDNCActivateRequest. Exception is:\n" + e)
352             exceptionUtil.buildAndThrowWorkflowException(execution, 1002,
353                     "Error Occured during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
354         }
355         logger.trace("COMPLETED  preProcessSDNCActivateRequest Process")
356     }
357
358
359     void validateSDNCResponse(DelegateExecution execution, String response, String method) {
360         tnNssmfUtils.validateSDNCResponse(execution, response, method)
361     }
362 }