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