7c2a2be6ac7ca46900259c9d05dc24bf7b7a5e72
[so.git] /
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.AllottedResource
25 import org.onap.aai.domain.yang.AllottedResources
26 import org.onap.aai.domain.yang.Relationship
27 import org.onap.aai.domain.yang.ServiceInstance
28 import org.onap.aai.domain.yang.SliceProfiles
29 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
30 import org.onap.so.bpmn.common.scripts.ExceptionUtil
31 import org.onap.so.client.aai.AAIObjectType
32 import org.onap.so.client.aai.AAIResourcesClient
33 import org.onap.so.client.aai.entities.AAIResultWrapper
34 import org.onap.so.client.aai.entities.uri.AAIResourceUri
35 import org.onap.so.client.aai.entities.uri.AAIUriFactory
36 import org.slf4j.Logger
37 import org.slf4j.LoggerFactory
38
39 import javax.ws.rs.NotFoundException
40
41 import static org.apache.commons.lang3.StringUtils.isBlank
42
43 /**
44  * This groovy class supports the <class>DoDeleteSliceService.bpmn</class> process.
45  *
46  * Inputs:
47  * @param - msoRequestId
48  * @param - globalSubscriberId - O
49  * @param - subscriptionServiceType - O
50  * @param - serviceInstanceId
51  *
52  */
53 class DoDeleteSliceService extends AbstractServiceTaskProcessor {
54     private final String PREFIX ="DoDeleteSliceService"
55     ExceptionUtil exceptionUtil = new ExceptionUtil()
56     private static final Logger LOGGER = LoggerFactory.getLogger( DoDeleteSliceService.class)
57
58     @Override
59     void preProcessRequest(DelegateExecution execution) {
60         LOGGER.debug(" *****${PREFIX} preProcessRequest *****")
61         String msg = ""
62
63         try {
64             //String requestId = execution.getVariable("msoRequestId")
65             execution.setVariable("prefix",PREFIX)
66
67             //Inputs
68             //requestDetails.subscriberInfo. for AAI GET & PUT
69              execution.getVariable("globalSubscriberId") ?: execution.setVariable("globalSubscriberId", "")
70
71             //requestDetails.requestParameters. for AAI PUT
72             execution.getVariable("serviceType") ?: execution.setVariable("serviceType", "")
73
74             //Generated in parent for AAI PUT
75             String serviceInstanceId = execution.getVariable("serviceInstanceId")
76             if (isBlank(serviceInstanceId)){
77                 msg = "Input serviceInstanceId is null"
78                 LOGGER.info(msg)
79                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
80             }
81         } catch (BpmnError e) {
82             throw e
83         } catch (Exception ex){
84             msg = "Exception in preProcessRequest " + ex.getMessage()
85             LOGGER.error(msg)
86             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
87         }
88         LOGGER.debug("*****${PREFIX} Exit preProcessRequest *****")
89     }
90
91     /**
92      * query E2ESliceService from AAI
93      * save snssai
94      * @param execution
95      */
96     void queryE2ESliceSeriveFromAAI(DelegateExecution execution)
97     {
98         LOGGER.trace(" *****${PREFIX} Start queryE2ESliceSeriveFromAAI *****")
99         String serviceInstanceId = execution.getVariable("serviceInstanceId")
100
101         String errorMsg = "query e2e slice service from aai failed"
102         AAIResultWrapper wrapper = queryAAI(execution, AAIObjectType.SERVICE_INSTANCE, serviceInstanceId, errorMsg)
103         Optional<ServiceInstance> si =wrapper.asBean(ServiceInstance.class)
104         if(si.isPresent())
105         {
106             String snssai = si.get()?.getEnvironmentContext()
107             execution.setVariable("snssai", snssai ?: "")
108             LOGGER.info("serviceInstanceId: ${serviceInstanceId}, snssai: ${snssai}")
109         }
110         LOGGER.trace(" *****${PREFIX} Exit queryE2ESliceSeriveFromAAI *****")
111     }
112
113     /**
114      * get allotted resource from AAI
115      * save nsi id
116      * @param execution
117      */
118     void getAllottedResFromAAI(DelegateExecution execution)
119     {
120         LOGGER.trace(" *****${PREFIX} Start getAllottedResFromAAI *****")
121         String serviceInstanceId = execution.getVariable("serviceInstanceId")
122         try
123         {
124             String errorMsg = "query allotted resource from aai failed."
125             AAIResultWrapper wrapper = queryAAI(execution, AAIObjectType.ALLOTTED_RESOURCE_ALL, serviceInstanceId, errorMsg)
126             Optional<AllottedResources> ars = wrapper?.asBean(AllottedResources.class)
127             if(ars.isPresent() && ars.get().getAllottedResource())
128             {
129                 List<AllottedResource> allottedResourceList = ars.get().getAllottedResource()
130                 AllottedResource ar = allottedResourceList.first()
131                 String relatedLink = ar?.getRelationshipList()?.getRelationship()?.first()?.getRelatedLink()
132                 String nsiId = relatedLink ? relatedLink.substring(relatedLink.lastIndexOf("/") + 1,relatedLink.length()) : ""
133                 execution.setVariable("nsiId", nsiId)
134                 LOGGER.info("serviceInstanceId: ${serviceInstanceId}, nsiId:${nsiId}")
135             }
136         }
137         catch(BpmnError e){
138             throw e
139         }
140         catch (Exception ex){
141             String msg = "Exception in getAllottedResFromAAI " + ex.getMessage()
142             LOGGER.error(msg)
143             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
144         }
145         LOGGER.trace(" *****${PREFIX} Exit getAllottedResFromAAI *****")
146     }
147
148     /**
149      * get nsi service instance from aai
150      * save nssi id
151      * @param execution
152      */
153     void getNSIFromAAI(DelegateExecution execution)
154     {
155         LOGGER.trace(" *****${PREFIX} Start getNSIFromAAI *****")
156         String nsiId = execution.getVariable("nsiId")
157         try
158         {
159             String errorMsg = "query nsi from aai failed."
160             AAIResultWrapper wrapper = queryAAI(execution, AAIObjectType.SERVICE_INSTANCE, nsiId, errorMsg)
161             Optional<ServiceInstance> si =wrapper.asBean(ServiceInstance.class)
162             List<String> nssiIdList = []
163             String msg = "nsiId:${nsiId},nssiIdList:"
164             if(si.isPresent())
165             {
166                 List<Relationship> relationshipList = si.get().getRelationshipList()?.getRelationship()
167                 for (Relationship relationship : relationshipList)
168                 {
169                     String relatedTo = relationship.getRelatedTo()
170                     if (relatedTo == "service-instance")
171                     {
172                         String relatedLink = relationship.getRelatedLink()?:""
173                         String nssiId = relatedLink ? relatedLink.substring(relatedLink.lastIndexOf("/") + 1,relatedLink.length()) : ""
174                         nssiIdList.add(nssiId)
175                         msg+="${nssiId}, "
176                     }
177                 }
178             }
179             LOGGER.info(msg)
180             execution.setVariable("nssiIdList", nssiIdList)
181         }
182         catch(BpmnError e){
183             throw e
184         }
185         catch (Exception ex){
186             String msg = "Exception in getNSIFromAAI " + ex.getMessage()
187             LOGGER.error(msg)
188             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
189         }
190         LOGGER.trace(" *****${PREFIX} Exit getNSIFromAAI *****")
191     }
192
193     /**
194      * get nssi service from AAI
195      * prepare list
196      * @param execution
197      */
198     void getNSSIListFromAAI(DelegateExecution execution)
199     {
200         LOGGER.trace("*****${PREFIX} Start getNSSIListFromAAI *****")
201         List<String> nssiIdList = execution.getVariable("nssiIdList")
202         List<ServiceInstance> nssiInstanceList = []
203         String errorMsg = "query nssi list from aai failed"
204         for(String nssiId : nssiIdList)
205         {
206             AAIResultWrapper wrapper = queryAAI(execution, AAIObjectType.SERVICE_INSTANCE, nssiId, errorMsg)
207             Optional<ServiceInstance> si =wrapper.asBean(ServiceInstance.class)
208             if(si.isPresent())
209             {
210                 nssiInstanceList.add(si.get())
211             }
212         }
213         int size = nssiInstanceList.size()
214         int proportion = size >0 ?((90/size) as int) : 90
215         execution.setVariable("nssiInstanceList", nssiInstanceList)
216         execution.setVariable("currentNSSIIndex", 0)
217         execution.setVariable("proportion", proportion)
218         String msg ="nssiInstanceList size: ${nssiInstanceList.size()}, proportion:${proportion}"
219         LOGGER.info(msg)
220         LOGGER.trace(" *****${PREFIX} Exit getNSSIListFromAAI *****")
221     }
222
223     /**
224      * get current NSSI
225      * @param execution
226      */
227     void getCurrentNSSI(DelegateExecution execution)
228     {
229         LOGGER.trace(" *****${PREFIX} Start getCurrentNSSI *****")
230         List<ServiceInstance> nssiInstanceList = execution.getVariable("nssiInstanceList")
231         int currentIndex = execution.getVariable("currentNSSIIndex") as int
232         ServiceInstance nssi = nssiInstanceList?.get(currentIndex)
233         def currentNSSI = [:]
234         currentNSSI['nssiServiceInstanceId'] = nssi?.getServiceInstanceId()
235         currentNSSI['modelInvariantId'] = nssi?.getModelInvariantId()
236         currentNSSI['modelVersionId'] = nssi?.getModelVersionId()
237         currentNSSI['snssai'] = execution.getVariable("snssai") ?: ""
238         currentNSSI['nsiServiceInstanceId'] = execution.getVariable("nsiId") ?: ""
239         currentNSSI['operationId'] = execution.getVariable("operationId") ?: ""
240         currentNSSI['e2eServiceInstanceId'] = execution.getVariable("serviceInstanceId") ?: ""
241         currentNSSI['msoRequestId'] = execution.getVariable("msoRequestId") ?: ""
242         currentNSSI['globalSubscriberId'] = execution.getVariable("globalSubscriberId") ?: ""
243         currentNSSI['serviceType'] = execution.getVariable("serviceType") ?: ""
244         currentNSSI['serviceModelInfo'] = execution.getVariable("serviceModelInfo") ?: ""
245         currentNSSI['proportion'] = (execution.getVariable("proportion") as int)*(currentIndex+1)
246         execution.setVariable("currentNSSI", currentNSSI)
247         String msg = "Now we deal with nssiServiceInstanceId: ${currentNSSI['nssiServiceInstanceId']}, current Index: ${currentIndex}, current proportion:${currentNSSI['proportion']}"
248         LOGGER.info(msg)
249         LOGGER.trace(" *****${PREFIX} Exit getCurrentNSSI *****")
250     }
251
252     /**
253      * parse next nssi
254      * @param execution
255      */
256     void parseNextNSSI(DelegateExecution execution)
257     {
258         LOGGER.trace(" *****${PREFIX} Start parseNextNSSI *****")
259         if(execution.getVariable("WorkflowException") != null){
260             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "current job failure!")
261         }
262         def currentIndex = execution.getVariable("currentNSSIIndex")
263         List<ServiceInstance> nssiInstanceList = execution.getVariable("nssiInstanceList")
264         def nextIndex = ++currentIndex
265         LOGGER.info("nextIndex: ${nextIndex}")
266         if(nextIndex >= nssiInstanceList.size()){
267             execution.setVariable("isAllNSSIFinished", "true")
268         }else{
269             execution.setVariable("isAllNSSIFinished", "false")
270             execution.setVariable("currentNSSIIndex", nextIndex)
271         }
272         LOGGER.trace(" *****${PREFIX} Exit parseNextNSSI *****")
273     }
274
275
276     /**
277      * query sliceProfile from AAI
278      * save profileId
279      * @param execution
280      */
281     void querySliceProfileFromAAI(DelegateExecution execution)
282     {
283         LOGGER.trace(" *****${PREFIX} Start querySliceProfileFromAAI *****")
284         def currentNSSI = execution.getVariable("currentNSSI")
285         String nssiId = currentNSSI['nssiServiceInstanceId']
286         String errorMsg = "query slice profile failed"
287         AAIResultWrapper wrapper = queryAAI(execution, AAIObjectType.SLICE_PROFILE_ALL, nssiId, errorMsg)
288         Optional<SliceProfiles> sliceProfiles =wrapper.asBean(SliceProfiles.class)
289         if(sliceProfiles.isPresent())
290         {
291             String profileId = sliceProfiles.get().getSliceProfile()?.get(0)?.getProfileId()
292             currentNSSI['profileId'] =  profileId ?: ""
293             LOGGER.info("nssiId: ${nssiId}, profileId: ${profileId}")
294         }
295         execution.setVariable("currentNSSI", currentNSSI)
296         LOGGER.trace(" *****${PREFIX} Exit querySliceProfileFromAAI *****")
297     }
298
299     /**
300      * query AAI
301      * @param execution
302      * @param aaiObjectType
303      * @param instanceId
304      * @return AAIResultWrapper
305      */
306     private AAIResultWrapper queryAAI(DelegateExecution execution, AAIObjectType aaiObjectType, String instanceId, String errorMsg)
307     {
308         LOGGER.trace(" *****${PREFIX} Start queryAAI *****")
309         String globalSubscriberId = execution.getVariable("globalSubscriberId")
310         String serviceType = execution.getVariable("serviceType")
311
312         AAIResourcesClient resourceClient = new AAIResourcesClient()
313         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(aaiObjectType, globalSubscriberId, serviceType, instanceId)
314         if (!resourceClient.exists(resourceUri)) {
315             exceptionUtil.buildAndThrowWorkflowException(execution, 2500, errorMsg)
316         }
317         AAIResultWrapper wrapper = resourceClient.get(resourceUri, NotFoundException.class)
318         LOGGER.trace(" *****${PREFIX} Exit queryAAI *****")
319         return wrapper
320     }
321
322 }