ca262172d0cca286fb3623f69a68f677579d3c2e
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  # Copyright (c) 2019, 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.core.type.TypeReference
24 import com.fasterxml.jackson.databind.ObjectMapper
25 import org.apache.commons.lang3.StringUtils
26 import org.camunda.bpm.engine.delegate.DelegateExecution
27 import org.onap.aai.domain.yang.ServiceInstance
28 import org.onap.aaiclient.client.aai.AAIObjectType
29 import org.onap.aaiclient.client.aai.AAIResourcesClient
30 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
31 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
32 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
33 import org.onap.logging.filter.base.ONAPComponents
34 import org.onap.so.beans.nsmf.*
35 import org.onap.so.beans.nsmf.oof.SubnetCapability
36 import org.onap.so.beans.nsmf.oof.TemplateInfo
37 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
38 import org.onap.so.bpmn.common.scripts.ExceptionUtil
39 import org.onap.so.bpmn.common.scripts.NssmfAdapterUtils
40 import org.onap.so.bpmn.common.scripts.OofUtils
41 import org.onap.so.bpmn.core.UrnPropertiesReader
42 import org.onap.so.bpmn.core.domain.ServiceDecomposition
43 import org.onap.so.bpmn.core.domain.ServiceProxy
44 import org.onap.so.bpmn.core.json.JsonUtils
45 import org.onap.so.client.HttpClient
46 import org.onap.so.client.HttpClientFactory
47 import org.slf4j.Logger
48 import org.slf4j.LoggerFactory
49
50 import javax.ws.rs.NotFoundException
51 import javax.ws.rs.core.Response
52
53 class DoCreateSliceServiceOptionV2 extends AbstractServiceTaskProcessor{
54
55     private static final Logger logger = LoggerFactory.getLogger( DoCreateSliceServiceOptionV2.class)
56
57     ExceptionUtil exceptionUtil = new ExceptionUtil()
58
59     JsonUtils jsonUtil = new JsonUtils()
60
61     OofUtils oofUtils = new OofUtils()
62
63     ObjectMapper objectMapper = new ObjectMapper()
64
65     void preProcessRequest (DelegateExecution execution) {
66     }
67
68
69     /**
70      * prepare select nsi request
71      * @param execution
72      */
73     void prepareSelectNSIRequest(DelegateExecution execution) {
74
75         String urlString = UrnPropertiesReader.getVariable("mso.oof.endpoint", execution)
76         logger.debug( "get NSI option OOF Url: " + urlString)
77
78         boolean isNSISuggested = true
79         execution.setVariable("isNSISuggested",isNSISuggested)
80         String requestId = execution.getVariable("msoRequestId")
81         String messageType = "NSISelectionResponse"
82
83         Map<String, Object> profileInfo = execution.getVariable("serviceProfile")
84         Map<String, Object> nstSolution = execution.getVariable("nstSolution")
85         logger.debug("Get NST selection from OOF: " + nstSolution.toString())
86         String nstInfo = """{
87             "modelInvariantId":"${nstSolution.invariantUUID}",
88             "modelVersionId":"${nstSolution.UUID}",
89             "modelName":"${nstSolution.NSTName}"
90          }"""
91
92         execution.setVariable("nsiSelectionUrl", "/api/oof/selection/nsi/v1")
93         execution.setVariable("nsiSelection_messageType",messageType)
94         execution.setVariable("nsiSelection_correlator",requestId)
95         String timeout = UrnPropertiesReader.getVariable("mso.adapters.oof.timeout", execution);
96         execution.setVariable("nsiSelection_timeout",timeout)
97         String oofRequest = oofUtils.buildSelectNSIRequest(requestId, nstInfo,messageType, profileInfo)
98         execution.setVariable("nsiSelection_oofRequest",oofRequest)
99         logger.debug("Sending request to OOF: " + oofRequest)
100     }
101
102     /**
103      * process select nsi response
104      * @param execution
105      */
106     void processOOFResponse(DelegateExecution execution) {
107
108         SliceTaskParams sliceTaskParams = execution.getVariable("sliceTaskParams") as SliceTaskParams
109         String OOFResponse = execution.getVariable("nsiSelection_oofResponse")
110         logger.debug("NSI OOFResponse is: " + OOFResponse)
111         execution.setVariable("OOFResponse", OOFResponse)
112         //This needs to be changed to derive a value when we add policy to decide the solution options.
113
114         Map<String, Object> resMap = objectMapper.readValue(OOFResponse, Map.class)
115         List<Map<String, Object>> nsiSolutions = (List<Map<String, Object>>) resMap.get("solutions")
116         Map<String, Object> solutions = nsiSolutions.get(0)
117
118         String resourceSharingLevel = execution.getVariable("resourceSharingLevel")
119         Boolean isSharable = resourceSharingLevel == "shared"
120
121         if (solutions != null) {
122             if (isSharable && solutions.get("existingNSI")) {
123                 //sharedNSISolution
124                 processSharedNSISolutions(solutions, execution)
125             }
126             else if(solutions.containsKey("newNSISolution")) {
127                 processNewNSISolutions(solutions, execution)
128             }
129         }
130         execution.setVariable("sliceTaskParams", sliceTaskParams)
131         logger.debug("sliceTaskParams: "+sliceTaskParams.convertToJson())
132         logger.debug("*** Completed options Call to OOF ***")
133
134         logger.debug("start parseServiceProfile")
135         parseServiceProfile(execution)
136         logger.debug("end parseServiceProfile")
137     }
138
139
140     private void processNewNSISolutions(Map solutions, DelegateExecution execution) {
141         int index = 0
142         List<Map> newNSISolutions = solutions.get("newNSISolution")
143         List<Map> NSSImap = new ArrayList<>()
144         if (newNSISolutions != null && newNSISolutions.size() > 0) {
145             NSSImap = newNSISolutions.get(index).get("NSSISolutions") as List<Map>
146             for (Map nssi : NSSImap) {
147                 Map oofSliceProfile = nssi.get("sliceProfile")
148                 String domain = oofSliceProfile.getOrDefault("domainType","")
149                 logger.debug("OOF newNSISolutions SliceProfile: " +oofSliceProfile.toString()+",domain:${domain}")
150                 if(null != domain){
151                     //TODO
152 //                    def nssiSolution = nssi.get("NSSISolution") as Map<String, ?>
153 //                    String nssiName = nssiSolution.getOrDefault("NSSIName", "")
154 //                    String nssiId = nssiSolution.getOrDefault("NSSIId", "")
155 //                    saveNSSIId(nssi, sliceTaskParams)
156                     Map<String, Object> sliceProfile = getSliceProfile(domain, execution, oofSliceProfile)
157                     saveSliceProfile(execution, domain, sliceProfile)
158
159                 }
160             }
161         }
162     }
163
164     private void processSharedNSISolutions(Map solutions, DelegateExecution execution) {
165         String nsiName, nsiInstanceId, nssiId, nssiName
166         SliceTaskParams sliceTaskParams = execution.getVariable("sliceTaskParams")
167
168         Map sharedNSIsolution = ((List) solutions.get("sharedNSISolutions"))?.get(0)
169         nsiInstanceId = sharedNSIsolution.getOrDefault("NSIId", "")
170         nsiName = sharedNSIsolution.getOrDefault("NSIName", "")
171         sliceTaskParams.setSuggestNsiId(nsiInstanceId)
172         sliceTaskParams.setSuggestNsiName(nsiName)
173
174         //Temporary modification
175         List NSSIs = sharedNSIsolution.get("NSSIs")
176         for(Map nssi : NSSIs){
177             Map oofSliceProfile = ((List)nssi.get("sliceProfile"))?.get(0)
178             String domain = oofSliceProfile.getOrDefault("domainType","")
179             nssiId = nssi.getOrDefault("NSSIId","")
180             nssiName = nssi.getOrDefault("NSSIName","")
181             saveNSSIId(domain, nssiId, nssiName,execution)
182             Map<String, Object> sliceProfile = getSliceProfile(domain, execution, oofSliceProfile)
183             saveSliceProfile(execution, domain, sliceProfile)
184             logger.debug("OOF sharedNSISolution SliceProfile:"+oofSliceProfile.toString()+",domain:${domain}")
185             logger.debug("OOF sharedNSISolution nsiInstanceId:${nsiInstanceId}, nsiName:${nsiName}, nssiId:${nssiId}, nssiName:${nssiName}")
186         }
187     }
188
189     private void parseServiceProfile(DelegateExecution execution) {
190         logger.debug("Start parseServiceProfile")
191         String serviceType = execution.getVariable("serviceType")
192         Map<String, Object> serviceProfile = execution.getVariable("serviceProfile")
193         SliceTaskParams sliceTaskParams = execution.getVariable("sliceTaskParams")
194         // set sliceProfile for three domains
195         if(!sliceTaskParams.getSliceProfileAn()){
196             Map<String, Object> sliceProfileAn = getSliceProfile( "AN", execution,null)
197             saveSliceProfile(execution,"AN",sliceProfileAn)
198         }
199
200         if(!sliceTaskParams.getSliceProfileTn()){
201             Map<String, Object> sliceProfileTn = getSliceProfile( "TN", execution,null)
202             saveSliceProfile(execution,"TN",sliceProfileTn)
203         }
204
205         if(!sliceTaskParams.getSliceProfileCn()){
206             Map<String, Object> sliceProfileCn = getSliceProfile( "CN", execution,null, )
207             saveSliceProfile(execution,"CN",sliceProfileCn)
208         }
209
210         logger.debug("Finish parseServiceProfile")
211     }
212
213     private void saveSliceProfile(DelegateExecution execution, String domain, Map<String, Object> sliceProfile){
214         SliceTaskParams sliceTaskParams = execution.getVariable("sliceTaskParams")
215         if(domain.equalsIgnoreCase("AN")){
216             execution.setVariable("sliceProfileAn", sliceProfile)
217             sliceTaskParams.setSliceProfileAn(sliceProfile)
218             logger.debug("sliceProfileAn: " + sliceProfile)
219         }
220         else if(domain.equalsIgnoreCase("TN")){
221             execution.setVariable("sliceProfileTn", sliceProfile)
222             sliceTaskParams.setSliceProfileTn(sliceProfile)
223             logger.debug("sliceProfileTn: " + sliceProfile)
224         }
225         else if(domain.equalsIgnoreCase("CN")){
226             execution.setVariable("sliceProfileCn", sliceProfile)
227             sliceTaskParams.setSliceProfileCn(sliceProfile)
228             logger.debug("sliceProfileCn: " + sliceProfile)
229         }
230     }
231
232     private void saveNSSIId(String domain, String nssiId, String nssiName, DelegateExecution execution) {
233         SliceTaskParams sliceTaskParams = execution.getVariable("sliceTaskParams")
234         if(domain.equalsIgnoreCase("AN")){
235             sliceTaskParams.setAnSuggestNssiId(nssiId)
236             sliceTaskParams.setAnSuggestNssiName(nssiName)
237         }
238         else if(domain.equalsIgnoreCase("CN")){
239             sliceTaskParams.setCnSuggestNssiId(nssiId)
240             sliceTaskParams.setCnSuggestNssiName(nssiName)
241         }
242         else if(domain.equalsIgnoreCase("TN")){
243             sliceTaskParams.setTnSuggestNssiId(nssiId)
244             sliceTaskParams.setTnSuggestNssiName(nssiName)
245         }
246     }
247
248     private Map getSliceProfile(String domain, DelegateExecution execution, Map<String, Object> oofSliceProfile) {
249         String profileMapStr
250         Map<String, Object> serviceProfile = execution.getVariable("serviceProfile")
251         Integer domainLatency = (Integer) serviceProfile.get("latency")/3
252
253         if(domain.equalsIgnoreCase("AN")){
254             profileMapStr = """ {
255                     "latency": ${domainLatency}, 
256                     "sNSSAI": "sNSSAI", 
257                     "uEMobilityLevel": "uEMobilityLevel", 
258                     "coverageAreaTAList": "coverageAreaTAList", 
259                     "5QI": 100
260                 }"""
261         }
262         else if(domain.equalsIgnoreCase("TN")){
263             profileMapStr =""" {
264                     "latency":${domainLatency},
265                     "sNSSAI":"sNSSAI", 
266                     "e2eLatency":"latency", 
267                     "bandwidth": 100
268                 }"""
269         }
270         else if(domain.equalsIgnoreCase("CN")){
271             profileMapStr = """ {
272                     "areaTrafficCapDL":"areaTrafficCapDL",
273                     "maxNumberofUEs":"maxNumberofUEs",
274                     "latency":${domainLatency},
275                     "expDataRateUL":"expDataRateUL", 
276                     "sNSSAI":"sNSSAI", 
277                     "areaTrafficCapUL":"areaTrafficCapUL",
278                     "uEMobilityLevel":"uEMobilityLevel", 
279                     "expDataRateDL":"expDataRateDL",  
280                     "activityFactor":"activityFactor",
281                     "resourceSharingLevel":"resourceSharingLevel"
282                 }"""
283         }
284
285         logger.debug("Profile map for " + domain + " : " + profileMapStr)
286         Map<String, Object> profileMaps = objectMapper.readValue(profileMapStr.trim().replaceAll(" ", ""), new TypeReference<Map<String, String>>(){})
287         Map<String, Object> sliceProfile = [:]
288         for (Map.Entry<String, String> profileMap : profileMaps) {
289             String key = profileMap.key
290             String value = profileMaps.get(key)
291             if(null != oofSliceProfile && oofSliceProfile.keySet().contains(key)){
292                 sliceProfile.put(key, oofSliceProfile.get(key))
293                 logger.debug("Get from oof, key:${key}, value: ${oofSliceProfile.get(key)}")
294             }
295             else if(serviceProfile.keySet().contains(value)){
296                 sliceProfile.put(key, serviceProfile.get(value))
297             }
298             else{
299                 sliceProfile.put(key, profileMaps.get(key))
300             }
301         }
302         return sliceProfile
303     }
304
305     void processDecomposition(DelegateExecution execution){
306         logger.debug("Start processDecomposition")
307
308         ServiceDecomposition serviceDecomposition= execution.getVariable("serviceDecomposition")
309         SliceTaskParams sliceTaskParams = execution.getVariable("sliceTaskParams")
310         String nstName = serviceDecomposition.getModelInfo().getModelName()
311         String nstId = serviceDecomposition.getModelInfo().getModelUuid()
312         sliceTaskParams.setNstName(nstName)
313         sliceTaskParams.setNstId(nstId)
314
315         logger.debug("End processDecomposition")
316     }
317
318
319     void prepareNSTDecompose(DelegateExecution execution) {
320
321         String modelUuid = execution.getVariable("nstModelUuid")
322         String modelInvariantUuid = execution.getVariable("nstModelInvariantUuid")
323
324         String serviceModelInfo = """{
325             "modelInvariantUuid":"${modelInvariantUuid}",
326             "modelUuid":"${modelUuid}",
327             "modelVersion":""
328              }"""
329         execution.setVariable("serviceModelInfo", serviceModelInfo)
330     }
331
332     void prepareNSSTDecompose(DelegateExecution execution) {
333         Boolean isMoreNSSTtoProcess = false
334         Integer maxNSST = execution.getVariable("maxNSST")
335         Integer currentNSST=execution.getVariable("currentNSST")
336         List<String> nsstModelUUIDList = new ArrayList<>()
337         nsstModelUUIDList = execution.getVariable("nsstModelUUIDList")
338         String modelUuid = nsstModelUUIDList.get(currentNSST)
339         String serviceModelInfo = """{
340             "modelInvariantUuid":"",
341             "modelUuid":"${modelUuid}",
342             "modelVersion":""
343              }"""
344         execution.setVariable("serviceModelInfo", serviceModelInfo)
345         currentNSST=currentNSST+1
346         if(currentNSST<maxNSST)
347             isMoreNSSTtoProcess=true
348         execution.setVariable("isMoreNSSTtoProcess",isMoreNSSTtoProcess)
349         execution.setVariable("maxNSST",maxNSST)
350         execution.setVariable("currentNSST",currentNSST)
351     }
352
353
354     void prepareNSSTlistfromNST(DelegateExecution execution) {
355         //Need to update this part from decomposition.
356         logger.trace("Enter prepareNSSTlistfromNST()")
357         Boolean isMoreNSSTtoProcess = false
358         ServiceDecomposition serviceDecomposition= execution.getVariable("serviceDecomposition")
359         SliceTaskParams sliceTaskParams = execution.getVariable("sliceTaskParams")
360         String nstName = serviceDecomposition.getModelInfo().getModelName()
361         sliceTaskParams.setNstName(nstName)
362         String nstId = serviceDecomposition.getModelInfo().getModelUuid()
363         sliceTaskParams.setNstId(nstId)
364         execution.setVariable("sliceTaskParams",sliceTaskParams)
365
366         List<ServiceProxy> proxyList = serviceDecomposition.getServiceProxy()
367         List<String> nsstModelUUIDList = new ArrayList<>()
368         for(ServiceProxy serviceProxy:proxyList)
369             nsstModelUUIDList.add(serviceProxy.getSourceModelUuid())
370         execution.setVariable("nsstModelUUIDList",nsstModelUUIDList)
371         Integer maxNSST = nsstModelUUIDList.size()
372         Integer currentNSST=0
373         execution.setVariable("maxNSST",maxNSST)
374         execution.setVariable("currentNSST",currentNSST)
375         if(currentNSST<maxNSST)
376             isMoreNSSTtoProcess=true
377         execution.setVariable("isMoreNSSTtoProcess",isMoreNSSTtoProcess)
378         logger.trace("Exit prepareNSSTlistfromNST()")
379
380     }
381
382     void getNSSTOption(DelegateExecution execution) {
383         ServiceDecomposition serviceDecomposition= execution.getVariable("serviceDecomposition")
384         String urlString = UrnPropertiesReader.getVariable("mso.oof.endpoint", execution)
385         String globalSubscriberId = execution.getVariable("globalSubscriberId")
386         String serviceType = execution.getVariable("subscriptionServiceType")
387         String nssiInstanceId =""
388         String nssiName =""
389         SliceTaskParams sliceTaskParams = execution.getVariable("sliceTaskParams")
390         logger.debug( "get NSI option OOF Url: " + urlString)
391         boolean isNSISuggested = false
392         execution.setVariable("isNSISuggested",isNSISuggested)
393
394         //Prepare auth for OOF - Begin
395         def authHeader = ""
396         String basicAuth = UrnPropertiesReader.getVariable("mso.oof.auth", execution)
397         String msokey = UrnPropertiesReader.getVariable("mso.msoKey", execution)
398
399         String basicAuthValue = utils.encrypt(basicAuth, msokey)
400         if (basicAuthValue != null) {
401             logger.debug( "Obtained BasicAuth username and password for OOF: " + basicAuthValue)
402             try {
403                 authHeader = utils.getBasicAuth(basicAuthValue, msokey)
404                 execution.setVariable("BasicAuthHeaderValue", authHeader)
405             } catch (Exception ex) {
406                 logger.debug( "Unable to encode username and password string: " + ex)
407                 exceptionUtil.buildAndThrowWorkflowException(execution, 401, "Internal Error - Unable to " +
408                         "encode username and password string")
409             }
410         } else {
411             logger.debug( "Unable to obtain BasicAuth - BasicAuth value null")
412             exceptionUtil.buildAndThrowWorkflowException(execution, 401, "Internal Error - BasicAuth " +
413                     "value null")
414         }
415         //Prepare auth for OOF - End
416         //Prepare send request to OOF - Begin
417         String requestId = execution.getVariable("msoRequestId")
418         Map<String, Object> profileInfo = execution.getVariable("serviceProfile")
419         String nsstModelInvariantUuid = serviceDecomposition.getModelInfo().getModelInvariantUuid()
420         String nsstModelUuid = serviceDecomposition.getModelInfo().getModelUuid()
421         String nsstInfo = """"NSSTInfo": {
422         "invariantUUID":"${nsstModelInvariantUuid}",
423         "UUID":"${nsstModelUuid}"
424          }"""
425         String oofRequest = oofUtils.buildSelectNSSIRequest(execution, requestId, nsstInfo ,profileInfo)
426
427
428         URL url = new URL(urlString+"/api/oof/v1/selectnssi")
429         HttpClient httpClient = new HttpClientFactory().newJsonClient(url, ONAPComponents.OOF)
430         httpClient.addAdditionalHeader("Authorization", authHeader)
431         Response httpResponse = httpClient.post(oofRequest)
432
433         int responseCode = httpResponse.getStatus()
434         logger.debug("OOF sync response code is: " + responseCode)
435
436         if(responseCode != 200){
437             exceptionUtil.buildAndThrowWorkflowException(execution, responseCode, "Received a Bad Sync Response from OOF.")
438         }
439
440         if(httpResponse.hasEntity()){
441             String OOFResponse = httpResponse.readEntity(String.class)
442             execution.setVariable("OOFResponse", OOFResponse)
443             nssiInstanceId = jsonUtil.getJsonValue(OOFResponse, "NSSIIInfo.NSSIID")
444             nssiName = jsonUtil.getJsonValue(OOFResponse, "NSSIInfo.NSSIName")
445             execution.setVariable("nssiInstanceId",nssiInstanceId)
446             execution.setVariable("nssiName",nssiName)
447         }
448         if(StringUtils.isBlank(nssiInstanceId)){
449             logger.debug( "There is no valid NSST suggested by OOF.")
450         }else
451         {
452             try {
453                 AAIResourcesClient resourceClient = new AAIResourcesClient()
454                 AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalSubscriberId, serviceType, nssiInstanceId)
455                 AAIResultWrapper wrapper = resourceClient.get(serviceInstanceUri, NotFoundException.class)
456                 Optional<ServiceInstance> si = wrapper.asBean(org.onap.aai.domain.yang.ServiceInstance.class)
457                 org.onap.aai.domain.yang.ServiceInstance nssi = si.get()
458
459                 String domain = nssi.getEnvironmentContext().toString().toUpperCase()
460                 switch (domain) {
461                     case "AN":
462                         sliceTaskParams.setAnSuggestNssiId(nssi.getServiceInstanceId())
463                         sliceTaskParams.setAnSuggestNssiName(nssi.getServiceInstanceName())
464                         break
465                     case "CN":
466                         sliceTaskParams.setCnSuggestNssiId(nssi.getServiceInstanceId())
467                         sliceTaskParams.setCnSuggestNssiName(nssi.getServiceInstanceName())
468                         break
469                     case "TN":
470                         sliceTaskParams.setTnSuggestNssiId(nssi.getServiceInstanceId())
471                         sliceTaskParams.setTnSuggestNssiName(nssi.getServiceInstanceName())
472                         break
473                     default:
474                         break
475                 }
476             }catch(NotFoundException e)
477             {
478                 logger.debug("NSSI Service Instance not found in AAI: " + nssiInstanceId)
479             }catch(Exception e)
480             {
481                 logger.debug("NSSI Service Instance not found in AAI: " + nssiInstanceId)
482             }
483         }
484         logger.debug("Prepare NSSI option completed ")
485     }
486
487
488     /**
489      * new
490      */
491
492     private static final ObjectMapper MAPPER = new ObjectMapper()
493
494     private NssmfAdapterUtils nssmfAdapterUtils = new NssmfAdapterUtils(httpClientFactory, jsonUtil)
495
496     private static final String QUERY_SUB_NET_CAPABILITY = "/api/rest/provMns/v1/NSS/subnetCapabilityQuery"
497
498     private static final String QUERY_NSSI_SELECTION_CAPABILITY = "/api/rest/provMns/v1/NSS/NSSISelectionCapability"
499
500     /**
501      * query Subnet Capability of TN
502      * @param execution
503      */
504     public void queryTNSubnetCapability(DelegateExecution execution) {
505
506         String vendor = execution.getVariable("vendor")
507
508         List<String> subnetTypes =  new ArrayList<>()
509         subnetTypes.add("TN_FH")
510         subnetTypes.add("TN_MH")
511         subnetTypes.add("TN_BH")
512
513         String strRequest = MAPPER.writeValueAsString(
514                 buildQuerySubnetCapRequest(vendor, subnetTypes, NetworkType.TRANSPORT))
515
516         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_SUB_NET_CAPABILITY, strRequest)
517         execution.setVariable("subnetCapabilityOfTN", response)
518     }
519
520     /**
521      * query Subnet Capability of CN
522      * @param execution
523      */
524     public void queryCNSubnetCapability(DelegateExecution execution) {
525
526         String vendor = execution.getVariable("vendor")
527
528         List<String> subnetTypes =  new ArrayList<>()
529         subnetTypes.add("CN")
530
531         String strRequest = MAPPER.writeValueAsString(buildQuerySubnetCapRequest(vendor, subnetTypes, NetworkType.CORE))
532
533         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_SUB_NET_CAPABILITY, strRequest)
534
535         execution.setVariable("subnetCapabilityOfCN", response)
536     }
537
538     /**
539      * query Subnet Capability of AN
540      * @param execution
541      */
542     public void queryANSubnetCapability(DelegateExecution execution) {
543
544         String vendor = execution.getVariable("vendor")
545
546         List<String> subnetTypes =  new ArrayList<>()
547         subnetTypes.add("AN-NF")
548
549         String strRequest = MAPPER.writeValueAsString(buildQuerySubnetCapRequest(vendor, subnetTypes, NetworkType.ACCESS))
550
551         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_SUB_NET_CAPABILITY, strRequest)
552
553         execution.setVariable("subnetCapabilityOfAN", response)
554     }
555
556     /**
557      * build request body for querying Subnet Capability
558      * @param vendor
559      * @param subnetTypes
560      * @param networkType
561      * @return
562      */
563     private static String buildQuerySubnetCapRequest(String vendor, List<String> subnetTypes, NetworkType networkType) {
564         NssmfAdapterNBIRequest request = new NssmfAdapterNBIRequest()
565
566         Map<String, Object> paramMap = new HashMap()
567         paramMap.put("subnetTypes", subnetTypes)
568
569         request.setSubnetCapabilityQuery(MAPPER.writeValueAsString(paramMap))
570
571         EsrInfo esrInfo = new EsrInfo()
572         esrInfo.setVendor(vendor)
573         esrInfo.setNetworkType(networkType)
574
575         request.setEsrInfo(esrInfo)
576
577         String strRequest = MAPPER.writeValueAsString(request)
578
579         return strRequest
580     }
581
582     /**
583      * handle response of Subnet Capability, generate SubnetCapabilities Info for request to oof
584      * @param execution
585      */
586     public void generateSubnetCapabilities(DelegateExecution execution) {
587         //todo:
588         execution.setVariable("subnetCapabilities", [])
589     }
590
591     /**
592      * prepare the params for decompose nst
593      * @param execution
594      */
595     public void prepareDecomposeNST(DelegateExecution execution) {
596
597         String modelUuid = execution.getVariable("nstModelUuid")
598         String modelInvariantUuid = execution.getVariable("nstModelInvariantUuid")
599
600         String serviceModelInfo = """{
601             "modelInvariantUuid":"${modelInvariantUuid}",
602             "modelUuid":"${modelUuid}",
603             "modelVersion":""
604              }"""
605         execution.setVariable("nstServiceModelInfo", serviceModelInfo)
606     }
607
608     /**
609      * process the result of NST Decomposition
610      * @param execution
611      */
612     public void processDecompositionNST(DelegateExecution execution) {
613
614         ServiceDecomposition nstServiceDecomposition = execution.getVariable("nstServiceDecomposition")
615         //todo:
616
617     }
618
619
620     /**
621      * prepare the params for decompose nsst
622      * @param execution
623      */
624     public void prepareDecomposeNSST(DelegateExecution execution) {
625         Boolean isMoreNSSTtoProcess = false
626         def maxNSST = execution.getVariable("maxNSST") as Integer
627         def currentNSST = execution.getVariable("currentNSST") as Integer
628         def nsstModelUUIDList = execution.getVariable("nsstModelUUIDList") as List
629         String modelUuid = nsstModelUUIDList.get(currentNSST)
630         String serviceModelInfo = """{
631             "modelInvariantUuid":"",
632             "modelUuid":"${modelUuid}",
633             "modelVersion":""
634              }"""
635         execution.setVariable("nsstServiceModelInfo", serviceModelInfo)
636         currentNSST = currentNSST + 1
637
638         if(currentNSST < maxNSST) {
639             isMoreNSSTtoProcess = true
640         }
641
642         execution.setVariable("isMoreNSSTtoProcess", isMoreNSSTtoProcess)
643         execution.setVariable("maxNSST", maxNSST)
644         execution.setVariable("currentNSST", currentNSST)
645
646         //todo:
647     }
648
649     /**
650      * process the result of NSST Decomposition
651      * @param execution
652      */
653     public void processDecompositionNSST(DelegateExecution execution) {
654         ServiceDecomposition nsstServiceDecomposition = execution.getVariable("nsstServiceDecomposition")
655         //todo:
656     }
657
658     /**
659      * todo: need rewrite
660      * prepare select nsi request
661      * @param execution
662      */
663     public void preNSIRequest(DelegateExecution execution) {
664
665         String urlString = UrnPropertiesReader.getVariable("mso.oof.endpoint", execution)
666         logger.debug( "get NSI option OOF Url: " + urlString)
667
668         boolean isNSISuggested = true
669         execution.setVariable("isNSISuggested", isNSISuggested)
670         String requestId = execution.getVariable("msoRequestId")
671         String messageType = "NSISelectionResponse"
672
673         ServiceProfile profileInfo = execution.getVariable("serviceProfile") as ServiceProfile
674         Map<String, Object> nstSolution = execution.getVariable("nstSolution") as Map
675         logger.debug("Get NST selection from OOF: " + nstSolution.toString())
676
677         execution.setVariable("nsiSelectionUrl", "/api/oof/selection/nsi/v1")
678         execution.setVariable("nsiSelection_messageType", messageType)
679         execution.setVariable("nsiSelection_correlator", requestId)
680         String timeout = UrnPropertiesReader.getVariable("mso.adapters.oof.timeout", execution)
681         execution.setVariable("nsiSelection_timeout", timeout)
682
683
684         TemplateInfo nstInfo = new TemplateInfo()
685         nstInfo.setInvariantUUID(nstSolution.get("invariantUUID") as String)
686         nstInfo.setUUID(nstSolution.get("UUID") as String)
687         nstInfo.setName(nstSolution.get("NSTName") as String)
688
689         execution.setVariable("NSTInfo", nstInfo)
690
691         List<TemplateInfo> nsstInfos = execution.getVariable("NSSTInfos") as List<TemplateInfo>
692
693         List<SubnetCapability> subnetCapabilities = execution.getVariable("subnetCapabilities") as List<SubnetCapability>
694
695         String oofRequest = oofUtils.buildSelectNSIRequest(requestId, nstInfo, nsstInfos,
696                 messageType, profileInfo, subnetCapabilities, timeout as Integer)
697
698         execution.setVariable("nsiSelection_oofRequest", oofRequest)
699         logger.debug("Sending request to OOF: " + oofRequest)
700     }
701
702     /**
703      * todo: need rewrite
704      * process select nsi response
705      * @param execution
706      */
707     public void processNSIResp(DelegateExecution execution) {
708
709         SliceTaskParams sliceTaskParams = execution.getVariable("sliceTaskParams") as SliceTaskParams
710         String OOFResponse = execution.getVariable("nsiSelection_oofResponse")
711         logger.debug("NSI OOFResponse is: " + OOFResponse)
712         execution.setVariable("OOFResponse", OOFResponse)
713         //This needs to be changed to derive a value when we add policy to decide the solution options.
714
715         Map<String, Object> resMap = objectMapper.readValue(OOFResponse, Map.class)
716         List<Map<String, Object>> nsiSolutions = (List<Map<String, Object>>) resMap.get("solutions")
717         Map<String, Object> solutions = nsiSolutions.get(0)
718
719         String resourceSharingLevel = execution.getVariable("resourceSharingLevel")
720         Boolean isSharable = resourceSharingLevel == "shared"
721
722         if (solutions != null) {
723             if (isSharable && solutions.get("existingNSI")) {
724                 //sharedNSISolution
725                 processSharedNSISolutions(solutions, execution)
726             }
727             else if(solutions.containsKey("newNSISolution")) {
728                 processNewNSISolutions(solutions, execution)
729             }
730         }
731         execution.setVariable("sliceTaskParams", sliceTaskParams)
732         logger.debug("sliceTaskParams: " + sliceTaskParams.convertToJson())
733         logger.debug("*** Completed options Call to OOF ***")
734
735         logger.debug("start parseServiceProfile")
736         parseServiceProfile(execution)
737         logger.debug("end parseServiceProfile")
738     }
739
740     /**
741      * get NSSI Selection Capability for AN
742      * @param execution
743      */
744     public void getNSSISelectionCap4AN(DelegateExecution execution) {
745
746         def vendor = execution.getVariable("vendor") as String
747
748         String strRequest = buildNSSISelectionReq(vendor, NetworkType.ACCESS)
749
750         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_NSSI_SELECTION_CAPABILITY, strRequest)
751
752         Map<String, Object> resMap = objectMapper.readValue(response, Map.class)
753
754         String selection = resMap.get("selection")
755
756
757         if ("NSMF".equalsIgnoreCase(selection)) {
758             execution.setVariable("NEED_AN_NSSI_SELECTION", true)
759         }
760     }
761
762     /**
763      * get NSSI Selection Capability for TN
764      * @param execution
765      */
766     public void getNSSISelectionCap4TN(DelegateExecution execution) {
767
768         def vendor = execution.getVariable("vendor") as String
769
770         String strRequest = buildNSSISelectionReq(vendor, NetworkType.TRANSPORT)
771
772         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_NSSI_SELECTION_CAPABILITY, strRequest)
773
774         Map<String, Object> resMap = objectMapper.readValue(response, Map.class)
775
776         String selection = resMap.get("selection")
777
778         if ("NSMF".equalsIgnoreCase(selection)) {
779             execution.setVariable("NEED_TN_NSSI_SELECTION", true)
780         }
781     }
782
783     /**
784      * get NSSI Selection Capability for CN
785      * @param execution
786      */
787     public void getNSSISelectionCap4CN(DelegateExecution execution) {
788
789         def vendor = execution.getVariable("vendor") as String
790
791         String strRequest = buildNSSISelectionReq(vendor, NetworkType.CORE)
792
793         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_NSSI_SELECTION_CAPABILITY, strRequest)
794
795         Map<String, Object> resMap = objectMapper.readValue(response, Map.class)
796
797         String selection = resMap.get("selection")
798
799         if ("NSMF".equalsIgnoreCase(selection)) {
800             execution.setVariable("NEED_CN_NSSI_SELECTION", true)
801         }
802     }
803
804     /**
805      * build NSSI Selection Capability Request body to nssmf adapter
806      * @param vendor
807      * @param networkType
808      * @return
809      */
810     private static String buildNSSISelectionReq(String vendor, NetworkType networkType) {
811         NssmfAdapterNBIRequest request = new NssmfAdapterNBIRequest()
812         EsrInfo esrInfo = new EsrInfo()
813         esrInfo.setVendor(vendor)
814         esrInfo.setNetworkType(networkType)
815         request.setEsrInfo(esrInfo)
816
817         return MAPPER.writeValueAsString(request)
818     }
819
820     /**
821      * if exist nssi need to select?
822      * @param execution
823      */
824     public void handleNssiSelect(DelegateExecution execution) {
825
826         SliceTaskParamsAdapter sliceTaskParams =
827                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
828
829         execution.setVariable()
830     }
831
832     /**
833      * todo: need rewrite
834      * prepare select nssi request
835      * @param execution
836      */
837     public void preNSSIRequest(DelegateExecution execution) {
838
839         String urlString = UrnPropertiesReader.getVariable("mso.oof.endpoint", execution)
840         logger.debug( "get NSI option OOF Url: " + urlString)
841
842         boolean isNSISuggested = true
843         execution.setVariable("isNSISuggested", isNSISuggested)
844         String requestId = execution.getVariable("msoRequestId")
845         String messageType = "NSISelectionResponse"
846
847         Map<String, Object> profileInfo = execution.getVariable("serviceProfile") as Map
848         Map<String, Object> nstSolution = execution.getVariable("nstSolution") as Map
849         logger.debug("Get NST selection from OOF: " + nstSolution.toString())
850         String nstInfo = """{
851             "modelInvariantId":"${nstSolution.invariantUUID}",
852             "modelVersionId":"${nstSolution.UUID}",
853             "modelName":"${nstSolution.NSTName}"
854          }"""
855
856         execution.setVariable("nsiSelectionUrl", "/api/oof/selection/nsi/v1")
857         execution.setVariable("nsiSelection_messageType", messageType)
858         execution.setVariable("nsiSelection_correlator", requestId)
859         String timeout = UrnPropertiesReader.getVariable("mso.adapters.oof.timeout", execution)
860         execution.setVariable("nsiSelection_timeout", timeout)
861
862         //todo
863         String oofRequest = oofUtils.buildSelectNSIRequest(requestId, nstInfo, messageType, profileInfo)
864
865         execution.setVariable("nsiSelection_oofRequest", oofRequest)
866         logger.debug("Sending request to OOF: " + oofRequest)
867     }
868
869     /**
870      * process select nssi response
871      * @param execution
872      */
873     public void processNSSIResp(DelegateExecution execution) {
874
875         SliceTaskParams sliceTaskParams = execution.getVariable("sliceTaskParams") as SliceTaskParams
876         String OOFResponse = execution.getVariable("nsiSelection_oofResponse")
877         logger.debug("NSI OOFResponse is: " + OOFResponse)
878         execution.setVariable("OOFResponse", OOFResponse)
879         //This needs to be changed to derive a value when we add policy to decide the solution options.
880
881         Map<String, Object> resMap = objectMapper.readValue(OOFResponse, Map.class)
882         List<Map<String, Object>> nsiSolutions = (List<Map<String, Object>>) resMap.get("solutions")
883         Map<String, Object> solutions = nsiSolutions.get(0)
884
885         String resourceSharingLevel = execution.getVariable("resourceSharingLevel")
886         Boolean isSharable = resourceSharingLevel == "shared"
887
888         if (solutions != null) {
889             if (isSharable && solutions.get("existingNSI")) {
890                 //sharedNSISolution
891                 processSharedNSISolutions(solutions, execution)
892             }
893             else if(solutions.containsKey("newNSISolution")) {
894                 processNewNSISolutions(solutions, execution)
895             }
896         }
897         execution.setVariable("sliceTaskParams", sliceTaskParams)
898         logger.debug("sliceTaskParams: "+sliceTaskParams.convertToJson())
899         logger.debug("*** Completed options Call to OOF ***")
900
901         logger.debug("start parseServiceProfile")
902         parseServiceProfile(execution)
903         logger.debug("end parseServiceProfile")
904     }
905
906
907 }