cfdbe98c343f26f753d26dcdf2b5b023f4675286
[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                 if(execution.getVariable("needQuerySliceProfile")){
375                     execution.setVariable("needQuerySliceProfile", false)
376                 }
377                 processNewNSI(solution, sliceTaskParams)
378             }
379         }
380         execution.setVariable("sliceTaskParams", sliceTaskParams)
381         //logger.debug("sliceTaskParams: " + sliceTaskParams.convertToJson())
382         logger.debug("*** Completed options Call to OOF ***")
383     }
384
385     private void processSharedNSI(Map<String, Object> solution, SliceTaskParamsAdapter sliceParams) {
386         Map<String, Object> sharedNSISolution = solution.get("sharedNSISolution") as Map
387
388         String nsiId = sharedNSISolution.get("NSIId")
389         String nsiName = sharedNSISolution.get("NSIName")
390         sliceParams.setSuggestNsiId(nsiId)
391         sliceParams.setSuggestNsiName(nsiName)
392
393     }
394
395     private void processNewNSI(Map<String, Object> solution, SliceTaskParamsAdapter sliceParams) {
396         Map<String, Object> newNSISolution = solution.get("newNSISolution") as Map
397         List<Map> sliceProfiles = newNSISolution.get("sliceProfiles") as List<Map>
398         for (Map sliceProfile : sliceProfiles) {
399             String domainType = sliceProfile.get("domainType")
400             switch (domainType.toLowerCase()) {
401                 case "tn-bh":
402                     sliceParams.tnBHSliceTaskInfo.sliceProfile = sliceProfile as TnSliceProfile
403                     break
404                 case "an-nf":
405                     sliceParams.anSliceTaskInfo.sliceProfile = sliceProfile as AnSliceProfile
406                     break
407                 case "cn":
408                     sliceParams.cnSliceTaskInfo.sliceProfile = sliceProfile as CnSliceProfile
409                     break
410                 default:
411                     break
412             }
413
414             //todo
415
416         }
417     }
418
419     /**
420      * get NSSI Selection Capability for AN
421      * @param execution
422      */
423     public void getNSSISelectionCap4AN(DelegateExecution execution) {
424
425         def vendor = execution.getVariable("vendor") as String
426
427         String strRequest = buildNSSISelectionReq(vendor, NetworkType.ACCESS)
428
429         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_NSSI_SELECTION_CAPABILITY, strRequest)
430
431         Map<String, Object> resMap = objectMapper.readValue(response, Map.class)
432
433         String selection = resMap.get("selection")
434
435
436         if ("NSMF".equalsIgnoreCase(selection)) {
437             execution.setVariable("NEED_AN_NSSI_SELECTION", true)
438         }
439     }
440
441     /**
442      * get NSSI Selection Capability for TN
443      * @param execution
444      */
445     public void getNSSISelectionCap4TN(DelegateExecution execution) {
446
447         def vendor = execution.getVariable("vendor") as String
448
449         String strRequest = buildNSSISelectionReq(vendor, NetworkType.TRANSPORT)
450
451         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_NSSI_SELECTION_CAPABILITY, strRequest)
452
453         Map<String, Object> resMap = objectMapper.readValue(response, Map.class)
454
455         String selection = resMap.get("selection")
456
457         if ("NSMF".equalsIgnoreCase(selection)) {
458             execution.setVariable("NEED_TN_NSSI_SELECTION", true)
459         }
460     }
461
462     /**
463      * get NSSI Selection Capability for CN
464      * @param execution
465      */
466     public void getNSSISelectionCap4CN(DelegateExecution execution) {
467
468         def vendor = execution.getVariable("vendor") as String
469
470         String strRequest = buildNSSISelectionReq(vendor, NetworkType.CORE)
471
472         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_NSSI_SELECTION_CAPABILITY, strRequest)
473
474         Map<String, Object> resMap = objectMapper.readValue(response, Map.class)
475
476         String selection = resMap.get("selection")
477
478         if ("NSMF".equalsIgnoreCase(selection)) {
479             execution.setVariable("NEED_CN_NSSI_SELECTION", true)
480         }
481     }
482
483     /**
484      * build NSSI Selection Capability Request body to nssmf adapter
485      * @param vendor
486      * @param networkType
487      * @return
488      */
489     private static String buildNSSISelectionReq(String vendor, NetworkType networkType) {
490         NssmfAdapterNBIRequest request = new NssmfAdapterNBIRequest()
491         EsrInfo esrInfo = new EsrInfo()
492         esrInfo.setVendor(vendor)
493         esrInfo.setNetworkType(networkType)
494         request.setEsrInfo(esrInfo)
495
496         return objectMapper.writeValueAsString(request)
497     }
498
499     /**
500      * if exist nssi need to select?
501      * @param execution
502      */
503     public void handleNssiSelect(DelegateExecution execution) {
504
505         SliceTaskParamsAdapter sliceTaskParams =
506                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
507
508         //todo
509         boolean needCnNssiSelection = execution.getVariable("NEED_CN_NSSI_SELECTION") as Boolean
510         boolean needAnNssiSelection = execution.getVariable("NEED_AN_NSSI_SELECTION") as Boolean
511         boolean needTnNssiSelection = execution.getVariable("NEED_TN_NSSI_SELECTION") as Boolean
512
513         /**
514          * [
515          * ​  {
516          * ​          "subType":  subtype,
517          * ​          "nsstInfo": object,
518          * ​          "sliceProfile": object
519          * ​  },
520          *      {
521          *          "subType":  subtype,
522          *          "nsstInfo": object,
523          *          "sliceProfile": object
524          *      }
525          * ]
526          */
527         List<Map> nssiNeedHandlerInfos = new ArrayList<>()
528         Map<String, Object> nssiNeedHandlerMap = new HashMap()
529
530         //List<TemplateInfo> nssiNeedHandlers = new ArrayList<>()
531         //List<Object> nssiProfileNeedHandlers = new ArrayList<>()
532         if (needCnNssiSelection) {
533             nssiNeedHandlerMap.put("subnetType", sliceTaskParams.cnSliceTaskInfo.subnetType)
534             nssiNeedHandlerMap.put("nsstInfo", sliceTaskParams.cnSliceTaskInfo.NSSTInfo)
535             nssiNeedHandlerMap.put("sliceProfile", sliceTaskParams.cnSliceTaskInfo.sliceProfile)
536             nssiNeedHandlerInfos.add(nssiNeedHandlerMap)
537         }
538         if (needAnNssiSelection) {
539             nssiNeedHandlerMap.clear()
540             nssiNeedHandlerMap.put("subnetType", sliceTaskParams.anSliceTaskInfo.subnetType)
541             nssiNeedHandlerMap.put("nsstInfo", sliceTaskParams.anSliceTaskInfo.NSSTInfo)
542             nssiNeedHandlerMap.put("sliceProfile", sliceTaskParams.anSliceTaskInfo.sliceProfile)
543             nssiNeedHandlerInfos.add(nssiNeedHandlerMap)
544         }
545         if (needTnNssiSelection) {
546             nssiNeedHandlerMap.clear()
547             nssiNeedHandlerMap.put("subnetType", sliceTaskParams.tnBHSliceTaskInfo.subnetType)
548             nssiNeedHandlerMap.put("nsstInfo", sliceTaskParams.tnBHSliceTaskInfo.NSSTInfo)
549             nssiNeedHandlerMap.put("sliceProfile", sliceTaskParams.tnBHSliceTaskInfo.sliceProfile)
550             nssiNeedHandlerInfos.add(nssiNeedHandlerMap)
551
552             nssiNeedHandlerMap.clear()
553             nssiNeedHandlerMap.put("subnetType", sliceTaskParams.tnMHSliceTaskInfo.subnetType)
554             nssiNeedHandlerMap.put("nsstInfo", sliceTaskParams.tnMHSliceTaskInfo.NSSTInfo)
555             nssiNeedHandlerMap.put("sliceProfile", sliceTaskParams.tnMHSliceTaskInfo.sliceProfile)
556             nssiNeedHandlerInfos.add(nssiNeedHandlerMap)
557
558             nssiNeedHandlerMap.clear()
559             nssiNeedHandlerMap.put("subnetType", sliceTaskParams.tnFHSliceTaskInfo.subnetType)
560             nssiNeedHandlerMap.put("nsstInfo", sliceTaskParams.tnFHSliceTaskInfo.NSSTInfo)
561             nssiNeedHandlerMap.put("sliceProfile", sliceTaskParams.tnFHSliceTaskInfo.sliceProfile)
562             nssiNeedHandlerInfos.add(nssiNeedHandlerMap)
563
564         }
565
566         if (nssiNeedHandlerInfos.size() > 0) {
567             execution.setVariable("needSelectNssi", true)
568             execution.setVariable("currNssiIndex", 0)
569             execution.setVariable("nssiNeedHandlerInfos", nssiNeedHandlerInfos)
570         } else {
571             execution.setVariable("needSelectNssi", false)
572         }
573
574         execution.setVariable("sliceTaskParams", sliceTaskParams)
575     }
576
577     /**
578      * prepare select nssi request
579      * @param execution
580      */
581     public void preNSSIRequest(DelegateExecution execution) {
582
583         List<Map> nssiNeedHandlerInfos =
584                 execution.getVariable("nssiNeedHandlerInfos") as List<Map>
585
586         int currNssiIndex = execution.getVariable("currNssiIndex") as Integer
587         Map nssiNeedHandlerInfo = nssiNeedHandlerInfos.get(currNssiIndex) as Map
588
589         TemplateInfo nsstInfo = nssiNeedHandlerInfo.get("nsstInfo") as TemplateInfo
590         Object profileInfo = nssiNeedHandlerInfo.get("sliceProfile")
591
592         String urlString = UrnPropertiesReader.getVariable("mso.oof.endpoint", execution)
593         logger.debug( "get NSI option OOF Url: " + urlString)
594
595         String requestId = execution.getVariable("msoRequestId")
596         String messageType = "NSSISelectionResponse"
597
598         execution.setVariable("nssiSelectionUrl", "/api/oof/selection/nssi/v1")
599         execution.setVariable("nssiSelection_messageType", messageType)
600         execution.setVariable("nssiSelection_correlator", requestId)
601         String timeout = UrnPropertiesReader.getVariable("mso.adapters.oof.timeout", execution)
602         execution.setVariable("nssiSelection_timeout", timeout)
603
604         String oofRequest = oofUtils.buildSelectNSSIRequest(requestId, nsstInfo, messageType,
605                 profileInfo, timeout as Integer)
606
607         execution.setVariable("nssiSelection_oofRequest", oofRequest)
608         logger.debug("Sending request to OOF: " + oofRequest)
609     }
610
611     /**
612      * process select nssi response
613      * @param execution
614      */
615     public void processNSSIResp(DelegateExecution execution) {
616
617         List<Map> nssiNeedHandlerInfos =
618                 execution.getVariable("nssiNeedHandlerInfos") as List<Map>
619
620         int currNssiIndex = execution.getVariable("currNssiIndex") as Integer
621         Map nssiNeedHandlerInfo = nssiNeedHandlerInfos.get(currNssiIndex) as Map
622         SubnetType subnetType = nssiNeedHandlerInfo.get("subnetType") as SubnetType
623
624         SliceTaskParamsAdapter sliceTaskParams =
625                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
626
627
628         String OOFResponse = execution.getVariable("nssiSelection_oofResponse")
629         logger.debug("NSI OOFResponse is: " + OOFResponse)
630         execution.setVariable("OOFResponse", OOFResponse)
631         //This needs to be changed to derive a value when we add policy to decide the solution options.
632
633         Map<String, Object> resMap = objectMapper.readValue(OOFResponse, Map.class)
634         List<Map<String, Object>> nsiSolutions = (List<Map<String, Object>>) resMap.get("solutions")
635         Map<String, Object> solution = nsiSolutions.get(0)
636
637         String resourceSharingLevel = execution.getVariable("resourceSharingLevel")
638         Boolean isSharable = resourceSharingLevel == "shared"   //todo
639
640         if (isSharable && solution != null) {
641             processNssiResult(sliceTaskParams, subnetType, solution)
642         }
643
644         execution.setVariable("sliceTaskParams", sliceTaskParams)
645         //logger.debug("sliceTaskParams: "+ sliceTaskParams.convertToJson())
646         logger.debug("*** Completed options Call to OOF ***")
647
648         logger.debug("start parseServiceProfile")
649         //parseServiceProfile(execution)
650         logger.debug("end parseServiceProfile")
651
652         if (currNssiIndex >= nssiNeedHandlerInfos.size() - 1) {
653             execution.setVariable("needSelectNssi", false)
654         } else {
655             execution.setVariable("currNssiIndex", currNssiIndex + 1)
656             execution.setVariable("needSelectNssi", true)
657         }
658
659     }
660
661     private void processNssiResult(SliceTaskParamsAdapter sliceTaskParams, SubnetType subnetType,
662                                    Map<String, Object> solution) {
663         switch (subnetType) {
664             case SubnetType.CN:
665                 sliceTaskParams.cnSliceTaskInfo.suggestNssiId = solution.get("NSSIId")
666                 sliceTaskParams.cnSliceTaskInfo.suggestNssiName = solution.get("NSSIName")
667                 break
668             case SubnetType.AN_NF:
669                 sliceTaskParams.anSliceTaskInfo.suggestNssiId = solution.get("NSSIId")
670                 sliceTaskParams.anSliceTaskInfo.suggestNssiName = solution.get("NSSIName")
671                 break
672             case SubnetType.TN_BH:
673                 sliceTaskParams.tnBHSliceTaskInfo.suggestNssiId = solution.get("NSSIId")
674                 sliceTaskParams.tnBHSliceTaskInfo.suggestNssiName = solution.get("NSSIName")
675                 break
676             case SubnetType.TN_FH:
677                 sliceTaskParams.tnFHSliceTaskInfo.suggestNssiId = solution.get("NSSIId")
678                 sliceTaskParams.tnFHSliceTaskInfo.suggestNssiName = solution.get("NSSIName")
679                 break
680             case SubnetType.TN_MH:
681                 sliceTaskParams.tnMHSliceTaskInfo.suggestNssiId = solution.get("NSSIId")
682                 sliceTaskParams.tnMHSliceTaskInfo.suggestNssiName = solution.get("NSSIName")
683                 break
684         }
685     }
686
687 }