[DCAEGEN2] Switch CBS client library to 1.8.7 and fix null pointer exception while... 30/126330/2
authorNiranjana <niranjana.y60@wipro.com>
Fri, 17 Dec 2021 14:46:22 +0000 (14:46 +0000)
committerNiranjana Y <niranjana.y60@wipro.com>
Fri, 17 Dec 2021 16:04:08 +0000 (16:04 +0000)
Issue-ID: DCAEGEN2-2966
Issue-ID: DCAEGEN2-3025
Signed-off-by: Niranjana <niranjana.y60@wipro.com>
Change-Id: I2fb459d461676243e2648be129e71ee51af30d0d

components/slice-analysis-ms/ChangeLog.md
components/slice-analysis-ms/pom.xml
components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/configdb/ConfigDbInterfaceService.java
components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/controller/ConfigFetchFromCbs.java
components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/SnssaiSamplesProcessor.java
components/slice-analysis-ms/src/test/java/org/onap/slice/analysis/ms/configdb/ConfigDbInterfaceServiceTest.java

index e09d10b..8d8a60d 100644 (file)
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
 ## [1.0.7] - 2021/12/16
          - [DCAEGEN2-2963](https://jira.onap.org/browse/DCAEGEN2-2963) - Use onap/integration-java11 image
 
+         - [DCAEGEN2-2966](https://jira.onap.org/browse/DCAEGEN2-2966) - Switch CBS client library to 1.8.7
+
+         - [DCAEGEN2-3025](https://jira.onap.org/browse/DCAEGEN2-3025) - Fix null pointer exception while fetching slice-config
+
 ## [1.0.6] - 2021/08/28
          - [DCAEGEN2-2885](https://jira.onap.org/browse/DCAEGEN2-2885) - DCAE SliceAnalysis MS - CPS Integration
 
index 5fede6f..faf6032 100644 (file)
@@ -39,7 +39,7 @@
         <packaging>jar</packaging>
         <properties>
                 <java.version>11</java.version>
-                <sdk.version>1.1.4</sdk.version>
+                <sdk.version>1.8.7</sdk.version>
                 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                 <maven.compiler.source>11</maven.compiler.source>
                 <maven.compiler.target>11</maven.compiler.target>
index 5798a40..50a60ac 100644 (file)
@@ -2,7 +2,7 @@
  *  ============LICENSE_START=======================================================
  *  slice-analysis-ms
  *  ================================================================================
- *   Copyright (C) 2020 Wipro Limited.
+ *   Copyright (C) 2020-2021 Wipro Limited.
  *   ==============================================================================
  *     Licensed under the Apache License, Version 2.0 (the "License");
  *     you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 import org.onap.slice.analysis.ms.models.Configuration;
 import org.onap.slice.analysis.ms.models.configdb.CellsModel;
@@ -35,82 +36,97 @@ import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 
 /**
- * 
+ *
  *  Service for config db interfaces
  *
  */
 @Service
 public class ConfigDbInterfaceService implements IConfigDbService {
 
-       @Autowired
-       private ConfigDbRestClient restclient;
-       private String configDbBaseUrl = Configuration.getInstance().getConfigDbService();
+        @Autowired
+        private ConfigDbRestClient restclient;
+        private String configDbBaseUrl = Configuration.getInstance().getConfigDbService();
+
+        /**
+         *  Fetches the current configuration of an S-NSSAI from config DB
+         */
+        public Map<String, Integer> fetchCurrentConfigurationOfSlice(String snssai){
+                Map<String,Integer> responseMap = null;
+                String reqUrl=configDbBaseUrl+"/api/sdnc-config-db/v4/profile-config/"+snssai;
 
-       /**
-        *  Fetches the current configuration of an S-NSSAI from config DB
-        */
-       public Map<String, Integer> fetchCurrentConfigurationOfSlice(String snssai){
-               Map<String,Integer> responseMap = null;
-               String reqUrl=configDbBaseUrl+"/api/sdnc-config-db/v4/profile-config/"+snssai;
+                ResponseEntity<Map<String,Integer>> response=restclient.sendGetRequest(reqUrl,new ParameterizedTypeReference<Map<String, Integer>>() {
+                });
+                responseMap=response.getBody();
+                return responseMap;
+        }
 
-               ResponseEntity<Map<String,Integer>> response=restclient.sendGetRequest(reqUrl,new ParameterizedTypeReference<Map<String, Integer>>() {
-               });
-               responseMap=response.getBody();
-               return responseMap;                     
-       }
+        /**
+         *  Fetches the current configuration of RIC from config DB
+         */
+        public Map<String, Map<String, Object>> fetchCurrentConfigurationOfRIC(String snssai) {
+                String reqUrl = configDbBaseUrl + "/api/sdnc-config-db/v4/slice-config/" + snssai;
+                Map<String, Map<String, Object>> responseMap = new HashMap<String, Map<String, Object>>();
+                ResponseEntity<Map<String, List<Map<String, Object>>>> response = restclient.sendGetRequest(reqUrl,
+                                new ParameterizedTypeReference<Map<String, List<Map<String, Object>>>>() {
+                                });
+                if (Objects.nonNull(response)) {
+                        for (Map.Entry<String, List<Map<String, Object>>> entry : response.getBody().entrySet()) {
+                                List<Map<String, Object>> list = entry.getValue();
+                                if (!list.isEmpty()) {
+                                        list.forEach(l -> {
+                                                if (l.containsKey("nearRTRICId")) {
+                                                        responseMap.put(String.valueOf(l.get("nearRTRICId")), l);
+                                                }
+                                        });
+                                }
 
-       /**
-        *  Fetches the current configuration of RIC from config DB
-        */
-       public Map<String,Map<String,Object>> fetchCurrentConfigurationOfRIC(String snssai){
-               String reqUrl=configDbBaseUrl+"/api/sdnc-config-db/v4/slice-config/"+snssai;
-               ResponseEntity<Map<String,Map<String,Object>>> response=restclient.sendGetRequest(reqUrl, new ParameterizedTypeReference<Map<String,Map<String,Object>>>() {
-               });
-               return response.getBody();
-       }
+                        }
+                }
+                return responseMap;
+        }
 
-       /**
-        *  Fetches all the network functions of an S-NSSAI from config DB
-        */
-       public List<String> fetchNetworkFunctionsOfSnssai(String snssai){
-               List<String> responseList=new ArrayList<>();
-               String reqUrl=configDbBaseUrl+"/api/sdnc-config-db/v4/du-list/"+snssai;
-               ResponseEntity<List<NetworkFunctionModel>> response=restclient.sendGetRequest(reqUrl, new ParameterizedTypeReference<List<NetworkFunctionModel>>() {
-               });
-               for(NetworkFunctionModel networkFn:response.getBody()) {
-                       responseList.add(networkFn.getgNBDUId());       
-               }
-               return responseList;
-       }
+        /**
+         *  Fetches all the network functions of an S-NSSAI from config DB
+         */
+        public List<String> fetchNetworkFunctionsOfSnssai(String snssai){
+                List<String> responseList=new ArrayList<>();
+                String reqUrl=configDbBaseUrl+"/api/sdnc-config-db/v4/du-list/"+snssai;
+                ResponseEntity<List<NetworkFunctionModel>> response=restclient.sendGetRequest(reqUrl, new ParameterizedTypeReference<List<NetworkFunctionModel>>() {
+                });
+                for(NetworkFunctionModel networkFn:response.getBody()) {
+                        responseList.add(networkFn.getgNBDUId());
+                }
+                return responseList;
+        }
 
-       /**
-        *  Fetches the RICS of an S-NSSAI from config DB
-        */
-       public Map<String, List<String>> fetchRICsOfSnssai(String snssai){
-               Map<String,List<String>> responseMap=new HashMap<>();
-               String reqUrl=configDbBaseUrl+"/api/sdnc-config-db/v4/du-cell-list/"+snssai;
-               ResponseEntity<Map<String,List<CellsModel>>> response = restclient.sendGetRequest(reqUrl, new ParameterizedTypeReference<Map<String,List<CellsModel>>>() {
-               });
+        /**
+         *  Fetches the RICS of an S-NSSAI from config DB
+         */
+        public Map<String, List<String>> fetchRICsOfSnssai(String snssai){
+                Map<String,List<String>> responseMap=new HashMap<>();
+                String reqUrl=configDbBaseUrl+"/api/sdnc-config-db/v4/du-cell-list/"+snssai;
+                ResponseEntity<Map<String,List<CellsModel>>> response = restclient.sendGetRequest(reqUrl, new ParameterizedTypeReference<Map<String,List<CellsModel>>>() {
+                });
 
-               for (Map.Entry<String, List<CellsModel>> entry : response.getBody().entrySet()) {
-                       List<String> cellslist=new ArrayList<>();
-                       for(CellsModel cellmodel:entry.getValue()) {
-                               cellslist.add(cellmodel.getCellLocalId());
-                       }
-                       responseMap.put(entry.getKey(), cellslist);
-               }
-               return responseMap;
-       }
+                for (Map.Entry<String, List<CellsModel>> entry : response.getBody().entrySet()) {
+                        List<String> cellslist=new ArrayList<>();
+                        for(CellsModel cellmodel:entry.getValue()) {
+                                cellslist.add(cellmodel.getCellLocalId());
+                        }
+                        responseMap.put(entry.getKey(), cellslist);
+                }
+                return responseMap;
+        }
 
-       /**
-        *  Fetches the details of a service 
-        */
-       public Map<String,String> fetchServiceDetails(String snssai){
-               String reqUrl=configDbBaseUrl+"/api/sdnc-config-db/v4/subscriber-details/"+snssai;
-               ResponseEntity<Map<String,String>> response=restclient.sendGetRequest(reqUrl, new ParameterizedTypeReference<Map<String,String>>() {
-               });
-               return response.getBody();
-       }       
+        /**
+         *  Fetches the details of a service
+         */
+        public Map<String,String> fetchServiceDetails(String snssai){
+                String reqUrl=configDbBaseUrl+"/api/sdnc-config-db/v4/subscriber-details/"+snssai;
+                ResponseEntity<Map<String,String>> response=restclient.sendGetRequest(reqUrl, new ParameterizedTypeReference<Map<String,String>>() {
+                });
+                return response.getBody();
+        }
 
 }
 
index 01fa91d..fbb47e3 100644 (file)
@@ -2,7 +2,7 @@
  *  ============LICENSE_START=======================================================
  *  slice-analysis-ms
  *  ================================================================================
- *   Copyright (C) 2020 Wipro Limited.
+ *   Copyright (C) 2020-2021 Wipro Limited.
  *   ==============================================================================
  *     Licensed under the Apache License, Version 2.0 (the "License");
  *     you may not use this file except in compliance with the License.
@@ -32,7 +32,8 @@ import java.util.Map;
 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory;
 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests;
 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest;
-import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration;
+import org.onap.dcaegen2.services.sdk.rest.services.model.logging.ImmutableRequestDiagnosticContext;
 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
 import org.onap.slice.analysis.ms.models.ConfigPolicy;
 import org.onap.slice.analysis.ms.models.Configuration;
@@ -69,8 +70,9 @@ public class ConfigFetchFromCbs implements Runnable {
         log.info("getAppconfig start ..");
         RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
         // Read necessary properties from the environment
-        final EnvProperties env = EnvProperties.fromEnvironment();
-        log.debug("environments {}", env);
+        final CbsClientConfiguration cbsClientConfiguration = CbsClientConfiguration.fromEnvironment();
+
+        log.debug("environments {}", cbsClientConfiguration);
         ConfigPolicy configPolicy = ConfigPolicy.getInstance();
 
         // Polling properties
@@ -79,7 +81,7 @@ public class ConfigFetchFromCbs implements Runnable {
 
         // Create the client and use it to get the configuration
         final CbsRequest request = CbsRequests.getAll(diagnosticContext);
-        return CbsClientFactory.createCbsClient(env)
+        return CbsClientFactory.createCbsClient(cbsClientConfiguration)
                 .flatMapMany(cbsClient -> cbsClient.updates(request, initialDelay, period)).subscribe(jsonObject -> {
                     log.info("configuration and policy from CBS {}", jsonObject);
                     JsonObject config = jsonObject.getAsJsonObject("config");
index ccb34ed..d802f81 100644 (file)
@@ -48,181 +48,201 @@ import org.springframework.stereotype.Component;
 @Component
 @Scope("prototype")
 public class SnssaiSamplesProcessor {
-       private static Logger log = LoggerFactory.getLogger(SnssaiSamplesProcessor.class);
-
-       @Autowired
-       private PolicyService policyService;
-
-       @Autowired
-       private IConfigDbService configDbService;
-
-       @Autowired
-       private PmDataQueue pmDataQueue;
-
-       @Autowired
-       private AverageCalculator averageCalculator;
-
-       @Autowired
-       private AaiInterface aaiInterface;
-
-       @Autowired
-       private CpsInterface cpsInterface;
-
-       private List<MeasurementObject> snssaiMeasurementList = new ArrayList<>();
-       private Map<String, List<String>> ricToCellMapping = new HashMap<>();
-       private Map<String, Map<String, Integer>> ricToPrbsMapping = new HashMap<>();
-       private Map<String, Map<String, Integer>> ricToThroughputMapping = new HashMap<>();
-       private int noOfSamples;
-       private List<String> pmsToCompute;
-       private Map<String, String> prbThroughputMapping = new HashMap<>();
-       private int minPercentageChange;
-
-       @PostConstruct
-       public void init() {
-               Configuration configuration = Configuration.getInstance();
-               noOfSamples = configuration.getSamples();
-               pmsToCompute = new ArrayList<>();
-               pmsToCompute.add("PrbUsedDl");
-               pmsToCompute.add("PrbUsedUl");
-               prbThroughputMapping = new HashMap<>();
-               prbThroughputMapping.put("PrbUsedDl", "dLThptPerSlice");
-               prbThroughputMapping.put("PrbUsedUl", "uLThptPerSlice");
-               minPercentageChange = configuration.getMinPercentageChange();
-       }
-
-       /**
-        * process the measurement data of an S-NSSAI
-        */
-       public boolean processSamplesOfSnnsai(String snssai, List<String> networkFunctions) {
-               Boolean isConfigDbEnabled = (Objects.isNull(Configuration.getInstance().getConfigDbEnabled())) ? true
-                               : Configuration.getInstance().getConfigDbEnabled();
-               List<MeasurementObject> sample = null;
-               List<List<MeasurementObject>> samples = null;
-               Map<String, String> serviceDetails =null;
-               log.info("Network Functions {} of snssai {}", networkFunctions, snssai);
-               for (String nf : networkFunctions) {
-                       log.debug("Average of samples for {}:", snssai);
-                       samples = pmDataQueue.getSamplesFromQueue(new SubCounter(nf, snssai), noOfSamples);
-                       if (samples != null) {
-                               sample = averageCalculator.findAverageOfSamples(samples);
-                               addToMeasurementList(sample);
-                       } else {
-                               log.info("Not enough samples present for nf {}", nf);
-                               return false;
-                       }
-               }
-               log.info("snssai measurement list {}", snssaiMeasurementList);
-               Map<String, Map<String, Object>> ricConfiguration;
-               Map<String, Integer> sliceConfiguration;
-               if (isConfigDbEnabled) {
-                       ricToCellMapping = configDbService.fetchRICsOfSnssai(snssai);
-                       ricConfiguration = configDbService.fetchCurrentConfigurationOfRIC(snssai);
-                       sliceConfiguration = configDbService.fetchCurrentConfigurationOfSlice(snssai);
-                       serviceDetails = configDbService.fetchServiceDetails(snssai);
-               } else {
-                       ricToCellMapping = cpsInterface.fetchRICsOfSnssai(snssai);
-                       ricConfiguration = cpsInterface.fetchCurrentConfigurationOfRIC(snssai);
-                       sliceConfiguration = aaiInterface.fetchCurrentConfigurationOfSlice(snssai);
-                       serviceDetails = aaiInterface.fetchServiceDetails(snssai);
-               }
-               log.info("RIC to Cell Mapping for {} S-NSSAI: {}", snssai, ricToCellMapping);
-               log.info("RIC Configuration {} and Slice Configuration {}", ricConfiguration, sliceConfiguration);
-               pmsToCompute.forEach(pm -> {
-                       log.debug("processing for pm {}", pm);
-                       sumOfPrbsAcrossCells(pm);
-                       int sum = computeSum(pm);
-                       computeThroughput(sliceConfiguration, sum, pm);
-                       calculatePercentageChange(ricConfiguration, prbThroughputMapping.get(pm));
-               });
-               updateConfiguration();
-               if (ricToThroughputMapping.size() > 0) {
-                       AdditionalProperties<Map<String, Map<String, Integer>>> addProps = new AdditionalProperties<>();
-                       addProps.setResourceConfig(ricToThroughputMapping);
-                       policyService.sendOnsetMessageToPolicy(snssai, addProps, serviceDetails);
-               }
-               return true;
-       }
-
-       /**
-        * process the measurement data of an S-NSSAI
-        */
-       protected void updateConfiguration() {
-               Iterator<Map.Entry<String, Map<String, Integer>>> it = ricToThroughputMapping.entrySet().iterator();
-               Map.Entry<String, Map<String, Integer>> entry = null;
-               while (it.hasNext()) {
-                       entry = it.next();
-                       if (entry.getValue().size() == 0) {
-                               it.remove();
-                       }
-               }
-       }
-
-       private void addToMeasurementList(List<MeasurementObject> sample) {
-               snssaiMeasurementList.addAll(sample);
-       }
-
-       /**
-        * Calculate the change in the configuration value and keep the configuration
-        * only if it is greater than a specific limit
-        */
-       protected void calculatePercentageChange(Map<String, Map<String, Object>> ricConfiguration, String pm) {
-               Iterator<Map.Entry<String, Map<String, Integer>>> it = ricToThroughputMapping.entrySet().iterator();
-               Map.Entry<String, Map<String, Integer>> entry = null;
-               float existing = 0;
-               float change = 0;
-               while (it.hasNext()) {
-                       entry = it.next();
-                       existing = (float) ((int) ricConfiguration.get(entry.getKey()).get(pm));
-                       change = ((Math.abs(entry.getValue().get(pm) - existing)) / existing) * 100;
-                       if (change <= minPercentageChange) {
-                               ricToThroughputMapping.get(entry.getKey()).remove(pm);
-                               log.info("Removing pm data {} for RIC {}", pm, entry.getKey());
-                       }
-               }
-       }
-
-       protected void sumOfPrbsAcrossCells(String pmName) {
-               ricToCellMapping.forEach((ric, cells) -> {
-                       int sumOfPrbs = 0;
-                       for (String cell : cells) {
-                               int index = MeasurementObject.findIndex(cell, snssaiMeasurementList);
-                               sumOfPrbs += snssaiMeasurementList.get(index).getPmData().get(pmName);
-                       }
-                       if (ricToPrbsMapping.containsKey(ric)) {
-                               ricToPrbsMapping.get(ric).put(pmName, sumOfPrbs);
-                       } else {
-                               Map<String, Integer> pmToPrbMapping = new HashMap<>();
-                               pmToPrbMapping.put(pmName, sumOfPrbs);
-                               ricToPrbsMapping.put(ric, pmToPrbMapping);
-                       }
-               });
-               log.info("PRBs sum computed for RIC {}", ricToPrbsMapping);
-       }
-
-       protected Integer computeSum(String pm) {
-               return ricToPrbsMapping.entrySet().stream().map(x -> x.getValue().get(pm)).reduce(0, Integer::sum);
-       }
-
-       protected void computeThroughput(Map<String, Integer> sliceConfiguration, int sum, String pm) {
-               Iterator<Map.Entry<String, Map<String, Integer>>> it = ricToPrbsMapping.entrySet().iterator();
-               Map.Entry<String, Map<String, Integer>> entry = null;
-               Map<String, Integer> throughtputMap = null;
-               String ric = "";
-               int value = 0;
-               while (it.hasNext()) {
-                       entry = it.next();
-                       ric = entry.getKey();
-                       value = Math.round(((float) entry.getValue().get(pm) / sum)
-                                       * (float) sliceConfiguration.get(prbThroughputMapping.get(pm)));
-                       if (ricToThroughputMapping.containsKey(ric)) {
-                               ricToThroughputMapping.get(ric).put(prbThroughputMapping.get(pm), value);
-                       } else {
-                               throughtputMap = new HashMap<>();
-                               throughtputMap.put(prbThroughputMapping.get(pm), value);
-                               ricToThroughputMapping.put(ric, throughtputMap);
-                       }
-               }
-               log.info("Throughput computed for RIC {}", ricToThroughputMapping);
-       }
+        private static Logger log = LoggerFactory.getLogger(SnssaiSamplesProcessor.class);
+
+        @Autowired
+        private PolicyService policyService;
+
+        @Autowired
+        private IConfigDbService configDbService;
+
+        @Autowired
+        private PmDataQueue pmDataQueue;
+
+        @Autowired
+        private AverageCalculator averageCalculator;
+
+        @Autowired
+        private AaiInterface aaiInterface;
+
+        @Autowired
+        private CpsInterface cpsInterface;
+
+        private List<MeasurementObject> snssaiMeasurementList = new ArrayList<>();
+        private Map<String, List<String>> ricToCellMapping = new HashMap<>();
+        private Map<String, Map<String, Integer>> ricToPrbsMapping = new HashMap<>();
+        private Map<String, Map<String, Integer>> ricToThroughputMapping = new HashMap<>();
+        private int noOfSamples;
+        private List<String> pmsToCompute;
+        private Map<String, String> prbThroughputMapping = new HashMap<>();
+        private int minPercentageChange;
+
+        @PostConstruct
+        public void init() {
+                Configuration configuration = Configuration.getInstance();
+                noOfSamples = configuration.getSamples();
+                pmsToCompute = new ArrayList<>();
+                pmsToCompute.add("PrbUsedDl");
+                pmsToCompute.add("PrbUsedUl");
+                prbThroughputMapping = new HashMap<>();
+                prbThroughputMapping.put("PrbUsedDl", "dLThptPerSlice");
+                prbThroughputMapping.put("PrbUsedUl", "uLThptPerSlice");
+                minPercentageChange = configuration.getMinPercentageChange();
+        }
+
+        /**
+         * process the measurement data of an S-NSSAI
+         */
+        public boolean processSamplesOfSnnsai(String snssai, List<String> networkFunctions) {
+                Boolean isConfigDbEnabled = (Objects.isNull(Configuration.getInstance().getConfigDbEnabled())) ? true
+                                : Configuration.getInstance().getConfigDbEnabled();
+                List<MeasurementObject> sample = null;
+                List<List<MeasurementObject>> samples = null;
+                Map<String, String> serviceDetails =null;
+                log.info("Network Functions {} of snssai {}", networkFunctions, snssai);
+                for (String nf : networkFunctions) {
+                        log.debug("Average of samples for {}:", snssai);
+                        samples = pmDataQueue.getSamplesFromQueue(new SubCounter(nf, snssai), noOfSamples);
+                        if (samples != null) {
+                                sample = averageCalculator.findAverageOfSamples(samples);
+                                addToMeasurementList(sample);
+                        } else {
+                                log.info("Not enough samples present for nf {}", nf);
+                                return false;
+                        }
+                }
+                log.info("snssai measurement list {}", snssaiMeasurementList);
+                Map<String, Map<String, Object>> ricConfiguration;
+                Map<String, Integer> sliceConfiguration;
+                if (isConfigDbEnabled) {
+                        ricToCellMapping = configDbService.fetchRICsOfSnssai(snssai);
+                        ricConfiguration = configDbService.fetchCurrentConfigurationOfRIC(snssai);
+                        sliceConfiguration = configDbService.fetchCurrentConfigurationOfSlice(snssai);
+                        serviceDetails = configDbService.fetchServiceDetails(snssai);
+                } else {
+                        ricToCellMapping = cpsInterface.fetchRICsOfSnssai(snssai);
+                        ricConfiguration = cpsInterface.fetchCurrentConfigurationOfRIC(snssai);
+                        serviceDetails = aaiInterface.fetchServiceDetails(snssai);
+                        sliceConfiguration = aaiInterface.fetchCurrentConfigurationOfSlice(snssai);
+                }
+                log.info("RIC to Cell Mapping for {} S-NSSAI: {}", snssai, ricToCellMapping);
+                log.info("RIC Configuration {} and Slice Configuration {}", ricConfiguration, sliceConfiguration);
+                pmsToCompute.forEach(pm -> {
+                        log.debug("processing for pm {}", pm);
+                        sumOfPrbsAcrossCells(pm);
+                        int sum = computeSum(pm);
+                        computeThroughput(sliceConfiguration, sum, pm);
+                        calculatePercentageChange(ricConfiguration, prbThroughputMapping.get(pm));
+                });
+                updateConfiguration();
+                if (ricToThroughputMapping.size() > 0) {
+                        AdditionalProperties<Map<String,  List<Map<String, Integer>>>> addProps = new AdditionalProperties<>();
+                        addProps.setResourceConfig(getChangedRIConfigFormat(ricToThroughputMapping));
+                        policyService.sendOnsetMessageToPolicy(snssai, addProps, serviceDetails);
+                }
+                return true;
+        }
+
+        /**
+         * change the RICConfig data format to be compatible with SDN-R
+         */
+        protected Map<String, List<Map<String, Integer>>> getChangedRIConfigFormat(
+                        Map<String, Map<String, Integer>> ricToThroughputMapping) {
+                Iterator<Map.Entry<String, Map<String, Integer>>> it = ricToThroughputMapping.entrySet().iterator();
+                Map.Entry<String, Map<String, Integer>> entry = null;
+                List<Map<String, Integer>> ricConfigList = new ArrayList<>();
+                Map<String, List<Map<String, Integer>>> ricConfigData = new HashMap<>();
+                while (it.hasNext()) {
+                        Map<String, Integer> newConfigMap = new HashMap<>();
+                        entry = it.next();
+                        newConfigMap = entry.getValue();
+                        newConfigMap.put("nearRTRICId", Integer.parseInt(entry.getKey()));
+                        ricConfigList.add(newConfigMap);
+                }
+                ricConfigData.put("data", ricConfigList);
+                return ricConfigData;
+        }
+
+        /**
+         * process the measurement data of an S-NSSAI
+         */
+        protected void updateConfiguration() {
+                Iterator<Map.Entry<String, Map<String, Integer>>> it = ricToThroughputMapping.entrySet().iterator();
+                Map.Entry<String, Map<String, Integer>> entry = null;
+                while (it.hasNext()) {
+                        entry = it.next();
+                        if (entry.getValue().size() == 0) {
+                                it.remove();
+                        }
+                }
+        }
+
+        private void addToMeasurementList(List<MeasurementObject> sample) {
+                snssaiMeasurementList.addAll(sample);
+        }
+
+        /**
+         * Calculate the change in the configuration value and keep the configuration
+         * only if it is greater than a specific limit
+         */
+         protected void calculatePercentageChange(Map<String, Map<String, Object>> ricConfiguration, String pm) {
+                Iterator<Map.Entry<String, Map<String, Integer>>> it = ricToThroughputMapping.entrySet().iterator();
+                Map.Entry<String, Map<String, Integer>> entry = null;
+                float existing = 0;
+                float change = 0;
+                while (it.hasNext()) {
+                        entry = it.next();
+                        existing = (float) ((int) ricConfiguration.get(entry.getKey()).get(pm));
+                        change = ((Math.abs(entry.getValue().get(pm) - existing)) / existing) * 100;
+                        if (change <= minPercentageChange) {
+                                ricToThroughputMapping.get(entry.getKey()).remove(pm);
+                                log.info("Removing pm data {} for RIC {}", pm, entry.getKey());
+                        }
+                }
+        }
+
+        protected void sumOfPrbsAcrossCells(String pmName) {
+                ricToCellMapping.forEach((ric, cells) -> {
+                        int sumOfPrbs = 0;
+                        for (String cell : cells) {
+                                int index = MeasurementObject.findIndex(cell, snssaiMeasurementList);
+                                sumOfPrbs += snssaiMeasurementList.get(index).getPmData().get(pmName);
+                        }
+                        if (ricToPrbsMapping.containsKey(ric)) {
+                                ricToPrbsMapping.get(ric).put(pmName, sumOfPrbs);
+                        } else {
+                                Map<String, Integer> pmToPrbMapping = new HashMap<>();
+                                pmToPrbMapping.put(pmName, sumOfPrbs);
+                                ricToPrbsMapping.put(ric, pmToPrbMapping);
+                        }
+                });
+                log.info("PRBs sum computed for RIC {}", ricToPrbsMapping);
+        }
+
+        protected Integer computeSum(String pm) {
+                return ricToPrbsMapping.entrySet().stream().map(x -> x.getValue().get(pm)).reduce(0, Integer::sum);
+        }
+
+        protected void computeThroughput(Map<String, Integer> sliceConfiguration, int sum, String pm) {
+                Iterator<Map.Entry<String, Map<String, Integer>>> it = ricToPrbsMapping.entrySet().iterator();
+                Map.Entry<String, Map<String, Integer>> entry = null;
+                Map<String, Integer> throughtputMap = null;
+                String ric = "";
+                int value = 0;
+                while (it.hasNext()) {
+                        entry = it.next();
+                        ric = entry.getKey();
+                        value = Math.round(((float) entry.getValue().get(pm) / sum)
+                                        * (float) sliceConfiguration.get(prbThroughputMapping.get(pm)));
+                        if (ricToThroughputMapping.containsKey(ric)) {
+                                ricToThroughputMapping.get(ric).put(prbThroughputMapping.get(pm), value);
+                        } else {
+                                throughtputMap = new HashMap<>();
+                                throughtputMap.put(prbThroughputMapping.get(pm), value);
+                                ricToThroughputMapping.put(ric, throughtputMap);
+                        }
+                }
+                log.info("Throughput computed for RIC {}", ricToThroughputMapping);
+        }
 
 }
index 3cb0a3b..885a917 100644 (file)
@@ -41,89 +41,100 @@ import org.springframework.http.ResponseEntity;
 @RunWith(org.mockito.junit.MockitoJUnitRunner.class)
 public class ConfigDbInterfaceServiceTest {
 
-       @InjectMocks
-       ConfigDbInterfaceService configdbservice;
-
-       @Mock
-       ConfigDbRestClient restclient;
-       
-       @Test
-       public void fetchCurrentConfigurationOfSlice() {
-
-               Map<String, Integer> responsemap=new HashMap<>();
-               responsemap.put("dLThptPerSlice", 1);
-               responsemap.put("uLThptPerSlice", 2);
-               Mockito.when(restclient.sendGetRequest(Mockito.anyString(), Mockito.any())).thenReturn(new ResponseEntity<Object>(responsemap, HttpStatus.OK));
-               assertEquals(responsemap, configdbservice.fetchCurrentConfigurationOfSlice("snssai"));
-       }
-
-       @Test
-       public void fetchCurrentConfigurationOfRIC() {
-               Map<String,Integer> map=new HashMap<>();
-               Map<String, Map<String,Integer>> responsemap=new HashMap<>();
-        map.put("dLThptPerSlice", 45);
-               map.put("uLThptPerSlice", 50);
-               responsemap.put("1", map);
-
-               Mockito.when(restclient.sendGetRequest(Mockito.anyString(), Mockito.any())).thenReturn(new ResponseEntity<Object>(responsemap, HttpStatus.OK)); 
-               assertEquals(responsemap, configdbservice.fetchCurrentConfigurationOfRIC("snssai"));
-
-       }
-       @Test
-       public void fetchRICsOfSnssai() {
-               Map<String, List<CellsModel>> response=new HashMap<>();
-               List<CellsModel> cellslist=new ArrayList<>();
-               List<CellsModel> cellslist1=new ArrayList<>();
-               CellsModel cellsmodel1=new CellsModel();
-               cellsmodel1.setCellLocalId("1111");
-               CellsModel cellsmodel2=new CellsModel();
-               cellsmodel2.setCellLocalId("2222");
-               cellslist.add(cellsmodel1);
-               cellslist.add(cellsmodel2);
-               response.put("1", cellslist);
-               CellsModel cellsmodel3=new CellsModel();
-               cellsmodel3.setCellLocalId("3333");
-               CellsModel cellsmodel4=new CellsModel();
-               cellsmodel4.setCellLocalId("4444");
-               cellslist1.add(cellsmodel3);
-               cellslist1.add(cellsmodel4);
-               response.put("2", cellslist1);
-               Mockito.when(restclient.sendGetRequest(Mockito.anyString(), Mockito.any())).thenReturn(new ResponseEntity<Object>(response, HttpStatus.OK));
-               List<String> outputlist=new ArrayList<>();
-               outputlist.add("1111");
-               outputlist.add("2222");
-               Map<String,List<String>> output= configdbservice.fetchRICsOfSnssai("snssai");
-               assertEquals(outputlist, output.get("1"));
-
-       }
-
-       @Test
-       public void fetchNetworkFunctionsOfSnssai() {
-
-               List<String> responsemap=new ArrayList<>();
-               List<NetworkFunctionModel> networkfunctionslist=new ArrayList<NetworkFunctionModel>();
-               NetworkFunctionModel nf1=new NetworkFunctionModel();
-               nf1.setgNBDUId("1111");
-               NetworkFunctionModel nf2=new NetworkFunctionModel();
-               nf2.setgNBDUId("2222");
-               NetworkFunctionModel nf3=new NetworkFunctionModel();
-               nf3.setgNBDUId("3333");
-               networkfunctionslist.add(nf1);
-               networkfunctionslist.add(nf2);
-               networkfunctionslist.add(nf3);
-               Mockito.when(restclient.sendGetRequest(Mockito.anyString(), Mockito.any())).thenReturn(new ResponseEntity<Object>(networkfunctionslist, HttpStatus.OK));
-               responsemap=configdbservice.fetchNetworkFunctionsOfSnssai("snssai");
-               assertEquals(3, responsemap.size());
-
-       }
-       public void fetchServiceProfile() {
-               Map<String,String> responseMap=new HashMap<String, String>();
-               responseMap.put("sNSSAI", "001-010");
-               responseMap.put("ranNFNSSIId","1111");
-               responseMap.put("sliceProfileId","2222");
-               responseMap.put("globalSubscriberId","110-345");
-               Mockito.when(restclient.sendGetRequest(Mockito.anyString(), Mockito.any())).thenReturn(new ResponseEntity<Object>(responseMap, HttpStatus.OK));
-               assertEquals(responseMap, configdbservice.fetchServiceDetails("snssai"));
-       }
+        @InjectMocks
+        ConfigDbInterfaceService configdbservice;
+
+        @Mock
+        ConfigDbRestClient restclient;
+
+        @Test
+        public void fetchCurrentConfigurationOfSlice() {
+
+                Map<String, Integer> responsemap=new HashMap<>();
+                responsemap.put("dLThptPerSlice", 1);
+                responsemap.put("uLThptPerSlice", 2);
+                Mockito.when(restclient.sendGetRequest(Mockito.anyString(), Mockito.any()))
+                            .thenReturn(new ResponseEntity<Object>(responsemap, HttpStatus.OK));
+                assertEquals(responsemap, configdbservice.fetchCurrentConfigurationOfSlice("snssai"));
+        }
+
+        @Test
+        public void fetchCurrentConfigurationOfRIC() {
+                Map<String,Integer> map=new HashMap<>();
+                Map<String, Map<String,Integer>> responsemap=new HashMap<>();
+                Map<String, List<Map<String,Integer>>> result =new HashMap<String, List<Map<String,Integer>>>();
+                map.put("dLThptPerSlice", 45);
+                map.put("uLThptPerSlice", 60);
+                map.put("nearRTRICId",1);
+                responsemap.put("1", map);
+                List<Map<String,Integer>> list = new ArrayList<Map<String,Integer>>();
+                list.add(map);
+                result.put("data",list);
+                Mockito.when(restclient.sendGetRequest(Mockito.anyString(), Mockito.any()))
+                            .thenReturn(new ResponseEntity<Object>(result, HttpStatus.OK));
+                assertEquals(responsemap, configdbservice.fetchCurrentConfigurationOfRIC("snssai"));
+
+        }
+
+        @Test
+        public void fetchRICsOfSnssai() {
+                Map<String, List<CellsModel>> response=new HashMap<>();
+                List<CellsModel> cellslist=new ArrayList<>();
+                List<CellsModel> cellslist1=new ArrayList<>();
+                CellsModel cellsmodel1=new CellsModel();
+                cellsmodel1.setCellLocalId("1111");
+                CellsModel cellsmodel2=new CellsModel();
+                cellsmodel2.setCellLocalId("2222");
+                cellslist.add(cellsmodel1);
+                cellslist.add(cellsmodel2);
+                response.put("1", cellslist);
+                CellsModel cellsmodel3=new CellsModel();
+                cellsmodel3.setCellLocalId("3333");
+                CellsModel cellsmodel4=new CellsModel();
+                cellsmodel4.setCellLocalId("4444");
+                cellslist1.add(cellsmodel3);
+                cellslist1.add(cellsmodel4);
+                response.put("2", cellslist1);
+                Mockito.when(restclient.sendGetRequest(Mockito.anyString(), Mockito.any()))
+                            .thenReturn(new ResponseEntity<Object>(response, HttpStatus.OK));
+                List<String> outputlist=new ArrayList<>();
+                outputlist.add("1111");
+                outputlist.add("2222");
+                Map<String,List<String>> output= configdbservice.fetchRICsOfSnssai("snssai");
+                assertEquals(outputlist, output.get("1"));
+
+        }
+
+        @Test
+        public void fetchNetworkFunctionsOfSnssai() {
+
+                List<String> responsemap=new ArrayList<>();
+                List<NetworkFunctionModel> networkfunctionslist=new ArrayList<NetworkFunctionModel>();
+                NetworkFunctionModel nf1=new NetworkFunctionModel();
+                nf1.setgNBDUId("1111");
+                NetworkFunctionModel nf2=new NetworkFunctionModel();
+                nf2.setgNBDUId("2222");
+                NetworkFunctionModel nf3=new NetworkFunctionModel();
+                nf3.setgNBDUId("3333");
+                networkfunctionslist.add(nf1);
+                networkfunctionslist.add(nf2);
+                networkfunctionslist.add(nf3);
+                Mockito.when(restclient.sendGetRequest(Mockito.anyString(), Mockito.any()))
+                            .thenReturn(new ResponseEntity<Object>(networkfunctionslist, HttpStatus.OK));
+                responsemap=configdbservice.fetchNetworkFunctionsOfSnssai("snssai");
+                assertEquals(3, responsemap.size());
+
+        }
+
+        public void fetchServiceProfile() {
+                Map<String,String> responseMap=new HashMap<String, String>();
+                responseMap.put("sNSSAI", "001-010");
+                responseMap.put("ranNFNSSIId","1111");
+                responseMap.put("sliceProfileId","2222");
+                responseMap.put("globalSubscriberId","110-345");
+                Mockito.when(restclient.sendGetRequest(Mockito.anyString(), Mockito.any()))
+                            .thenReturn(new ResponseEntity<Object>(responseMap, HttpStatus.OK));
+                assertEquals(responseMap, configdbservice.fetchServiceDetails("snssai"));
+        }
 }