Fix issues in shared NSI flow in NSMF
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / AAISliceUtil.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  # Copyright (c) 2019, CMCC Technologies Co., Ltd.
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 package org.onap.so.bpmn.infrastructure.scripts
21
22 import org.camunda.bpm.engine.delegate.BpmnError
23 import org.camunda.bpm.engine.delegate.DelegateExecution
24 import org.onap.aai.domain.yang.Relationship
25 import org.onap.aai.domain.yang.ServiceInstance
26 import org.onap.aaiclient.client.aai.AAIObjectName
27 import org.onap.aaiclient.client.aai.AAIResourcesClient
28 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
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.ExceptionUtil
32 import org.slf4j.Logger
33 import org.slf4j.LoggerFactory
34
35 import javax.ws.rs.NotFoundException
36
37 class AAISliceUtil {
38     private static final Logger LOGGER = LoggerFactory.getLogger(AAISliceUtil.class);
39     ExceptionUtil exceptionUtil = new ExceptionUtil()
40     /**
41      * Get NSSI Id from AAI
42      * @param execution
43      * @param nsiId
44      * @return
45      */
46     List<String> getNSSIIdList(DelegateExecution execution, String nsiId){
47         List<String> nssiIdList = []
48
49         try
50         {
51             String errorMsg = "query nssi from aai failed."
52             AAIResultWrapper wrapper = queryAAI(execution, AAIFluentTypeBuilder.Types.SERVICE_INSTANCE, nsiId, errorMsg)
53             Optional<ServiceInstance> si = wrapper.asBean(ServiceInstance.class)
54             if(si.isPresent())
55             {
56                 List<Relationship> relationshipList = si.get().getRelationshipList()?.getRelationship()
57                 for (Relationship relationship : relationshipList)
58                 {
59                     String relatedTo = relationship.getRelatedTo()
60                     if (relatedTo == "service-instance")
61                     {
62                         String relatedLink = relationship.getRelatedLink()?:""
63                         String instanceId = relatedLink ? relatedLink.substring(relatedLink.lastIndexOf("/") + 1,relatedLink.length()) : ""
64                         AAIResultWrapper wrapper1 = queryAAI(execution, AAIFluentTypeBuilder.Types.SERVICE_INSTANCE, instanceId, errorMsg)
65                         Optional<ServiceInstance> serviceInstance = wrapper1.asBean(ServiceInstance.class)
66                         def nssiId
67                         if (serviceInstance.isPresent()) {
68                             ServiceInstance instance = serviceInstance.get()
69                             if ("nssi".equalsIgnoreCase(instance.getServiceRole())) {
70                                 nssiId = instance.getServiceInstanceId()
71                                 nssiIdList.add(nssiId)
72                             }
73                         }
74                     }
75                 }
76             }
77         }
78         catch(BpmnError e){
79             throw e
80         }
81         catch (Exception ex){
82             String msg = "Exception in getNSIFromAAI " + ex.getMessage()
83             LOGGER.error(msg)
84             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
85         }
86         return nssiIdList
87     }
88
89
90     /**
91      * get nssi service from AAI
92      * prepare list
93      * @param execution
94      */
95     List<ServiceInstance> getNSSIListFromAAI(DelegateExecution execution, List<String> nssiIdList)
96     {
97         LOGGER.trace("***** Start getNSSIListFromAAI *****")
98         List<ServiceInstance> nssiInstanceList = []
99         String errorMsg = "query nssi list from aai failed"
100         for(String nssiId : nssiIdList){
101             AAIResultWrapper wrapper = queryAAI(execution, AAIFluentTypeBuilder.Types.SERVICE_INSTANCE, nssiId, errorMsg)
102             Optional<ServiceInstance> si =wrapper.asBean(ServiceInstance.class)
103             if(si.isPresent()){
104                 nssiInstanceList.add(si.get())
105             }
106         }
107         LOGGER.trace(" ***** Exit getNSSIListFromAAI *****")
108         return nssiInstanceList
109     }
110
111
112     /**
113      * query AAI
114      * @param execution
115      * @param aaiObjectName
116      * @param instanceId
117      * @return AAIResultWrapper
118      */
119     private AAIResultWrapper queryAAI(DelegateExecution execution, AAIObjectName aaiObjectName, String instanceId, String errorMsg)
120     {
121         LOGGER.trace(" ***** Start queryAAI *****")
122         String globalSubscriberId = execution.getVariable("globalSubscriberId")
123         String serviceType = execution.getVariable("serviceType")
124
125         org.onap.aaiclient.client.generated.fluentbuilders.ServiceInstance serviceInstanceType = AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(instanceId)
126         def type
127         if (aaiObjectName == AAIFluentTypeBuilder.Types.ALLOTTED_RESOURCE) {
128             type = serviceInstanceType.allottedResources()
129         } else if (aaiObjectName == AAIFluentTypeBuilder.Types.SLICE_PROFILES) {
130             type = serviceInstanceType.sliceProfiles()
131         } else {
132             type = serviceInstanceType
133         }
134         def uri = AAIUriFactory.createResourceUri(type)
135         if (!getAAIClient().exists(uri)) {
136             exceptionUtil.buildAndThrowWorkflowException(execution, 2500, errorMsg)
137         }
138         AAIResultWrapper wrapper = getAAIClient().get(uri, NotFoundException.class)
139         LOGGER.trace(" ***** Exit queryAAI *****")
140         return wrapper
141     }
142
143     AAIResourcesClient getAAIClient(){
144         return  new AAIResourcesClient()
145     }
146 }