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