2e56190f84877d6065207a782fb3a95b97dbd9cd
[dcaegen2/services.git] /
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  slice-analysis-ms
4  *  ================================================================================
5  *   Copyright (C) 2020 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 package org.onap.slice.analysis.ms.service;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28
29 import javax.annotation.PostConstruct;
30
31 import org.onap.slice.analysis.ms.configdb.IConfigDbService;
32 import org.onap.slice.analysis.ms.models.Configuration;
33 import org.onap.slice.analysis.ms.models.MeasurementObject;
34 import org.onap.slice.analysis.ms.models.SubCounter;
35 import org.onap.slice.analysis.ms.models.policy.AdditionalProperties;
36 import org.onap.slice.analysis.ms.utils.BeanUtil;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.context.annotation.Scope;
40 import org.springframework.stereotype.Component;
41
42 /** 
43  * This class process the measurement data of an S-NSSAI
44  */
45 @Component
46 @Scope("Prototype")
47 public class SnssaiSamplesProcessor {
48         private static Logger log = LoggerFactory.getLogger(SnssaiSamplesProcessor.class);
49
50         private PolicyService policyService;
51         private IConfigDbService configDbService;
52         private PmDataQueue pmDataQueue;
53         private AverageCalculator averageCalculator;
54         private List<MeasurementObject> snssaiMeasurementList = new ArrayList<>();
55         private Map<String, List<String>> ricToCellMapping = new HashMap<>();
56         private Map<String, Map<String, Integer>> ricToPrbsMapping = new HashMap<>();
57         private Map<String, Map<String, Integer>> ricToThroughputMapping = new HashMap<>();
58         private int samples;
59         private List<String> pmsToCompute;      
60         private Map<String, String> prbThroughputMapping = new HashMap<>(); 
61         private int minPercentageChange;
62         
63         @PostConstruct
64         public void init() {
65                 Configuration configuration = Configuration.getInstance();
66                 samples = configuration.getSamples();
67                 pmsToCompute = new ArrayList<>();
68                 pmsToCompute.add("PrbUsedDl");
69                 pmsToCompute.add("PrbUsedUl");
70                 prbThroughputMapping = new HashMap<>();
71                 prbThroughputMapping.put("PrbUsedDl", "dLThptPerSlice");
72                 prbThroughputMapping.put("PrbUsedUl", "uLThptPerSlice");
73                 minPercentageChange = configuration.getMinPercentageChange();
74                 policyService = BeanUtil.getBean(PolicyService.class);
75                 configDbService = BeanUtil.getBean(IConfigDbService.class);
76                 pmDataQueue = BeanUtil.getBean(PmDataQueue.class);
77                 averageCalculator = BeanUtil.getBean(AverageCalculator.class);
78         }
79
80         /**
81          * process the measurement data of an S-NSSAI
82          */
83         public void processSamplesOfSnnsai(String snssai, List<String> networkFunctions) {
84                 networkFunctions.forEach(nf -> {
85                         log.debug("Average of samples for {}:", snssai);
86                         addToMeasurementList(averageCalculator.findAverageOfSamples(pmDataQueue.getSamplesFromQueue(new SubCounter(nf, snssai), samples)));
87                 });             
88                 ricToCellMapping = configDbService.fetchRICsOfSnssai(snssai);   
89                 log.debug("RIC to Cell Mapping for {} S-NSSAI: {}", snssai, ricToCellMapping);
90                 Map<String, Map<String, Integer>> ricConfiguration = configDbService.fetchCurrentConfigurationOfRIC(snssai);
91                 Map<String, Integer> sliceConfiguration = configDbService.fetchCurrentConfigurationOfSlice(snssai);
92                 log.debug("RIC Configuration: {}", ricConfiguration);
93                 log.debug("Slice Configuration: {}", sliceConfiguration);
94                 pmsToCompute.forEach(pm -> {
95                         sumOfPrbsAcrossCells(pm);
96                         int sum = computeSum(pm);
97                         computeThroughput(sliceConfiguration, sum, pm);
98                         calculatePercentageChange(ricConfiguration, pm);
99                 });
100                 updateConfiguration();  
101                 if(ricToThroughputMapping.size() > 0) {
102                         AdditionalProperties<Map<String, Map<String, Integer>>> addProps = new AdditionalProperties<>();
103                         addProps.setResourceConfig(ricToThroughputMapping);
104                         policyService.sendOnsetMessageToPolicy(snssai, addProps, configDbService.fetchServiceDetails(snssai));
105                 }
106
107         }
108
109         /**
110          * process the measurement data of an S-NSSAI
111          */
112         protected void updateConfiguration() {
113                 Iterator<Map.Entry<String, Map<String,Integer>>> it = ricToThroughputMapping.entrySet().iterator();
114                 Map.Entry<String, Map<String,Integer>> entry = null;
115                 while(it.hasNext()) {
116                         entry = it.next();
117                         if(entry.getValue().size() == 0) {
118                                 it.remove();
119                         }
120                 }
121         }
122
123         private void addToMeasurementList(List<MeasurementObject> sample) {
124                 snssaiMeasurementList.addAll(sample);
125         }
126
127         /**
128          * Calculate the change in the configuration value and keep the configuration only if it is greater than a
129          * specific limit 
130          */
131         protected void calculatePercentageChange(Map<String, Map<String, Integer>> ricConfiguration, String pm) {
132                 Iterator<Map.Entry<String, Map<String,Integer>>> it = ricToThroughputMapping.entrySet().iterator();
133                 Map.Entry<String, Map<String,Integer>> entry = null;
134                 float existing = 0;
135                 float change = 0;
136                 while(it.hasNext()) {
137                         entry = it.next();
138                         existing = ricConfiguration.get(entry.getKey()).get(pm);
139                         change = ((Math.abs(entry.getValue().get(pm) - existing))/existing)*100;
140                         if (change <= minPercentageChange) {
141                                 ricToThroughputMapping.get(entry.getKey()).remove(pm);
142                         }
143                 }
144         }
145         
146         protected void sumOfPrbsAcrossCells(String pmName) {
147                 ricToCellMapping.forEach((ric,cells) -> {
148                         int sumOfPrbs = 0;
149                         for(String cell : cells) {
150                                 int index = snssaiMeasurementList.indexOf(new MeasurementObject(cell));
151                                 sumOfPrbs += snssaiMeasurementList.get(index).getPmData().get(pmName);
152                         }
153                         if(ricToPrbsMapping.containsKey(ric)) {
154                                 ricToPrbsMapping.get(ric).put(pmName, sumOfPrbs);
155                         }
156                         else {
157                                 Map<String, Integer> pmToPrbMapping  = new HashMap<>();
158                                 pmToPrbMapping.put(pmName, sumOfPrbs);
159                                 ricToPrbsMapping.put(ric, pmToPrbMapping);
160                         }
161                 });
162         }
163
164         protected Integer computeSum(String pm) {
165                 return ricToPrbsMapping.entrySet().stream().map(x -> x.getValue().get(pm)).reduce(0, Integer::sum);
166         }
167
168         protected void computeThroughput(Map<String, Integer> sliceConfiguration, int sum, String pm) {
169                 Iterator<Map.Entry<String, Map<String,Integer>>> it = ricToPrbsMapping.entrySet().iterator();
170                 Map.Entry<String, Map<String,Integer>> entry = null;
171                 Map<String, Integer> throughtputMap = null;
172                 String ric = "";
173                 int value = 0;
174                 while(it.hasNext()) {
175                         entry = it.next();
176                         ric = entry.getKey();
177                         value = Math.round(((float)entry.getValue().get(pm)/sum)*(float)sliceConfiguration.get(prbThroughputMapping.get(pm)));
178                         if(ricToThroughputMapping.containsKey(ric)) {
179                                 ricToThroughputMapping.get(ric).put(prbThroughputMapping.get(pm), value);
180                         }
181                         else {
182                                 throughtputMap = new HashMap<>();
183                                 throughtputMap.put(prbThroughputMapping.get(pm), value);
184                                 ricToThroughputMapping.put(ric, throughtputMap);
185                         }
186                 }
187
188         }
189
190 }