From fadaccdb8396a178134ef95982e96ed8a5cddc26 Mon Sep 17 00:00:00 2001 From: dhebeha Date: Tue, 13 Oct 2020 21:24:08 +0530 Subject: Fix Issues - Fix certificate issue for authenticated topic - Fix consumer thread issue Issue-ID: DCAEGEN2-2485 Signed-off-by: dhebeha Change-Id: I91fe9b1b86579da7142955ac7adaa25ac4bc22e9 --- components/slice-analysis-ms/entry.sh | 3 ++ components/slice-analysis-ms/pom.xml | 10 ++++- .../org/onap/slice/analysis/ms/Application.java | 19 +++++---- .../ms/configdb/ConfigDbInterfaceService.java | 12 ++---- .../analysis/ms/configdb/IConfigDbService.java | 2 +- .../slice/analysis/ms/models/Configuration.java | 3 ++ .../analysis/ms/models/MeasurementObject.java | 20 ++++++++- .../slice/analysis/ms/restclients/RestClient.java | 8 ++-- .../analysis/ms/service/AverageCalculator.java | 39 ++++++++++-------- .../slice/analysis/ms/service/ConsumerThread.java | 25 ++++++++++-- .../slice/analysis/ms/service/PmDataQueue.java | 22 +++++++--- .../analysis/ms/service/PmEventProcessor.java | 47 +++++++++++----------- .../onap/slice/analysis/ms/service/PmThread.java | 6 +-- .../slice/analysis/ms/service/PolicyService.java | 2 +- .../ms/service/SnssaiSamplesProcessor.java | 47 ++++++++++++++-------- .../analysis/ms/restclients/RestClientTest.java | 35 ++++++---------- .../analysis/ms/service/AverageCalculatorTest.java | 4 +- .../ms/service/SnssaiSamplesProcessorTest.java | 4 +- .../src/test/resources/appendData.json | 2 +- .../slice-analysis-ms/src/test/resources/av.json | 14 +++++++ .../src/test/resources/average.json | 2 +- .../slice-analysis-ms/src/test/resources/test.json | 44 ++++++++++++++++++++ 22 files changed, 245 insertions(+), 125 deletions(-) create mode 100644 components/slice-analysis-ms/entry.sh create mode 100644 components/slice-analysis-ms/src/test/resources/av.json create mode 100644 components/slice-analysis-ms/src/test/resources/test.json (limited to 'components/slice-analysis-ms') diff --git a/components/slice-analysis-ms/entry.sh b/components/slice-analysis-ms/entry.sh new file mode 100644 index 00000000..2859fe93 --- /dev/null +++ b/components/slice-analysis-ms/entry.sh @@ -0,0 +1,3 @@ +#!/bin/bash +export trustpass=`cat /opt/app/sliceanalysisms/etc/cert/trust.pass` +java -Djavax.net.ssl.trustStore=/opt/app/sliceanalysisms/etc/cert/trust.jks -Djavax.net.ssl.trustStorePassword=$trustpass -jar /bin/application.jar \ No newline at end of file diff --git a/components/slice-analysis-ms/pom.xml b/components/slice-analysis-ms/pom.xml index 08028404..1eac47fe 100644 --- a/components/slice-analysis-ms/pom.xml +++ b/components/slice-analysis-ms/pom.xml @@ -32,7 +32,7 @@ org.onap.dcaegen2.services.components slice-analysis-ms - 1.0.0-SNAPSHOT + 1.0.1-SNAPSHOT dcaegen2-services-slice-analysis-ms Network slice PM analyser jar @@ -313,17 +313,23 @@ ${project.build.directory} ${project.artifactId}-${project.version}.jar + + /bin + ${project.basedir} + entry.sh + adduser --disabled-password sliceanalysis mv /bin/*.jar /bin/application.jar chmod -R 777 /bin + chmod +x /bin/entry.sh 8080 - java -jar /bin/application.jar + ./bin/entry.sh diff --git a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/Application.java b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/Application.java index 759706e4..2ecaf7e4 100644 --- a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/Application.java +++ b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/Application.java @@ -22,13 +22,6 @@ package org.onap.slice.analysis.ms; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -import com.google.gson.Gson; -import com.google.gson.JsonObject; -import com.google.gson.reflect.TypeToken; - import java.io.BufferedReader; import java.io.FileReader; import java.lang.reflect.Type; @@ -42,9 +35,16 @@ import org.onap.slice.analysis.ms.models.ConfigPolicy; import org.onap.slice.analysis.ms.models.Configuration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.web.client.RestTemplate; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.google.gson.reflect.TypeToken; /** * @@ -121,6 +121,11 @@ public class Application { return DataSourceBuilder.create().url(url).username(configuration.getPgUsername()) .password(configuration.getPgPassword()).build(); } + + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } private static String readFromFile(String file) { String content = ""; diff --git a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/configdb/ConfigDbInterfaceService.java b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/configdb/ConfigDbInterfaceService.java index b3cd7b1c..8a11bdb5 100644 --- a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/configdb/ConfigDbInterfaceService.java +++ b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/configdb/ConfigDbInterfaceService.java @@ -62,9 +62,9 @@ public class ConfigDbInterfaceService implements IConfigDbService { /** * Fetches the current configuration of RIC from config DB */ - public Map> fetchCurrentConfigurationOfRIC(String snssai){ + public Map> fetchCurrentConfigurationOfRIC(String snssai){ String reqUrl=configDbBaseUrl+"/api/sdnc-config-db/v4/slice-config/"+snssai; - ResponseEntity>> response=restclient.sendGetRequest(reqUrl, new ParameterizedTypeReference>>() { + ResponseEntity>> response=restclient.sendGetRequest(reqUrl, new ParameterizedTypeReference>>() { }); return response.getBody(); } @@ -87,24 +87,18 @@ public class ConfigDbInterfaceService implements IConfigDbService { * Fetches the RICS of an S-NSSAI from config DB */ public Map> fetchRICsOfSnssai(String snssai){ - Map> responseMap=new HashMap<>(); - String reqUrl=configDbBaseUrl+"/api/sdnc-config-db/v4/du-cell-list/"+snssai; - - ResponseEntity>> response=restclient.sendGetRequest(reqUrl, new ParameterizedTypeReference>>() { + ResponseEntity>> response = restclient.sendGetRequest(reqUrl, new ParameterizedTypeReference>>() { }); - for (Map.Entry> entry : response.getBody().entrySet()) { List cellslist=new ArrayList<>(); for(CellsModel cellmodel:entry.getValue()) { - cellslist.add(cellmodel.getCellLocalId()); } responseMap.put(entry.getKey(), cellslist); } - return responseMap; } diff --git a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/configdb/IConfigDbService.java b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/configdb/IConfigDbService.java index 3c8a9f8a..7413c181 100644 --- a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/configdb/IConfigDbService.java +++ b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/configdb/IConfigDbService.java @@ -34,6 +34,6 @@ public interface IConfigDbService { public Map> fetchRICsOfSnssai(String snssai); public List fetchNetworkFunctionsOfSnssai(String snssai); public Map fetchCurrentConfigurationOfSlice(String snssai); - public Map> fetchCurrentConfigurationOfRIC(String snssai); + public Map> fetchCurrentConfigurationOfRIC(String snssai); public Map fetchServiceDetails(String snssai); } diff --git a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/models/Configuration.java b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/models/Configuration.java index 25a80463..8bdbb1a8 100644 --- a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/models/Configuration.java +++ b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/models/Configuration.java @@ -260,6 +260,9 @@ public class Configuration { minPercentageChange = jsonObject.get("sliceanalysisms.minPercentageChange").getAsInt(); initialDelaySeconds = jsonObject.get("sliceanalysisms.initialDelaySeconds").getAsLong(); + aafUsername = jsonObject.get("aafUsername").getAsString(); + aafPassword = jsonObject.get("aafPassword").getAsString(); + log.info("configuration from CBS {}", this); } } diff --git a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/models/MeasurementObject.java b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/models/MeasurementObject.java index 047c9856..b94d75da 100644 --- a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/models/MeasurementObject.java +++ b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/models/MeasurementObject.java @@ -22,6 +22,7 @@ package org.onap.slice.analysis.ms.models; +import java.util.List; import java.util.Map; /** @@ -31,6 +32,17 @@ public class MeasurementObject { private String measurementObjectId; private Map pmData; + public static int findIndex(String measurementObjectId, List list) { + int index = -1; + int len = list.size(); + for(int i=0; i responseType) { HttpEntity requestEntity = new HttpEntity<>(requestBody, headers); try { - RestTemplate restTemplate = BeanUtil.getBean(RestTemplate.class); return restTemplate.exchange(requestUrl, HttpMethod.POST, requestEntity, responseType); } catch (Exception e) { log.debug(EXCEPTION_MSG, e.getMessage()); @@ -68,7 +69,6 @@ public class RestClient { public ResponseEntity sendGetRequest(HttpHeaders headers, String requestUrl, ParameterizedTypeReference responseType) { HttpEntity requestEntity = new HttpEntity<>(headers); try { - RestTemplate restTemplate = BeanUtil.getBean(RestTemplate.class); return restTemplate.exchange(requestUrl, HttpMethod.GET, requestEntity, responseType); } catch (Exception e) { log.debug(EXCEPTION_MSG, e.getMessage()); diff --git a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/AverageCalculator.java b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/AverageCalculator.java index a003e9c0..52670d53 100644 --- a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/AverageCalculator.java +++ b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/AverageCalculator.java @@ -50,19 +50,22 @@ public class AverageCalculator { * Find average of samples */ public List findAverageOfSamples(List> samples) { + log.debug("find average for samples {}", samples); int numOfSamples = samples.size(); List result = new ArrayList<>(); - samples.forEach(sample -> - sample.forEach(cellMeasObj -> { - int index = result.indexOf(cellMeasObj); - if(index != -1) { - result.set(index, findSum(result.get(index), cellMeasObj)); + if(!samples.isEmpty()) { + for(List sample : samples) { + for(MeasurementObject cellMeasObj : sample) { + int index = MeasurementObject.findIndex(cellMeasObj.getMeasurementObjectId(), result); + if(index != -1) { + result.set(index, findSum(result.get(index), cellMeasObj)); + } + else { + result.add(cellMeasObj); + } + } } - else { - result.add(cellMeasObj); - } - }) - ); + } return findAvg(result, numOfSamples); } @@ -81,13 +84,15 @@ public class AverageCalculator { * Calculate the average */ public List findAvg(List result, int numOfSamples) { - result.forEach(cellMeasObj -> - pmNames.forEach(pmName -> { - int value = (cellMeasObj.getPmData().get(pmName))/numOfSamples; - cellMeasObj.getPmData().put(pmName, value); - }) - ); - log.debug("Average of measurement data samples {}",result); + if(!result.isEmpty()) { + result.forEach(cellMeasObj -> + pmNames.forEach(pmName -> { + int value = (cellMeasObj.getPmData().get(pmName))/numOfSamples; + cellMeasObj.getPmData().put(pmName, value); + }) + ); + log.debug("Average of measurement data samples {}",result); + } return result; } } diff --git a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/ConsumerThread.java b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/ConsumerThread.java index 39235cd5..6868bc46 100644 --- a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/ConsumerThread.java +++ b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/ConsumerThread.java @@ -21,6 +21,8 @@ package org.onap.slice.analysis.ms.service; +import java.util.List; + import org.onap.slice.analysis.ms.configdb.IConfigDbService; import org.onap.slice.analysis.ms.models.Configuration; import org.onap.slice.analysis.ms.utils.BeanUtil; @@ -31,7 +33,7 @@ import org.slf4j.LoggerFactory; * This Thread class consumes message from pm data queue and sends onset message to policy */ public class ConsumerThread extends Thread { - private static Logger log = LoggerFactory.getLogger(PmThread.class); + private static Logger log = LoggerFactory.getLogger(ConsumerThread.class); private PmDataQueue pmDataQueue; private IConfigDbService configDbService; private SnssaiSamplesProcessor snssaiSamplesProcessor; @@ -55,16 +57,31 @@ public class ConsumerThread extends Thread { public void run() { boolean done = false; String snssai = ""; + boolean result = false; + List nfs = null; while (!done) { try { Thread.sleep(initialDelaySec); snssai = pmDataQueue.getSnnsaiFromQueue(); if (!snssai.equals("")) { - log.info("Consumer thread started for s-nssai {}",snssai); - snssaiSamplesProcessor.processSamplesOfSnnsai(snssai, configDbService.fetchNetworkFunctionsOfSnssai(snssai)); + log.info("Consumer thread processing data for s-nssai {}",snssai); + try { + nfs = configDbService.fetchNetworkFunctionsOfSnssai(snssai); + } + catch(Exception e) { + pmDataQueue.putSnssaiToQueue(snssai); + log.error("Exception caught while fetching nfs of snssai {}, {}", snssai, e.getMessage()); + } + if(nfs != null) { + result = snssaiSamplesProcessor.processSamplesOfSnnsai(snssai, nfs); + if(!result) { + log.info("Not enough samples to process for {}",snssai); + pmDataQueue.putSnssaiToQueue(snssai); + } + } } } catch (Exception e) { - log.error("Exception in Consumer Thread ", e); + log.error("Exception in Consumer Thread, {}", e.getMessage()); done = true; } } diff --git a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/PmDataQueue.java b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/PmDataQueue.java index d907bfed..4f196979 100644 --- a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/PmDataQueue.java +++ b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/PmDataQueue.java @@ -31,6 +31,8 @@ import java.util.concurrent.LinkedBlockingQueue; import org.onap.slice.analysis.ms.models.MeasurementObject; import org.onap.slice.analysis.ms.models.SubCounter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; /** @@ -38,6 +40,8 @@ import org.springframework.stereotype.Component; */ @Component public class PmDataQueue { + private static Logger log = LoggerFactory.getLogger(PmDataQueue.class); + private Map>> subCounterMap = Collections.synchronizedMap(new LinkedHashMap>>()); private Queue snssaiList = new LinkedBlockingQueue<>(); @@ -54,6 +58,7 @@ public class PmDataQueue { measQueue.add(measurementObjectData); subCounterMap.put(subCounter, measQueue); } + log.debug("Queue: {}", subCounterMap); } /** @@ -61,12 +66,15 @@ public class PmDataQueue { * returns the specified number of samples */ public List> getSamplesFromQueue(SubCounter subCounter, int samples) { - List> sampleList = new LinkedList<>(); + List> sampleList = null; if (subCounterMap.containsKey(subCounter)){ Queue> measQueue = subCounterMap.get(subCounter); - while(samples > 0) { - sampleList.add(measQueue.remove()); - samples --; + if(measQueue.size() >= samples) { + sampleList = new LinkedList<>(); + while(samples > 0) { + sampleList.add(measQueue.remove()); + samples --; + } } } return sampleList; @@ -86,10 +94,12 @@ public class PmDataQueue { public String getSnnsaiFromQueue() { String snssai = ""; try { - snssai = snssaiList.remove(); + if(!snssaiList.isEmpty()){ + snssai = snssaiList.remove(); + } } catch(Exception e) { - + log.error("Problem fetching from the Queue, {}", e.getMessage()); } return snssai; } diff --git a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/PmEventProcessor.java b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/PmEventProcessor.java index 99c24c8a..51a04660 100644 --- a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/PmEventProcessor.java +++ b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/PmEventProcessor.java @@ -31,6 +31,8 @@ import org.onap.slice.analysis.ms.models.pmnotification.Event; import org.onap.slice.analysis.ms.models.pmnotification.MeasInfoList; import org.onap.slice.analysis.ms.models.pmnotification.MeasResult; import org.onap.slice.analysis.ms.models.pmnotification.MeasValuesList; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @@ -41,7 +43,7 @@ import org.springframework.stereotype.Component; @Scope("prototype") public class PmEventProcessor implements IPmEventProcessor{ protected Map> instanceMap = new HashMap<>(); - + private static Logger log = LoggerFactory.getLogger(PmEventProcessor.class); /** * Process the PM event @@ -53,6 +55,7 @@ public class PmEventProcessor implements IPmEventProcessor{ List subCounterMeasurements = measurement.getMeasValuesList(); subCounterMeasurements.forEach(subCounterMeasurement -> processMeasurementObjectData(collectedSubCounters, subCounterMeasurement)); }); + log.info("Processed Event: {}", instanceMap); return instanceMap; } @@ -65,26 +68,28 @@ public class PmEventProcessor implements IPmEventProcessor{ measResultList.forEach(measResult -> { String pmName = collectedSubCounters.get(measResult.getP()-1); Integer pmValue = Integer.valueOf(measResult.getsValue()); - Map pmMapping = getMapKey(pmName); - String snssai = pmMapping.get("snssai"); - String pm = pmMapping.get("pm"); - Map pmData = new HashMap<>(); - pmData.put(pm, pmValue); - if (instanceMap.containsKey(snssai)) { - int index = instanceMap.get(snssai).indexOf(new MeasurementObject(measObjId)); - if (index == -1) { - instanceMap.get(snssai).add(new MeasurementObject(measObjId,pmData)); + if(pmName.contains("PrbUsedUl") || pmName.contains("PrbUsedDl")) { + Map pmMapping = getMapKey(pmName); + String snssai = pmMapping.get("snssai"); + String pm = pmMapping.get("pm"); + Map pmData = new HashMap<>(); + pmData.put(pm, pmValue); + if (instanceMap.containsKey(snssai)) { + int index = MeasurementObject.findIndex(measObjId, instanceMap.get(snssai)); + if (index == -1) { + instanceMap.get(snssai).add(new MeasurementObject(measObjId,pmData)); + } + else { + instanceMap.get(snssai).get(index).getPmData().put(pm, pmValue); + } } else { - instanceMap.get(snssai).get(index).getPmData().put(pmName, pmValue); - } - } - else { - List l = new LinkedList<>(); - l.add(new MeasurementObject(measObjId,pmData)); - instanceMap.put(snssai, l); + List l = new LinkedList<>(); + l.add(new MeasurementObject(measObjId,pmData)); + instanceMap.put(snssai, l); + } } - }); + }); } /** @@ -92,14 +97,10 @@ public class PmEventProcessor implements IPmEventProcessor{ */ public Map getMapKey(String pmName) { String [] pmNameArr = pmName.split("\\."); - String snssai = ""; String pm = pmNameArr[1]; Map result = new HashMap<>(); result.put("pm", pm); - if ((pm.equalsIgnoreCase("PrbUsedDl")) || (pm.equalsIgnoreCase("PrbUsedUl"))){ - snssai = pmNameArr[2]; - } - result.put("snssai", snssai); + result.put("snssai", pmNameArr[2]); return result; } } \ No newline at end of file diff --git a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/PmThread.java b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/PmThread.java index d8bcbf64..e2903ae1 100644 --- a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/PmThread.java +++ b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/PmThread.java @@ -52,7 +52,6 @@ public class PmThread extends Thread { super(); this.newPmNotification = BeanUtil.getBean(NewPmNotification.class); this.performanceNotificationsRepository = BeanUtil.getBean(PerformanceNotificationsRepository.class); - this.pmEventProcessor = BeanUtil.getBean(IPmEventProcessor.class); this.pmDataQueue = BeanUtil.getBean(PmDataQueue.class); } @@ -68,12 +67,13 @@ public class PmThread extends Thread { while (!done) { try { Thread.sleep(1000); - if (newPmNotification.getNewNotif()) { - log.info("New PM notification from Dmaap"); + if (newPmNotification.getNewNotif()) { String pmNotificationString = performanceNotificationsRepository.getPerformanceNotificationFromQueue(); if(pmNotificationString != null) { + log.info("New PM notification"); ObjectMapper mapper = new ObjectMapper(); pmNotification = mapper.readValue(pmNotificationString, PmNotification.class); + this.pmEventProcessor = BeanUtil.getBean(IPmEventProcessor.class); processedData = pmEventProcessor.processEvent(pmNotification.getEvent()); String networkFunction = pmNotification.getEvent().getPerf3gppFields().getMeasDataCollection().getMeasuredEntityDn(); processedData.forEach((key,value) -> { diff --git a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/PolicyService.java b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/PolicyService.java index 80063398..165bef90 100644 --- a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/PolicyService.java +++ b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/PolicyService.java @@ -99,8 +99,8 @@ public class PolicyService { ObjectMapper obj = new ObjectMapper(); String msg = ""; try { - log.debug("Policy onset message for S-NSSAI: {} is {}", snssai, msg); msg = obj.writeValueAsString(onsetMessage); + log.info("Policy onset message for S-NSSAI: {} is {}", snssai, msg); policyDmaapClient.sendNotificationToPolicy(msg); } catch (Exception e) { diff --git a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/SnssaiSamplesProcessor.java b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/SnssaiSamplesProcessor.java index 16fb41ad..e8d5de3e 100644 --- a/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/SnssaiSamplesProcessor.java +++ b/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/SnssaiSamplesProcessor.java @@ -63,7 +63,7 @@ public class SnssaiSamplesProcessor { private Map> ricToCellMapping = new HashMap<>(); private Map> ricToPrbsMapping = new HashMap<>(); private Map> ricToThroughputMapping = new HashMap<>(); - private int samples; + private int noOfSamples; private List pmsToCompute; private Map prbThroughputMapping = new HashMap<>(); private int minPercentageChange; @@ -71,7 +71,7 @@ public class SnssaiSamplesProcessor { @PostConstruct public void init() { Configuration configuration = Configuration.getInstance(); - samples = configuration.getSamples(); + noOfSamples = configuration.getSamples(); pmsToCompute = new ArrayList<>(); pmsToCompute.add("PrbUsedDl"); pmsToCompute.add("PrbUsedUl"); @@ -84,22 +84,34 @@ public class SnssaiSamplesProcessor { /** * process the measurement data of an S-NSSAI */ - public void processSamplesOfSnnsai(String snssai, List networkFunctions) { - networkFunctions.forEach(nf -> { + public boolean processSamplesOfSnnsai(String snssai, List networkFunctions) { + List sample = null; + List> samples = null; + log.info("Network Functions {} of snssai {}", networkFunctions, snssai); + for(String nf : networkFunctions) { log.debug("Average of samples for {}:", snssai); - addToMeasurementList(averageCalculator.findAverageOfSamples(pmDataQueue.getSamplesFromQueue(new SubCounter(nf, snssai), samples))); - }); + 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); ricToCellMapping = configDbService.fetchRICsOfSnssai(snssai); - log.debug("RIC to Cell Mapping for {} S-NSSAI: {}", snssai, ricToCellMapping); - Map> ricConfiguration = configDbService.fetchCurrentConfigurationOfRIC(snssai); + log.info("RIC to Cell Mapping for {} S-NSSAI: {}", snssai, ricToCellMapping); + Map> ricConfiguration = configDbService.fetchCurrentConfigurationOfRIC(snssai); Map sliceConfiguration = configDbService.fetchCurrentConfigurationOfSlice(snssai); - log.debug("RIC Configuration: {}", ricConfiguration); - log.debug("Slice Configuration: {}", sliceConfiguration); + 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, pm); + calculatePercentageChange(ricConfiguration, prbThroughputMapping.get(pm)); }); updateConfiguration(); if(ricToThroughputMapping.size() > 0) { @@ -107,7 +119,7 @@ public class SnssaiSamplesProcessor { addProps.setResourceConfig(ricToThroughputMapping); policyService.sendOnsetMessageToPolicy(snssai, addProps, configDbService.fetchServiceDetails(snssai)); } - + return true; } /** @@ -132,17 +144,18 @@ public class SnssaiSamplesProcessor { * Calculate the change in the configuration value and keep the configuration only if it is greater than a * specific limit */ - protected void calculatePercentageChange(Map> ricConfiguration, String pm) { + protected void calculatePercentageChange(Map> ricConfiguration, String pm) { Iterator>> it = ricToThroughputMapping.entrySet().iterator(); Map.Entry> entry = null; float existing = 0; float change = 0; while(it.hasNext()) { entry = it.next(); - existing = ricConfiguration.get(entry.getKey()).get(pm); + 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()); } } } @@ -151,7 +164,7 @@ public class SnssaiSamplesProcessor { ricToCellMapping.forEach((ric,cells) -> { int sumOfPrbs = 0; for(String cell : cells) { - int index = snssaiMeasurementList.indexOf(new MeasurementObject(cell)); + int index = MeasurementObject.findIndex(cell, snssaiMeasurementList); sumOfPrbs += snssaiMeasurementList.get(index).getPmData().get(pmName); } if(ricToPrbsMapping.containsKey(ric)) { @@ -163,6 +176,7 @@ public class SnssaiSamplesProcessor { ricToPrbsMapping.put(ric, pmToPrbMapping); } }); + log.info("PRBs sum computed for RIC {}", ricToPrbsMapping); } protected Integer computeSum(String pm) { @@ -188,7 +202,6 @@ public class SnssaiSamplesProcessor { ricToThroughputMapping.put(ric, throughtputMap); } } - + log.info("Throughput computed for RIC {}", ricToThroughputMapping); } - } diff --git a/components/slice-analysis-ms/src/test/java/org/onap/slice/analysis/ms/restclients/RestClientTest.java b/components/slice-analysis-ms/src/test/java/org/onap/slice/analysis/ms/restclients/RestClientTest.java index b19b4801..95da3667 100644 --- a/components/slice-analysis-ms/src/test/java/org/onap/slice/analysis/ms/restclients/RestClientTest.java +++ b/components/slice-analysis-ms/src/test/java/org/onap/slice/analysis/ms/restclients/RestClientTest.java @@ -22,6 +22,7 @@ package org.onap.slice.analysis.ms.restclients; import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; import java.util.Collections; import java.util.HashMap; @@ -31,49 +32,39 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.Mockito; +import org.onap.slice.analysis.ms.service.SnssaiSamplesProcessorTest; import org.onap.slice.analysis.ms.utils.BeanUtil; -import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunnerDelegate; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.core.ParameterizedTypeReference; -import org.springframework.http.ResponseEntity; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.web.client.RestTemplate; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.client.RestTemplate; -@RunWith(PowerMockRunner.class) -@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"}) -@PowerMockRunnerDelegate(SpringRunner.class) -@PrepareForTest({ BeanUtil.class }) -@SpringBootTest(classes = RestClient.class) +@RunWith(SpringRunner.class) +@SpringBootTest(classes = RestClientTest.class) public class RestClientTest { - @Mock RestTemplate restTemplate; - @InjectMocks RestClient restclient; - - @SuppressWarnings({ "static-access", "unchecked", "rawtypes" }) + @SuppressWarnings({ "static-access"}) @Test public void sendGetRequestTest() { - - PowerMockito.mockStatic(BeanUtil.class); - PowerMockito.when(BeanUtil.getBean(Mockito.any())).thenReturn(restTemplate); - ParameterizedTypeReference> responseType = null; + ParameterizedTypeReference> responseType = null; HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); headers.setContentType(MediaType.APPLICATION_JSON); @@ -82,7 +73,7 @@ public class RestClientTest { responsemap.put("dLThptPerSlice", 1); responsemap.put("uLThptPerSlice", 2); String requestUrl=""; - PowerMockito.when(restTemplate.exchange(requestUrl, HttpMethod.GET,requestEntity,responseType)).thenReturn(ResponseEntity.ok(responsemap)); + when(restTemplate.exchange(requestUrl, HttpMethod.GET,requestEntity,responseType)).thenReturn(ResponseEntity.ok(responsemap)); ResponseEntity> resp = restclient.sendGetRequest(headers, requestUrl, responseType); assertEquals(resp.getBody(),responsemap); } @@ -90,15 +81,13 @@ public class RestClientTest { @SuppressWarnings({ "static-access", "unchecked", "rawtypes" }) @Test public void sendPostRequestTest() { - PowerMockito.mockStatic(BeanUtil.class); - PowerMockito.when(BeanUtil.getBean(RestTemplate.class)).thenReturn(restTemplate); ParameterizedTypeReference responseType = null; HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); headers.setContentType(MediaType.APPLICATION_JSON); String requestUrl = "Url"; String requestBody = null; HttpEntity requestEntity = new HttpEntity<>(requestBody, headers); - PowerMockito.when(restTemplate.exchange(requestUrl, HttpMethod.POST,requestEntity,responseType)).thenReturn(new ResponseEntity(HttpStatus.OK)); + when(restTemplate.exchange(requestUrl, HttpMethod.POST,requestEntity,responseType)).thenReturn(new ResponseEntity(HttpStatus.OK)); ResponseEntity resp = restclient.sendPostRequest(headers, requestUrl, requestBody,responseType); assertEquals(resp.getStatusCode(), HttpStatus.OK); } @@ -111,7 +100,7 @@ public class RestClientTest { headers.setContentType(MediaType.APPLICATION_JSON); String requestUrl = "Url"; String requestBody = null; HttpEntity requestEntity = new HttpEntity<>(requestBody, headers); - PowerMockito.when(restTemplate.exchange(requestUrl, HttpMethod.POST,requestEntity,responseType)).thenReturn(new ResponseEntity(HttpStatus.NOT_FOUND)); + when(restTemplate.exchange(requestUrl, HttpMethod.POST,requestEntity,responseType)).thenReturn(new ResponseEntity(HttpStatus.NOT_FOUND)); ResponseEntity resp = restclient.sendPostRequest(headers, requestUrl, requestBody,responseType); assertEquals(resp.getStatusCode(), HttpStatus.NOT_FOUND); } diff --git a/components/slice-analysis-ms/src/test/java/org/onap/slice/analysis/ms/service/AverageCalculatorTest.java b/components/slice-analysis-ms/src/test/java/org/onap/slice/analysis/ms/service/AverageCalculatorTest.java index e9c134f7..05e60171 100644 --- a/components/slice-analysis-ms/src/test/java/org/onap/slice/analysis/ms/service/AverageCalculatorTest.java +++ b/components/slice-analysis-ms/src/test/java/org/onap/slice/analysis/ms/service/AverageCalculatorTest.java @@ -70,7 +70,6 @@ public class AverageCalculatorTest { catch (IOException e) { e.printStackTrace(); } - assertEquals(output, averageCalculator.findAverageOfSamples(input)); } @@ -113,5 +112,4 @@ public class AverageCalculatorTest { assertEquals(new MeasurementObject("cell1", result), averageCalculator.findSum(new MeasurementObject("cell1", existingMap), new MeasurementObject("cell1", currentMap))); } -} - +} \ No newline at end of file diff --git a/components/slice-analysis-ms/src/test/java/org/onap/slice/analysis/ms/service/SnssaiSamplesProcessorTest.java b/components/slice-analysis-ms/src/test/java/org/onap/slice/analysis/ms/service/SnssaiSamplesProcessorTest.java index b8316dfe..e8cc2145 100644 --- a/components/slice-analysis-ms/src/test/java/org/onap/slice/analysis/ms/service/SnssaiSamplesProcessorTest.java +++ b/components/slice-analysis-ms/src/test/java/org/onap/slice/analysis/ms/service/SnssaiSamplesProcessorTest.java @@ -130,7 +130,7 @@ public class SnssaiSamplesProcessorTest { @Test public void calculatePercentageChangeTest() { - Map> ricConfiguration = null; + Map> ricConfiguration = null; Map> exp = new HashMap<>(); Map ric1 = new HashMap<>(); Map ric2 = new HashMap<>(); @@ -140,7 +140,7 @@ public class SnssaiSamplesProcessorTest { exp.put("1", ric1); exp.put("2", ric2); try { - ricConfiguration = obj.readValue(new String(Files.readAllBytes(Paths.get("src/test/resources/ricConfiguration.json"))), new TypeReference>>(){}); + ricConfiguration = obj.readValue(new String(Files.readAllBytes(Paths.get("src/test/resources/ricConfiguration.json"))), new TypeReference>>(){}); } catch (IOException e) { e.printStackTrace(); diff --git a/components/slice-analysis-ms/src/test/resources/appendData.json b/components/slice-analysis-ms/src/test/resources/appendData.json index 39802dd2..8cd06727 100644 --- a/components/slice-analysis-ms/src/test/resources/appendData.json +++ b/components/slice-analysis-ms/src/test/resources/appendData.json @@ -54,7 +54,7 @@ }, {"measurementObjectId":"cell3", "pmData":{ - "PrbUsedDl":"25", + "PrbUsedDl":"30", "PrbUsedUl":"40" } } diff --git a/components/slice-analysis-ms/src/test/resources/av.json b/components/slice-analysis-ms/src/test/resources/av.json new file mode 100644 index 00000000..f9e8ecef --- /dev/null +++ b/components/slice-analysis-ms/src/test/resources/av.json @@ -0,0 +1,14 @@ +[ + {"measurementObjectId":"103593989", + "pmData":{ + "PrbUsedDl":"33", + "PrbUsedUl":"42" + } + }, + {"measurementObjectId":"103593999", + "pmData":{ + "PrbUsedDl":"53", + "PrbUsedUl":"53" + } + } +] \ No newline at end of file diff --git a/components/slice-analysis-ms/src/test/resources/average.json b/components/slice-analysis-ms/src/test/resources/average.json index 81838b6d..e0b0779b 100644 --- a/components/slice-analysis-ms/src/test/resources/average.json +++ b/components/slice-analysis-ms/src/test/resources/average.json @@ -13,7 +13,7 @@ }, {"measurementObjectId":"cell3", "pmData":{ - "PrbUsedDl":"25", + "PrbUsedDl":"30", "PrbUsedUl":"40" } } diff --git a/components/slice-analysis-ms/src/test/resources/test.json b/components/slice-analysis-ms/src/test/resources/test.json new file mode 100644 index 00000000..e852e244 --- /dev/null +++ b/components/slice-analysis-ms/src/test/resources/test.json @@ -0,0 +1,44 @@ +[ +[ + {"measurementObjectId":"103593989", + "pmData":{ + "PrbUsedDl":"30", + "PrbUsedUl":"40" + } + }, + {"measurementObjectId":"103593999", + "pmData":{ + "PrbUsedDl":"60", + "PrbUsedUl":"80" + } + } + ], +[ + {"measurementObjectId":"103593989", + "pmData":{ + "PrbUsedDl":"35", + "PrbUsedUl":"44" + } + }, + {"measurementObjectId":"103593999", + "pmData":{ + "PrbUsedDl":"50", + "PrbUsedUl":"40" + } + } + ], +[ + {"measurementObjectId":"103593989", + "pmData":{ + "PrbUsedDl":"35", + "PrbUsedUl":"44" + } + }, + {"measurementObjectId":"103593999", + "pmData":{ + "PrbUsedDl":"50", + "PrbUsedUl":"40" + } + } + ] +] \ No newline at end of file -- cgit 1.2.3-korg