5cde856251c4e880572c3dd3892b422e185489ea
[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.SliceTaskParams
31 import org.onap.so.beans.nsmf.SliceTaskParamsAdapter
32 import org.onap.so.beans.nsmf.TnSliceProfile
33 import org.onap.so.beans.nsmf.oof.SubnetCapability
34 import org.onap.so.beans.nsmf.oof.SubnetType
35 import org.onap.so.beans.nsmf.oof.TemplateInfo
36 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
37 import org.onap.so.bpmn.common.scripts.ExceptionUtil
38 import org.onap.so.bpmn.common.scripts.NssmfAdapterUtils
39 import org.onap.so.bpmn.common.scripts.OofUtils
40 import org.onap.so.bpmn.core.UrnPropertiesReader
41 import org.onap.so.bpmn.core.domain.AllottedResource
42 import org.onap.so.bpmn.core.domain.ModelInfo
43 import org.onap.so.bpmn.core.domain.ServiceDecomposition
44 import org.onap.so.bpmn.core.json.JsonUtils
45
46 import org.slf4j.Logger
47 import org.slf4j.LoggerFactory
48 import org.springframework.http.ResponseEntity
49
50 class DoCreateSliceServiceOption extends AbstractServiceTaskProcessor{
51
52     private static final Logger logger = LoggerFactory.getLogger(DoCreateSliceServiceOption.class)
53
54     ExceptionUtil exceptionUtil = new ExceptionUtil()
55
56     JsonUtils jsonUtil = new JsonUtils()
57
58     OofUtils oofUtils = new OofUtils()
59
60     private static final ObjectMapper objectMapper = new ObjectMapper()
61
62     private NssmfAdapterUtils nssmfAdapterUtils = new NssmfAdapterUtils(httpClientFactory, jsonUtil)
63
64     private static final String QUERY_SUB_NET_CAPABILITY = "/api/rest/provMns/v1/NSS/subnetCapabilityQuery"
65
66     private static final String QUERY_NSSI_SELECTION_CAPABILITY = "/api/rest/provMns/v1/NSS/NSSISelectionCapability"
67
68     void preProcessRequest (DelegateExecution execution) {
69     }
70
71     /**
72      * prepare the params for decompose nst
73      * @param execution
74      */
75     public void prepareDecomposeNST(DelegateExecution execution) {
76
77         SliceTaskParamsAdapter sliceTaskParams =
78                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
79
80         String modelUuid = sliceTaskParams.getNSTInfo().getUUID()
81         String modelInvariantUuid = sliceTaskParams.getNSTInfo().getInvariantUUID()
82
83         String serviceModelInfo = """{
84             "modelInvariantUuid":"${modelInvariantUuid}",
85             "modelUuid":"${modelUuid}",
86             "modelVersion":""
87              }"""
88         execution.setVariable("nstServiceModelInfo", serviceModelInfo)
89     }
90
91     /**
92      * process the result of NST Decomposition
93      * @param execution
94      */
95     public void processDecompositionNST(DelegateExecution execution) {
96
97         List<TemplateInfo> nsstInfos = new ArrayList<>()
98         ServiceDecomposition nstServiceDecomposition =
99                 execution.getVariable("nstServiceDecomposition") as ServiceDecomposition
100         //todo:
101         List<AllottedResource> allottedResources = nstServiceDecomposition.getAllottedResources()
102         for (AllottedResource allottedResource : allottedResources) {
103             TemplateInfo nsstInfo = new TemplateInfo()
104             nsstInfo.setUUID(allottedResource.getProvidingServiceModelUuid())
105             nsstInfo.setInvariantUUID(allottedResource.getProvidingServiceModelInvariantUuid())
106             nsstInfo.setName(allottedResource.getProvidingServiceModelName())
107             nsstInfos.add(nsstInfo)
108         }
109         execution.setVariable("nsstInfos", nsstInfos)
110
111         execution.setVariable("maxNsstIndex", allottedResources.size() - 1)
112         execution.setVariable("currentNsstIndex", 0)
113
114         List<ServiceDecomposition> nsstServiceDecompositions = new ArrayList<>()
115         execution.setVariable("nsstServiceDecompositions", nsstServiceDecompositions)
116     }
117
118     /**
119      * prepare the params for decompose nsst
120      * @param execution
121      */
122     public void prepareDecomposeNSST(DelegateExecution execution) {
123
124         List<TemplateInfo> nsstInfos = execution.getVariable("nsstInfos") as List<TemplateInfo>
125         int index = execution.getVariable("currentNsstIndex") as Integer
126
127         String modelUuid = nsstInfos.get(index).getUUID()
128         String modelInvariantUuid = nsstInfos.get(index).getInvariantUUID()
129
130         String serviceModelInfo = """{
131             "modelInvariantUuid":"${modelInvariantUuid}",
132             "modelUuid":"${modelUuid}",
133             "modelVersion":""
134              }"""
135         execution.setVariable("nsstServiceModelInfo", serviceModelInfo)
136
137     }
138
139     /**
140      * process the result of NSST Decomposition
141      * @param execution
142      */
143     public void processDecompositionNSST(DelegateExecution execution) {
144
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
156
157
158         int num = execution.getVariable("maxNsstIndex") as Integer
159         int index = execution.getVariable("currentNsstIndex") as Integer
160
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         //todo: set to sliceTaskParams by type
177
178         SliceTaskParamsAdapter sliceParams =
179                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
180
181         List<ServiceDecomposition> nsstServiceDecompositions =
182                 execution.getVariable("nsstServiceDecompositions") as List<ServiceDecomposition>
183
184         List<SubnetCapability> subnetCapabilities = new ArrayList<>()
185
186
187
188         for (ServiceDecomposition serviceDecomposition : nsstServiceDecompositions) {
189             //todo:
190             SubnetCapability subnetCapability = new SubnetCapability()
191
192             handleByType(execution, serviceDecomposition, sliceParams, subnetCapability)
193
194             subnetCapabilities.add(subnetCapability)
195         }
196
197         execution.setVariable("sliceTaskParams", sliceParams)
198         execution.setVariable("subnetCapabilities", subnetCapabilities)
199     }
200
201     private void handleByType(DelegateExecution execution, ServiceDecomposition serviceDecomposition,
202                               SliceTaskParamsAdapter sliceParams, SubnetCapability subnetCapability) {
203         //todo:
204         String domainType = ""
205         ModelInfo modelInfo = serviceDecomposition.getModelInfo()
206         String vendor = serviceDecomposition.getServiceRole()
207         SubnetType subnetType
208
209         switch (domainType) {
210             case "tn_bh":
211                 subnetType = SubnetType.TN_BH
212                 sliceParams.tnBHSliceTaskInfo.vendor = vendor
213                 sliceParams.tnBHSliceTaskInfo.subnetType = subnetType
214                 sliceParams.tnBHSliceTaskInfo.networkType = subnetType.networkType
215                 sliceParams.tnBHSliceTaskInfo.NSSTInfo.UUID = modelInfo.getModelUuid()
216                 sliceParams.tnBHSliceTaskInfo.NSSTInfo.invariantUUID = modelInfo.getModelInvariantUuid()
217                 sliceParams.tnBHSliceTaskInfo.NSSTInfo.name = modelInfo.getModelName()
218
219                 break
220             case "tn_mh":
221                 subnetType = SubnetType.TN_MH
222                 sliceParams.tnMHSliceTaskInfo.vendor = vendor
223                 sliceParams.tnMHSliceTaskInfo.subnetType = subnetType
224                 sliceParams.tnMHSliceTaskInfo.networkType = subnetType.networkType
225                 sliceParams.tnMHSliceTaskInfo.NSSTInfo.UUID = modelInfo.getModelUuid()
226                 sliceParams.tnMHSliceTaskInfo.NSSTInfo.invariantUUID = modelInfo.getModelInvariantUuid()
227                 sliceParams.tnMHSliceTaskInfo.NSSTInfo.name = modelInfo.getModelName()
228
229                 break
230             case "an_nf":
231                 subnetType = SubnetType.AN_NF
232                 sliceParams.anSliceTaskInfo.vendor = vendor
233                 sliceParams.anSliceTaskInfo.subnetType = subnetType
234                 sliceParams.anSliceTaskInfo.networkType = subnetType.networkType
235                 sliceParams.anSliceTaskInfo.NSSTInfo.UUID = modelInfo.getModelUuid()
236                 sliceParams.anSliceTaskInfo.NSSTInfo.invariantUUID = modelInfo.getModelInvariantUuid()
237                 sliceParams.anSliceTaskInfo.NSSTInfo.name = modelInfo.getModelName()
238                 break
239             case "cn":
240                 subnetType = SubnetType.CN
241                 sliceParams.cnSliceTaskInfo.vendor = vendor
242                 sliceParams.cnSliceTaskInfo.subnetType = subnetType
243                 sliceParams.cnSliceTaskInfo.networkType = subnetType.networkType
244                 sliceParams.cnSliceTaskInfo.NSSTInfo.UUID = modelInfo.getModelUuid()
245                 sliceParams.cnSliceTaskInfo.NSSTInfo.invariantUUID = modelInfo.getModelInvariantUuid()
246                 sliceParams.cnSliceTaskInfo.NSSTInfo.name = modelInfo.getModelName()
247                 break
248             default:
249                 subnetType = null
250                 break
251
252         //todo
253
254         }
255         if (subnetType == null) {
256             //todo: throw error
257             return
258         }
259         String response = querySubnetCapability(execution, vendor, subnetType)
260         ResponseEntity responseEntity = objectMapper.readValue(response, ResponseEntity.class)
261
262         Map<String, Object> result = responseEntity.getBody() as Map
263         for (Map.Entry<String, Object> entry : result.entrySet()) {
264             subnetCapability.setDomainType(entry.getKey())
265             subnetCapability.setCapabilityDetails(entry.getValue())
266         }
267     }
268
269     /**
270      * query Subnet Capability of TN AN CN
271      * @param execution
272      */
273     private String querySubnetCapability(DelegateExecution execution, String vendor, SubnetType subnetType) {
274
275         String strRequest = objectMapper.writeValueAsString(buildQuerySubnetCapRequest(vendor, subnetType))
276
277         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_SUB_NET_CAPABILITY, strRequest)
278         return response
279     }
280
281     /**
282      * build request body for querying Subnet Capability
283      * @param vendor
284      * @param subnetTypes
285      * @param networkType
286      * @return
287      */
288     private static String buildQuerySubnetCapRequest(String vendor, SubnetType subnetType) {
289         NssmfAdapterNBIRequest request = new NssmfAdapterNBIRequest()
290
291         List<String> subnetTypes =  new ArrayList<>()
292         subnetTypes.add(subnetType.subnetType)
293         Map<String, Object> paramMap = new HashMap()
294         paramMap.put("subnetTypes", subnetTypes)
295
296         request.setSubnetCapabilityQuery(objectMapper.writeValueAsString(paramMap))
297
298         EsrInfo esrInfo = new EsrInfo()
299         esrInfo.setVendor(vendor)
300         esrInfo.setNetworkType(subnetType.networkType)
301
302         request.setEsrInfo(esrInfo)
303
304         String strRequest = objectMapper.writeValueAsString(request)
305
306         return strRequest
307     }
308
309     /**
310      * todo: need rewrite
311      * prepare select nsi request
312      * @param execution
313      */
314     public void preNSIRequest(DelegateExecution execution) {
315
316         String urlString = UrnPropertiesReader.getVariable("mso.oof.endpoint", execution)
317         logger.debug( "get NSI option OOF Url: " + urlString)
318
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         TemplateInfo nstInfo = sliceParams.getNSTInfo()
334
335         List<TemplateInfo> nsstInfos = execution.getVariable("nsstInfos") as List<TemplateInfo>
336
337         List<SubnetCapability> subnetCapabilities =
338                 execution.getVariable("subnetCapabilities") as List<SubnetCapability>
339
340         String oofRequest = oofUtils.buildSelectNSIRequest(requestId, nstInfo, nsstInfos,
341                 messageType, profileInfo, subnetCapabilities, timeout as Integer)
342
343         execution.setVariable("nsiSelection_oofRequest", oofRequest)
344         logger.debug("Sending request to OOF: " + oofRequest)
345     }
346
347     /**
348      * todo: need rewrite
349      * process select nsi response
350      * @param execution
351      */
352     public void processNSIResp(DelegateExecution execution) {
353
354         SliceTaskParamsAdapter sliceTaskParams =
355                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
356
357         String OOFResponse = execution.getVariable("nsiSelection_oofResponse")
358         logger.debug("NSI OOFResponse is: " + OOFResponse)
359         execution.setVariable("OOFResponse", OOFResponse)
360         //This needs to be changed to derive a value when we add policy to decide the solution options.
361
362         Map<String, Object> resMap = objectMapper.readValue(OOFResponse, Map.class)
363         List<Map<String, Object>> nsiSolutions = (List<Map<String, Object>>) resMap.get("solutions")
364         Map<String, Object> solution = nsiSolutions.get(0)
365
366         String resourceSharingLevel = execution.getVariable("resourceSharingLevel")
367         Boolean isSharable = resourceSharingLevel == "shared"
368
369         if (solution != null) {
370             if (isSharable && solution.get("existingNSI")) {
371                 //sharedNSISolution
372                 processSharedNSI(solution, sliceTaskParams)
373             }
374             else if(solution.containsKey("newNSISolution")) {
375                 processNewNSI(solution, sliceTaskParams)
376             }
377         }
378         execution.setVariable("sliceTaskParams", sliceTaskParams)
379         //logger.debug("sliceTaskParams: " + sliceTaskParams.convertToJson())
380         logger.debug("*** Completed options Call to OOF ***")
381     }
382
383     private void processSharedNSI(Map<String, Object> solution, SliceTaskParamsAdapter sliceParams) {
384         Map<String, Object> sharedNSISolution = solution.get("sharedNSISolution") as Map
385
386         String nsiId = sharedNSISolution.get("NSIId")
387         String nsiName = sharedNSISolution.get("NSIName")
388         sliceParams.setSuggestNsiId(nsiId)
389         sliceParams.setSuggestNsiName(nsiName)
390     }
391
392     private void processNewNSI(Map<String, Object> solution, SliceTaskParamsAdapter sliceParams) {
393         Map<String, Object> newNSISolution = solution.get("newNSISolution") as Map
394         List<Map> sliceProfiles = newNSISolution.get("sliceProfiles") as List<Map>
395         for (Map sliceProfile : sliceProfiles) {
396             String domainType = sliceProfile.get("domainType")
397             switch (domainType.toLowerCase()) {
398                 case "tn-bh":
399                     sliceParams.tnBHSliceTaskInfo.sliceProfile = sliceProfile as TnSliceProfile
400                     break
401                 case "an-nf":
402                     sliceParams.anSliceTaskInfo.sliceProfile = sliceProfile as AnSliceProfile
403                     break
404                 case "cn":
405                     sliceParams.cnSliceTaskInfo.sliceProfile = sliceProfile as CnSliceProfile
406                     break
407                 default:
408                     break
409             }
410
411             //todo
412
413         }
414     }
415
416     /**
417      * get NSSI Selection Capability for AN
418      * @param execution
419      */
420     public void getNSSISelectionCap4AN(DelegateExecution execution) {
421
422         def vendor = execution.getVariable("vendor") as String
423
424         String strRequest = buildNSSISelectionReq(vendor, NetworkType.ACCESS)
425
426         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_NSSI_SELECTION_CAPABILITY, strRequest)
427
428         Map<String, Object> resMap = objectMapper.readValue(response, Map.class)
429
430         String selection = resMap.get("selection")
431
432
433         if ("NSMF".equalsIgnoreCase(selection)) {
434             execution.setVariable("NEED_AN_NSSI_SELECTION", true)
435         }
436     }
437
438     /**
439      * get NSSI Selection Capability for TN
440      * @param execution
441      */
442     public void getNSSISelectionCap4TN(DelegateExecution execution) {
443
444         def vendor = execution.getVariable("vendor") as String
445
446         String strRequest = buildNSSISelectionReq(vendor, NetworkType.TRANSPORT)
447
448         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_NSSI_SELECTION_CAPABILITY, strRequest)
449
450         Map<String, Object> resMap = objectMapper.readValue(response, Map.class)
451
452         String selection = resMap.get("selection")
453
454         if ("NSMF".equalsIgnoreCase(selection)) {
455             execution.setVariable("NEED_TN_NSSI_SELECTION", true)
456         }
457     }
458
459     /**
460      * get NSSI Selection Capability for CN
461      * @param execution
462      */
463     public void getNSSISelectionCap4CN(DelegateExecution execution) {
464
465         def vendor = execution.getVariable("vendor") as String
466
467         String strRequest = buildNSSISelectionReq(vendor, NetworkType.CORE)
468
469         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, QUERY_NSSI_SELECTION_CAPABILITY, strRequest)
470
471         Map<String, Object> resMap = objectMapper.readValue(response, Map.class)
472
473         String selection = resMap.get("selection")
474
475         if ("NSMF".equalsIgnoreCase(selection)) {
476             execution.setVariable("NEED_CN_NSSI_SELECTION", true)
477         }
478     }
479
480     /**
481      * build NSSI Selection Capability Request body to nssmf adapter
482      * @param vendor
483      * @param networkType
484      * @return
485      */
486     private static String buildNSSISelectionReq(String vendor, NetworkType networkType) {
487         NssmfAdapterNBIRequest request = new NssmfAdapterNBIRequest()
488         EsrInfo esrInfo = new EsrInfo()
489         esrInfo.setVendor(vendor)
490         esrInfo.setNetworkType(networkType)
491         request.setEsrInfo(esrInfo)
492
493         return objectMapper.writeValueAsString(request)
494     }
495
496     /**
497      * if exist nssi need to select?
498      * @param execution
499      */
500     public void handleNssiSelect(DelegateExecution execution) {
501
502         SliceTaskParamsAdapter sliceTaskParams =
503                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
504
505         //todo
506     }
507
508     /**
509      * todo: need rewrite
510      * prepare select nssi request
511      * @param execution
512      */
513     public void preNSSIRequest(DelegateExecution execution) {
514
515         String urlString = UrnPropertiesReader.getVariable("mso.oof.endpoint", execution)
516         logger.debug( "get NSI option OOF Url: " + urlString)
517
518         boolean isNSISuggested = true
519         execution.setVariable("isNSISuggested", isNSISuggested)
520         String requestId = execution.getVariable("msoRequestId")
521         String messageType = "NSISelectionResponse"
522
523         Map<String, Object> profileInfo = execution.getVariable("serviceProfile") as Map
524         Map<String, Object> nstSolution = execution.getVariable("nstSolution") as Map
525         logger.debug("Get NST selection from OOF: " + nstSolution.toString())
526         String nstInfo = """{
527             "modelInvariantId":"${nstSolution.invariantUUID}",
528             "modelVersionId":"${nstSolution.UUID}",
529             "modelName":"${nstSolution.NSTName}"
530          }"""
531
532         execution.setVariable("nsiSelectionUrl", "/api/oof/selection/nsi/v1")
533         execution.setVariable("nsiSelection_messageType", messageType)
534         execution.setVariable("nsiSelection_correlator", requestId)
535         String timeout = UrnPropertiesReader.getVariable("mso.adapters.oof.timeout", execution)
536         execution.setVariable("nsiSelection_timeout", timeout)
537
538         //todo
539         String oofRequest = oofUtils.buildSelectNSIRequest(requestId, nstInfo, messageType, profileInfo)
540
541         execution.setVariable("nsiSelection_oofRequest", oofRequest)
542         logger.debug("Sending request to OOF: " + oofRequest)
543     }
544
545     /**
546      * process select nssi response
547      * todo: unfinished
548      * @param execution
549      */
550     public void processNSSIResp(DelegateExecution execution) {
551
552         SliceTaskParams sliceTaskParams = execution.getVariable("sliceTaskParams") as SliceTaskParams
553         String OOFResponse = execution.getVariable("nsiSelection_oofResponse")
554         logger.debug("NSI OOFResponse is: " + OOFResponse)
555         execution.setVariable("OOFResponse", OOFResponse)
556         //This needs to be changed to derive a value when we add policy to decide the solution options.
557
558         Map<String, Object> resMap = objectMapper.readValue(OOFResponse, Map.class)
559         List<Map<String, Object>> nsiSolutions = (List<Map<String, Object>>) resMap.get("solutions")
560         Map<String, Object> solutions = nsiSolutions.get(0)
561
562         String resourceSharingLevel = execution.getVariable("resourceSharingLevel")
563         Boolean isSharable = resourceSharingLevel == "shared"
564
565         if (solutions != null) {
566             if (isSharable && solutions.get("existingNSI")) {
567                 //sharedNSISolution
568                 //processSharedNSISolutions(solutions, execution)
569             }
570             else if(solutions.containsKey("newNSISolution")) {
571                 //processNewNSISolutions(solutions, execution)
572             }
573         }
574         execution.setVariable("sliceTaskParams", sliceTaskParams)
575         logger.debug("sliceTaskParams: "+sliceTaskParams.convertToJson())
576         logger.debug("*** Completed options Call to OOF ***")
577
578         logger.debug("start parseServiceProfile")
579         //parseServiceProfile(execution)
580         logger.debug("end parseServiceProfile")
581     }
582
583
584 }