754c35738c4c172316d42185601e3f1923737539
[dcaegen2/services.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 Deutsche Telekom AG. All rights reserved.
4  * Copyright (C) 2022 Wipro Limited. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.dcaegen2.kpi.computation;
23
24 import java.math.BigDecimal;
25 import java.math.RoundingMode;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.LinkedList;
29 import java.util.ListIterator;
30 import java.util.Map;
31 import java.util.UUID;
32
33 import org.onap.dcaegen2.kpi.config.ControlLoopSchemaType;
34 import org.onap.dcaegen2.kpi.exception.KpiComputationException;
35 import org.onap.dcaegen2.kpi.models.CommonEventHeader;
36 import org.onap.dcaegen2.kpi.models.KpiOperand;
37 import org.onap.dcaegen2.kpi.models.MeasDataCollection;
38 import org.onap.dcaegen2.kpi.models.MeasInfo;
39 import org.onap.dcaegen2.kpi.models.MeasInfoId;
40 import org.onap.dcaegen2.kpi.models.MeasResult;
41 import org.onap.dcaegen2.kpi.models.MeasTypes;
42 import org.onap.dcaegen2.kpi.models.MeasValues;
43 import org.onap.dcaegen2.kpi.models.Perf3gppFields;
44 import org.onap.dcaegen2.kpi.models.PerformanceEvent;
45 import org.onap.dcaegen2.kpi.models.VesEvent;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * RatioKpiComputation.
51  *
52  * @author Tarun Agrawal
53  */
54 public class RatioKpiComputation extends BaseKpiComputation {
55
56     private static Logger logger = LoggerFactory.getLogger(RatioKpiComputation.class);
57
58     @Override
59     public List <VesEvent> handle(PerformanceEvent pmEvent, ControlLoopSchemaType schemaType,
60            Map <String,List <KpiOperand>> measInfoMap, String measType, List < String > operands) {
61
62         final List < VesEvent > vesEvents = new LinkedList < > ();
63         if (operands.size() == 2) {
64             List <KpiOperand> k1 = measInfoMap.get(operands.get(0));
65             List <KpiOperand> k2 = measInfoMap.get(operands.get(1));
66             if (k1.size() != k2.size()) {
67                 return null;
68             }
69             ListIterator <KpiOperand> listIteratorK1 = k1.listIterator();
70             ListIterator <KpiOperand> listIteratorK2 = k2.listIterator();
71             while (listIteratorK1.hasNext()) {
72                 final KpiOperand myK1 = listIteratorK1.next();
73                 final KpiOperand myK2 = listIteratorK2.next();
74                 if (myK2.getValue().compareTo(BigDecimal.ZERO) != 0) {
75                     final BigDecimal result = myK1.getValue().multiply(new BigDecimal("100"))
76                         .divide(myK2.getValue(), 0, RoundingMode.HALF_UP);
77                     vesEvents.add(generateVesEvent(pmEvent, schemaType.toString(), result, measType));
78                 }
79             }
80         }
81         else {
82            throw new KpiComputationException("Insufficient number of operands to perform Ratio computation");
83         }
84         return vesEvents;
85     }
86 }
87