[DCAEGEN2] Calculate slice utilization data
[dcaegen2/services.git] / components / slice-analysis-ms / src / main / java / org / onap / slice / analysis / ms / service / MLMessageProcessor.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  slice-analysis-ms
4  *  ================================================================================
5  *   Copyright (C) 2020-2022 Wipro Limited.
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
22 package org.onap.slice.analysis.ms.service;
23
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Objects;
27
28 import org.onap.slice.analysis.ms.aai.AaiInterface;
29 import org.onap.slice.analysis.ms.configdb.IConfigDbService;
30 import org.onap.slice.analysis.ms.cps.CpsInterface;
31 import org.onap.slice.analysis.ms.models.CUModel;
32 import org.onap.slice.analysis.ms.models.Configuration;
33 import org.onap.slice.analysis.ms.models.MLOutputModel;
34 import org.onap.slice.analysis.ms.models.policy.AdditionalProperties;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.context.annotation.Scope;
39 import org.springframework.stereotype.Component;
40
41 /**
42  * Process the message sent by ML service and sends notification to policy
43  */
44 @Component
45 @Scope("prototype")
46 public class MLMessageProcessor {
47     private static Logger log = LoggerFactory.getLogger(MLMessageProcessor.class);
48
49     @Autowired
50     private IConfigDbService configDbService;
51
52     @Autowired
53     private PolicyService policyService;
54
55     @Autowired
56     private AaiInterface aaiInterface;
57
58     @Autowired
59     private CpsInterface cpsInterface;
60
61     /**
62      * Process the message sent by ML service and sends notification to policy
63      */
64     public void processMLMsg(MLOutputModel mlOutputMsg) {
65         Boolean isConfigDbEnabled = (Objects.isNull(Configuration.getInstance().getConfigDbEnabled())) ? true
66                 : Configuration.getInstance().getConfigDbEnabled();
67         Map<String, List<String>> ricToCellMapping = null;
68         Map<String, String> serviceDetails = null;
69         String snssai = mlOutputMsg.getSnssai();
70         List<CUModel> cuData = mlOutputMsg.getData();
71         if (isConfigDbEnabled) {
72             ricToCellMapping = configDbService.fetchRICsOfSnssai(snssai);
73         } else {
74             ricToCellMapping = cpsInterface.fetchRICsOfSnssai(snssai);
75         }
76         log.debug("RIC to cell mapping of S-NSSAI {} is {}", snssai, ricToCellMapping);
77         for (CUModel cuModel : cuData) {
78             String cellId = String.valueOf(cuModel.getCellCUList().get(0).getCellLocalId());
79             ricToCellMapping.forEach((ricId, cells) -> {
80                 if (cells.contains(cellId)) {
81                     cuModel.setNearRTRICId(ricId);
82                 }
83             });
84         }
85         AdditionalProperties<MLOutputModel> addProps = new AdditionalProperties<>();
86         addProps.setResourceConfig(mlOutputMsg);
87
88         if (isConfigDbEnabled) {
89             serviceDetails = configDbService.fetchServiceDetails(snssai);
90         } else {
91             serviceDetails = aaiInterface.fetchServiceDetails(snssai);
92
93         }
94         policyService.sendOnsetMessageToPolicy(snssai, addProps, serviceDetails);
95     }
96
97 }