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("CN NSST".equals(serviceCategory)){
276             return SubnetType.CN
277         }
278         if ("AN NF NSST".equals(serviceCategory)){
279             return SubnetType.AN_NF
280         }
281         if ("AN NSST".equals(serviceCategory)){
282             return SubnetType.AN
283         }
284         if ("TN BH NSST".equals(serviceCategory)){
285             return SubnetType.TN_BH
286         }
287         if("TN MH NSST".equals(serviceCategory)){
288             return SubnetType.TN_MH
289         }
290         if("TN FH NSST".equals(serviceCategory)){
291             return SubnetType.TN_FH
292         }
293         return null
294     }
295
296     /**
297      * query Subnet Capability of TN AN CN
298      * @param execution
299      */
300     private String querySubnetCapability(DelegateExecution execution, String vendor, SubnetType subnetType) {
301
302         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_SUB_NET_CAPABILITY,
303                 buildQuerySubnetCapRequest(vendor, subnetType))
304         return response
305     }
306
307     /**
308      * build request body for querying Subnet Capability
309      * @param vendor
310      * @param subnetTypes
311      * @param networkType
312      * @return
313      */
314     private static NssmfAdapterNBIRequest buildQuerySubnetCapRequest(String vendor, SubnetType subnetType) {
315         NssmfAdapterNBIRequest request = new NssmfAdapterNBIRequest()
316
317         List<String> subnetTypes =  new ArrayList<>()
318         subnetTypes.add(subnetType.subnetType)
319
320         QuerySubnetCapability req = new QuerySubnetCapability()
321         req.setSubnetTypes(subnetTypes)
322
323         request.setSubnetCapabilityQuery(req)
324
325         EsrInfo esrInfo = new EsrInfo()
326         esrInfo.setVendor(vendor)
327         esrInfo.setNetworkType(subnetType.networkType)
328         request.setEsrInfo(esrInfo)
329
330         return request
331     }
332
333     /**
334      * todo: need rewrite
335      * prepare select nsi request
336      * @param execution
337      */
338     public void preNSIRequest(DelegateExecution execution) {
339         boolean preferReuse = execution.getVariable("needQuerySliceProfile") ? false : true
340
341         String urlString = UrnPropertiesReader.getVariable("mso.oof.endpoint", execution)
342         logger.debug( "get NSI option OOF Url: " + urlString)
343
344         String requestId = execution.getVariable("msoRequestId")
345         String messageType = "NSISelectionResponse"
346
347         execution.setVariable("nsiSelectionUrl", "/api/oof/selection/nsi/v1")
348         execution.setVariable("nsiSelection_messageType", messageType)
349         execution.setVariable("nsiSelection_correlator", requestId)
350         String timeout = UrnPropertiesReader.getVariable("mso.adapters.oof.timeout", execution)
351         execution.setVariable("nsiSelection_timeout", timeout)
352
353         SliceTaskParamsAdapter sliceParams =
354                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
355
356         Map<String, Object> profileInfo = sliceParams.getServiceProfile()
357         profileInfo.remove("profileId")
358         TemplateInfo nstInfo = sliceParams.getNSTInfo()
359
360         List<TemplateInfo> nsstInfos = execution.getVariable("nsstInfos") as List<TemplateInfo>
361
362         List<SubnetCapability> subnetCapabilities =
363                 execution.getVariable("subnetCapabilities") as List<SubnetCapability>
364
365         String oofRequest = oofUtils.buildSelectNSIRequest(requestId, nstInfo, nsstInfos,
366                 messageType, profileInfo, subnetCapabilities, 600, preferReuse)
367
368         execution.setVariable("nsiSelection_oofRequest", oofRequest)
369         logger.debug("Sending request to OOF: " + oofRequest)
370     }
371
372     /**
373      * process select nsi response
374      * @param execution
375      */
376     public void processNSIResp(DelegateExecution execution) {
377
378         SliceTaskParamsAdapter sliceTaskParams =
379                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
380
381         String oofResponse = execution.getVariable("nsiSelection_oofResponse")
382         logger.debug("NSI oofResponse is: " + oofResponse)
383         execution.setVariable("oofResponse", oofResponse)
384         //This needs to be changed to derive a value when we add policy to decide the solution options.
385
386         Map<String, Object> resMap = objectMapper.readValue(oofResponse, Map.class)
387         String requestStatus = resMap.get("requestStatus")
388         if (!StringUtils.isEmpty(requestStatus) && requestStatus == "error") {
389             exceptionUtil.buildWorkflowException(execution, 7000, "get nsi from oof error: " + oofResponse)
390             return
391         }
392
393         List<Map<String, Object>> nsiSolutions = (List<Map<String, Object>>) resMap.get("solutions")
394
395         Map<String, Object> solution = nsiSolutions?.get(0)
396
397         if (solution != null) {
398             if (execution.getVariable("queryNsiFirst")) {
399                 if (solution.get("existingNSI")) {
400                     processSharedNSI(solution, sliceTaskParams, execution)
401                     execution.setVariable("needQuerySliceProfile", true)
402                 } else {
403                     processNewSliceProfiles(solution, sliceTaskParams)
404                     execution.setVariable("needQuerySliceProfile", false)
405                 }
406                 execution.setVariable("queryNsiFirst", false)
407             } else {
408                 processNewSliceProfiles(solution, sliceTaskParams)
409                 execution.setVariable("needQuerySliceProfile", false)
410             }
411         }
412         execution.setVariable("sliceTaskParams", sliceTaskParams)
413         logger.debug("after req to oof for nis select, sliceTaskParams = " + sliceTaskParams)
414         logger.debug("*** Completed options Call to OOF ***")
415     }
416
417     private void processSharedNSI(Map<String, Object> solution, SliceTaskParamsAdapter sliceParams, DelegateExecution execution) {
418         Map<String, Object> sharedNSISolution = solution.get("sharedNSISolution") as Map
419         String nsiId = sharedNSISolution.get("NSIId")
420         String nsiName = sharedNSISolution.get("NSIName")
421         sliceParams.setSuggestNsiId(nsiId)
422         sliceParams.setSuggestNsiName(nsiName)
423
424         List<String> nssiId = aaiSliceUtil.getNSSIIdList(execution,nsiId)
425         List<ServiceInstance> nssiInstances = aaiSliceUtil.getNSSIListFromAAI(execution, nssiId)
426
427         Map<String, Object> nssiSolution = new HashMap<>()
428         for(ServiceInstance instance: nssiInstances){
429             nssiSolution.put("NSSIId", instance.getServiceInstanceId())
430             nssiSolution.put("NSSIName", instance.getServiceInstanceName())
431             SubnetType subnetType = instance.getWorkloadContext() as SubnetType
432             processNssiResult(sliceParams, subnetType, nssiSolution)
433         }
434
435     }
436
437     private void processNewSliceProfiles(Map<String, Object> solution, SliceTaskParamsAdapter sliceParams) {
438         Map<String, Object> newNSISolution = solution.get("newNSISolution") as Map
439         List<Map> sliceProfiles = newNSISolution.get("sliceProfiles") as List<Map>
440         handleSliceProfiles(sliceProfiles, sliceParams)
441     }
442
443     static def handleSliceProfiles(List<Map> sliceProfiles, SliceTaskParamsAdapter sliceParams) {
444         for (Map sliceProfile : sliceProfiles) {
445             String domainType = sliceProfile.get("domainType")
446             sliceProfile.remove("domainType")
447             SliceProfileAdapter adapter = objectMapper.readValue(objectMapper.writeValueAsString(sliceProfile), SliceProfileAdapter.class)
448             switch (domainType.toLowerCase()) {
449                 case "tn_bh":
450                     sliceParams.tnBHSliceTaskInfo.sliceProfile = adapter
451                     break
452                 case "tn_fh":
453                     sliceParams.tnFHSliceTaskInfo.sliceProfile = adapter
454                     break
455                 case "tn_mh":
456                     sliceParams.tnMHSliceTaskInfo.sliceProfile = adapter
457                     break
458                 case "an_nf":
459                     sliceParams.anNFSliceTaskInfo.sliceProfile = adapter
460                     break
461                 case "an":
462                     sliceParams.anSliceTaskInfo.sliceProfile = adapter
463                     break
464                 case "cn":
465                     sliceParams.cnSliceTaskInfo.sliceProfile = adapter
466                     break
467                 default:
468                     break
469             }
470         }
471     }
472
473     /**
474      * get NSSI Selection Capability for AN
475      * @param execution
476      */
477     void getNSSISelectionCap4AN(DelegateExecution execution) {
478
479        SliceTaskParamsAdapter sliceParams =
480                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
481
482         def vendor = sliceParams.anSliceTaskInfo.vendor
483
484         String strRequest = buildNSSISelectionReq(vendor, NetworkType.ACCESS)
485
486         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_NSSI_SELECTION_CAPABILITY, strRequest)
487
488         Map<String, Object> resMap = objectMapper.readValue(response, Map.class)
489
490         String selection = resMap.get("selection")
491
492
493         if ("NSMF".equalsIgnoreCase(selection)) {
494             execution.setVariable("NEED_AN_NSSI_SELECTION", true)
495         }
496     }
497
498     /**
499      * get NSSI Selection Capability for TN
500      * @param execution
501      */
502     void getNSSISelectionCap4TN(DelegateExecution execution) {
503
504         SliceTaskParamsAdapter sliceParams =
505                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
506
507         def vendor = sliceParams.tnBHSliceTaskInfo.vendor
508
509         String strRequest = buildNSSISelectionReq(vendor, NetworkType.TRANSPORT)
510
511         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_NSSI_SELECTION_CAPABILITY, strRequest)
512
513         Map<String, Object> resMap = objectMapper.readValue(response, Map.class)
514
515         String selection = resMap.get("selection")
516
517         if ("NSMF".equalsIgnoreCase(selection)) {
518             execution.setVariable("NEED_TN_NSSI_SELECTION", true)
519         }
520     }
521
522     /**
523      * get NSSI Selection Capability for CN
524      * @param execution
525      */
526     void getNSSISelectionCap4CN(DelegateExecution execution) {
527
528         SliceTaskParamsAdapter sliceParams =
529                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
530
531         def vendor = sliceParams.cnSliceTaskInfo.vendor
532
533         String strRequest = buildNSSISelectionReq(vendor, NetworkType.CORE)
534
535         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_NSSI_SELECTION_CAPABILITY, strRequest)
536
537         Map<String, Object> resMap = objectMapper.readValue(response, Map.class)
538
539         String selection = resMap.get("selection")
540
541         if ("NSMF".equalsIgnoreCase(selection)) {
542             //execution.setVariable("NEED_CN_NSSI_SELECTION", true)
543         }
544     }
545
546     /**
547      * build NSSI Selection Capability Request body to nssmf adapter
548      * @param vendor
549      * @param networkType
550      * @return
551      */
552     private static String buildNSSISelectionReq(String vendor, NetworkType networkType) {
553         NssmfAdapterNBIRequest request = new NssmfAdapterNBIRequest()
554         EsrInfo esrInfo = new EsrInfo()
555         esrInfo.setVendor(vendor)
556         esrInfo.setNetworkType(networkType)
557         request.setEsrInfo(esrInfo)
558
559         return objectMapper.writeValueAsString(request)
560     }
561
562     /**
563      * if exist nssi need to select
564      * @param execution
565      */
566     public void handleNssiSelect(DelegateExecution execution) {
567
568         SliceTaskParamsAdapter sliceTaskParams =
569                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
570
571         //todo
572         boolean needCnNssiSelection = execution.getVariable("NEED_CN_NSSI_SELECTION") as Boolean
573         boolean needAnNssiSelection = execution.getVariable("NEED_AN_NSSI_SELECTION") as Boolean
574         boolean needTnNssiSelection = execution.getVariable("NEED_TN_NSSI_SELECTION") as Boolean
575
576         List<Map> nssiNeedHandlerInfos = new ArrayList<>()
577         Map<String, Object> nssiNeedHandlerMap = new HashMap()
578
579         //List<TemplateInfo> nssiNeedHandlers = new ArrayList<>()
580         //List<Object> nssiProfileNeedHandlers = new ArrayList<>()
581         if (needCnNssiSelection) {
582             nssiNeedHandlerMap.put("subnetType", sliceTaskParams.cnSliceTaskInfo.subnetType)
583             nssiNeedHandlerMap.put("nsstInfo", sliceTaskParams.cnSliceTaskInfo.NSSTInfo)
584             nssiNeedHandlerMap.put("sliceProfile", sliceTaskParams.cnSliceTaskInfo.sliceProfile)
585             nssiNeedHandlerInfos.add(nssiNeedHandlerMap)
586         }
587         if (needAnNssiSelection) {
588             nssiNeedHandlerMap.clear()
589             nssiNeedHandlerMap.put("subnetType", sliceTaskParams.anSliceTaskInfo.subnetType)
590             nssiNeedHandlerMap.put("nsstInfo", sliceTaskParams.anSliceTaskInfo.NSSTInfo)
591             nssiNeedHandlerMap.put("sliceProfile", sliceTaskParams.anSliceTaskInfo.sliceProfile)
592             nssiNeedHandlerInfos.add(nssiNeedHandlerMap)
593         }
594         if (needTnNssiSelection) {
595             nssiNeedHandlerMap.clear()
596             nssiNeedHandlerMap.put("subnetType", sliceTaskParams.tnBHSliceTaskInfo.subnetType)
597             nssiNeedHandlerMap.put("nsstInfo", sliceTaskParams.tnBHSliceTaskInfo.NSSTInfo)
598             nssiNeedHandlerMap.put("sliceProfile", sliceTaskParams.tnBHSliceTaskInfo.sliceProfile)
599             nssiNeedHandlerInfos.add(nssiNeedHandlerMap)
600
601             nssiNeedHandlerMap.clear()
602             nssiNeedHandlerMap.put("subnetType", sliceTaskParams.tnMHSliceTaskInfo.subnetType)
603             nssiNeedHandlerMap.put("nsstInfo", sliceTaskParams.tnMHSliceTaskInfo.NSSTInfo)
604             nssiNeedHandlerMap.put("sliceProfile", sliceTaskParams.tnMHSliceTaskInfo.sliceProfile)
605             nssiNeedHandlerInfos.add(nssiNeedHandlerMap)
606
607             nssiNeedHandlerMap.clear()
608             nssiNeedHandlerMap.put("subnetType", sliceTaskParams.tnFHSliceTaskInfo.subnetType)
609             nssiNeedHandlerMap.put("nsstInfo", sliceTaskParams.tnFHSliceTaskInfo.NSSTInfo)
610             nssiNeedHandlerMap.put("sliceProfile", sliceTaskParams.tnFHSliceTaskInfo.sliceProfile)
611             nssiNeedHandlerInfos.add(nssiNeedHandlerMap)
612
613         }
614
615         if (nssiNeedHandlerInfos.size() > 0) {
616             execution.setVariable("needSelectNssi", true)
617             execution.setVariable("currNssiIndex", 0)
618             execution.setVariable("nssiNeedHandlerInfos", nssiNeedHandlerInfos)
619         } else {
620             execution.setVariable("needSelectNssi", false)
621         }
622
623         execution.setVariable("sliceTaskParams", sliceTaskParams)
624     }
625
626     /**
627      * prepare select nssi request
628      * @param execution
629      */
630     public void preNSSIRequest(DelegateExecution execution) {
631
632         List<Map> nssiNeedHandlerInfos =
633                 execution.getVariable("nssiNeedHandlerInfos") as List<Map>
634
635         int currNssiIndex = execution.getVariable("currNssiIndex") as Integer
636         Map nssiNeedHandlerInfo = nssiNeedHandlerInfos.get(currNssiIndex) as Map
637
638         TemplateInfo nsstInfo = nssiNeedHandlerInfo.get("nsstInfo") as TemplateInfo
639         SliceProfileAdapter sliceProfileInfo = nssiNeedHandlerInfo.get("sliceProfile") as SliceProfileAdapter
640
641         Map profileInfo = objectMapper.convertValue(sliceProfileInfo, new TypeReference<Map<String, Object>>() {});
642         while (profileInfo.values().remove(null));
643
644         String urlString = UrnPropertiesReader.getVariable("mso.oof.endpoint", execution)
645         logger.debug( "get NSI option OOF Url: " + urlString)
646
647         String requestId = execution.getVariable("msoRequestId")
648         String messageType = "NSSISelectionResponse"
649
650         execution.setVariable("nssiSelectionUrl", "/api/oof/selection/nssi/v1")
651         execution.setVariable("nssiSelection_messageType", messageType)
652         execution.setVariable("nssiSelection_correlator", requestId)
653         String timeout = UrnPropertiesReader.getVariable("mso.adapters.oof.timeout", execution)
654         execution.setVariable("nssiSelection_timeout", timeout)
655
656         String oofRequest = oofUtils.buildSelectNSSIRequest(requestId, messageType, nsstInfo.UUID,
657                 nsstInfo.invariantUUID, nsstInfo.name, profileInfo)
658
659         execution.setVariable("nssiSelection_oofRequest", oofRequest)
660         logger.debug("Sending request to OOF: " + oofRequest)
661     }
662
663     /**
664      * process select nssi response
665      * @param execution
666      */
667     public void processNSSIResp(DelegateExecution execution) {
668
669         List<Map> nssiNeedHandlerInfos =
670                 execution.getVariable("nssiNeedHandlerInfos") as List<Map>
671
672         int currNssiIndex = execution.getVariable("currNssiIndex") as Integer
673         Map nssiNeedHandlerInfo = nssiNeedHandlerInfos.get(currNssiIndex) as Map
674         SubnetType subnetType = nssiNeedHandlerInfo.get("subnetType") as SubnetType
675
676         SliceTaskParamsAdapter sliceTaskParams =
677                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
678
679
680         String OOFResponse = execution.getVariable("nssiSelection_oofResponse")
681         logger.debug("NSI OOFResponse is: " + OOFResponse)
682         execution.setVariable("OOFResponse", OOFResponse)
683         //This needs to be changed to derive a value when we add policy to decide the solution options.
684
685         Map<String, Object> resMap = objectMapper.readValue(OOFResponse, Map.class)
686         List<Map<String, Object>> nsiSolutions = (List<Map<String, Object>>) resMap.get("solutions")
687
688         if(nsiSolutions.size()>=1) {
689         Map<String,Object> solution = nsiSolutions.get(0) as Map
690         processNssiResult(sliceTaskParams, subnetType, solution)
691         }
692
693         execution.setVariable("sliceTaskParams", sliceTaskParams)
694         //logger.debug("sliceTaskParams: "+ sliceTaskParams.convertToJson())
695         logger.debug("*** Completed options Call to OOF ***")
696
697         logger.debug("start parseServiceProfile")
698         //parseServiceProfile(execution)
699         logger.debug("end parseServiceProfile")
700
701         if (currNssiIndex >= nssiNeedHandlerInfos.size() - 1) {
702             execution.setVariable("needSelectNssi", false)
703         } else {
704             execution.setVariable("currNssiIndex", currNssiIndex + 1)
705             execution.setVariable("needSelectNssi", true)
706         }
707
708     }
709
710     private void processNssiResult(SliceTaskParamsAdapter sliceTaskParams, SubnetType subnetType,
711             Map<String, Object> solution) {
712         switch (subnetType) {
713             case SubnetType.CN:
714                 sliceTaskParams.cnSliceTaskInfo.suggestNssiId = solution.get("NSSIId")
715                 sliceTaskParams.cnSliceTaskInfo.suggestNssiName = solution.get("NSSIName")
716                 break
717             case SubnetType.AN:
718                 sliceTaskParams.anSliceTaskInfo.suggestNssiId = solution.get("NSSIId")
719                 sliceTaskParams.anSliceTaskInfo.suggestNssiName = solution.get("NSSIName")
720                 break
721             case SubnetType.TN_BH:
722                 sliceTaskParams.tnBHSliceTaskInfo.suggestNssiId = solution.get("NSSIId")
723                 sliceTaskParams.tnBHSliceTaskInfo.suggestNssiName = solution.get("NSSIName")
724                 break
725             case SubnetType.TN_FH:
726                 sliceTaskParams.tnFHSliceTaskInfo.suggestNssiId = solution.get("NSSIId")
727                 sliceTaskParams.tnFHSliceTaskInfo.suggestNssiName = solution.get("NSSIName")
728                 break
729             case SubnetType.TN_MH:
730                 sliceTaskParams.tnMHSliceTaskInfo.suggestNssiId = solution.get("NSSIId")
731                 sliceTaskParams.tnMHSliceTaskInfo.suggestNssiName = solution.get("NSSIName")
732                 break
733         }
734     }
735
736 }