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