45af88d25359f655edbd03ca5ae215be86cde593
[so.git] /
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 com.fasterxml.jackson.databind.ObjectMapper
24 import groovy.json.JsonSlurper
25 import org.camunda.bpm.engine.delegate.BpmnError
26 import org.camunda.bpm.engine.delegate.DelegateExecution
27 import org.onap.aai.domain.yang.ServiceInstance
28 import org.onap.aai.domain.yang.SliceProfile
29 import org.onap.aaiclient.client.aai.AAIObjectType
30 import org.onap.aaiclient.client.aai.AAIResourcesClient
31 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
32 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
33 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
34 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
35 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
36 import org.onap.so.bpmn.common.scripts.ExceptionUtil
37 import org.onap.so.bpmn.common.scripts.RequestDBUtil
38 import org.onap.so.bpmn.core.json.JsonUtils
39 import org.onap.so.db.request.beans.ResourceOperationStatus
40 import org.slf4j.Logger
41 import org.slf4j.LoggerFactory
42
43 import static org.apache.commons.lang3.StringUtils.isBlank
44
45 public class DoModifyTnNssi extends AbstractServiceTaskProcessor {
46     String Prefix = "TNMOD_"
47
48     ExceptionUtil exceptionUtil = new ExceptionUtil()
49     JsonUtils jsonUtil = new JsonUtils()
50     RequestDBUtil requestDBUtil = new RequestDBUtil()
51     TnNssmfUtils tnNssmfUtils = new TnNssmfUtils()
52     JsonSlurper jsonSlurper = new JsonSlurper()
53     ObjectMapper objectMapper = new ObjectMapper()
54     private static final Logger logger = LoggerFactory.getLogger(DoModifyTnNssi.class)
55
56
57     void preProcessRequest(DelegateExecution execution) {
58         logger.debug("Start preProcessRequest")
59         execution.setVariable("prefix", Prefix)
60         String msg = ""
61
62         try {
63             execution.setVariable("startTime", System.currentTimeMillis())
64             msg = tnNssmfUtils.getExecutionInputParams(execution)
65             logger.debug("Modify TN NSSI input parameters: " + msg)
66
67             execution.setVariable("prefix", Prefix)
68
69             tnNssmfUtils.setSdncCallbackUrl(execution, true)
70             logger.debug("SDNC Callback URL: " + execution.getVariable("sdncCallbackUrl"))
71
72             String additionalPropJsonStr = execution.getVariable("sliceParams")
73
74             String sliceServiceInstanceId = execution.getVariable("serviceInstanceID")
75             execution.setVariable("sliceServiceInstanceId", sliceServiceInstanceId)
76
77             String sliceServiceInstanceName = execution.getVariable("servicename")
78             execution.setVariable("sliceServiceInstanceName", sliceServiceInstanceName)
79
80             String operationId = UUID.randomUUID().toString()
81             execution.setVariable("operationId", operationId)
82
83             String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
84             String modelUuid = execution.getVariable("modelUuid")
85             //here modelVersion is not set, we use modelUuid to decompose the service.
86             def isDebugLogEnabled = true
87             execution.setVariable("isDebugLogEnabled", isDebugLogEnabled)
88             String serviceModelInfo = """{
89             "modelInvariantUuid":"${modelInvariantUuid}",
90             "modelUuid":"${modelUuid}",
91             "modelVersion":""
92              }"""
93             execution.setVariable("serviceModelInfo", serviceModelInfo)
94
95             //additional properties
96             String sliceProfile = jsonUtil.getJsonValue(additionalPropJsonStr, "sliceProfile")
97             if (isBlank(sliceProfile)) {
98                 msg = "Input sliceProfile is null"
99                 logger.debug(msg)
100                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
101             } else {
102                 execution.setVariable("sliceProfile", sliceProfile)
103             }
104
105             String transportSliceNetworks = jsonUtil.getJsonValue(additionalPropJsonStr, "transportSliceNetworks")
106             if (isBlank(transportSliceNetworks)) {
107                 msg = "Input transportSliceNetworks is null"
108                 logger.debug(msg)
109                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
110             } else {
111                 execution.setVariable("transportSliceNetworks", transportSliceNetworks)
112             }
113             logger.debug("transportSliceNetworks: " + transportSliceNetworks)
114
115             String nsiInfo = jsonUtil.getJsonValue(additionalPropJsonStr, "nsiInfo")
116             if (isBlank(nsiInfo)) {
117                 msg = "Input nsiInfo is null"
118                 logger.debug(msg)
119                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
120             } else {
121                 execution.setVariable("nsiInfo", nsiInfo)
122             }
123
124             if (isBlank(tnNssmfUtils.setExecVarFromJsonIfExists(execution, additionalPropJsonStr,
125                     "enableSdnc", "enableSdnc"))) {
126                 tnNssmfUtils.setEnableSdncConfig(execution)
127             }
128         } catch (BpmnError e) {
129             throw e
130         } catch (Exception ex) {
131             msg = "Exception in preProcessRequest " + ex.getMessage()
132             logger.debug(msg)
133             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
134         }
135         logger.debug("Finish preProcessRequest")
136     }
137
138
139     void deleteServiceInstance(DelegateExecution execution) {
140         try {
141             AAIResourcesClient client = getAAIClient()
142             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(execution.getVariable("globalSubscriberId")).serviceSubscription(execution.getVariable("subscriptionServiceType")).serviceInstance(execution.getVariable("serviceInstanceID")))
143             client.delete(uri)
144         } catch (BpmnError e) {
145             throw e
146         } catch (Exception ex) {
147             String msg = "Exception in DoDeallocateTnNssi.deleteServiceInstance. " + ex.getMessage()
148             logger.info(msg)
149             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
150         }
151     }
152
153
154     void getExistingServiceInstance(DelegateExecution execution) {
155         String serviceInstanceId = execution.getVariable("serviceInstanceID")
156
157         AAIResourcesClient resourceClient = getAAIClient()
158         AAIResourceUri ssServiceuri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(serviceInstanceId))
159
160         try {
161             if (resourceClient.exists(ssServiceuri)) {
162                 execution.setVariable("ssi_resourceLink", ssServiceuri.build().toString())
163                 org.onap.aai.domain.yang.ServiceInstance ss =
164                         resourceClient.get(org.onap.aai.domain.yang.ServiceInstance.class, ssServiceuri)
165                 org.onap.aai.domain.yang.SliceProfile sliceProfile = ss.getSliceProfiles().getSliceProfile().get(0)
166                 execution.setVariable("sliceProfileId", sliceProfile.getProfileId())
167
168                 org.onap.aai.domain.yang.AllottedResources ars = ss.getAllottedResources()
169                 List<org.onap.aai.domain.yang.AllottedResource> arList = ars.getAllottedResource()
170                 List<String> arIdList = new ArrayList<>()
171                 for (org.onap.aai.domain.yang.AllottedResource ar : arList) {
172                     String arId = ar.getId()
173                     arIdList.add(arId)
174                 }
175                 execution.setVariable("arIdList", arIdList)
176             } else {
177                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service instance was not found in aai to " +
178                         "associate allotted resource for service :" + serviceInstanceId)
179             }
180         } catch (BpmnError e) {
181             throw e;
182         } catch (Exception ex) {
183             String msg = "Exception in getServiceInstance. " + ex.getMessage()
184             logger.debug(msg)
185             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
186         }
187
188     }
189
190     public void updateTnNssiInAAI(DelegateExecution execution) {
191         getExistingServiceInstance(execution)
192
193         updateServiceInstance(execution)
194         updateSliceProfile(execution)
195         updateAllottedResource(execution)
196     }
197
198     void updateServiceInstance(DelegateExecution execution) {
199         String serviceRole = "TN"
200         String serviceType = execution.getVariable("subscriptionServiceType")
201         String sliceProfileStr = execution.getVariable("sliceProfile")
202         String ssInstanceId = execution.getVariable("sliceServiceInstanceId")
203         try {
204             org.onap.aai.domain.yang.ServiceInstance ss = new org.onap.aai.domain.yang.ServiceInstance()
205             ss.setServiceInstanceId(ssInstanceId)
206             String sliceInstanceName = execution.getVariable("sliceServiceInstanceName")
207             ss.setServiceInstanceName(sliceInstanceName)
208             ss.setServiceType(serviceType)
209             String serviceStatus = "modified"
210             ss.setOrchestrationStatus(serviceStatus)
211             String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
212             String modelUuid = execution.getVariable("modelUuid")
213             ss.setModelInvariantId(modelInvariantUuid)
214             ss.setModelVersionId(modelUuid)
215             String serviceInstanceLocationid = tnNssmfUtils.getFirstPlmnIdFromSliceProfile(sliceProfileStr)
216             ss.setServiceInstanceLocationId(serviceInstanceLocationid)
217             String snssai = tnNssmfUtils.getFirstSnssaiFromSliceProfile(sliceProfileStr)
218             ss.setEnvironmentContext(snssai)
219             ss.setServiceRole(serviceRole)
220             AAIResourcesClient client = getAAIClient()
221             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(execution.getVariable("globalSubscriberId")).serviceSubscription(execution.getVariable("subscriptionServiceType")).serviceInstance(ssInstanceId))
222             client.update(uri, ss)
223         } catch (BpmnError e) {
224             throw e
225         } catch (Exception ex) {
226             String msg = "Exception in DoCreateTnNssiInstance.createServiceInstance. " + ex.getMessage()
227             logger.info(msg)
228             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
229         }
230     }
231
232     void updateSliceProfile(DelegateExecution execution) {
233
234         String sliceserviceInstanceId = execution.getVariable("sliceServiceInstanceId")
235         String sliceProfileStr = execution.getVariable("sliceProfile")
236         String sliceProfileId = execution.getVariable("sliceProfileId")
237         SliceProfile sliceProfile = new SliceProfile();
238         sliceProfile.setProfileId(sliceProfileId)
239         sliceProfile.setLatency(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "latency")))
240         sliceProfile.setResourceSharingLevel(jsonUtil.getJsonValue(sliceProfileStr, "resourceSharingLevel"))
241         sliceProfile.setSNssai(tnNssmfUtils.getFirstSnssaiFromSliceProfile(sliceProfileStr))    //TODO: should be list
242
243         sliceProfile.setE2ELatency(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "latency")))
244         sliceProfile.setMaxBandwidth(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "maxBandwidth")))
245
246         //TODO: new API
247         sliceProfile.setReliability(new Object())
248         try {
249             AAIResourcesClient client = getAAIClient()
250             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(execution.getVariable("globalSubscriberId")).serviceSubscription(execution.getVariable("subscriptionServiceType")).serviceInstance(sliceserviceInstanceId).sliceProfile(sliceProfileId))
251             client.update(uri, sliceProfile)
252
253         } catch (BpmnError e) {
254             throw e
255         } catch (Exception ex) {
256             String msg = "Exception in updateSliceProfile. " + ex.getMessage()
257             logger.info(msg)
258             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
259         }
260     }
261
262     void updateAllottedResource(DelegateExecution execution) {
263         String serviceInstanceId = execution.getVariable('serviceInstanceID')
264
265         List<String> arIdList = execution.getVariable("arIdList")
266         try {
267             for (String arId : arIdList) {
268                 AAIResourceUri arUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(execution.getVariable("globalSubscriberId")).serviceSubscription(execution.getVariable("subscriptionServiceType")).serviceInstance(serviceInstanceId).allottedResource(arId))
269
270                 getAAIClient().delete(arUri)
271             }
272
273             List<String> networkStrList = jsonUtil.StringArrayToList(execution.getVariable("transportSliceNetworks"))
274
275             for (String networkStr : networkStrList) {
276                 String allottedResourceId = UUID.randomUUID().toString()
277                 AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(execution.getVariable("globalSubscriberId")).serviceSubscription(execution.getVariable("subscriptionServiceType")).serviceInstance(execution.getVariable("sliceserviceInstanceId")).allottedResource(allottedResourceId))
278                 execution.setVariable("allottedResourceUri", allottedResourceUri)
279                 String modelInvariantId = execution.getVariable("modelInvariantUuid")
280                 String modelVersionId = execution.getVariable("modelUuid")
281
282                 org.onap.aai.domain.yang.AllottedResource resource = new org.onap.aai.domain.yang.AllottedResource()
283                 resource.setId(allottedResourceId)
284                 resource.setType("TsciNetwork")
285                 resource.setAllottedResourceName("network_" + execution.getVariable("sliceServiceInstanceName"))
286                 resource.setModelInvariantId(modelInvariantId)
287                 resource.setModelVersionId(modelVersionId)
288                 getAAIClient().create(allottedResourceUri, resource)
289
290                 String linkArrayStr = jsonUtil.getJsonValue(networkStr, "connectionLinks")
291                 createLogicalLinksForAllocatedResource(execution, linkArrayStr, serviceInstanceId, allottedResourceId)
292             }
293
294         } catch (Exception ex) {
295             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Exception in createAaiAR " + ex.getMessage())
296         }
297     }
298
299     void createLogicalLinksForAllocatedResource(DelegateExecution execution,
300                                                 String linkArrayStr, String serviceInstanceId,
301                                                 String allottedResourceId) {
302
303         try {
304             List<String> linkStrList = jsonUtil.StringArrayToList(linkArrayStr)
305
306             for (String linkStr : linkStrList) {
307                 String logicalLinkId = UUID.randomUUID().toString()
308                 String epA = jsonUtil.getJsonValue(linkStr, "transportEndpointA")
309                 String epB = jsonUtil.getJsonValue(linkStr, "transportEndpointB")
310                 String modelInvariantId = execution.getVariable("modelInvariantUuid")
311                 String modelVersionId = execution.getVariable("modelUuid")
312
313                 org.onap.aai.domain.yang.LogicalLink resource = new org.onap.aai.domain.yang.LogicalLink()
314                 resource.setLinkId(logicalLinkId)
315                 resource.setLinkName(epA)
316                 resource.setLinkName2(epB)
317                 resource.setModelInvariantId(modelInvariantId)
318                 resource.setModelVersionId(modelVersionId)
319
320                 AAIResourceUri logicalLinkUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().logicalLink(logicalLinkId))
321                 getAAIClient().create(logicalLinkUri, resource)
322             }
323         } catch (Exception ex) {
324             exceptionUtil.buildAndThrowWorkflowException(execution, 7000,
325                     "Exception in createLogicalLinksForAllocatedResource" + ex.getMessage())
326         }
327     }
328
329
330     void preprocessSdncModifyTnNssiRequest(DelegateExecution execution) {
331         def method = getClass().getSimpleName() + '.preprocessSdncModifyTnNssiRequest(' +
332                 'execution=' + execution.getId() + ')'
333         def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
334         logger.trace('Entered ' + method)
335
336         try {
337             String serviceInstanceId = execution.getVariable("serviceInstanceID")
338
339             String sdncRequest = tnNssmfUtils.buildSDNCRequest(execution, serviceInstanceId, "modify")
340
341             execution.setVariable("TNNSSMF_SDNCRequest", sdncRequest)
342             logger.debug("Outgoing SDNCRequest is: \n" + sdncRequest)
343
344         } catch (Exception e) {
345             logger.debug("Exception Occured Processing preprocessSdncModifyTnNssiRequest. Exception is:\n" + e)
346             exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
347         }
348         logger.trace("COMPLETED preprocessSdncModifyTnNssiRequest Process")
349     }
350
351
352     void validateSDNCResponse(DelegateExecution execution, String response, String method) {
353         tnNssmfUtils.validateSDNCResponse(execution, response, method)
354     }
355
356
357     void updateAAIOrchStatus(DelegateExecution execution) {
358         logger.debug("Start updateAAIOrchStatus")
359         String sliceServiceInstanceId = execution.getVariable("sliceServiceInstanceId")
360         String orchStatus = execution.getVariable("orchestrationStatus")
361
362         try {
363             ServiceInstance si = new ServiceInstance()
364             si.setOrchestrationStatus(orchStatus)
365             AAIResourcesClient client = getAAIClient()
366             AAIResourceUri uri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(sliceServiceInstanceId))
367             client.update(uri, si)
368         } catch (BpmnError e) {
369             throw e
370         } catch (Exception ex) {
371             String msg = "Exception in CreateSliceService.updateAAIOrchStatus " + ex.getMessage()
372             logger.info(msg)
373             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
374         }
375
376         logger.debug("Finish updateAAIOrchStatus")
377     }
378
379     void prepareUpdateJobStatus(DelegateExecution execution,
380                                 String status,
381                                 String progress,
382                                 String statusDescription) {
383         String serviceId = execution.getVariable("serviceInstanceID")
384         String jobId = execution.getVariable("jobId")
385         String nsiId = execution.getVariable("nsiId")
386
387         ResourceOperationStatus roStatus = new ResourceOperationStatus()
388         roStatus.setServiceId(serviceId)
389         roStatus.setOperationId(jobId)
390         roStatus.setResourceTemplateUUID(nsiId)
391         roStatus.setOperType("Modify")
392         roStatus.setProgress(progress)
393         roStatus.setStatus(status)
394         roStatus.setStatusDescription(statusDescription)
395         requestDBUtil.prepareUpdateResourceOperationStatus(execution, roStatus)
396     }
397
398 }
399