a003e9c0dbd101a0d65c054e37edc87f211c9af9
[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                 int numOfSamples = samples.size();
54                 List<MeasurementObject> result = new ArrayList<>();
55                 samples.forEach(sample -> 
56                 sample.forEach(cellMeasObj -> {
57                         int index = result.indexOf(cellMeasObj);
58                         if(index != -1) {
59                                 result.set(index, findSum(result.get(index), cellMeasObj));
60                         }
61                         else { 
62                                 result.add(cellMeasObj);
63                         }
64                 })
65                                 );
66                 return findAvg(result, numOfSamples);
67         }
68
69         /**
70          * Calculate the sum
71          */
72         public MeasurementObject findSum(MeasurementObject existing, MeasurementObject current) {
73                 pmNames.forEach(pmName -> {
74                         int newValue = current.getPmData().get(pmName) + existing.getPmData().get(pmName);
75                         existing.getPmData().put(pmName, newValue);
76                 });
77                 return existing;
78         }
79
80         /**
81          * Calculate the average
82          */
83         public List<MeasurementObject> findAvg(List<MeasurementObject> result, int numOfSamples) {
84                 result.forEach(cellMeasObj ->
85                 pmNames.forEach(pmName -> {
86                         int value = (cellMeasObj.getPmData().get(pmName))/numOfSamples;
87                         cellMeasObj.getPmData().put(pmName, value);
88                 })
89                                 );
90                 log.debug("Average of measurement data samples {}",result);
91                 return result;
92         }
93 }
94