Merge "Fix issues in shared RAN NSSI flow"
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCreateSliceServiceOption.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  # Copyright (c) 2020, CMCC Technologies Co., Ltd.
6  #
7  # Licensed under the Apache License, Version 2.0 (the "License")
8  # you may not use this file except in compliance with the License.
9  # You may obtain a copy of the License at
10  #
11  #       http://www.apache.org/licenses/LICENSE-2.0
12  #
13  # Unless required by applicable law or agreed to in writing, software
14  # distributed under the License is distributed on an "AS IS" BASIS,
15  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  # See the License for the specific language governing permissions and
17  # limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.scripts
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import com.fasterxml.jackson.core.type.TypeReference
25 import org.camunda.bpm.engine.delegate.DelegateExecution
26 import org.onap.aai.domain.yang.ServiceInstance
27 import org.onap.so.beans.nsmf.EsrInfo
28 import org.onap.so.beans.nsmf.NetworkType
29 import org.onap.so.beans.nsmf.NssmfAdapterNBIRequest
30 import org.onap.so.beans.nsmf.QuerySubnetCapability
31 import org.onap.so.beans.nsmf.SliceProfileAdapter
32 import org.onap.so.beans.nsmf.SliceTaskParamsAdapter
33 import org.onap.so.beans.nsmf.oof.SubnetCapability
34 import org.onap.so.beans.nsmf.oof.SubnetType
35 import org.onap.so.beans.nsmf.oof.TemplateInfo
36 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
37 import org.onap.so.bpmn.common.scripts.ExceptionUtil
38 import org.onap.so.bpmn.common.scripts.NssmfAdapterUtils
39 import org.onap.so.bpmn.common.scripts.OofUtils
40 import org.onap.so.bpmn.core.UrnPropertiesReader
41 import org.onap.so.bpmn.core.domain.AllottedResource
42 import org.onap.so.bpmn.core.domain.ModelInfo
43 import org.onap.so.bpmn.core.domain.ServiceDecomposition
44 import org.onap.so.bpmn.core.json.JsonUtils
45 import org.slf4j.Logger
46 import org.slf4j.LoggerFactory
47 import org.springframework.util.StringUtils
48
49
50 class DoCreateSliceServiceOption extends AbstractServiceTaskProcessor{
51
52     private static final Logger logger = LoggerFactory.getLogger(DoCreateSliceServiceOption.class)
53
54     ExceptionUtil exceptionUtil = new ExceptionUtil()
55
56     JsonUtils jsonUtil = new JsonUtils()
57
58     OofUtils oofUtils = new OofUtils()
59
60     AAISliceUtil aaiSliceUtil = new AAISliceUtil()
61
62     private static final ObjectMapper objectMapper = new ObjectMapper()
63
64     private NssmfAdapterUtils nssmfAdapterUtils = new NssmfAdapterUtils(httpClientFactory, jsonUtil)
65
66     private static final String QUERY_SUB_NET_CAPABILITY = "/api/rest/provMns/v1/NSS/subnetCapabilityQuery"
67
68     private static final String QUERY_NSSI_SELECTION_CAPABILITY = "/api/rest/provMns/v1/NSS/NSSISelectionCapability"
69
70     void preProcessRequest (DelegateExecution execution) {
71     }
72
73     /**
74      * prepare the params for decompose nst
75      * @param execution
76      */
77     void prepareDecomposeNST(DelegateExecution execution) {
78
79         SliceTaskParamsAdapter sliceTaskParams =
80                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
81
82         String modelUuid = sliceTaskParams.getNSTInfo().getUUID()
83         String modelInvariantUuid = sliceTaskParams.getNSTInfo().getInvariantUUID()
84
85         String serviceModelInfo = """{
86             "modelInvariantUuid":"${modelInvariantUuid}",
87             "modelUuid":"${modelUuid}",
88             "modelVersion":""
89              }"""
90         execution.setVariable("nstServiceModelInfo", serviceModelInfo)
91     }
92
93     /**
94      * process the result of NST Decomposition
95      * @param execution
96      */
97     public void processDecompositionNST(DelegateExecution execution) {
98
99         List<TemplateInfo> nsstInfos = new ArrayList<>()
100         ServiceDecomposition nstServiceDecomposition =
101                 execution.getVariable("nstServiceDecomposition") as ServiceDecomposition
102
103         List<AllottedResource> allottedResources = nstServiceDecomposition.getAllottedResources()
104         for (AllottedResource allottedResource : allottedResources) {
105             TemplateInfo nsstInfo = new TemplateInfo()
106             nsstInfo.setUUID(allottedResource.getProvidingServiceModelUuid())
107             nsstInfo.setInvariantUUID(allottedResource.getProvidingServiceModelInvariantUuid())
108             nsstInfo.setName(allottedResource.getProvidingServiceModelName())
109             nsstInfos.add(nsstInfo)
110         }
111         execution.setVariable("nsstInfos", nsstInfos)
112
113         execution.setVariable("maxNsstIndex", allottedResources.size() - 1)
114         execution.setVariable("currentNsstIndex", 0)
115
116         List<ServiceDecomposition> nsstServiceDecompositions = new ArrayList<>()
117         execution.setVariable("nsstServiceDecompositions", nsstServiceDecompositions)
118     }
119
120     /**
121      * prepare the params for decompose nsst
122      * @param execution
123      */
124     public void prepareDecomposeNSST(DelegateExecution execution) {
125
126         List<TemplateInfo> nsstInfos = execution.getVariable("nsstInfos") as List<TemplateInfo>
127         int index = execution.getVariable("currentNsstIndex") as Integer
128
129         String modelUuid = nsstInfos.get(index).getUUID()
130         String modelInvariantUuid = nsstInfos.get(index).getInvariantUUID()
131
132         String serviceModelInfo = """{
133             "modelInvariantUuid":"${modelInvariantUuid}",
134             "modelUuid":"${modelUuid}",
135             "modelVersion":""
136              }"""
137         execution.setVariable("nsstServiceModelInfo", serviceModelInfo)
138     }
139
140     /**
141      * process the result of NSST Decomposition
142      * @param execution
143      */
144     public void processDecompositionNSST(DelegateExecution execution) {
145         List<ServiceDecomposition> nsstServiceDecompositions =
146                 execution.getVariable("nsstServiceDecompositions") as List<ServiceDecomposition>
147
148         ServiceDecomposition nsstServiceDecomposition =
149                 execution.getVariable("nsstServiceDecomposition") as ServiceDecomposition
150
151         nsstServiceDecompositions.add(nsstServiceDecomposition)
152
153         execution.setVariable("nsstServiceDecompositions", nsstServiceDecompositions)
154
155         int num = execution.getVariable("maxNsstIndex") as Integer
156         int index = execution.getVariable("currentNsstIndex") as Integer
157
158         List<TemplateInfo> nsstInfos = execution.getVariable("nsstInfos") as List<TemplateInfo>
159         nsstInfos.get(index).name = nsstServiceDecomposition.modelInfo.modelName
160         execution.setVariable("nsstInfos", nsstInfos)
161         execution.setVariable("currentNsstIndex", index + 1)
162
163         if (index >= num) {
164             execution.setVariable("nsstHandleContinue", false)
165         } else {
166             execution.setVariable("nsstHandleContinue", true)
167         }
168     }
169
170     /**
171      * set nsst info to sliceTaskParams by type
172      * @param execution
173      */
174     public void handleNsstByType(DelegateExecution execution) {
175
176         SliceTaskParamsAdapter sliceParams =
177                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
178
179         List<ServiceDecomposition> nsstServiceDecompositions =
180                 execution.getVariable("nsstServiceDecompositions") as List<ServiceDecomposition>
181
182         List<SubnetCapability> subnetCapabilities = new ArrayList<>()
183
184         for (ServiceDecomposition serviceDecomposition : nsstServiceDecompositions) {
185             handleByType(execution, serviceDecomposition, sliceParams, subnetCapabilities)
186         }
187
188         execution.setVariable("sliceTaskParams", sliceParams)
189         execution.setVariable("subnetCapabilities", subnetCapabilities)
190         execution.setVariable("queryNsiFirst", true)
191         logger.debug("sliceTaskParams= " + sliceParams.toString())
192     }
193
194     private void handleByType(DelegateExecution execution, ServiceDecomposition serviceDecomposition,
195             SliceTaskParamsAdapter sliceParams, List<SubnetCapability> subnetCapabilities) {
196         ModelInfo modelInfo = serviceDecomposition.getModelInfo()
197         String vendor = serviceDecomposition.getServiceRole()
198         SubnetType subnetType = convertServiceCategory(serviceDecomposition.getServiceCategory())
199
200         switch (subnetType) {
201             case SubnetType.TN_BH:
202                 sliceParams.tnBHSliceTaskInfo.vendor = vendor
203                 sliceParams.tnBHSliceTaskInfo.subnetType = subnetType
204                 sliceParams.tnBHSliceTaskInfo.networkType = subnetType.networkType
205                 sliceParams.tnBHSliceTaskInfo.NSSTInfo.UUID = modelInfo.getModelUuid()
206                 sliceParams.tnBHSliceTaskInfo.NSSTInfo.invariantUUID = modelInfo.getModelInvariantUuid()
207                 sliceParams.tnBHSliceTaskInfo.NSSTInfo.name = modelInfo.getModelName()
208                 break
209             case SubnetType.TN_MH:
210                 sliceParams.tnMHSliceTaskInfo.vendor = vendor
211                 sliceParams.tnMHSliceTaskInfo.subnetType = subnetType
212                 sliceParams.tnMHSliceTaskInfo.networkType = subnetType.networkType
213                 sliceParams.tnMHSliceTaskInfo.NSSTInfo.UUID = modelInfo.getModelUuid()
214                 sliceParams.tnMHSliceTaskInfo.NSSTInfo.invariantUUID = modelInfo.getModelInvariantUuid()
215                 sliceParams.tnMHSliceTaskInfo.NSSTInfo.name = modelInfo.getModelName()
216                 break
217             case SubnetType.TN_FH:
218                 sliceParams.tnFHSliceTaskInfo.vendor = vendor
219                 sliceParams.tnFHSliceTaskInfo.subnetType = subnetType
220                 sliceParams.tnFHSliceTaskInfo.networkType = subnetType.networkType
221                 sliceParams.tnFHSliceTaskInfo.NSSTInfo.UUID = modelInfo.getModelUuid()
222                 sliceParams.tnFHSliceTaskInfo.NSSTInfo.invariantUUID = modelInfo.getModelInvariantUuid()
223                 sliceParams.tnFHSliceTaskInfo.NSSTInfo.name = modelInfo.getModelName()
224                 break
225             case SubnetType.AN_NF:
226                 sliceParams.anNFSliceTaskInfo.vendor = vendor
227                 sliceParams.anNFSliceTaskInfo.subnetType = subnetType
228                 sliceParams.anNFSliceTaskInfo.networkType = subnetType.networkType
229                 sliceParams.anNFSliceTaskInfo.NSSTInfo.UUID = modelInfo.getModelUuid()
230                 sliceParams.anNFSliceTaskInfo.NSSTInfo.invariantUUID = modelInfo.getModelInvariantUuid()
231                 sliceParams.anNFSliceTaskInfo.NSSTInfo.name = modelInfo.getModelName()
232                 break
233             case SubnetType.AN:
234                 sliceParams.anSliceTaskInfo.vendor = vendor
235                 sliceParams.anSliceTaskInfo.subnetType = subnetType
236                 sliceParams.anSliceTaskInfo.networkType = subnetType.networkType
237                 sliceParams.anSliceTaskInfo.NSSTInfo.UUID = modelInfo.getModelUuid()
238                 sliceParams.anSliceTaskInfo.NSSTInfo.invariantUUID = modelInfo.getModelInvariantUuid()
239                 sliceParams.anSliceTaskInfo.NSSTInfo.name = modelInfo.getModelName()
240                 break
241             case SubnetType.CN:
242                 sliceParams.cnSliceTaskInfo.vendor = vendor
243                 sliceParams.cnSliceTaskInfo.subnetType = subnetType
244                 sliceParams.cnSliceTaskInfo.networkType = subnetType.networkType
245                 sliceParams.cnSliceTaskInfo.NSSTInfo.UUID = modelInfo.getModelUuid()
246                 sliceParams.cnSliceTaskInfo.NSSTInfo.invariantUUID = modelInfo.getModelInvariantUuid()
247                 sliceParams.cnSliceTaskInfo.NSSTInfo.name = modelInfo.getModelName()
248                 break
249             default:
250                 subnetType = null
251                 break
252         }
253         if (null == subnetType) {
254             def msg = "Get subnetType failed, modelUUId=" + modelInfo.getModelUuid()
255             logger.error(msg)
256             exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
257         }
258         String response = querySubnetCapability(execution, vendor, subnetType)
259         if (!StringUtils.isEmpty(response)) {
260             SubnetCapability subnetCapability = new SubnetCapability()
261             Map<String, Object> result = objectMapper.readValue(response, Map.class)
262             for (Map.Entry<String, Object> entry : result.entrySet()) {
263                 subnetCapability.setDomainType(entry.getKey())
264                 subnetCapability.setCapabilityDetails(entry.getValue())
265             }
266             subnetCapabilities.add(subnetCapability)
267         }
268     }
269
270     /**
271      * get subnetType from serviceCategory
272      * @return
273      */
274     private SubnetType convertServiceCategory(String serviceCategory){
275         if(serviceCategory ==~ /CN.*/){
276             return SubnetType.CN
277         }
278         if (serviceCategory ==~ /AN.*/){
279             return SubnetType.AN
280         }
281         if (serviceCategory ==~ /TN.*BH.*/){
282             return SubnetType.TN_BH
283         }
284         if(serviceCategory ==~ /TN.*MH.*/){
285             return SubnetType.TN_MH
286         }
287         if(serviceCategory ==~ /TN.*FH.*/){
288             return SubnetType.TN_FH
289         }
290
291         return null
292     }
293
294     /**
295      * query Subnet Capability of TN AN CN
296      * @param execution
297      */
298     private String querySubnetCapability(DelegateExecution execution, String vendor, SubnetType subnetType) {
299
300         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_SUB_NET_CAPABILITY,
301                 buildQuerySubnetCapRequest(vendor, subnetType))
302         return response
303     }
304
305     /**
306      * build request body for querying Subnet Capability
307      * @param vendor
308      * @param subnetTypes
309      * @param networkType
310      * @return
311      */
312     private static NssmfAdapterNBIRequest buildQuerySubnetCapRequest(String vendor, SubnetType subnetType) {
313         NssmfAdapterNBIRequest request = new NssmfAdapterNBIRequest()
314
315         List<String> subnetTypes =  new ArrayList<>()
316         subnetTypes.add(subnetType.subnetType)
317
318         QuerySubnetCapability req = new QuerySubnetCapability()
319         req.setSubnetTypes(subnetTypes)
320
321         request.setSubnetCapabilityQuery(req)
322
323         EsrInfo esrInfo = new EsrInfo()
324         esrInfo.setVendor(vendor)
325         esrInfo.setNetworkType(subnetType.networkType)
326         request.setEsrInfo(esrInfo)
327
328         return request
329     }
330
331     /**
332      * todo: need rewrite
333      * prepare select nsi request
334      * @param execution
335      */
336     public void preNSIRequest(DelegateExecution execution) {
337         boolean preferReuse = execution.getVariable("needQuerySliceProfile") ? false : true
338
339         String urlString = UrnPropertiesReader.getVariable("mso.oof.endpoint", execution)
340         logger.debug( "get NSI option OOF Url: " + urlString)
341
342         String requestId = execution.getVariable("msoRequestId")
343         String messageType = "NSISelectionResponse"
344
345         execution.setVariable("nsiSelectionUrl", "/api/oof/selection/nsi/v1")
346         execution.setVariable("nsiSelection_messageType", messageType)
347         execution.setVariable("nsiSelection_correlator", requestId)
348         String timeout = UrnPropertiesReader.getVariable("mso.adapters.oof.timeout", execution)
349         execution.setVariable("nsiSelection_timeout", timeout)
350
351         SliceTaskParamsAdapter sliceParams =
352                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
353
354         Map<String, Object> profileInfo = sliceParams.getServiceProfile()
355         profileInfo.remove("profileId")
356         TemplateInfo nstInfo = sliceParams.getNSTInfo()
357
358         List<TemplateInfo> nsstInfos = execution.getVariable("nsstInfos") as List<TemplateInfo>
359
360         List<SubnetCapability> subnetCapabilities =
361                 execution.getVariable("subnetCapabilities") as List<SubnetCapability>
362
363         String oofRequest = oofUtils.buildSelectNSIRequest(requestId, nstInfo, nsstInfos,
364                 messageType, profileInfo, subnetCapabilities, 600, preferReuse)
365
366         execution.setVariable("nsiSelection_oofRequest", oofRequest)
367         logger.debug("Sending request to OOF: " + oofRequest)
368     }
369
370     /**
371      * process select nsi response
372      * @param execution
373      */
374     public void processNSIResp(DelegateExecution execution) {
375
376         SliceTaskParamsAdapter sliceTaskParams =
377                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
378
379         String oofResponse = execution.getVariable("nsiSelection_oofResponse")
380         logger.debug("NSI oofResponse is: " + oofResponse)
381         execution.setVariable("oofResponse", oofResponse)
382         //This needs to be changed to derive a value when we add policy to decide the solution options.
383
384         Map<String, Object> resMap = objectMapper.readValue(oofResponse, Map.class)
385         String requestStatus = resMap.get("requestStatus")
386         if (!StringUtils.isEmpty(requestStatus) && requestStatus == "error") {
387             exceptionUtil.buildWorkflowException(execution, 7000, "get nsi from oof error: " + oofResponse)
388             return
389         }
390
391         List<Map<String, Object>> nsiSolutions = (List<Map<String, Object>>) resMap.get("solutions")
392
393         Map<String, Object> solution = nsiSolutions?.get(0)
394
395         if (solution != null) {
396             if (execution.getVariable("queryNsiFirst")) {
397                 if (solution.get("existingNSI")) {
398                     processSharedNSI(solution, sliceTaskParams, execution)
399                     execution.setVariable("needQuerySliceProfile", true)
400                 } else {
401                     processNewSliceProfiles(solution, sliceTaskParams)
402                     execution.setVariable("needQuerySliceProfile", false)
403                 }
404                 execution.setVariable("queryNsiFirst", false)
405             } else {
406                 processNewSliceProfiles(solution, sliceTaskParams)
407                 execution.setVariable("needQuerySliceProfile", false)
408             }
409         }
410         execution.setVariable("sliceTaskParams", sliceTaskParams)
411         logger.debug("after req to oof for nis select, sliceTaskParams = " + sliceTaskParams)
412         logger.debug("*** Completed options Call to OOF ***")
413     }
414
415     private void processSharedNSI(Map<String, Object> solution, SliceTaskParamsAdapter sliceParams, DelegateExecution execution) {
416         Map<String, Object> sharedNSISolution = solution.get("sharedNSISolution") as Map
417         String nsiId = sharedNSISolution.get("NSIId")
418         String nsiName = sharedNSISolution.get("NSIName")
419         sliceParams.setSuggestNsiId(nsiId)
420         sliceParams.setSuggestNsiName(nsiName)
421
422         List<String> nssiId = aaiSliceUtil.getNSSIIdList(execution,nsiId)
423         List<ServiceInstance> nssiInstances = aaiSliceUtil.getNSSIListFromAAI(execution, nssiId)
424
425         Map<String, Object> nssiSolution = new HashMap<>()
426         for(ServiceInstance instance: nssiInstances){
427             nssiSolution.put("NSSIId", instance.getServiceInstanceId())
428             nssiSolution.put("NSSIName", instance.getServiceInstanceName())
429             SubnetType subnetType = instance.getWorkloadContext() as SubnetType
430             processNssiResult(sliceParams, subnetType, nssiSolution)
431         }
432
433     }
434
435     private void processNewSliceProfiles(Map<String, Object> solution, SliceTaskParamsAdapter sliceParams) {
436         Map<String, Object> newNSISolution = solution.get("newNSISolution") as Map
437         List<Map> sliceProfiles = newNSISolution.get("sliceProfiles") as List<Map>
438         handleSliceProfiles(sliceProfiles, sliceParams)
439     }
440
441     static def handleSliceProfiles(List<Map> sliceProfiles, SliceTaskParamsAdapter sliceParams) {
442         for (Map sliceProfile : sliceProfiles) {
443             String domainType = sliceProfile.get("domainType")
444             sliceProfile.remove("domainType")
445             SliceProfileAdapter adapter = objectMapper.readValue(objectMapper.writeValueAsString(sliceProfile), SliceProfileAdapter.class)
446             switch (domainType.toLowerCase()) {
447                 case "tn_bh":
448                     sliceParams.tnBHSliceTaskInfo.sliceProfile = adapter
449                     break
450                 case "tn_fh":
451                     sliceParams.tnFHSliceTaskInfo.sliceProfile = adapter
452                     break
453                 case "tn_mh":
454                     sliceParams.tnMHSliceTaskInfo.sliceProfile = adapter
455                     break
456                 case "an_nf":
457                     sliceParams.anNFSliceTaskInfo.sliceProfile = adapter
458                     break
459                 case "an":
460                     sliceParams.anSliceTaskInfo.sliceProfile = adapter
461                     break
462                 case "cn":
463                     sliceParams.cnSliceTaskInfo.sliceProfile = adapter
464                     break
465                 default:
466                     break
467             }
468         }
469     }
470
471     /**
472      * get NSSI Selection Capability for AN
473      * @param execution
474      */
475     void getNSSISelectionCap4AN(DelegateExecution execution) {
476
477        SliceTaskParamsAdapter sliceParams =
478                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
479
480         def vendor = sliceParams.anSliceTaskInfo.vendor
481
482         String strRequest = buildNSSISelectionReq(vendor, NetworkType.ACCESS)
483
484         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_NSSI_SELECTION_CAPABILITY, strRequest)
485
486         Map<String, Object> resMap = objectMapper.readValue(response, Map.class)
487
488         String selection = resMap.get("selection")
489
490
491         if ("NSMF".equalsIgnoreCase(selection)) {
492             execution.setVariable("NEED_AN_NSSI_SELECTION", true)
493         }
494     }
495
496     /**
497      * get NSSI Selection Capability for TN
498      * @param execution
499      */
500     void getNSSISelectionCap4TN(DelegateExecution execution) {
501
502         SliceTaskParamsAdapter sliceParams =
503                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
504
505         def vendor = sliceParams.tnBHSliceTaskInfo.vendor
506
507         String strRequest = buildNSSISelectionReq(vendor, NetworkType.TRANSPORT)
508
509         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_NSSI_SELECTION_CAPABILITY, strRequest)
510
511         Map<String, Object> resMap = objectMapper.readValue(response, Map.class)
512
513         String selection = resMap.get("selection")
514
515         if ("NSMF".equalsIgnoreCase(selection)) {
516             execution.setVariable("NEED_TN_NSSI_SELECTION", true)
517         }
518     }
519
520     /**
521      * get NSSI Selection Capability for CN
522      * @param execution
523      */
524     void getNSSISelectionCap4CN(DelegateExecution execution) {
525
526         SliceTaskParamsAdapter sliceParams =
527                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
528
529         def vendor = sliceParams.cnSliceTaskInfo.vendor
530
531         String strRequest = buildNSSISelectionReq(vendor, NetworkType.CORE)
532
533         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_NSSI_SELECTION_CAPABILITY, strRequest)
534
535         Map<String, Object> resMap = objectMapper.readValue(response, Map.class)
536
537         String selection = resMap.get("selection")
538
539         if ("NSMF".equalsIgnoreCase(selection)) {
540             //execution.setVariable("NEED_CN_NSSI_SELECTION", true)
541         }
542     }
543
544     /**
545      * build NSSI Selection Capability Request body to nssmf adapter
546      * @param vendor
547      * @param networkType
548      * @return
549      */
550     private static String buildNSSISelectionReq(String vendor, NetworkType networkType) {
551         NssmfAdapterNBIRequest request = new NssmfAdapterNBIRequest()
552         EsrInfo esrInfo = new EsrInfo()
553         esrInfo.setVendor(vendor)
554         esrInfo.setNetworkType(networkType)
555         request.setEsrInfo(esrInfo)
556
557         return objectMapper.writeValueAsString(request)
558     }
559
560     /**
561      * if exist nssi need to select
562      * @param execution
563      */
564     public void handleNssiSelect(DelegateExecution execution) {
565
566         SliceTaskParamsAdapter sliceTaskParams =
567                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
568
569         //todo
570         boolean needCnNssiSelection = execution.getVariable("NEED_CN_NSSI_SELECTION") as Boolean
571         boolean needAnNssiSelection = execution.getVariable("NEED_AN_NSSI_SELECTION") as Boolean
572         boolean needTnNssiSelection = execution.getVariable("NEED_TN_NSSI_SELECTION") as Boolean
573
574         List<Map> nssiNeedHandlerInfos = new ArrayList<>()
575         Map<String, Object> nssiNeedHandlerMap = new HashMap()
576
577         //List<TemplateInfo> nssiNeedHandlers = new ArrayList<>()
578         //List<Object> nssiProfileNeedHandlers = new ArrayList<>()
579         if (needCnNssiSelection) {
580             nssiNeedHandlerMap.put("subnetType", sliceTaskParams.cnSliceTaskInfo.subnetType)
581             nssiNeedHandlerMap.put("nsstInfo", sliceTaskParams.cnSliceTaskInfo.NSSTInfo)
582             nssiNeedHandlerMap.put("sliceProfile", sliceTaskParams.cnSliceTaskInfo.sliceProfile)
583             nssiNeedHandlerInfos.add(nssiNeedHandlerMap)
584         }
585         if (needAnNssiSelection) {
586             nssiNeedHandlerMap.clear()
587             nssiNeedHandlerMap.put("subnetType", sliceTaskParams.anSliceTaskInfo.subnetType)
588             nssiNeedHandlerMap.put("nsstInfo", sliceTaskParams.anSliceTaskInfo.NSSTInfo)
589             nssiNeedHandlerMap.put("sliceProfile", sliceTaskParams.anSliceTaskInfo.sliceProfile)
590             nssiNeedHandlerInfos.add(nssiNeedHandlerMap)
591         }
592         if (needTnNssiSelection) {
593             nssiNeedHandlerMap.clear()
594             nssiNeedHandlerMap.put("subnetType", sliceTaskParams.tnBHSliceTaskInfo.subnetType)
595             nssiNeedHandlerMap.put("nsstInfo", sliceTaskParams.tnBHSliceTaskInfo.NSSTInfo)
596             nssiNeedHandlerMap.put("sliceProfile", sliceTaskParams.tnBHSliceTaskInfo.sliceProfile)
597             nssiNeedHandlerInfos.add(nssiNeedHandlerMap)
598
599             nssiNeedHandlerMap.clear()
600             nssiNeedHandlerMap.put("subnetType", sliceTaskParams.tnMHSliceTaskInfo.subnetType)
601             nssiNeedHandlerMap.put("nsstInfo", sliceTaskParams.tnMHSliceTaskInfo.NSSTInfo)
602             nssiNeedHandlerMap.put("sliceProfile", sliceTaskParams.tnMHSliceTaskInfo.sliceProfile)
603             nssiNeedHandlerInfos.add(nssiNeedHandlerMap)
604
605             nssiNeedHandlerMap.clear()
606             nssiNeedHandlerMap.put("subnetType", sliceTaskParams.tnFHSliceTaskInfo.subnetType)
607             nssiNeedHandlerMap.put("nsstInfo", sliceTaskParams.tnFHSliceTaskInfo.NSSTInfo)
608             nssiNeedHandlerMap.put("sliceProfile", sliceTaskParams.tnFHSliceTaskInfo.sliceProfile)
609             nssiNeedHandlerInfos.add(nssiNeedHandlerMap)
610
611         }
612
613         if (nssiNeedHandlerInfos.size() > 0) {
614             execution.setVariable("needSelectNssi", true)
615             execution.setVariable("currNssiIndex", 0)
616             execution.setVariable("nssiNeedHandlerInfos", nssiNeedHandlerInfos)
617         } else {
618             execution.setVariable("needSelectNssi", false)
619         }
620
621         execution.setVariable("sliceTaskParams", sliceTaskParams)
622     }
623
624     /**
625      * prepare select nssi request
626      * @param execution
627      */
628     public void preNSSIRequest(DelegateExecution execution) {
629
630         List<Map> nssiNeedHandlerInfos =
631                 execution.getVariable("nssiNeedHandlerInfos") as List<Map>
632
633         int currNssiIndex = execution.getVariable("currNssiIndex") as Integer
634         Map nssiNeedHandlerInfo = nssiNeedHandlerInfos.get(currNssiIndex) as Map
635
636         TemplateInfo nsstInfo = nssiNeedHandlerInfo.get("nsstInfo") as TemplateInfo
637         SliceProfileAdapter sliceProfileInfo = nssiNeedHandlerInfo.get("sliceProfile") as SliceProfileAdapter
638
639         Map profileInfo = objectMapper.convertValue(sliceProfileInfo, new TypeReference<Map<String, Object>>() {});
640         while (profileInfo.values().remove(null));
641
642         String urlString = UrnPropertiesReader.getVariable("mso.oof.endpoint", execution)
643         logger.debug( "get NSI option OOF Url: " + urlString)
644
645         String requestId = execution.getVariable("msoRequestId")
646         String messageType = "NSSISelectionResponse"
647
648         execution.setVariable("nssiSelectionUrl", "/api/oof/selection/nssi/v1")
649         execution.setVariable("nssiSelection_messageType", messageType)
650         execution.setVariable("nssiSelection_correlator", requestId)
651         String timeout = UrnPropertiesReader.getVariable("mso.adapters.oof.timeout", execution)
652         execution.setVariable("nssiSelection_timeout", timeout)
653
654         String oofRequest = oofUtils.buildSelectNSSIRequest(requestId, messageType, nsstInfo.UUID,
655                 nsstInfo.invariantUUID, nsstInfo.name, profileInfo)
656
657         execution.setVariable("nssiSelection_oofRequest", oofRequest)
658         logger.debug("Sending request to OOF: " + oofRequest)
659     }
660
661     /**
662      * process select nssi response
663      * @param execution
664      */
665     public void processNSSIResp(DelegateExecution execution) {
666
667         List<Map> nssiNeedHandlerInfos =
668                 execution.getVariable("nssiNeedHandlerInfos") as List<Map>
669
670         int currNssiIndex = execution.getVariable("currNssiIndex") as Integer
671         Map nssiNeedHandlerInfo = nssiNeedHandlerInfos.get(currNssiIndex) as Map
672         SubnetType subnetType = nssiNeedHandlerInfo.get("subnetType") as SubnetType
673
674         SliceTaskParamsAdapter sliceTaskParams =
675                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
676
677
678         String OOFResponse = execution.getVariable("nssiSelection_oofResponse")
679         logger.debug("NSI OOFResponse is: " + OOFResponse)
680         execution.setVariable("OOFResponse", OOFResponse)
681         //This needs to be changed to derive a value when we add policy to decide the solution options.
682
683         Map<String, Object> resMap = objectMapper.readValue(OOFResponse, Map.class)
684         List<Map<String, Object>> nsiSolutions = (List<Map<String, Object>>) resMap.get("solutions")
685
686         if(nsiSolutions.size()>=1) {
687         Map<String,Object> solution = nsiSolutions.get(0) as Map
688         processNssiResult(sliceTaskParams, subnetType, solution)
689         }
690
691         execution.setVariable("sliceTaskParams", sliceTaskParams)
692         //logger.debug("sliceTaskParams: "+ sliceTaskParams.convertToJson())
693         logger.debug("*** Completed options Call to OOF ***")
694
695         logger.debug("start parseServiceProfile")
696         //parseServiceProfile(execution)
697         logger.debug("end parseServiceProfile")
698
699         if (currNssiIndex >= nssiNeedHandlerInfos.size() - 1) {
700             execution.setVariable("needSelectNssi", false)
701         } else {
702             execution.setVariable("currNssiIndex", currNssiIndex + 1)
703             execution.setVariable("needSelectNssi", true)
704         }
705
706     }
707
708     private void processNssiResult(SliceTaskParamsAdapter sliceTaskParams, SubnetType subnetType,
709             Map<String, Object> solution) {
710         switch (subnetType) {
711             case SubnetType.CN:
712                 sliceTaskParams.cnSliceTaskInfo.suggestNssiId = solution.get("NSSIId")
713                 sliceTaskParams.cnSliceTaskInfo.suggestNssiName = solution.get("NSSIName")
714                 break
715             case SubnetType.AN:
716                 sliceTaskParams.anSliceTaskInfo.suggestNssiId = solution.get("NSSIId")
717                 sliceTaskParams.anSliceTaskInfo.suggestNssiName = solution.get("NSSIName")
718                 break
719             case SubnetType.TN_BH:
720                 sliceTaskParams.tnBHSliceTaskInfo.suggestNssiId = solution.get("NSSIId")
721                 sliceTaskParams.tnBHSliceTaskInfo.suggestNssiName = solution.get("NSSIName")
722                 break
723             case SubnetType.TN_FH:
724                 sliceTaskParams.tnFHSliceTaskInfo.suggestNssiId = solution.get("NSSIId")
725                 sliceTaskParams.tnFHSliceTaskInfo.suggestNssiName = solution.get("NSSIName")
726                 break
727             case SubnetType.TN_MH:
728                 sliceTaskParams.tnMHSliceTaskInfo.suggestNssiId = solution.get("NSSIId")
729                 sliceTaskParams.tnMHSliceTaskInfo.suggestNssiName = solution.get("NSSIName")
730                 break
731         }
732     }
733
734 }