Fix bugs found in TN Slicing integration involving OOF
[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         String sst = execution.getVariable("sst")
115         try {
116             if (sliceProfileStr == null || sliceProfileStr.isEmpty()) {
117                 String msg = "ERROR: createServiceInstance: sliceProfile is null"
118                 logger.error(msg)
119                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
120             }
121
122             org.onap.aai.domain.yang.ServiceInstance ss = new org.onap.aai.domain.yang.ServiceInstance()
123             ss.setServiceInstanceId(ssInstanceId)
124             String sliceInstanceName = execution.getVariable("sliceServiceInstanceName")
125             if (isBlank(sliceInstanceName)) {
126                 logger.error("ERROR: createServiceInstance: sliceInstanceName is null")
127                 sliceInstanceName = ssInstanceId
128             }
129             ss.setServiceInstanceName(sliceInstanceName)
130             ss.setServiceType(sst)
131             String serviceStatus = "deactivated"
132             ss.setOrchestrationStatus(serviceStatus)
133             String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
134             String modelUuid = execution.getVariable("modelUuid")
135             ss.setModelInvariantId(modelInvariantUuid)
136             ss.setModelVersionId(modelUuid)
137             String serviceInstanceLocationId = tnNssmfUtils.getFirstPlmnIdFromSliceProfile(sliceProfileStr)
138             ss.setServiceInstanceLocationId(serviceInstanceLocationId)
139             String snssai = tnNssmfUtils.getFirstSnssaiFromSliceProfile(sliceProfileStr)
140             //ss.setEnvironmentContext(snssai)
141             ss.setEnvironmentContext("tn")
142             ss.setServiceRole(serviceRole)
143
144             String domainTypeStr = jsonUtil.getJsonValue(sliceProfileStr, "domainType")
145             if (isNotBlank(domainTypeStr)) {
146                 ss.setWorkloadContext(domainTypeStr)
147             }
148
149             String resourceSharingLevel = jsonUtil.getJsonValue(sliceProfileStr, "resourceSharingLevel")
150             if (isNotBlank(resourceSharingLevel)) {
151                 ss.setServiceFunction(resourceSharingLevel)
152             }
153
154             AAIResourcesClient client = getAAIClient()
155             AAIResourceUri uri =
156                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
157                             .customer(execution.getVariable("globalSubscriberId"))
158                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
159                             .serviceInstance(ssInstanceId))
160             client.create(uri, ss)
161         } catch (BpmnError e) {
162             throw e
163         } catch (Exception ex) {
164             String msg = "Exception in DoCreateTnNssiInstance.createServiceInstance: " + ex.getMessage()
165             logger.error(msg)
166             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
167         }
168     }
169
170
171     void createAllottedResource(DelegateExecution execution) {
172         String ssInstanceId = execution.getVariable('sliceServiceInstanceId')
173
174         try {
175             List<String> networkStrList = jsonUtil.StringArrayToList(execution.getVariable("transportSliceNetworks"))
176
177             for (String networkStr : networkStrList) {
178                 String allottedResourceId = UUID.randomUUID().toString()
179                 AAIResourceUri allottedResourceUri =
180                         AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
181                                 .customer(execution.getVariable("globalSubscriberId"))
182                                 .serviceSubscription(execution.getVariable("subscriptionServiceType"))
183                                 .serviceInstance(execution.getVariable("sliceServiceInstanceId"))
184                                 .allottedResource(allottedResourceId))
185                 execution.setVariable("allottedResourceUri", allottedResourceUri)
186                 String modelInvariantId = execution.getVariable("modelInvariantUuid")
187                 String modelVersionId = execution.getVariable("modelUuid")
188
189                 org.onap.aai.domain.yang.AllottedResource resource = new org.onap.aai.domain.yang.AllottedResource()
190                 resource.setId(allottedResourceId)
191                 resource.setType("TsciNetwork")
192                 resource.setAllottedResourceName("network_" + execution.getVariable("sliceServiceInstanceName"))
193                 getAAIClient().create(allottedResourceUri, resource)
194
195                 createNetworkPolicyForAllocatedResource(execution, ssInstanceId, allottedResourceId)
196
197                 String linkArrayStr = jsonUtil.getJsonValue(networkStr, "connectionLinks")
198                 createLogicalLinksForAllocatedResource(execution, linkArrayStr, ssInstanceId, allottedResourceId)
199             }
200         } catch (BpmnError e) {
201             throw e
202         } catch (Exception ex) {
203             String msg = "Exception in DoCreateTnNssiInstance.createAllottedResource: " + ex.getMessage()
204             logger.error(msg)
205             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
206         }
207     }
208
209     void createNetworkPolicy(DelegateExecution execution, String ssInstanceId, String networkPolicyId) {
210         try {
211
212             String sliceProfileStr = execution.getVariable("sliceProfile")
213             if (sliceProfileStr == null || sliceProfileStr.isEmpty()) {
214                 String msg = "ERROR: createNetworkPolicy: sliceProfile is null"
215                 logger.error(msg)
216                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
217             }
218
219             NetworkPolicy networkPolicy = new NetworkPolicy();
220             networkPolicy.setNetworkPolicyId(networkPolicyId)
221             networkPolicy.setName("TSCi policy")
222             networkPolicy.setType("SLA")
223             networkPolicy.setNetworkPolicyFqdn(ssInstanceId)
224
225             String latencyStr = jsonUtil.getJsonValue(sliceProfileStr, "latency")
226             if (latencyStr != null && !latencyStr.isEmpty()) {
227                 networkPolicy.setLatency(Integer.parseInt(latencyStr))
228             }
229
230             String bwStr = jsonUtil.getJsonValue(sliceProfileStr, "maxBandwidth")
231             if (bwStr != null && !bwStr.isEmpty()) {
232                 networkPolicy.setMaxBandwidth(Integer.parseInt(bwStr))
233             } else {
234                 logger.debug("ERROR: createNetworkPolicy: maxBandwidth is null")
235             }
236
237             //networkPolicy.setReliability(new Object())
238
239             AAIResourceUri networkPolicyUri =
240                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().networkPolicy(networkPolicyId))
241             getAAIClient().create(networkPolicyUri, networkPolicy)
242
243         } catch (BpmnError e) {
244             throw e
245         } catch (Exception ex) {
246             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
247             logger.error(msg)
248             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
249         }
250     }
251
252     void createNetworkPolicyForAllocatedResource(DelegateExecution execution,
253                                                  String ssInstanceId,
254                                                  String allottedResourceId) {
255         try {
256             AAIResourceUri allottedResourceUri =
257                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
258                             .customer(execution.getVariable("globalSubscriberId"))
259                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
260                             .serviceInstance(ssInstanceId)
261                             .allottedResource(allottedResourceId))
262
263             if (!getAAIClient().exists(allottedResourceUri)) {
264                 logger.info("ERROR: createLogicalLinksForAllocatedResource: allottedResource not exist: uri={}",
265                         allottedResourceUri)
266                 return
267             }
268
269             String networkPolicyId = UUID.randomUUID().toString()
270             createNetworkPolicy(execution, ssInstanceId, networkPolicyId)
271
272             tnNssmfUtils.attachNetworkPolicyToAllottedResource(execution, tnNssmfUtils.AAI_VERSION,
273                     allottedResourceUri,
274                     networkPolicyId);
275
276         } catch (BpmnError e) {
277             throw e
278         } catch (Exception ex) {
279             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
280             logger.error(msg)
281             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
282         }
283     }
284
285     void createLogicalLinksForAllocatedResource(DelegateExecution execution,
286                                                 String linkArrayStr, String ssInstanceId,
287                                                 String allottedResourceId) {
288         try {
289             AAIResourceUri allottedResourceUri =
290                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
291                             .customer(execution.getVariable("globalSubscriberId"))
292                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
293                             .serviceInstance(ssInstanceId)
294                             .allottedResource(allottedResourceId))
295
296             if (!getAAIClient().exists(allottedResourceUri)) {
297                 logger.info("ERROR: createLogicalLinksForAllocatedResource: allottedResource not exist: uri={}",
298                         allottedResourceUri)
299                 return
300             }
301
302             List<String> linkStrList = jsonUtil.StringArrayToList(linkArrayStr)
303
304             for (String linkStr : linkStrList) {
305                 String linkId = jsonUtil.getJsonValue(linkStr, "name")
306                 if (isBlank(linkId)) {
307                     linkId = "tn-nssmf-" + UUID.randomUUID().toString()
308                 }
309                 logger.debug("createLogicalLinksForAllocatedResource: linkId=" + linkId)
310
311                 String epA = jsonUtil.getJsonValue(linkStr, "transportEndpointA")
312                 String epB = jsonUtil.getJsonValue(linkStr, "transportEndpointB")
313                 String modelInvariantId = execution.getVariable("modelInvariantUuid")
314                 String modelVersionId = execution.getVariable("modelUuid")
315
316                 org.onap.aai.domain.yang.LogicalLink resource = new org.onap.aai.domain.yang.LogicalLink()
317                 resource.setLinkId(linkId)
318                 resource.setLinkName(epA)
319                 resource.setLinkName2(epB)
320                 resource.setLinkType("TsciConnectionLink")
321                 resource.setInMaint(false)
322
323                 //epA is link-name
324                 AAIResourceUri logicalLinkUri =
325                         AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().logicalLink(epA))
326                 getAAIClient().create(logicalLinkUri, resource)
327
328                 tnNssmfUtils.attachLogicalLinkToAllottedResource(execution, tnNssmfUtils.AAI_VERSION,
329                         allottedResourceUri, epA);
330             }
331         } catch (BpmnError e) {
332             throw e
333         } catch (Exception ex) {
334             String msg = "Exception in DoCreateTnNssiInstance.createLogicalLinksForAllocatedResource: " + ex.getMessage()
335             logger.error(msg)
336             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
337         }
338     }
339
340     void preprocessSdncAllocateTnNssiRequest(DelegateExecution execution) {
341         def method = getClass().getSimpleName() + '.preProcessSDNCActivateRequest(' +
342                 'execution=' + execution.getId() +
343                 ')'
344         def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
345         logger.trace('Entered ' + method)
346
347         logger.trace("STARTED preProcessSDNCActivateRequest Process")
348         try {
349             String serviceInstanceId = execution.getVariable("sliceServiceInstanceId")
350
351             String createSDNCRequest = tnNssmfUtils.buildSDNCRequest(execution, serviceInstanceId, "create")
352
353             execution.setVariable("TNNSSMF_SDNCRequest", createSDNCRequest)
354             logger.debug("Outgoing SDNCRequest is: \n" + createSDNCRequest)
355
356         } catch (Exception e) {
357             logger.debug("Exception Occured Processing preProcessSDNCActivateRequest. Exception is:\n" + e)
358             exceptionUtil.buildAndThrowWorkflowException(execution, 1002,
359                     "Error Occured during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
360         }
361         logger.trace("COMPLETED  preProcessSDNCActivateRequest Process")
362     }
363
364
365     void validateSDNCResponse(DelegateExecution execution, String response, String method) {
366         tnNssmfUtils.validateSDNCResponse(execution, response, method)
367     }
368 }