d5b554d841dbfbf7ab2e38dd282350283e13ba01
[so.git] /
1 package org.onap.so.bpmn.infrastructure.scripts
2
3 import com.google.common.reflect.TypeToken
4 import com.google.gson.Gson
5 import org.camunda.bpm.engine.delegate.BpmnError
6 import org.camunda.bpm.engine.delegate.DelegateExecution
7 import org.onap.aai.domain.yang.AllottedResource
8 import org.onap.aai.domain.yang.Relationship
9 import org.onap.aai.domain.yang.RelationshipList
10 import org.onap.aai.domain.yang.ServiceInstance
11 import org.onap.so.beans.nsmf.SliceTaskParams
12 import org.onap.so.bpmn.common.scripts.ExceptionUtil
13 import org.onap.so.bpmn.core.domain.ServiceDecomposition
14 import org.onap.so.bpmn.core.domain.ServiceProxy
15 import org.onap.so.bpmn.core.json.JsonUtils
16 import org.onap.so.client.aai.AAIObjectType
17 import org.onap.so.client.aai.AAIResourcesClient
18 import org.onap.so.client.aai.entities.AAIEdgeLabel
19 import org.onap.so.client.aai.entities.AAIResultWrapper
20 import org.onap.so.client.aai.entities.uri.AAIResourceUri
21 import org.onap.so.client.aai.entities.uri.AAIUriFactory
22 import org.onap.so.db.request.client.RequestsDbClient
23 import org.onap.so.db.request.beans.OrchestrationTask
24 import org.slf4j.Logger
25 import org.slf4j.LoggerFactory
26
27 import javax.ws.rs.NotFoundException
28 import javax.ws.rs.core.UriBuilder
29
30 import static org.apache.commons.lang3.StringUtils.isBlank
31
32 class DoAllocateNSIandNSSI extends org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor{
33
34     private static final Logger logger = LoggerFactory.getLogger( DoAllocateNSIandNSSI.class);
35
36     ExceptionUtil exceptionUtil = new ExceptionUtil()
37
38     JsonUtils jsonUtil = new JsonUtils()
39     RequestsDbClient requestsDbClient = new RequestsDbClient()
40
41     /**
42      * Pre Process the BPMN Flow Request
43      * Inclouds:
44      * generate the nsOperationKey
45      * generate the nsParameters
46      */
47
48     void preProcessRequest (DelegateExecution execution) {
49         String msg = ""
50         logger.trace("Enter preProcessRequest()")
51         Map<String, Object> nssiMap = new HashMap<>()
52         execution.setVariable("nssiMap", nssiMap)
53         boolean isMoreNSSTtoProcess = true
54         execution.setVariable("isMoreNSSTtoProcess", isMoreNSSTtoProcess)
55         List<String> nsstSequence = new ArrayList<>(Arrays.asList("cn"))
56         execution.setVariable("nsstSequence", nsstSequence)
57         logger.trace("Exit preProcessRequest")
58     }
59
60     void retriveSliceOption(DelegateExecution execution) {
61         logger.trace("Enter retriveSliceOption() of DoAllocateNSIandNSSI")
62         String uuiRequest = execution.getVariable("uuiRequest")
63         boolean isNSIOptionAvailable = false
64         List<String> nssiAssociated = new ArrayList<>()
65         SliceTaskParams sliceParams = execution.getVariable("sliceTaskParams")
66         try
67         {
68             String modelUuid = jsonUtil.getJsonValue(uuiRequest, "service.parameters.requestInputs.nstar0_allottedresource0_providing_service_uuid")
69             String modelInvariantUuid = jsonUtil.getJsonValue(uuiRequest, "service.parameters.requestInputs.nstar0_allottedresource0_providing_service_invariant_uuid")
70             String serviceModelInfo = """{
71             "modelInvariantUuid":"${modelInvariantUuid}",
72             "modelUuid":"${modelUuid}",
73             "modelVersion":""
74              }"""
75             execution.setVariable("serviceModelInfo", serviceModelInfo)
76             //Params sliceParams = new Gson().fromJson(params, new TypeToken<Params>() {}.getType());
77             execution.setVariable("sliceParams", sliceParams)
78         }catch (Exception ex) {
79             logger.debug( "Unable to get the task information from request DB: " + ex)
80             exceptionUtil.buildAndThrowWorkflowException(execution, 401, "Unable to get task information from request DB.")
81         }
82
83         if(isBlank(sliceParams.getSuggestNsiId()))
84         {
85             isNSIOptionAvailable=false
86         }
87         else
88         {
89             isNSIOptionAvailable=true
90             execution.setVariable('nsiServiceInstanceId',sliceParams.getSuggestNsiId())
91             execution.setVariable('nsiServiceInstanceName',sliceParams.getSuggestNsiName())
92         }
93         execution.setVariable("isNSIOptionAvailable",isNSIOptionAvailable)
94         logger.trace("Exit retriveSliceOption() of DoAllocateNSIandNSSI")
95     }
96
97     void updateRelationship(DelegateExecution execution) {
98         logger.trace("Enter update relationship in DoAllocateNSIandNSSI()")
99         String nsiServiceInstanceId = execution.getVariable("nsiServiceInstanceId")
100         String allottedResourceId = execution.getVariable("allottedResourceId")
101         //Need to check whether nsi exist : Begin
102         org.onap.aai.domain.yang.ServiceInstance nsiServiceInstance = new org.onap.aai.domain.yang.ServiceInstance()
103         SliceTaskParams sliceParams = execution.getVariable("sliceParams")
104
105         String nsiServiceInstanceID = sliceParams.getSuggestNsiId()
106
107         AAIResourcesClient resourceClient = new AAIResourcesClient()
108         AAIResourceUri nsiServiceuri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, execution.getVariable("globalSubscriberId"), execution.getVariable("serviceType"), nsiServiceInstanceID)
109         //AAIResourceUri nsiServiceuri = AAIUriFactory.createResourceUri(AAIObjectType.QUERY_ALLOTTED_RESOURCE, execution.getVariable("globalSubscriberId"), execution.getVariable("serviceType"), nsiServiceInstanceID)
110
111         try {
112             AAIResultWrapper wrapper = resourceClient.get(nsiServiceuri, NotFoundException.class)
113             Optional<org.onap.aai.domain.yang.ServiceInstance> si = wrapper.asBean(org.onap.aai.domain.yang.ServiceInstance.class)
114             nsiServiceInstance = si.get()
115             //allottedResourceId=nsiServiceInstance.getAllottedResources().getAllottedResource().get(0).getId()
116
117 //            if(resourceClient.exists(nsiServiceuri)){
118 //                execution.setVariable("nsi_resourceLink", nsiServiceuri.build().toString())
119 //            }else{
120 //                exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service instance was not found in aai to " +
121 //                        "associate for service :"+serviceInstanceId)
122 //            }
123         }catch(BpmnError e) {
124             throw e;
125         }catch (Exception ex){
126             String msg = "NSI suggested in the option doesn't exist. " + nsiServiceInstanceID
127             logger.debug(msg)
128             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
129         }
130         AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIObjectType.ALLOTTED_RESOURCE, execution.getVariable("globalSubscriberId"), execution.getVariable("serviceType"), nsiServiceInstanceId, allottedResourceId)
131         getAAIClient().connect(allottedResourceUri,nsiServiceuri)
132
133         List<String> nssiAssociated = new ArrayList<>()
134         RelationshipList relationshipList = nsiServiceInstance.getRelationshipList()
135         List<Relationship> relationships = relationshipList.getRelationship()
136         for(Relationship relationship in relationships)
137         {
138             if(relationship.getRelatedTo().equalsIgnoreCase("service-instance"))
139             {
140                 String NSSIassociated = relationship.getRelatedLink().substring(relationship.getRelatedLink().lastIndexOf("/") + 1);
141                 if(!NSSIassociated.equals(nsiServiceInstanceID))
142                     nssiAssociated.add(NSSIassociated)
143             }
144         }
145         execution.setVariable("nssiAssociated",nssiAssociated)
146         execution.setVariable("nsiServiceInstanceName",nsiServiceInstance.getServiceInstanceName())
147         logger.trace("Exit update relationship in DoAllocateNSIandNSSI()")
148     }
149
150     void prepareNssiModelInfo(DelegateExecution execution){
151         logger.trace("Enter prepareNssiModelInfo in DoAllocateNSIandNSSI()")
152         List<String> nssiAssociated = new ArrayList<>()
153         Map<String, Object> nssiMap = new HashMap<>()
154         nssiAssociated=execution.getVariable("nssiAssociated")
155         for(String nssiID in nssiAssociated)
156         {
157             try {
158                 AAIResourcesClient resourceClient = new AAIResourcesClient()
159                 AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, execution.getVariable("globalSubscriberId"), execution.getVariable("serviceType"), nssiID)
160                 AAIResultWrapper wrapper = resourceClient.get(serviceInstanceUri, NotFoundException.class)
161                 Optional<org.onap.aai.domain.yang.ServiceInstance> si = wrapper.asBean(org.onap.aai.domain.yang.ServiceInstance.class)
162                 org.onap.aai.domain.yang.ServiceInstance nssi = si.get()
163                 nssiMap.put(nssi.getEnvironmentContext(),"""{
164                     "serviceInstanceId":"${nssi.getServiceInstanceId()}",
165                     "modelUuid":"${nssi.getModelVersionId()}"
166                      }""")
167
168             }catch(NotFoundException e)
169             {
170                 logger.debug("NSSI Service Instance not found in AAI: " + nssiID)
171             }catch(Exception e)
172             {
173                 logger.debug("NSSI Service Instance not found in AAI: " + nssiID)
174             }
175             execution.setVariable("nssiMap",nssiMap)
176
177         }
178         logger.trace("Exit prepareNssiModelInfo in DoAllocateNSIandNSSI()")
179     }
180
181     void createNSIinAAI(DelegateExecution execution) {
182         logger.trace("Enter CreateNSIinAAI in DoAllocateNSIandNSSI()")
183         ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
184         org.onap.aai.domain.yang.ServiceInstance nsi = new ServiceInstance();
185         String sliceInstanceId = UUID.randomUUID().toString()
186         execution.setVariable("sliceInstanceId",sliceInstanceId)
187         nsi.setServiceInstanceId(sliceInstanceId)
188         String sliceInstanceName = "nsi_"+execution.getVariable("serviceInstanceName")
189         nsi.setServiceInstanceName(sliceInstanceName)
190         String serviceType = execution.getVariable("serviceType")
191         nsi.setServiceType(serviceType)
192         String serviceStatus = "deactivated"
193         nsi.setOrchestrationStatus(serviceStatus)
194         String modelInvariantUuid = serviceDecomposition.getModelInfo().getModelInvariantUuid()
195         String modelUuid = serviceDecomposition.getModelInfo().getModelUuid()
196         nsi.setModelInvariantId(modelInvariantUuid)
197         nsi.setModelVersionId(modelUuid)
198         String uuiRequest = execution.getVariable("uuiRequest")
199         String serviceInstanceLocationid = jsonUtil.getJsonValue(uuiRequest, "service.parameters.requestInputs.plmnIdList")
200         nsi.setServiceInstanceLocationId(serviceInstanceLocationid)
201         //String snssai = jsonUtil.getJsonValue(uuiRequest, "service.requestInputs.snssai")
202         //nsi.setEnvironmentContext(snssai)
203         String serviceRole = "nsi"
204         nsi.setServiceRole(serviceRole)
205         try {
206
207             AAIResourcesClient client = new AAIResourcesClient()
208             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, execution.getVariable("globalSubscriberId"), execution.getVariable("subscriptionServiceType"), sliceInstanceId)
209             client.create(uri, nsi)
210
211         } catch (BpmnError e) {
212             throw e
213         } catch (Exception ex) {
214             msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
215             logger.info(msg)
216             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
217         }
218         Map<String, Object> nssiMap = new HashMap<>()
219         List<ServiceProxy> serviceProxyList = serviceDecomposition.getServiceProxy()
220         List<String> nsstModelInfoList = new ArrayList<>()
221         for(ServiceProxy serviceProxy : serviceProxyList)
222         {
223             //String nsstModelUuid = serviceProxy.getModelInfo().getModelUuid()
224             String nsstModelUuid = serviceProxy.getSourceModelUuid()
225             //String nsstModelInvariantUuid = serviceProxy.getModelInfo().getModelInvariantUuid()
226             String nsstServiceModelInfo = """{
227             "modelInvariantUuid":"",
228             "modelUuid":"${nsstModelUuid}",
229             "modelVersion":""
230              }"""
231             nsstModelInfoList.add(nsstServiceModelInfo)
232         }
233         int currentIndex=0
234         int maxIndex=nsstModelInfoList.size()
235         if(maxIndex < 1)
236         {
237             msg = "Exception in DoAllocateNSIandNSSI. There is no NSST associated with NST "
238             logger.info(msg)
239             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
240         }
241         execution.setVariable("nsstModelInfoList",nsstModelInfoList)
242         execution.setVariable("currentIndex",currentIndex)
243         execution.setVariable("maxIndex",maxIndex)
244         execution.setVariable('nsiServiceInstanceId',sliceInstanceId)
245         execution.setVariable("nsiServiceInstanceName",sliceInstanceName)
246         logger.trace("Exit CreateNSIinAAI in DoAllocateNSIandNSSI()")
247     }
248
249     void getOneNsstInfo(DelegateExecution execution){
250         List<String> nsstModelInfoList = new ArrayList<>()
251         nsstModelInfoList = execution.getVariable("nsstModelInfoList")
252         int currentIndex = execution.getVariable("currentIndex")
253         int maxIndex = execution.getVariable("maxIndex")
254         boolean isMoreNSSTtoProcess = true
255         String nsstServiceModelInfo = nsstModelInfoList.get(currentIndex)
256         execution.setVariable("serviceModelInfo", nsstServiceModelInfo)
257         execution.setVariable("currentIndex", currentIndex)
258         currentIndex = currentIndex+1
259         if(currentIndex <= maxIndex )
260             isMoreNSSTtoProcess = false
261         execution.setVariable("isMoreNSSTtoProcess", isMoreNSSTtoProcess)
262     }
263
264     void createNSSTMap(DelegateExecution execution){
265         ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
266         String modelUuid= serviceDecomposition.getModelInfo().getModelUuid()
267         String content = serviceDecomposition.getServiceInfo().getServiceArtifact().get(0).getContent()
268         //String nsstID = jsonUtil.getJsonValue(content, "metadata.id")
269         //String vendor = jsonUtil.getJsonValue(content, "metadata.vendor")
270         //String type = jsonUtil.getJsonValue(content, "metadata.type")
271         String domain = jsonUtil.getJsonValue(content, "metadata.domainType")
272
273         Map<String, Object> nssiMap = execution.getVariable("nssiMap")
274         String servicename = execution.getVariable("serviceInstanceName")
275         String nsiname = "nsi_"+servicename
276         nssiMap.put(domain,"""{
277                     "serviceInstanceId":"",
278                     "modelUuid":"${modelUuid}"
279                      }""")
280         execution.setVariable("nssiMap",nssiMap)
281     }
282
283     void prepareNSSIList(DelegateExecution execution){
284         logger.trace("Enter prepareNSSIList in DoAllocateNSIandNSSI()")
285         Map<String, Object> nssiMap = new HashMap<>()
286         Boolean isMoreNSSI = false
287         nssiMap = execution.getVariable("nssiMap")
288         List<String> keys=new ArrayList<String>(nssiMap.values())
289         List<String> nsstSequence = execution.getVariable("nsstSequence")
290         Integer currentIndex=0;
291         execution.setVariable("currentNssiIndex",currentIndex)
292         Integer maxIndex=keys.size()
293         execution.setVariable("maxIndex",maxIndex)
294         if(maxIndex>0)
295             isMoreNSSI=true
296         execution.setVariable("isMoreNSSI",isMoreNSSI)
297         logger.trace("Exit prepareNSSIList in DoAllocateNSIandNSSI()")
298     }
299
300
301     void getOneNSSIInfo(DelegateExecution execution){
302         logger.trace("Enter getOneNSSIInfo in DoAllocateNSIandNSSI()")
303
304         //ServiceDecomposition serviceDecompositionObj = execution.getVariable("serviceDecompositionObj")
305         Map<String, Object> nssiMap=execution.getVariable("nssiMap")
306         List<String> nsstSequence = execution.getVariable("nsstSequence")
307         String currentNSST= nsstSequence.get(execution.getVariable("currentNssiIndex"))
308         boolean isNSSIOptionAvailable = false
309         String nsstInput=nssiMap.get(currentNSST)
310         execution.setVariable("nsstInput",nsstInput)
311         String modelUuid = jsonUtil.getJsonValue(nsstInput, "modelUuid")
312         String nssiInstanceId = jsonUtil.getJsonValue(nsstInput, "serviceInstanceId")
313         String nssiserviceModelInfo = """{
314             "modelInvariantUuid":"",
315             "modelUuid":"${modelUuid}",
316             "modelVersion":""
317              }"""
318         Integer currentIndex = execution.getVariable("currentNssiIndex")
319         currentIndex=currentIndex+1;
320         execution.setVariable("currentNssiIndex",currentIndex)
321         execution.setVariable("nssiserviceModelInfo",nssiserviceModelInfo)
322         execution.setVariable("nssiInstanceId",nssiInstanceId)
323         logger.trace("Exit getOneNSSIInfo in DoAllocateNSIandNSSI()")
324     }
325
326     void updateCurrentIndex(DelegateExecution execution){
327
328         logger.trace("Enter updateCurrentIndex in DoAllocateNSIandNSSI()")
329         Integer currentIndex = execution.getVariable("currentNssiIndex")
330         Integer maxIndex = execution.getVariable("maxIndex")
331         if(currentIndex>=maxIndex)
332         {
333             Boolean isMoreNSSI=false
334             execution.setVariable("isMoreNSSI",isMoreNSSI)
335         }
336         logger.trace("Exit updateCurrentIndex in DoAllocateNSIandNSSI()")
337     }
338 }