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