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.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;
 
  43  * This class process the measurement data of an S-NSSAI
 
  47 public class SnssaiSamplesProcessor {
 
  48         private static Logger log = LoggerFactory.getLogger(SnssaiSamplesProcessor.class);
 
  51         private PolicyService policyService;
 
  54         private IConfigDbService configDbService;
 
  57         private PmDataQueue pmDataQueue;
 
  60         private AverageCalculator averageCalculator;
 
  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<>();
 
  67         private List<String> pmsToCompute;      
 
  68         private Map<String, String> prbThroughputMapping = new HashMap<>(); 
 
  69         private int minPercentageChange;
 
  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();
 
  85          * process the measurement data of an S-NSSAI
 
  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)));
 
  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);
 
 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));
 
 114          * process the measurement data of an S-NSSAI
 
 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()) {
 
 121                         if(entry.getValue().size() == 0) {
 
 127         private void addToMeasurementList(List<MeasurementObject> sample) {
 
 128                 snssaiMeasurementList.addAll(sample);
 
 132          * Calculate the change in the configuration value and keep the configuration only if it is greater than a
 
 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;
 
 140                 while(it.hasNext()) {
 
 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);
 
 150         protected void sumOfPrbsAcrossCells(String pmName) {
 
 151                 ricToCellMapping.forEach((ric,cells) -> {
 
 153                         for(String cell : cells) {
 
 154                                 int index = snssaiMeasurementList.indexOf(new MeasurementObject(cell));
 
 155                                 sumOfPrbs += snssaiMeasurementList.get(index).getPmData().get(pmName);
 
 157                         if(ricToPrbsMapping.containsKey(ric)) {
 
 158                                 ricToPrbsMapping.get(ric).put(pmName, sumOfPrbs);
 
 161                                 Map<String, Integer> pmToPrbMapping  = new HashMap<>();
 
 162                                 pmToPrbMapping.put(pmName, sumOfPrbs);
 
 163                                 ricToPrbsMapping.put(ric, pmToPrbMapping);
 
 168         protected Integer computeSum(String pm) {
 
 169                 return ricToPrbsMapping.entrySet().stream().map(x -> x.getValue().get(pm)).reduce(0, Integer::sum);
 
 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;
 
 178                 while(it.hasNext()) {
 
 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);
 
 186                                 throughtputMap = new HashMap<>();
 
 187                                 throughtputMap.put(prbThroughputMapping.get(pm), value);
 
 188                                 ricToThroughputMapping.put(ric, throughtputMap);