Merge "5G core nssmf - Allocate api"
[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
39 class DoCreateTnNssiInstance extends AbstractServiceTaskProcessor {
40
41     private static final Logger logger = LoggerFactory.getLogger(DoCreateTnNssiInstance.class);
42     final String AAI_VERSION = "v21"
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         tnNssmfUtils.setEnableSdncConfig(execution)
67
68         logger.trace("Exit preProcessRequest")
69     }
70
71
72     void createSliceProfile(DelegateExecution execution) {
73
74         String ssInstanceId = execution.getVariable("sliceServiceInstanceId")
75         String sliceProfileStr = execution.getVariable("sliceProfile")
76         String sliceProfileId = UUID.randomUUID().toString()
77         SliceProfile sliceProfile = new SliceProfile();
78         sliceProfile.setProfileId(sliceProfileId)
79         sliceProfile.setLatency(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "latency")))
80         sliceProfile.setResourceSharingLevel(jsonUtil.getJsonValue(sliceProfileStr, "resourceSharingLevel"))
81         //sliceProfile.setSNssai(tnNssmfUtils.getFirstSnssaiFromSliceProfile(sliceProfileStr))
82
83         sliceProfile.setMaxBandwidth(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "maxBandwidth")))
84
85         //sliceProfile.setReliability(new Object())
86         try {
87             AAIResourcesClient client = getAAIClient()
88             AAIResourceUri uri =
89                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
90                             .customer(execution.getVariable("globalSubscriberId"))
91                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
92                             .serviceInstance(ssInstanceId)
93                             .sliceProfile(sliceProfileId))
94             client.create(uri, sliceProfile)
95
96         } catch (BpmnError e) {
97             throw e
98         } catch (Exception ex) {
99             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
100             logger.info(msg)
101             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
102         }
103     }
104
105
106     void createServiceInstance(DelegateExecution execution) {
107
108         String serviceRole = "nssi"
109         String serviceType = execution.getVariable("subscriptionServiceType")
110         String ssInstanceId = execution.getVariable("sliceServiceInstanceId")
111         String sliceProfileStr = execution.getVariable("sliceProfile")
112         try {
113             org.onap.aai.domain.yang.ServiceInstance ss = new org.onap.aai.domain.yang.ServiceInstance()
114             ss.setServiceInstanceId(ssInstanceId)
115             String sliceInstanceName = execution.getVariable("sliceServiceInstanceName")
116             if (isBlank(sliceInstanceName)) {
117                 logger.error("ERROR: createServiceInstance: sliceInstanceName is null")
118                 sliceInstanceName = ssInstanceId
119             }
120             ss.setServiceInstanceName(sliceInstanceName)
121             ss.setServiceType(serviceType)
122             String serviceStatus = "deactivated"
123             ss.setOrchestrationStatus(serviceStatus)
124             String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
125             String modelUuid = execution.getVariable("modelUuid")
126             ss.setModelInvariantId(modelInvariantUuid)
127             ss.setModelVersionId(modelUuid)
128             String serviceInstanceLocationid = tnNssmfUtils.getFirstPlmnIdFromSliceProfile(sliceProfileStr)
129             ss.setServiceInstanceLocationId(serviceInstanceLocationid)
130             String snssai = tnNssmfUtils.getFirstSnssaiFromSliceProfile(sliceProfileStr)
131             //ss.setEnvironmentContext(snssai)
132             ss.setEnvironmentContext("tn")
133             ss.setServiceRole(serviceRole)
134             AAIResourcesClient client = getAAIClient()
135             AAIResourceUri uri =
136                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
137                             .customer(execution.getVariable("globalSubscriberId"))
138                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
139                             .serviceInstance(ssInstanceId))
140             client.create(uri, ss)
141         } catch (BpmnError e) {
142             throw e
143         } catch (Exception ex) {
144             String msg = "Exception in DoCreateTnNssiInstance.createServiceInstance: " + ex.getMessage()
145             logger.error(msg)
146             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
147         }
148     }
149
150
151     void createAllottedResource(DelegateExecution execution) {
152         String ssInstanceId = execution.getVariable('sliceServiceInstanceId')
153
154         try {
155             List<String> networkStrList = jsonUtil.StringArrayToList(execution.getVariable("transportSliceNetworks"))
156
157             for (String networkStr : networkStrList) {
158                 String allottedResourceId = UUID.randomUUID().toString()
159                 AAIResourceUri allottedResourceUri =
160                         AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
161                                 .customer(execution.getVariable("globalSubscriberId"))
162                                 .serviceSubscription(execution.getVariable("subscriptionServiceType"))
163                                 .serviceInstance(execution.getVariable("sliceServiceInstanceId"))
164                                 .allottedResource(allottedResourceId))
165                 execution.setVariable("allottedResourceUri", allottedResourceUri)
166                 String modelInvariantId = execution.getVariable("modelInvariantUuid")
167                 String modelVersionId = execution.getVariable("modelUuid")
168
169                 org.onap.aai.domain.yang.AllottedResource resource = new org.onap.aai.domain.yang.AllottedResource()
170                 resource.setId(allottedResourceId)
171                 resource.setType("TsciNetwork")
172                 resource.setAllottedResourceName("network_" + execution.getVariable("sliceServiceInstanceName"))
173                 getAAIClient().create(allottedResourceUri, resource)
174
175                 createNetworkPolicyForAllocatedResource(execution, ssInstanceId, allottedResourceId)
176
177                 String linkArrayStr = jsonUtil.getJsonValue(networkStr, "connectionLinks")
178                 createLogicalLinksForAllocatedResource(execution, linkArrayStr, ssInstanceId, allottedResourceId)
179             }
180         } catch (BpmnError e) {
181             throw e
182         } catch (Exception ex) {
183             String msg = "Exception in DoCreateTnNssiInstance.createAllottedResource: " + ex.getMessage()
184             logger.error(msg)
185             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
186         }
187     }
188
189     void createNetworkPolicy(DelegateExecution execution, String ssInstanceId, String networkPolicyId) {
190         try {
191
192             String sliceProfileStr = execution.getVariable("sliceProfile")
193             if (sliceProfileStr == null || sliceProfileStr.isEmpty()) {
194                 String msg = "ERROR: createNetworkPolicy: sliceProfile is null"
195                 logger.error(msg)
196                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
197             }
198
199             NetworkPolicy networkPolicy = new NetworkPolicy();
200             networkPolicy.setNetworkPolicyId(networkPolicyId)
201             networkPolicy.setName("TSCi policy")
202             networkPolicy.setType("SLA")
203             networkPolicy.setNetworkPolicyFqdn(ssInstanceId)
204
205             String latencyStr = jsonUtil.getJsonValue(sliceProfileStr, "latency")
206             if (latencyStr != null && !latencyStr.isEmpty()) {
207                 networkPolicy.setLatency(Integer.parseInt(latencyStr))
208             }
209
210             String bwStr = jsonUtil.getJsonValue(sliceProfileStr, "maxBandwidth")
211             if (bwStr != null && !bwStr.isEmpty()) {
212                 networkPolicy.setMaxBandwidth(Integer.parseInt(bwStr))
213             } else {
214                 log.debug("ERROR: createNetworkPolicy: maxBandwidth is null")
215             }
216
217             //networkPolicy.setReliability(new Object())
218
219             AAIResourceUri networkPolicyUri =
220                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().networkPolicy(networkPolicyId))
221             getAAIClient().create(networkPolicyUri, networkPolicy)
222
223         } catch (BpmnError e) {
224             throw e
225         } catch (Exception ex) {
226             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
227             logger.error(msg)
228             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
229         }
230     }
231
232     void createNetworkPolicyForAllocatedResource(DelegateExecution execution,
233                                                  String ssInstanceId,
234                                                  String allottedResourceId) {
235         try {
236             AAIResourceUri allottedResourceUri =
237                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
238                             .customer(execution.getVariable("globalSubscriberId"))
239                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
240                             .serviceInstance(ssInstanceId)
241                             .allottedResource(allottedResourceId))
242
243             if (!getAAIClient().exists(allottedResourceUri)) {
244                 logger.info("ERROR: createLogicalLinksForAllocatedResource: allottedResource not exist: uri={}",
245                         allottedResourceUri)
246                 return
247             }
248
249             String networkPolicyId = UUID.randomUUID().toString()
250             createNetworkPolicy(execution, ssInstanceId, networkPolicyId)
251
252             tnNssmfUtils.attachNetworkPolicyToAllottedResource(execution, AAI_VERSION, allottedResourceUri, networkPolicyId);
253
254         } catch (BpmnError e) {
255             throw e
256         } catch (Exception ex) {
257             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
258             logger.error(msg)
259             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
260         }
261     }
262
263     void createLogicalLinksForAllocatedResource(DelegateExecution execution,
264                                                 String linkArrayStr, String ssInstanceId,
265                                                 String allottedResourceId) {
266         try {
267             AAIResourceUri allottedResourceUri =
268                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
269                             .customer(execution.getVariable("globalSubscriberId"))
270                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
271                             .serviceInstance(ssInstanceId)
272                             .allottedResource(allottedResourceId))
273
274             if (!getAAIClient().exists(allottedResourceUri)) {
275                 logger.info("ERROR: createLogicalLinksForAllocatedResource: allottedResource not exist: uri={}",
276                         allottedResourceUri)
277                 return
278             }
279
280             List<String> linkStrList = jsonUtil.StringArrayToList(linkArrayStr)
281
282             for (String linkStr : linkStrList) {
283                 String logicalLinkId = UUID.randomUUID().toString()
284                 String epA = jsonUtil.getJsonValue(linkStr, "transportEndpointA")
285                 String epB = jsonUtil.getJsonValue(linkStr, "transportEndpointB")
286                 String modelInvariantId = execution.getVariable("modelInvariantUuid")
287                 String modelVersionId = execution.getVariable("modelUuid")
288
289                 org.onap.aai.domain.yang.LogicalLink resource = new org.onap.aai.domain.yang.LogicalLink()
290                 resource.setLinkId(logicalLinkId)
291                 resource.setLinkName(epA)
292                 resource.setLinkName2(epB)
293                 resource.setLinkType("TsciConnectionLink")
294                 resource.setInMaint(false)
295
296                 //epA is link-name
297                 AAIResourceUri logicalLinkUri =
298                         AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().logicalLink(epA))
299                 getAAIClient().create(logicalLinkUri, resource)
300
301                 tnNssmfUtils.attachLogicalLinkToAllottedResource(execution, AAI_VERSION, allottedResourceUri, epA);
302             }
303         } catch (BpmnError e) {
304             throw e
305         } catch (Exception ex) {
306             String msg = "Exception in DoCreateTnNssiInstance.createLogicalLinksForAllocatedResource: " + ex.getMessage()
307             logger.error(msg)
308             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
309         }
310     }
311
312     void preprocessSdncAllocateTnNssiRequest(DelegateExecution execution) {
313         def method = getClass().getSimpleName() + '.preProcessSDNCActivateRequest(' +
314                 'execution=' + execution.getId() +
315                 ')'
316         def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
317         logger.trace('Entered ' + method)
318
319         logger.trace("STARTED preProcessSDNCActivateRequest Process")
320         try {
321             String serviceInstanceId = execution.getVariable("sliceServiceInstanceId")
322
323             String createSDNCRequest = tnNssmfUtils.buildSDNCRequest(execution, serviceInstanceId, "create")
324
325             execution.setVariable("TNNSSMF_SDNCRequest", createSDNCRequest)
326             logger.debug("Outgoing SDNCRequest is: \n" + createSDNCRequest)
327
328         } catch (Exception e) {
329             logger.debug("Exception Occured Processing preProcessSDNCActivateRequest. Exception is:\n" + e)
330             exceptionUtil.buildAndThrowWorkflowException(execution, 1002,
331                     "Error Occured during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
332         }
333         logger.trace("COMPLETED  preProcessSDNCActivateRequest Process")
334     }
335
336
337     void validateSDNCResponse(DelegateExecution execution, String response, String method) {
338         tnNssmfUtils.validateSDNCResponse(execution, response, method)
339     }
340 }