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