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