04cf7bc1a402a58800d1f132483ad1e5ea1ada18
[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.debug("Enter update relationship in DoAllocateNSIandNSSI()")
95         String allottedResourceId = execution.getVariable("allottedResourceId")
96         //Need to check whether nsi exist : Begin
97         org.onap.aai.domain.yang.ServiceInstance nsiServiceInstance = new org.onap.aai.domain.yang.ServiceInstance()
98         SliceTaskParams sliceParams = execution.getVariable("sliceParams")
99         String nsiServiceInstanceID = sliceParams.getSuggestNsiId()
100
101         AAIResourcesClient resourceClient = new AAIResourcesClient()
102         AAIResourceUri nsiServiceuri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, execution.getVariable("globalSubscriberId"), execution.getVariable("subscriptionServiceType"), nsiServiceInstanceID)
103         //AAIResourceUri nsiServiceuri = AAIUriFactory.createResourceUri(AAIObjectType.QUERY_ALLOTTED_RESOURCE, execution.getVariable("globalSubscriberId"), execution.getVariable("serviceType"), nsiServiceInstanceID)
104
105         try {
106             AAIResultWrapper wrapper = resourceClient.get(nsiServiceuri, NotFoundException.class)
107             Optional<org.onap.aai.domain.yang.ServiceInstance> si = wrapper.asBean(org.onap.aai.domain.yang.ServiceInstance.class)
108             nsiServiceInstance = si.get()
109             //allottedResourceId=nsiServiceInstance.getAllottedResources().getAllottedResource().get(0).getId()
110
111 //            if(resourceClient.exists(nsiServiceuri)){
112 //                execution.setVariable("nsi_resourceLink", nsiServiceuri.build().toString())
113 //            }else{
114 //                exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service instance was not found in aai to " +
115 //                        "associate for service :"+serviceInstanceId)
116 //            }
117
118         AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIObjectType.ALLOTTED_RESOURCE, execution.getVariable("globalSubscriberId"), execution.getVariable("subscriptionServiceType"), execution.getVariable("sliceServiceInstanceId"), allottedResourceId)
119         getAAIClient().connect(allottedResourceUri,nsiServiceuri)
120
121         List<String> nssiAssociated = new ArrayList<>()
122         RelationshipList relationshipList = nsiServiceInstance.getRelationshipList()
123         List<Relationship> relationships = relationshipList.getRelationship()
124         for(Relationship relationship in relationships)
125         {
126             if(relationship.getRelatedTo().equalsIgnoreCase("service-instance"))
127             {
128                 String NSSIassociated = relationship.getRelatedLink().substring(relationship.getRelatedLink().lastIndexOf("/") + 1);
129                 if(!NSSIassociated.equals(nsiServiceInstanceID))
130                     nssiAssociated.add(NSSIassociated)
131             }
132         }
133         execution.setVariable("nssiAssociated",nssiAssociated)
134         execution.setVariable("nsiServiceInstanceName",nsiServiceInstance.getServiceInstanceName())
135         }catch(BpmnError e) {
136             throw e
137         }catch (Exception ex){
138             String msg = "NSI suggested in the option doesn't exist. " + nsiServiceInstanceID
139             logger.debug(msg)
140             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
141         }
142         logger.debug("Exit update relationship in DoAllocateNSIandNSSI()")
143     }
144
145     void prepareNssiModelInfo(DelegateExecution execution){
146         logger.trace("Enter prepareNssiModelInfo in DoAllocateNSIandNSSI()")
147         List<String> nssiAssociated = new ArrayList<>()
148         Map<String, Object> nssiMap = new HashMap<>()
149         nssiAssociated=execution.getVariable("nssiAssociated")
150         for(String nssiID in nssiAssociated)
151         {
152             try {
153                 AAIResourcesClient resourceClient = new AAIResourcesClient()
154                 AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, execution.getVariable("globalSubscriberId"), execution.getVariable("subscriptionServiceType"), nssiID)
155                 AAIResultWrapper wrapper = resourceClient.get(serviceInstanceUri, NotFoundException.class)
156                 Optional<org.onap.aai.domain.yang.ServiceInstance> si = wrapper.asBean(org.onap.aai.domain.yang.ServiceInstance.class)
157                 org.onap.aai.domain.yang.ServiceInstance nssi = si.get()
158                 nssiMap.put(nssi.getEnvironmentContext(),"""{
159                     "serviceInstanceId":"${nssi.getServiceInstanceId()}",
160                     "modelUuid":"${nssi.getModelVersionId()}"
161                      }""")
162
163             }catch(NotFoundException e)
164             {
165                 logger.debug("NSSI Service Instance not found in AAI: " + nssiID)
166             }catch(Exception e)
167             {
168                 logger.debug("NSSI Service Instance not found in AAI: " + nssiID)
169             }
170             execution.setVariable("nssiMap",nssiMap)
171
172         }
173         logger.trace("Exit prepareNssiModelInfo in DoAllocateNSIandNSSI()")
174     }
175
176     void createNSIinAAI(DelegateExecution execution) {
177         logger.debug("Enter CreateNSIinAAI in DoAllocateNSIandNSSI()")
178         ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
179         org.onap.aai.domain.yang.ServiceInstance nsi = new ServiceInstance();
180         String sliceInstanceId = UUID.randomUUID().toString()
181         execution.setVariable("sliceInstanceId",sliceInstanceId)
182         nsi.setServiceInstanceId(sliceInstanceId)
183         String sliceInstanceName = "nsi_"+execution.getVariable("sliceServiceInstanceName")
184         nsi.setServiceInstanceName(sliceInstanceName)
185         String serviceType = execution.getVariable("serviceType")
186         nsi.setServiceType(serviceType)
187         String serviceStatus = "deactivated"
188         nsi.setOrchestrationStatus(serviceStatus)
189         String modelInvariantUuid = serviceDecomposition.getModelInfo().getModelInvariantUuid()
190         String modelUuid = serviceDecomposition.getModelInfo().getModelUuid()
191         nsi.setModelInvariantId(modelInvariantUuid)
192         nsi.setModelVersionId(modelUuid)
193         String uuiRequest = execution.getVariable("uuiRequest")
194         String serviceInstanceLocationid = jsonUtil.getJsonValue(uuiRequest, "service.parameters.requestInputs.plmnIdList")
195         nsi.setServiceInstanceLocationId(serviceInstanceLocationid)
196         //String snssai = jsonUtil.getJsonValue(uuiRequest, "service.requestInputs.snssai")
197         //nsi.setEnvironmentContext(snssai)
198         String serviceRole = "nsi"
199         nsi.setServiceRole(serviceRole)
200         String msg = ""
201         try {
202
203             AAIResourcesClient client = new AAIResourcesClient()
204             AAIResourceUri nsiServiceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, execution.getVariable("globalSubscriberId"), execution.getVariable("subscriptionServiceType"), sliceInstanceId)
205             client.create(nsiServiceUri, nsi)
206
207             Relationship relationship = new Relationship()
208             logger.info("Creating Allotted resource relationship, nsiServiceUri: " + nsiServiceUri.build().toString())
209             relationship.setRelatedLink(nsiServiceUri.build().toString())
210             AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIObjectType.ALLOTTED_RESOURCE,
211                     execution.getVariable("globalSubscriberId"),execution.getVariable("subscriptionServiceType"),
212                     execution.getVariable("sliceServiceInstanceId"), execution.getVariable("allottedResourceId")).relationshipAPI()
213             client.create(allottedResourceUri, relationship)
214
215         } catch (BpmnError e) {
216             throw e
217         } catch (Exception ex) {
218             msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
219             logger.info(msg)
220             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
221         }
222         Map<String, Object> nssiMap = new HashMap<>()
223         List<ServiceProxy> serviceProxyList = serviceDecomposition.getServiceProxy()
224         List<String> nsstModelInfoList = new ArrayList<>()
225         for(ServiceProxy serviceProxy : serviceProxyList)
226         {
227             //String nsstModelUuid = serviceProxy.getModelInfo().getModelUuid()
228             String nsstModelUuid = serviceProxy.getSourceModelUuid()
229             //String nsstModelInvariantUuid = serviceProxy.getModelInfo().getModelInvariantUuid()
230             String nsstServiceModelInfo = """{
231             "modelInvariantUuid":"",
232             "modelUuid":"${nsstModelUuid}",
233             "modelVersion":""
234              }"""
235             nsstModelInfoList.add(nsstServiceModelInfo)
236         }
237         int currentIndex=0
238         int maxIndex=nsstModelInfoList.size()
239         if(maxIndex < 1)
240         {
241             msg = "Exception in DoAllocateNSIandNSSI. There is no NSST associated with NST "
242             logger.info(msg)
243             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
244         }
245         execution.setVariable("nsstModelInfoList",nsstModelInfoList)
246         execution.setVariable("currentIndex",currentIndex)
247         execution.setVariable("maxIndex",maxIndex)
248         execution.setVariable('nsiServiceInstanceId',sliceInstanceId)
249         execution.setVariable("nsiServiceInstanceName",sliceInstanceName)
250         logger.debug("Exit CreateNSIinAAI in DoAllocateNSIandNSSI()")
251     }
252
253     void getOneNsstInfo(DelegateExecution execution){
254         List<String> nsstModelInfoList = new ArrayList<>()
255         nsstModelInfoList = execution.getVariable("nsstModelInfoList")
256         int currentIndex = execution.getVariable("currentIndex")
257         int maxIndex = execution.getVariable("maxIndex")
258         boolean isMoreNSSTtoProcess = true
259         String nsstServiceModelInfo = nsstModelInfoList.get(currentIndex)
260         execution.setVariable("serviceModelInfo", nsstServiceModelInfo)
261         execution.setVariable("currentIndex", currentIndex)
262         currentIndex = currentIndex+1
263         if(currentIndex <= maxIndex )
264             isMoreNSSTtoProcess = false
265         execution.setVariable("isMoreNSSTtoProcess", isMoreNSSTtoProcess)
266     }
267
268     void createNSSTMap(DelegateExecution execution){
269         ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
270         String modelUuid= serviceDecomposition.getModelInfo().getModelUuid()
271         String content = serviceDecomposition.getServiceInfo().getServiceArtifact().get(0).getContent()
272         //String nsstID = jsonUtil.getJsonValue(content, "metadata.id")
273         //String vendor = jsonUtil.getJsonValue(content, "metadata.vendor")
274         //String type = jsonUtil.getJsonValue(content, "metadata.type")
275         String domain = jsonUtil.getJsonValue(content, "metadata.domainType")
276
277         Map<String, Object> nssiMap = execution.getVariable("nssiMap")
278         String servicename = execution.getVariable("sliceServiceInstanceName")
279         String nsiname = "nsi_"+servicename
280         nssiMap.put(domain,"""{
281                     "serviceInstanceId":"",
282                     "modelUuid":"${modelUuid}"
283                      }""")
284         execution.setVariable("nssiMap",nssiMap)
285     }
286
287     void prepareNSSIList(DelegateExecution execution){
288         logger.trace("Enter prepareNSSIList in DoAllocateNSIandNSSI()")
289         Map<String, Object> nssiMap = new HashMap<>()
290         Boolean isMoreNSSI = false
291         nssiMap = execution.getVariable("nssiMap")
292         List<String> keys=new ArrayList<String>(nssiMap.values())
293         List<String> nsstSequence = execution.getVariable("nsstSequence")
294         Integer currentIndex=0;
295         execution.setVariable("currentNssiIndex",currentIndex)
296         Integer maxIndex=keys.size()
297         execution.setVariable("maxIndex",maxIndex)
298         if(maxIndex>0)
299             isMoreNSSI=true
300         execution.setVariable("isMoreNSSI",isMoreNSSI)
301         logger.trace("Exit prepareNSSIList in DoAllocateNSIandNSSI()")
302     }
303
304
305     void getOneNSSIInfo(DelegateExecution execution){
306         logger.trace("Enter getOneNSSIInfo in DoAllocateNSIandNSSI()")
307
308         //ServiceDecomposition serviceDecompositionObj = execution.getVariable("serviceDecompositionObj")
309         Map<String, Object> nssiMap=execution.getVariable("nssiMap")
310         List<String> nsstSequence = execution.getVariable("nsstSequence")
311         String currentNSST= nsstSequence.get(execution.getVariable("currentNssiIndex"))
312         boolean isNSSIOptionAvailable = false
313         String nsstInput=nssiMap.get(currentNSST)
314         execution.setVariable("nsstInput",nsstInput)
315         String modelUuid = jsonUtil.getJsonValue(nsstInput, "modelUuid")
316         String nssiInstanceId = jsonUtil.getJsonValue(nsstInput, "serviceInstanceId")
317         String nssiserviceModelInfo = """{
318             "modelInvariantUuid":"",
319             "modelUuid":"${modelUuid}",
320             "modelVersion":""
321              }"""
322         Integer currentIndex = execution.getVariable("currentNssiIndex")
323         currentIndex=currentIndex+1;
324         execution.setVariable("currentNssiIndex",currentIndex)
325         execution.setVariable("nssiserviceModelInfo",nssiserviceModelInfo)
326         execution.setVariable("nssiInstanceId",nssiInstanceId)
327         logger.trace("Exit getOneNSSIInfo in DoAllocateNSIandNSSI()")
328     }
329
330     void updateCurrentIndex(DelegateExecution execution){
331
332         logger.trace("Enter updateCurrentIndex in DoAllocateNSIandNSSI()")
333         Integer currentIndex = execution.getVariable("currentNssiIndex")
334         Integer maxIndex = execution.getVariable("maxIndex")
335         if(currentIndex>=maxIndex)
336         {
337             Boolean isMoreNSSI=false
338             execution.setVariable("isMoreNSSI",isMoreNSSI)
339         }
340         logger.trace("Exit updateCurrentIndex in DoAllocateNSIandNSSI()")
341     }
342 }