1 package org.onap.so.bpmn.infrastructure.scripts
3 import com.fasterxml.jackson.core.type.TypeReference
4 import groovy.json.JsonBuilder
5 import groovy.json.JsonSlurper
6 import org.camunda.bpm.engine.delegate.BpmnError
7 import org.camunda.bpm.engine.delegate.DelegateExecution
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.logging.filter.base.ONAPComponents
12 import org.onap.so.beans.nsmf.SliceTaskParams
13 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
14 import org.onap.so.bpmn.common.scripts.ExceptionUtil
15 import org.onap.so.bpmn.common.scripts.OofUtils
16 import org.onap.so.bpmn.core.UrnPropertiesReader
17 import org.onap.so.bpmn.core.domain.ServiceDecomposition
18 import org.onap.so.bpmn.core.domain.ServiceProxy
19 import org.onap.so.bpmn.core.json.JsonUtils
20 import org.onap.so.client.HttpClient
21 import org.onap.so.client.HttpClientFactory
22 import org.onap.so.client.aai.AAIObjectType
23 import org.onap.so.client.aai.AAIResourcesClient
24 import org.onap.so.client.aai.entities.AAIResultWrapper
25 import org.onap.so.client.aai.entities.uri.AAIResourceUri
26 import org.onap.so.client.aai.entities.uri.AAIUriFactory
27 import org.onap.so.db.request.client.RequestsDbClient
28 import org.onap.so.db.request.beans.OrchestrationTask
29 import org.slf4j.Logger
30 import org.slf4j.LoggerFactory
31 import javax.ws.rs.NotFoundException
32 import javax.ws.rs.core.Response
34 import static org.apache.commons.lang3.StringUtils.isBlank
36 public class DoCreateSliceServiceOption extends AbstractServiceTaskProcessor{
38 private static final Logger logger = LoggerFactory.getLogger( DoCreateSliceServiceOption.class)
41 ExceptionUtil exceptionUtil = new ExceptionUtil()
43 JsonUtils jsonUtil = new JsonUtils()
45 RequestsDbClient requestsDbClient = new RequestsDbClient()
47 OofUtils oofUtils = new OofUtils()
50 * Pre Process the BPMN Flow Request
52 * generate the nsOperationKey
53 * generate the nsParameters
55 void preProcessRequest (DelegateExecution execution) {
57 logger.trace("Enter preProcessRequest()")
58 String taskID = execution.getVariable("taskID")
59 Boolean isSharable = true
60 String resourceSharingLevel = execution.getVariable("resourceSharingLevel")
61 if (resourceSharingLevel.equals("shared"))
63 execution.setVariable("isSharable",isSharable)
64 logger.trace("Exit preProcessRequest")
69 void getNSIOptionfromOOF(DelegateExecution execution) {
71 String urlString = UrnPropertiesReader.getVariable("mso.oof.endpoint", execution)
72 logger.debug( "get NSI option OOF Url: " + urlString)
73 boolean isNSISuggested = true
74 execution.setVariable("isNSISuggested",isNSISuggested)
75 String nsiInstanceId = ""
77 SliceTaskParams sliceTaskParams = execution.getVariable("sliceTaskParams")
78 //Prepare auth for OOF - Begin
80 String basicAuth = UrnPropertiesReader.getVariable("mso.oof.auth", execution)
81 String msokey = UrnPropertiesReader.getVariable("mso.msoKey", execution)
83 String basicAuthValue = utils.encrypt(basicAuth, msokey)
84 if (basicAuthValue != null) {
85 logger.debug( "Obtained BasicAuth username and password for OOF: " + basicAuthValue)
87 authHeader = utils.getBasicAuth(basicAuthValue, msokey)
88 execution.setVariable("BasicAuthHeaderValue", authHeader)
89 } catch (Exception ex) {
90 logger.debug( "Unable to encode username and password string: " + ex)
91 exceptionUtil.buildAndThrowWorkflowException(execution, 401, "Internal Error - Unable to " +
92 "encode username and password string")
95 logger.debug( "Unable to obtain BasicAuth - BasicAuth value null")
96 exceptionUtil.buildAndThrowWorkflowException(execution, 401, "Internal Error - BasicAuth " +
99 //Prepare auth for OOF - End
101 String requestId = execution.getVariable("msoRequestId")
102 Map<String, Object> profileInfo = execution.getVariable("serviceProfile")
103 String nstModelUuid = execution.getVariable("nstModelUuid")
104 String nstModelInvariantUuid = execution.getVariable("nstModelInvariantUuid")
105 String nstInfo = """"NSTInfo" : {
106 "invariantUUID":"${nstModelInvariantUuid}",
107 "UUID":"${nstModelUuid}"
110 String oofRequest = oofUtils.buildSelectNSIRequest(execution, requestId, nstInfo, profileInfo)
112 //send request to get NSI option - Begin
113 URL url = new URL(urlString+"/api/oof/v1/selectnsi")
114 HttpClient httpClient = new HttpClientFactory().newJsonClient(url, ONAPComponents.OOF)
115 httpClient.addAdditionalHeader("Authorization", authHeader)
116 Response httpResponse = httpClient.post(oofRequest)
118 int responseCode = httpResponse.getStatus()
119 logger.debug("OOF sync response code is: " + responseCode)
121 if(responseCode != 200){
122 exceptionUtil.buildAndThrowWorkflowException(execution, responseCode, "Received a Bad Sync Response from OOF.")
123 logger.debug("Info: No NSI suggested by OOF" )
126 if(httpResponse.hasEntity()){
127 String OOFResponse = httpResponse.readEntity(String.class)
128 execution.setVariable("OOFResponse", OOFResponse)
129 int index = 0 //This needs to be changed to derive a value when we add policy to decide the solution options.
130 Map OOFResponseObject = new JsonSlurper().parseText(OOFResponse)
131 if(execution.getVariable("isSharable" ) == true && OOFResponseObject.get("solutions").containsKey("sharedNSIsolutions")) {
132 nsiInstanceId = OOFResponseObject.get("solutions").get("sharedNSIsolutions").get(0).get("NSISolution").NSIId
133 nsiName = OOFResponseObject.get("solutions").get("sharedNSIsolutions").get(0).get("NSISolution").NSIName
134 sliceTaskParams.setNstId(nsiInstanceId)
135 sliceTaskParams.setSuggestNsiName(nsiName)
136 execution.setVariable("nsiInstanceId",nsiInstanceId)
137 execution.setVariable("nsiName",nsiName)
139 if(OOFResponseObject.get("solutions").containsKey("newNSISolutions")) {
140 List NSSImap = OOFResponseObject.get("solutions").get("newNSISolutions").get(index).get("NSSISolutions")
141 for(Map nssi : NSSImap) {
142 String nssiName = nssi.get("NSSISolution").NSSIName
143 String nssiId = nssi.get("NSSISolution").NSSIId
144 String domain = nssi.get("NSSISolution").domain.toUpperCase()
147 sliceTaskParams.setAnSuggestNssiId(nssiId)
148 sliceTaskParams.setAnSuggestNssiName(nssiName)
151 sliceTaskParams.setCnSuggestNssiId(nssiId)
152 sliceTaskParams.setCnSuggestNssiName(nssiName)
155 sliceTaskParams.setTnSuggestNssiId(nssiId)
156 sliceTaskParams.setTnSuggestNssiName(nssiName)
165 execution.setVariable("sliceTaskParams", sliceTaskParams)
166 logger.debug("Info: No NSI suggested by OOF" )
168 //send request to get NSI option - Begin
171 //send request to get NSI service Info - Begin
174 * Need to check whether its needed.
176 // logger.debug("Begin to query OOF suggetsed NSI from AAI ")
177 // if(isBlank(nsiInstanceId)){
178 // isNSISuggested = false
179 // execution.setVariable("isNSISuggested",isNSISuggested)
183 // String globalSubscriberId = execution.getVariable('globalSubscriberId')
184 // String serviceType = execution.getVariable('subscriptionServiceType')
185 // AAIResourcesClient resourceClient = new AAIResourcesClient()
186 // AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalSubscriberId, serviceType, nsiInstanceId)
187 // AAIResultWrapper wrapper = resourceClient.get(serviceInstanceUri, NotFoundException.class)
188 // Optional<org.onap.aai.domain.yang.ServiceInstance> si = wrapper.asBean(org.onap.aai.domain.yang.ServiceInstance.class)
189 // org.onap.aai.domain.yang.ServiceInstance nsiServiceInstance = si.get()
190 // execution.setVariable("nsiServiceInstance",nsiServiceInstance)
191 // isNSISuggested = true
192 // execution.setVariable("isNSISuggested",isNSISuggested)
193 // SliceTaskParams sliceTaskParams = execution.getVariable("sliceTaskParams")
194 // sliceTaskParams.setSuggestNsiId(nsiInstanceId)
195 // sliceTaskParams.setSuggestNsiName(si.get().getServiceInstanceName())
196 // execution.setVariable("sliceTaskParams", sliceTaskParams)
197 // logger.debug("Info: NSI suggested by OOF exist in AAI ")
198 // }catch(BpmnError e) {
200 // }catch(Exception ex) {
201 // String msg = "Internal Error in getServiceInstance: " + ex.getMessage()
202 // //exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
203 // logger.debug("Info: NSI suggested by OOF doesnt exist in AAI " + nsiInstanceId)
206 //send request to get NSI service Info - End
207 //${OrchestrationTaskHandler.createOrchestrationTask(execution.getVariable("OrchestrationTask"))}
208 logger.debug( "*** Completed options Call to OOF ***")
213 public void parseServiceProfile(DelegateExecution execution) {
214 logger.debug("Start parseServiceProfile")
215 String serviceType = execution.getVariable("serviceType")
216 Map<String, Object> serviceProfile = execution.getVariable("serviceProfile")
218 // set sliceProfile for three domains
219 Map<String, Object> sliceProfileTn = getSliceProfile(serviceType, "TN", serviceProfile)
220 Map<String, Object> sliceProfileCn = getSliceProfile(serviceType, "CN", serviceProfile)
221 Map<String, Object> sliceProfileAn = getSliceProfile(serviceType, "AN", serviceProfile)
223 execution.setVariable("sliceProfileTn", sliceProfileTn)
224 execution.setVariable("sliceProfileCn", sliceProfileCn)
225 execution.setVariable("sliceProfileAn", sliceProfileAn)
226 logger.debug("sliceProfileTn: " + sliceProfileTn)
227 logger.debug("sliceProfileCn: " + sliceProfileCn)
228 logger.debug("sliceProfileAn: " + sliceProfileAn)
230 logger.debug("Finish parseServiceProfile")
233 public Map getSliceProfile(String serviceType, String domain, Map<String, Object> serviceProfile) {
234 String variablePath = "nsmf." + serviceType + ".profileMap" + domain
235 String profileMapStr = UrnPropertiesReader.getVariable(variablePath)
236 logger.debug("Profile map for " + domain + " : " + profileMapStr)
237 Map<String, String> profileMaps = objectMapper.readValue(profileMapStr, new TypeReference<Map<String, String>>(){})
238 Map<String, Object> sliceProfileTn = [:]
239 for (Map.Entry<String, String> profileMap : profileMaps) {
240 sliceProfileTn.put(profileMap.key, serviceProfile.get(profileMap.value))
243 return sliceProfileTn
247 void prepareNSSIList(DelegateExecution execution)
249 ServiceDecomposition serviceDecomposition= execution.getVariable("serviceDecomposition")
250 List<String> nssiAssociated = new ArrayList<>()
251 Map<String, String> nssimap = new HashMap<>()
252 String nsiInstanceId=execution.getVariable("nsiInstanceId")
253 String globalSubscriberId = execution.getVariable("globalSubscriberId")
254 String serviceType = execution.getVariable("subscriptionServiceType")
258 ServiceInstance si = execution.getVariable("nsiServiceInstance")
259 //List<Relationship> relationships = si.getRelationshipList().getRelationship().stream().filter(relation ->
260 // relation.getRelatedTo().equalsIgnoreCase("service-instance"))
261 RelationshipList relationshipList = si.getRelationshipList()
262 List<Relationship> relationships = relationshipList.getRelationship()
263 for(Relationship relationship in relationships)
265 if(relationship.getRelatedTo().equalsIgnoreCase("service-instance"))
267 String NSSIassociated = relationship.getRelatedLink().substring(relationship.getRelatedLink().lastIndexOf("/") + 1);
268 if(!NSSIassociated.equals(nsiInstanceId))
269 nssiAssociated.add(NSSIassociated)
272 }catch(BpmnError e) {
274 }catch(Exception ex) {
275 String msg = "Internal Error in getServiceInstance: " + ex.getMessage()
276 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
278 Map<String, Object> params = execution.getVariable("params")
279 SliceTaskParams sliceTaskParams = execution.getVariable("sliceTaskParams")
280 for(String nssiID in nssiAssociated)
283 AAIResourcesClient resourceClient = new AAIResourcesClient()
284 AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalSubscriberId, serviceType, nssiID)
285 AAIResultWrapper wrapper = resourceClient.get(serviceInstanceUri, NotFoundException.class)
286 Optional<org.onap.aai.domain.yang.ServiceInstance> si = wrapper.asBean(org.onap.aai.domain.yang.ServiceInstance.class)
287 org.onap.aai.domain.yang.ServiceInstance nssi = si.get()
289 String domain = nssi.getEnvironmentContext().toString().toUpperCase()
292 sliceTaskParams.setAnSuggestNssiId(nssi.getServiceInstanceId())
293 sliceTaskParams.setAnSuggestNssiName(nssi.getServiceInstanceName())
296 sliceTaskParams.setCnSuggestNssiId(nssi.getServiceInstanceId())
297 sliceTaskParams.setCnSuggestNssiName(nssi.getServiceInstanceName())
300 sliceTaskParams.setTnSuggestNssiId(nssi.getServiceInstanceId())
301 sliceTaskParams.setTnSuggestNssiName(nssi.getServiceInstanceName())
306 }catch(NotFoundException e)
308 logger.debug("NSSI Service Instance not found in AAI: " + nssiID)
311 logger.debug("NSSI Service Instance not found in AAI: " + nssiID)
315 String nstName = serviceDecomposition.getModelInfo().getModelName()
316 sliceTaskParams.setNstName(nstName)
317 String nstId = serviceDecomposition.getModelInfo().getModelUuid()
318 sliceTaskParams.setNstId(nstId)
319 execution.setVariable("sliceTaskParams",sliceTaskParams)
324 void updateOptionsInDB(DelegateExecution execution) {
325 logger.debug("Updating options with default value since not sharable : Begin ")
326 String taskID = execution.getVariable("taskID")
327 String params = execution.getVariable("params")
328 logger.debug("Updating options with default value since not sharable : End ")
332 void prepareNSTDecompose(DelegateExecution execution) {
334 String modelUuid = execution.getVariable("nstModelUuid")
335 String modelInvariantUuid = execution.getVariable("nstModelInvariantUuid")
337 String serviceModelInfo = """{
338 "modelInvariantUuid":"${modelInvariantUuid}",
339 "modelUuid":"${modelUuid}",
342 execution.setVariable("serviceModelInfo", serviceModelInfo)
345 void prepareNSSTDecompose(DelegateExecution execution) {
346 Boolean isMoreNSSTtoProcess = false
347 Integer maxNSST = execution.getVariable("maxNSST")
348 Integer currentNSST=execution.getVariable("currentNSST")
349 List<String> nsstModelUUIDList = new ArrayList<>()
350 nsstModelUUIDList = execution.getVariable("nsstModelUUIDList")
351 String modelUuid = nsstModelUUIDList.get(currentNSST)
352 String serviceModelInfo = """{
353 "modelInvariantUuid":"",
354 "modelUuid":"${modelUuid}",
357 execution.setVariable("serviceModelInfo", serviceModelInfo)
358 currentNSST=currentNSST+1
359 if(currentNSST<maxNSST)
360 isMoreNSSTtoProcess=true
361 execution.setVariable("isMoreNSSTtoProcess",isMoreNSSTtoProcess)
362 execution.setVariable("maxNSST",maxNSST)
363 execution.setVariable("currentNSST",currentNSST)
367 void updateStatusInDB(DelegateExecution execution) {
369 String taskID = execution.getVariable("taskID")
370 //OrchestrationTask orchestrationTask = requestsDbClient.getNetworkSliceOption(taskID);
371 //orchestrationTask.setTaskStage("wait to confirm")
372 //requestsDbClient.updateNetworkSliceOption(orchestrationTask)
375 void prepareNSSTlistfromNST(DelegateExecution execution) {
376 //Need to update this part from decomposition.
377 logger.trace("Enter prepareNSSTlistfromNST()")
378 Boolean isMoreNSSTtoProcess = false
379 ServiceDecomposition serviceDecomposition= execution.getVariable("serviceDecomposition")
380 SliceTaskParams sliceTaskParams = execution.getVariable("sliceTaskParams")
381 String nstName = serviceDecomposition.getModelInfo().getModelName()
382 sliceTaskParams.setNstName(nstName)
383 String nstId = serviceDecomposition.getModelInfo().getModelUuid()
384 sliceTaskParams.setNstId(nstId)
385 execution.setVariable("sliceTaskParams",sliceTaskParams)
387 List<ServiceProxy> proxyList = serviceDecomposition.getServiceProxy()
388 List<String> nsstModelUUIDList = new ArrayList<>()
389 for(ServiceProxy serviceProxy:proxyList)
390 nsstModelUUIDList.add(serviceProxy.getSourceModelUuid())
391 execution.setVariable("nsstModelUUIDList",nsstModelUUIDList)
392 Integer maxNSST = nsstModelUUIDList.size()
393 Integer currentNSST=0
394 execution.setVariable("maxNSST",maxNSST)
395 execution.setVariable("currentNSST",currentNSST)
396 if(currentNSST<maxNSST)
397 isMoreNSSTtoProcess=true
398 execution.setVariable("isMoreNSSTtoProcess",isMoreNSSTtoProcess)
399 logger.trace("Exit prepareNSSTlistfromNST()")
404 void getNSSTOption(DelegateExecution execution) {
405 ServiceDecomposition serviceDecomposition= execution.getVariable("serviceDecomposition")
406 String urlString = UrnPropertiesReader.getVariable("mso.oof.endpoint", execution)
407 String globalSubscriberId = execution.getVariable("globalSubscriberId")
408 String serviceType = execution.getVariable("subscriptionServiceType")
409 String nssiInstanceId =""
411 SliceTaskParams sliceTaskParams = execution.getVariable("sliceTaskParams")
412 logger.debug( "get NSI option OOF Url: " + urlString)
413 boolean isNSISuggested = false
414 execution.setVariable("isNSISuggested",isNSISuggested)
416 //Prepare auth for OOF - Begin
418 String basicAuth = UrnPropertiesReader.getVariable("mso.oof.auth", execution)
419 String msokey = UrnPropertiesReader.getVariable("mso.msoKey", execution)
421 String basicAuthValue = utils.encrypt(basicAuth, msokey)
422 if (basicAuthValue != null) {
423 logger.debug( "Obtained BasicAuth username and password for OOF: " + basicAuthValue)
425 authHeader = utils.getBasicAuth(basicAuthValue, msokey)
426 execution.setVariable("BasicAuthHeaderValue", authHeader)
427 } catch (Exception ex) {
428 logger.debug( "Unable to encode username and password string: " + ex)
429 exceptionUtil.buildAndThrowWorkflowException(execution, 401, "Internal Error - Unable to " +
430 "encode username and password string")
433 logger.debug( "Unable to obtain BasicAuth - BasicAuth value null")
434 exceptionUtil.buildAndThrowWorkflowException(execution, 401, "Internal Error - BasicAuth " +
437 //Prepare auth for OOF - End
438 //Prepare send request to OOF - Begin
439 String requestId = execution.getVariable("msoRequestId")
440 Map<String, Object> profileInfo = execution.getVariable("serviceProfile")
441 String nsstModelInvariantUuid = serviceDecomposition.getModelInfo().getModelInvariantUuid()
442 String nsstModelUuid = serviceDecomposition.getModelInfo().getModelUuid()
443 String nsstInfo = """"NSSTInfo": {
444 "invariantUUID":"${nsstModelInvariantUuid}",
445 "UUID":"${nsstModelUuid}"
447 String oofRequest = oofUtils.buildSelectNSSIRequest(execution, requestId, nsstInfo ,profileInfo)
450 URL url = new URL(urlString+"/api/oof/v1/selectnssi")
451 HttpClient httpClient = new HttpClientFactory().newJsonClient(url, ONAPComponents.OOF)
452 httpClient.addAdditionalHeader("Authorization", authHeader)
453 Response httpResponse = httpClient.post(oofRequest)
455 int responseCode = httpResponse.getStatus()
456 logger.debug("OOF sync response code is: " + responseCode)
458 if(responseCode != 200){
459 exceptionUtil.buildAndThrowWorkflowException(execution, responseCode, "Received a Bad Sync Response from OOF.")
462 if(httpResponse.hasEntity()){
463 String OOFResponse = httpResponse.readEntity(String.class)
464 execution.setVariable("OOFResponse", OOFResponse)
465 nssiInstanceId = jsonUtil.getJsonValue(OOFResponse, "NSSIIInfo.NSSIID")
466 nssiName = jsonUtil.getJsonValue(OOFResponse, "NSSIInfo.NSSIName")
467 execution.setVariable("nssiInstanceId",nssiInstanceId)
468 execution.setVariable("nssiName",nssiName)
470 if(isBlank(nssiInstanceId)){
471 logger.debug( "There is no valid NSST suggested by OOF.")
475 AAIResourcesClient resourceClient = new AAIResourcesClient()
476 AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalSubscriberId, serviceType, nssiInstanceId)
477 AAIResultWrapper wrapper = resourceClient.get(serviceInstanceUri, NotFoundException.class)
478 Optional<org.onap.aai.domain.yang.ServiceInstance> si = wrapper.asBean(org.onap.aai.domain.yang.ServiceInstance.class)
479 org.onap.aai.domain.yang.ServiceInstance nssi = si.get()
481 String domain = nssi.getEnvironmentContext().toString().toUpperCase()
484 sliceTaskParams.setAnSuggestNssiId(nssi.getServiceInstanceId())
485 sliceTaskParams.setAnSuggestNssiName(nssi.getServiceInstanceName())
488 sliceTaskParams.setCnSuggestNssiId(nssi.getServiceInstanceId())
489 sliceTaskParams.setCnSuggestNssiName(nssi.getServiceInstanceName())
492 sliceTaskParams.setTnSuggestNssiId(nssi.getServiceInstanceId())
493 sliceTaskParams.setTnSuggestNssiName(nssi.getServiceInstanceName())
498 }catch(NotFoundException e)
500 logger.debug("NSSI Service Instance not found in AAI: " + nssiInstanceId)
503 logger.debug("NSSI Service Instance not found in AAI: " + nssiInstanceId)
508 //Prepare send request to OOF - End
510 // String content = serviceDecomposition.getServiceInfo().getServiceArtifact().get(0).getContent()
511 // String nsstID = jsonUtil.getJsonValue(content, "metadata.id")
512 // String vendor = jsonUtil.getJsonValue(content, "metadata.vendor")
513 // String domain = jsonUtil.getJsonValue(content, "metadata.domainType")
514 // String type = jsonUtil.getJsonValue(content, "metadata.type")
515 // String nsstContentInfo = """{
516 // "NsstID":"${nsstID}",
517 // "Vendor":"${vendor}",
521 logger.debug("Prepare NSSI option completed ")