36d579c7ab5688784048bcd51fde8637548caf27
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019 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 static org.apache.commons.lang3.StringUtils.isBlank
24 import java.lang.reflect.Type
25 import javax.ws.rs.NotFoundException
26 import org.camunda.bpm.engine.delegate.BpmnError
27 import org.camunda.bpm.engine.delegate.DelegateExecution
28 import org.onap.aai.domain.yang.*
29 import org.onap.aaiclient.client.aai.AAIResourcesClient
30 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
31 import org.onap.aaiclient.client.aai.entities.uri.AAIPluralResourceUri
32 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
33 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
34 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
35 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
36 import org.onap.logging.filter.base.ErrorCode
37 import org.onap.so.beans.nsmf.NSSI
38 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
39 import org.onap.so.bpmn.common.scripts.ExceptionUtil
40 import org.onap.so.bpmn.common.scripts.MsoUtils
41 import org.onap.so.bpmn.common.scripts.RequestDBUtil
42 import org.onap.so.bpmn.core.WorkflowException
43 import org.onap.so.bpmn.core.json.JsonUtils
44 import org.onap.so.db.request.beans.OperationStatus
45 import org.onap.so.logger.LoggingAnchor
46 import org.onap.so.logger.MessageEnum
47 import org.slf4j.Logger
48 import org.slf4j.LoggerFactory
49 import com.google.gson.Gson
50 import com.google.gson.reflect.TypeToken
51
52 /**
53  * This groovy class supports the <class>ActivateSliceService.bpmn</class> process.
54  * AlaCarte flow for 1702 slice service activate
55  *
56  */
57
58 class ActivateSliceService extends AbstractServiceTaskProcessor {
59
60
61     String Prefix = "ACTSS_"
62
63     ExceptionUtil exceptionUtil = new ExceptionUtil()
64
65     JsonUtils jsonUtil = new JsonUtils()
66
67     RequestDBUtil requestDBUtil = new RequestDBUtil()
68
69     private static final Logger logger = LoggerFactory.getLogger(ActivateSliceService.class)
70
71     void preProcessRequest(DelegateExecution execution) {
72         logger.debug(Prefix + "preProcessRequest Start")
73         execution.setVariable("prefix", Prefix)
74         String msg
75
76         try {
77             // check for incoming json message/input
78             String siRequest = execution.getVariable("bpmnRequest")
79             logger.debug(siRequest)
80
81             String requestId = execution.getVariable("mso-request-id")
82             execution.setVariable("msoRequestId", requestId)
83             logger.info("Input Request:" + siRequest + " reqId:" + requestId)
84
85             String serviceInstanceId = execution.getVariable("serviceInstanceId")
86             if (isBlank(serviceInstanceId)) {
87                 msg = "Input serviceInstanceId' is null"
88                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
89             }
90             String source = jsonUtil.getJsonValue(siRequest, "source")
91             execution.setVariable("source", source)
92
93             //subscriberInfo
94             String globalSubscriberId = jsonUtil.getJsonValue(siRequest, "globalSubscriberId")
95             if (isBlank(globalSubscriberId)) {
96                 msg = "Input globalSubscriberId' is null"
97                 logger.info(msg)
98                 execution.setVariable("globalSubscriberId", "5GCustomer")
99             } else {
100                 execution.setVariable("globalSubscriberId", globalSubscriberId)
101             }
102
103             //requestParameters
104             String subscriptionServiceType = jsonUtil.getJsonValue(siRequest, "serviceType")
105             if (isBlank(subscriptionServiceType)) {
106                 msg = "Input subscriptionServiceType is null"
107                 logger.debug(msg)
108                 execution.setVariable("subscriptionServiceType", "5G")
109             } else {
110                 execution.setVariable("subscriptionServiceType", subscriptionServiceType)
111             }
112             String operationId = jsonUtil.getJsonValue(siRequest, "operationId")
113             execution.setVariable("operationId", operationId)
114
115             String operationType = execution.getVariable("operationType")
116             execution.setVariable("operationType", operationType.toUpperCase())
117
118             logger.info("operationType is " + execution.getVariable("operationType") )
119         } catch (BpmnError e) {
120             throw e
121         } catch (Exception ex) {
122             msg = "Exception in preProcessRequest " + ex.getMessage()
123             logger.info(msg)
124             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
125         }
126         logger.debug(Prefix + "preProcessRequest Exit")
127     }
128
129
130     def sendSyncResponse = { DelegateExecution execution ->
131         logger.debug(Prefix + "sendSyncResponse Start")
132         try {
133             String operationId = execution.getVariable("operationId")
134             // RESTResponse for API Handler (APIH) Reply Task
135             String Activate5GsliceServiceRestRequest = """{"operationId":"${operationId}"}""".trim()
136             logger.debug(" sendSyncResponse to APIH:" + "\n" + Activate5GsliceServiceRestRequest)
137             sendWorkflowResponse(execution, 202, Activate5GsliceServiceRestRequest)
138             execution.setVariable("sentSyncResponse", true)
139         } catch (Exception ex) {
140             String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
141             logger.debug(msg)
142             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
143         }
144         logger.debug(Prefix + "sendSyncResponse Exit")
145     }
146
147
148     public sendSyncError = { DelegateExecution execution ->
149         logger.debug("sendSyncError Start")
150         try {
151             String errorMessage
152             if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
153                 WorkflowException wfe = execution.getVariable("WorkflowException") as WorkflowException
154                 errorMessage = wfe.getErrorMessage()
155             } else {
156                 errorMessage = "Sending Sync Error."
157             }
158
159             String buildWorkflowException =
160                     """<aetgt:WorkflowException xmlns:aetgt="http://org.onap/so/workflow/schema/v1">
161                                         <aetgt:ErrorMessage>${MsoUtils.xmlEscape(errorMessage)}</aetgt:ErrorMessage>
162                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
163                                    </aetgt:WorkflowException>"""
164
165             logger.debug(buildWorkflowException)
166             sendWorkflowResponse(execution, 500, buildWorkflowException)
167
168         } catch (Exception ex) {
169             logger.debug("Sending Sync Error Activity Failed. " + "\n" + ex.getMessage())
170         }
171         logger.debug(Prefix + "sendSyncError Exit")
172     }
173
174
175     def prepareCompletionRequest = { DelegateExecution execution ->
176         logger.debug(Prefix + "prepareCompletionRequest Start")
177         String serviceId = execution.getVariable("serviceInstanceId")
178         String operationId = execution.getVariable("operationId")
179         String userId = execution.getVariable("globalSubscriberId")
180         //String result = execution.getVariable("result")
181         String result = "finished"
182         String progress = "100"
183         String reason = ""
184         String operationContent = execution.getVariable("operationContent")
185         String operationType = execution.getVariable("operationType")
186
187         OperationStatus initStatus = new OperationStatus()
188         initStatus.setServiceId(serviceId)
189         initStatus.setOperationId(operationId)
190         initStatus.setOperation(operationType)
191         initStatus.setUserId(userId)
192         initStatus.setResult(result)
193         initStatus.setProgress(progress)
194         initStatus.setReason(reason)
195         initStatus.setOperationContent(operationContent)
196
197         requestDBUtil.prepareUpdateOperationStatus(execution, initStatus)
198
199         logger.debug(Prefix + "prepareCompletionRequest Exit")
200     }
201
202
203     /**
204      * Init the service Operation Status
205      */
206     def prepareInitServiceOperationStatus = { DelegateExecution execution ->
207         logger.debug(Prefix + "prepareActivateServiceOperationStatus Start")
208         try {
209             String serviceId = execution.getVariable("serviceInstanceId")
210             String operationId = execution.getVariable("operationId")
211             String operationType = execution.getVariable("operationType")
212             String userId = execution.getVariable("globalSubscriberId")
213             String result = "processing"
214             String progress = "0"
215             String reason = ""
216             String operationContent = "Prepare service activation"
217
218             execution.setVariable("e2eserviceInstanceId", serviceId)
219             execution.setVariable("operationType", operationType)
220
221             OperationStatus initStatus = new OperationStatus()
222             initStatus.setServiceId(serviceId)
223             initStatus.setOperationId(operationId)
224             initStatus.setOperation(operationType)
225             initStatus.setUserId(userId)
226             initStatus.setResult(result)
227             initStatus.setProgress(progress)
228             initStatus.setReason(reason)
229             initStatus.setOperationContent(operationContent)
230
231             requestDBUtil.prepareUpdateOperationStatus(execution, initStatus)
232
233         } catch (Exception e) {
234             logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
235                     "Exception Occured Processing prepareInitServiceOperationStatus.", "BPMN",
236                     ErrorCode.UnknownError.getValue(), "Exception is:\n" + e)
237             execution.setVariable("CVFMI_ErrorResponse",
238                     "Error Occurred during prepareInitServiceOperationStatus Method:\n" + e.getMessage())
239         }
240         logger.debug(Prefix + "prepareInitServiceOperationStatus Exit")
241     }
242
243
244     private getSNSSIStatusByNsi = { DelegateExecution execution, String NSIServiceId ->
245
246         logger.debug(Prefix + "getSNSSIStatusByNsi Start")
247         String globalSubscriberId = execution.getVariable("globalSubscriberId")
248         String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
249
250         AAIResourcesClient client = new AAIResourcesClient()
251         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(NSIServiceId))
252         if (!client.exists(uri)) {
253             exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service Instance was not found in aai")
254         }
255         AAIResultWrapper wrapper = client.get(uri, NotFoundException.class)
256         Optional<ServiceInstance> si = wrapper.asBean(ServiceInstance.class)
257         if (si.isPresent()) {
258
259             List<Relationship> relatedList = si.get().getRelationshipList().getRelationship()
260             for (Relationship relationship : relatedList) {
261                 String relatedTo = relationship.getRelatedTo()
262                 if (relatedTo.toLowerCase() == "allotted-resource") {
263                     //get snssi from allotted resource in list by nsi
264                     List<String> SNSSIList = new ArrayList<>()
265                     List<RelationshipData> relationshipDataList = relationship.getRelationshipData()
266                     for (RelationshipData relationshipData : relationshipDataList) {
267                         if (relationshipData.getRelationshipKey() == "service-instance.service-instance-id") {
268                             SNSSIList.add(relationshipData.getRelationshipValue())
269                         }
270                     }
271                     for (String snssi : SNSSIList) {
272                         AAIResourcesClient client01 = new AAIResourcesClient()
273                         AAIResourceUri uri01 = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(snssi))
274                         if (!client.exists(uri01)) {
275                             exceptionUtil.buildAndThrowWorkflowException(execution, 2500,
276                                     "Service Instance was not found in aai")
277                         }
278                         AAIResultWrapper wrapper01 = client01.get(uri01, NotFoundException.class)
279                         Optional<ServiceInstance> nssiSi = wrapper01.asBean(ServiceInstance.class)
280                         if (nssiSi.isPresent()) {
281                             return nssiSi.get().getOrchestrationStatus() == "deactivated"
282                         }
283                     }
284
285                 }
286             }
287
288         }
289         logger.debug(Prefix + "getSNSSIStatusByNsi Exit")
290     }
291
292
293     def updateStatusSNSSAIandNSIandNSSI = { DelegateExecution execution ->
294         logger.debug(Prefix + "updateStatusSNSSAIandNSIandNSSI Start")
295         logger.debug(" ***** update SNSSAI NSI NSSI slicing ***** ")
296         String e2eserviceInstanceId = execution.getVariable("e2eserviceInstanceId")
297         String NSIserviceInstanceId = execution.getVariable("NSIserviceid")
298
299         String globalCustId = execution.getVariable("globalSubscriberId")
300         String serviceType = execution.getVariable("serviceType")
301         String operationType = execution.getVariable("operationType")
302
303         String nssiMap = execution.getVariable("nssiMap")
304         Type type = new TypeToken<HashMap<String, NSSI>>() {}.getType()
305         Map<String, NSSI> activateNssiMap = new Gson().fromJson(nssiMap, type)
306         //update tn/cn/an nssi
307         for (Map.Entry<String, NSSI> entry : activateNssiMap.entrySet()) {
308             NSSI nssi = entry.getValue()
309             String nssiid = nssi.getNssiId()
310             updateStratus(execution, globalCustId, serviceType, nssiid, operationType)
311         }
312         if (operationType.equalsIgnoreCase("activation")) {
313             //update the s-nssai
314             updateStratus(execution, globalCustId, serviceType, e2eserviceInstanceId, operationType)
315             //update the nsi
316             updateStratus(execution, globalCustId, serviceType, NSIserviceInstanceId, operationType)
317         } else {
318             //update the s-nssai
319             updateStratus(execution, globalCustId, serviceType, e2eserviceInstanceId, operationType)
320             boolean flag = getSNSSIStatusByNsi(execution, NSIserviceInstanceId)
321             if (flag) {
322                 //update the nsi
323                 updateStratus(execution, globalCustId, serviceType, NSIserviceInstanceId, operationType)
324             } else {
325                 logger.error("Service's status update failed")
326                 String msg = "Service's status update failed"
327                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
328             }
329         }
330         logger.debug(Prefix + "updateStatusSNSSAIandNSIandNSSI Exit")
331     }
332
333
334     def updateStratus = { DelegateExecution execution, String globalCustId,
335                           String serviceType, String serviceId, String operationType ->
336         logger.debug(Prefix + "updateStratus Start")
337
338         try {
339             AAIResourcesClient client = new AAIResourcesClient()
340             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalCustId).serviceSubscription(serviceType).serviceInstance(serviceId))
341             if (!client.exists(uri)) {
342                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service Instance was not found in aai")
343             }
344             AAIResultWrapper wrapper = client.get(uri, NotFoundException.class)
345             Optional<ServiceInstance> si = wrapper.asBean(ServiceInstance.class)
346
347             if (si.isPresent()) {
348                 if (operationType.equalsIgnoreCase("activation")) {
349                     if (si.get().getOrchestrationStatus() == "deactivated") {
350                         si.get().setOrchestrationStatus("activated")
351                         client.update(uri, si.get())
352                     }
353                 } else {
354                     if (si.get().getOrchestrationStatus() == "activated") {
355                         si.get().setOrchestrationStatus("deactivated")
356                         client.update(uri, si.get())
357                     }
358                 }
359
360             }
361         } catch (Exception e) {
362             logger.info("Service is already in active state")
363             String msg = "Service is already in active state, " + e.getMessage()
364             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
365         }
366
367         logger.debug(Prefix + "updateStratus Exit")
368     }
369
370
371     def prepareActivation = { DelegateExecution execution ->
372         logger.debug(Prefix + "prepareActivation Start")
373
374         logger.debug(" ***** prepare active NSI/AN/CN/TN slice ***** ")
375         String NSIserviceInstanceId = execution.getVariable("NSIserviceid")
376
377         String globalSubscriberId = execution.getVariable("globalSubscriberId")
378         String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
379
380         Map<String, NSSI> nssiMap = new HashMap<>()
381
382         List<String> activationSequence = new ArrayList<>(Arrays.asList("an", "tn", "cn"))
383
384         def activationCount = activationSequence.size()
385
386         execution.setVariable("activationIndex", "0")
387
388         execution.setVariable("activationCount", activationCount)
389         try {
390             //get the TN NSSI id by NSI id, active NSSI TN slicing
391             AAIResourcesClient client = new AAIResourcesClient()
392             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(NSIserviceInstanceId))
393             if (!client.exists(uri)) {
394                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service Instance was not found in aai")
395             }
396             AAIResultWrapper wrapper = client.get(uri, NotFoundException.class)
397             Optional<ServiceInstance> si = wrapper.asBean(ServiceInstance.class)
398             if (si.isPresent()) {
399
400                 List<Relationship> relatedList = si.get().getRelationshipList().getRelationship()
401                 for (Relationship relationship : relatedList) {
402                     String relatedTo = relationship.getRelatedTo()
403                     if (relatedTo.toLowerCase() == "service-instance") {
404                         String relatioshipurl = relationship.getRelatedLink()
405                         String nssiserviceid =
406                                 relatioshipurl.substring(relatioshipurl.lastIndexOf("/") + 1, relatioshipurl.length())
407
408                         AAIResourcesClient client01 = new AAIResourcesClient()
409                         AAIResourceUri uri01 = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(nssiserviceid))
410                         if (!client.exists(uri01)) {
411                             exceptionUtil.buildAndThrowWorkflowException(execution, 2500,
412                                     "Service Instance was not found in aai")
413                         }
414                         AAIResultWrapper wrapper01 = client01.get(uri01, NotFoundException.class)
415                         Optional<ServiceInstance> nssiSi = wrapper01.asBean(ServiceInstance.class)
416                         if (nssiSi.isPresent()) {
417                             if (nssiSi.get().getEnvironmentContext().toLowerCase().contains("an")
418                                     || nssiSi.get().getEnvironmentContext().toLowerCase().contains("cn")
419                                     || nssiSi.get().getEnvironmentContext().toLowerCase().contains("tn")) {
420                                 nssiMap.put(nssiSi.get().getEnvironmentContext(),
421                                         new NSSI(nssiSi.get().getServiceInstanceId(),
422                                                 nssiSi.get().getModelInvariantId(), nssiSi.get().getModelVersionId()))
423                             }
424                         }
425                     }
426                 }
427
428
429             }
430         } catch (Exception e) {
431             String msg = "Requested service does not exist:" + e.getMessage()
432             logger.info("Service doesnt exist")
433             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
434         }
435
436         if (nssiMap.size() > 0) {
437             execution.setVariable("isNSSIActivate", "true")
438             String nssiMap01 = mapToJsonStr(nssiMap)
439             execution.setVariable("nssiMap", nssiMap01)
440             execution.setVariable("operation_type", "activate")
441             execution.setVariable("activationCount", nssiMap.size())
442             logger.info("the nssiMap01 is :" + nssiMap01)
443         } else {
444             execution.setVariable("isNSSIActivate", "false")
445         }
446
447         logger.debug(Prefix + "prepareActivation Exit")
448     }
449
450
451     private mapToJsonStr = { HashMap<String, NSSI> stringNSSIHashMap ->
452         HashMap<String, NSSI> map = new HashMap<String, NSSI>()
453         for (Map.Entry<String, NSSI> child : stringNSSIHashMap.entrySet()) {
454             map.put(child.getKey(), child.getValue())
455         }
456         return new Gson().toJson(map)
457     }
458
459
460     def checkAAIOrchStatusofslice = { DelegateExecution execution ->
461         logger.debug(Prefix + "CheckAAIOrchStatus Start")
462
463         String msg = ""
464         String serviceInstanceId = execution.getVariable("serviceInstanceId")
465         String globalSubscriberId = execution.getVariable("globalSubscriberId")
466         String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
467         String operationType = execution.getVariable("operationType")
468
469         logger.debug("serviceInstanceId: " + serviceInstanceId)
470
471         //check the e2e slice status
472         try {
473             try {
474                 AAIResourcesClient client = new AAIResourcesClient()
475                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(serviceInstanceId))
476                 if (!client.exists(uri)) {
477                     exceptionUtil.buildAndThrowWorkflowException(execution, 2500,
478                             "Service Instance was not found in aai")
479                 }
480                 AAIResultWrapper wrapper = client.get(uri, NotFoundException.class)
481                 Optional<ServiceInstance> si = wrapper.asBean(ServiceInstance.class)
482                 if (si.isPresent()) {
483                     if (si.get().getOrchestrationStatus().toLowerCase() == "activated" &&
484                             operationType.equalsIgnoreCase("deactivation")) {
485                         logger.info("Service is in active state")
486                         execution.setVariable("e2eservicestatus", "activated")
487                         execution.setVariable("isContinue", "true")
488                         String snssai = si.get().getEnvironmentContext()
489                         execution.setVariable("snssai", snssai)
490                     } else if (si.get().getOrchestrationStatus().toLowerCase() == "deactivated" &&
491                             operationType.equalsIgnoreCase("activation")) {
492                         logger.info("Service is  in de-activated state")
493                         execution.setVariable("e2eservicestatus", "deactivated")
494                         execution.setVariable("isContinue", "true")
495                         String snssai = si.get().getEnvironmentContext()
496                         execution.setVariable("snssai", snssai)
497                     } else {
498                         execution.setVariable("isContinue", "false")
499                     }
500                 }
501             } catch (Exception e) {
502                 msg = "Requested e2eservice does not exist"
503                 logger.info("e2eservice doesnt exist")
504                 execution.setVariable("isContinue", "false")
505                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
506             }
507
508             //check the NSI is exist or the status of NSI is active or de-active
509             try {
510
511                 //get the allotted-resources by e2e slice id
512                 AAIResourcesClient client_allotted = new AAIResourcesClient()
513                 AAIPluralResourceUri uri_allotted = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(serviceInstanceId).allottedResources()
514                         )
515                 if (!client_allotted.exists(uri_allotted)) {
516                     exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service Instance was not found in aai")
517                 }
518                 AAIResultWrapper wrapper_allotted = client_allotted.get(uri_allotted, NotFoundException.class)
519                 Optional<AllottedResources> all_allotted = wrapper_allotted.asBean(AllottedResources.class)
520
521                 if (all_allotted.isPresent() && all_allotted.get().getAllottedResource()) {
522                     List<AllottedResource> AllottedResourceList = all_allotted.get().getAllottedResource()
523                     AllottedResource ar = AllottedResourceList.first()
524                     String relatedLink = ar.getRelationshipList().getRelationship().first().getRelatedLink()
525                     String nsiserviceid = relatedLink.substring(relatedLink.lastIndexOf("/") + 1, relatedLink.length())
526                     execution.setVariable("NSIserviceid", nsiserviceid)
527                     logger.info("the NSI ID is:" + nsiserviceid)
528
529                     //Query nsi by nsi id
530                     try {
531                         //get the NSI id by e2e slice id
532                         AAIResourcesClient client = new AAIResourcesClient()
533                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(nsiserviceid))
534                         if (!client.exists(uri)) {
535                             exceptionUtil.buildAndThrowWorkflowException(execution, 2500,
536                                     "Service Instance was not found in aai")
537                         }
538                         AAIResultWrapper wrapper = client.get(uri, NotFoundException.class)
539                         Optional<ServiceInstance> si = wrapper.asBean(ServiceInstance.class)
540
541                         if (si.isPresent()) {
542                             if (si.get().getServiceRole().toLowerCase() == "nsi") {
543                                 if (si.get().getOrchestrationStatus() == "activated") {
544                                     logger.info("NSI services is  in activated state")
545                                     execution.setVariable("NSIservicestatus", "activated")
546                                 } else {
547                                     logger.info("NSI services is  in deactivated state")
548                                     execution.setVariable("NSIservicestatus", "deactivated")
549                                 }
550                             } else {
551                                 logger.info("the service id" + si.get().getServiceInstanceId() + "is " +
552                                         si.get().getServiceRole())
553                                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
554                             }
555                         }
556                     } catch (Exception e) {
557                         msg = "Requested NSI service does not exist:" + e.getMessage()
558                         logger.info("NSI service doesnt exist")
559                         execution.setVariable("isContinue", "false")
560                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
561                     }
562                 }
563             } catch (Exception e) {
564                 msg = "Requested service does not exist: " + e.getMessage()
565                 logger.info("NSI Service doesnt exist")
566                 execution.setVariable("isActivate", "false")
567                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
568             }
569         } catch (BpmnError e) {
570             throw e
571         } catch (Exception ex) {
572             msg = "Exception in org.onap.so.bpmn.common.scripts.CompleteMsoProcess.CheckAAIOrchStatus " + ex.getMessage()
573             logger.info(msg)
574             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
575         }
576
577         logger.debug(Prefix + "CheckAAIOrchStatus Exit")
578     }
579
580 }