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