1 /*******************************************************************************
 
   2  *  ============LICENSE_START=======================================================
 
   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
 
  11  *          http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  20  *******************************************************************************/
 
  21 package org.onap.slice.analysis.ms.service;
 
  23 import java.util.ArrayList;
 
  24 import java.util.HashMap;
 
  25 import java.util.Iterator;
 
  26 import java.util.List;
 
  29 import javax.annotation.PostConstruct;
 
  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;
 
  43  * This class process the measurement data of an S-NSSAI
 
  47 public class SnssaiSamplesProcessor {
 
  48         private static Logger log = LoggerFactory.getLogger(SnssaiSamplesProcessor.class);
 
  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<>();
 
  59         private List<String> pmsToCompute;      
 
  60         private Map<String, String> prbThroughputMapping = new HashMap<>(); 
 
  61         private int minPercentageChange;
 
  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);
 
  81          * process the measurement data of an S-NSSAI
 
  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)));
 
  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);
 
 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));
 
 110          * process the measurement data of an S-NSSAI
 
 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()) {
 
 117                         if(entry.getValue().size() == 0) {
 
 123         private void addToMeasurementList(List<MeasurementObject> sample) {
 
 124                 snssaiMeasurementList.addAll(sample);
 
 128          * Calculate the change in the configuration value and keep the configuration only if it is greater than a
 
 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;
 
 136                 while(it.hasNext()) {
 
 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);
 
 146         protected void sumOfPrbsAcrossCells(String pmName) {
 
 147                 ricToCellMapping.forEach((ric,cells) -> {
 
 149                         for(String cell : cells) {
 
 150                                 int index = snssaiMeasurementList.indexOf(new MeasurementObject(cell));
 
 151                                 sumOfPrbs += snssaiMeasurementList.get(index).getPmData().get(pmName);
 
 153                         if(ricToPrbsMapping.containsKey(ric)) {
 
 154                                 ricToPrbsMapping.get(ric).put(pmName, sumOfPrbs);
 
 157                                 Map<String, Integer> pmToPrbMapping  = new HashMap<>();
 
 158                                 pmToPrbMapping.put(pmName, sumOfPrbs);
 
 159                                 ricToPrbsMapping.put(ric, pmToPrbMapping);
 
 164         protected Integer computeSum(String pm) {
 
 165                 return ricToPrbsMapping.entrySet().stream().map(x -> x.getValue().get(pm)).reduce(0, Integer::sum);
 
 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;
 
 174                 while(it.hasNext()) {
 
 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);
 
 182                                 throughtputMap = new HashMap<>();
 
 183                                 throughtputMap.put(prbThroughputMapping.get(pm), value);
 
 184                                 ricToThroughputMapping.put(ric, throughtputMap);