1 package org.onap.so.bpmn.infrastructure.scripts
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
27 import javax.ws.rs.NotFoundException
28 import javax.ws.rs.core.UriBuilder
30 import static org.apache.commons.lang3.StringUtils.isBlank
32 class DoAllocateNSIandNSSI extends org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor{
34 private static final Logger logger = LoggerFactory.getLogger( DoAllocateNSIandNSSI.class);
36 ExceptionUtil exceptionUtil = new ExceptionUtil()
38 JsonUtils jsonUtil = new JsonUtils()
39 RequestsDbClient requestsDbClient = new RequestsDbClient()
42 * Pre Process the BPMN Flow Request
44 * generate the nsOperationKey
45 * generate the nsParameters
48 void preProcessRequest (DelegateExecution execution) {
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")
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")
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}",
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.")
83 if(isBlank(sliceParams.getSuggestNsiId()))
85 isNSIOptionAvailable=false
89 isNSIOptionAvailable=true
90 execution.setVariable('nsiServiceInstanceId',sliceParams.getSuggestNsiId())
91 execution.setVariable('nsiServiceInstanceName',sliceParams.getSuggestNsiName())
93 execution.setVariable("isNSIOptionAvailable",isNSIOptionAvailable)
94 logger.trace("Exit retriveSliceOption() of DoAllocateNSIandNSSI")
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")
105 String nsiServiceInstanceID = sliceParams.getSuggestNsiId()
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)
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()
117 // if(resourceClient.exists(nsiServiceuri)){
118 // execution.setVariable("nsi_resourceLink", nsiServiceuri.build().toString())
120 // exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service instance was not found in aai to " +
121 // "associate for service :"+serviceInstanceId)
123 }catch(BpmnError e) {
125 }catch (Exception ex){
126 String msg = "NSI suggested in the option doesn't exist. " + nsiServiceInstanceID
128 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
130 AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIObjectType.ALLOTTED_RESOURCE, execution.getVariable("globalSubscriberId"), execution.getVariable("serviceType"), nsiServiceInstanceId, allottedResourceId)
131 getAAIClient().connect(allottedResourceUri,nsiServiceuri)
133 List<String> nssiAssociated = new ArrayList<>()
134 RelationshipList relationshipList = nsiServiceInstance.getRelationshipList()
135 List<Relationship> relationships = relationshipList.getRelationship()
136 for(Relationship relationship in relationships)
138 if(relationship.getRelatedTo().equalsIgnoreCase("service-instance"))
140 String NSSIassociated = relationship.getRelatedLink().substring(relationship.getRelatedLink().lastIndexOf("/") + 1);
141 if(!NSSIassociated.equals(nsiServiceInstanceID))
142 nssiAssociated.add(NSSIassociated)
145 execution.setVariable("nssiAssociated",nssiAssociated)
146 execution.setVariable("nsiServiceInstanceName",nsiServiceInstance.getServiceInstanceName())
147 logger.trace("Exit update relationship in DoAllocateNSIandNSSI()")
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)
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()}"
168 }catch(NotFoundException e)
170 logger.debug("NSSI Service Instance not found in AAI: " + nssiID)
173 logger.debug("NSSI Service Instance not found in AAI: " + nssiID)
175 execution.setVariable("nssiMap",nssiMap)
178 logger.trace("Exit prepareNssiModelInfo in DoAllocateNSIandNSSI()")
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)
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)
211 } catch (BpmnError e) {
213 } catch (Exception ex) {
214 msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
216 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
218 Map<String, Object> nssiMap = new HashMap<>()
219 List<ServiceProxy> serviceProxyList = serviceDecomposition.getServiceProxy()
220 List<String> nsstModelInfoList = new ArrayList<>()
221 for(ServiceProxy serviceProxy : serviceProxyList)
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}",
231 nsstModelInfoList.add(nsstServiceModelInfo)
234 int maxIndex=nsstModelInfoList.size()
237 msg = "Exception in DoAllocateNSIandNSSI. There is no NSST associated with NST "
239 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
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()")
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)
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")
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}"
280 execution.setVariable("nssiMap",nssiMap)
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)
296 execution.setVariable("isMoreNSSI",isMoreNSSI)
297 logger.trace("Exit prepareNSSIList in DoAllocateNSIandNSSI()")
301 void getOneNSSIInfo(DelegateExecution execution){
302 logger.trace("Enter getOneNSSIInfo in DoAllocateNSIandNSSI()")
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}",
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()")
326 void updateCurrentIndex(DelegateExecution execution){
328 logger.trace("Enter updateCurrentIndex in DoAllocateNSIandNSSI()")
329 Integer currentIndex = execution.getVariable("currentNssiIndex")
330 Integer maxIndex = execution.getVariable("maxIndex")
331 if(currentIndex>=maxIndex)
333 Boolean isMoreNSSI=false
334 execution.setVariable("isMoreNSSI",isMoreNSSI)
336 logger.trace("Exit updateCurrentIndex in DoAllocateNSIandNSSI()")