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