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