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