52670d536fc573132fa5f2c3840ac8c313a717dd
[dcaegen2/services.git] /
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  slice-analysis-ms
4  *  ================================================================================
5  *   Copyright (C) 2020 Wipro Limited.
6  *   ==============================================================================
7  *     Licensed under the Apache License, Version 2.0 (the "License");
8  *     you may not use this file except in compliance with the License.
9  *     You may obtain a copy of the License at
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *     Unless required by applicable law or agreed to in writing, software
14  *     distributed under the License is distributed on an "AS IS" BASIS,
15  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *     See the License for the specific language governing permissions and
17  *     limitations under the License.
18  *     ============LICENSE_END=========================================================
19  *
20  *******************************************************************************/
21
22 package org.onap.slice.analysis.ms.service;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import javax.annotation.PostConstruct;
28
29 import org.onap.slice.analysis.ms.models.MeasurementObject;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.stereotype.Component;
33
34 /** 
35  * This class has utility methods for calculating the average of samples
36  */
37 @Component
38 public class AverageCalculator {
39         private static Logger log = LoggerFactory.getLogger(AverageCalculator.class);
40         private List<String> pmNames;
41
42         @PostConstruct
43         public void init() {
44                 pmNames = new ArrayList<>();
45                 pmNames.add("PrbUsedDl");
46                 pmNames.add("PrbUsedUl");
47         }
48
49         /**
50          * Find average of samples
51          */
52         public List<MeasurementObject> findAverageOfSamples(List<List<MeasurementObject>> samples) {
53                 log.debug("find average for samples {}", samples);
54                 int numOfSamples = samples.size();
55                 List<MeasurementObject> result = new ArrayList<>();
56                 if(!samples.isEmpty()) {
57                         for(List<MeasurementObject> sample : samples) {
58                                 for(MeasurementObject cellMeasObj : sample) {
59                                         int index = MeasurementObject.findIndex(cellMeasObj.getMeasurementObjectId(), result);
60                                         if(index != -1) {
61                                                 result.set(index, findSum(result.get(index), cellMeasObj));
62                                         }
63                                         else { 
64                                                 result.add(cellMeasObj);
65                                         }
66                                 }
67                         }
68                 }
69                 return findAvg(result, numOfSamples);
70         }
71
72         /**
73          * Calculate the sum
74          */
75         public MeasurementObject findSum(MeasurementObject existing, MeasurementObject current) {
76                 pmNames.forEach(pmName -> {
77                         int newValue = current.getPmData().get(pmName) + existing.getPmData().get(pmName);
78                         existing.getPmData().put(pmName, newValue);
79                 });
80                 return existing;
81         }
82
83         /**
84          * Calculate the average
85          */
86         public List<MeasurementObject> findAvg(List<MeasurementObject> result, int numOfSamples) {
87                 if(!result.isEmpty()) {
88                         result.forEach(cellMeasObj ->
89                         pmNames.forEach(pmName -> {
90                                 int value = (cellMeasObj.getPmData().get(pmName))/numOfSamples;
91                                 cellMeasObj.getPmData().put(pmName, value);
92                         })
93                         );
94                         log.debug("Average of measurement data samples {}",result);
95                 }
96                 return result;
97         }
98 }
99