0fe1c4b63cc39eabbefd5acc9335ecde956d8d61
[dcaegen2/services.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020-2021 China Mobile.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dcaegen2.kpi.computation;
22
23 import java.math.BigDecimal;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.UUID;
28
29 import org.onap.dcaegen2.kpi.config.ControlLoopSchemaType;
30 import org.onap.dcaegen2.kpi.models.CommonEventHeader;
31 import org.onap.dcaegen2.kpi.models.MeasDataCollection;
32 import org.onap.dcaegen2.kpi.models.MeasInfo;
33 import org.onap.dcaegen2.kpi.models.MeasInfoId;
34 import org.onap.dcaegen2.kpi.models.MeasResult;
35 import org.onap.dcaegen2.kpi.models.MeasTypes;
36 import org.onap.dcaegen2.kpi.models.MeasValues;
37 import org.onap.dcaegen2.kpi.models.Perf3gppFields;
38 import org.onap.dcaegen2.kpi.models.PerformanceEvent;
39 import org.onap.dcaegen2.kpi.models.VesEvent;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * SumKpiComputation.
45  *
46  * @author Kai Lu
47  */
48 public class SumKpiComputation implements Command {
49
50     private static Logger logger = LoggerFactory.getLogger(SumKpiComputation.class);
51
52     @Override
53     public VesEvent handle(PerformanceEvent pmEvent, ControlLoopSchemaType schemaType,
54             Map<String, List<BigDecimal>> measInfoMap, String measType) {
55
56         return generateSumVesEvent(pmEvent, schemaType, measInfoMap, measType);
57     }
58
59     private VesEvent generateSumVesEvent(PerformanceEvent pmEvent, ControlLoopSchemaType schemaType,
60             Map<String, List<BigDecimal>> measInfoMap, String measType) {
61
62         // Create ves kpi data
63         CommonEventHeader commonEventHeader = new CommonEventHeader();
64         commonEventHeader.setDomain(pmEvent.getCommonEventHeader().getDomain());
65         commonEventHeader.setEventId(UUID.randomUUID().toString());
66         commonEventHeader.setSequence(0);
67         commonEventHeader.setEventName(pmEvent.getCommonEventHeader().getEventName());
68         commonEventHeader.setSourceName(pmEvent.getCommonEventHeader().getSourceName());
69         commonEventHeader.setReportingEntityName(pmEvent.getCommonEventHeader().getReportingEntityName());
70         commonEventHeader.setPriority(pmEvent.getCommonEventHeader().getPriority());
71         commonEventHeader.setStartEpochMicrosec(pmEvent.getCommonEventHeader().getStartEpochMicrosec());
72         commonEventHeader.setLastEpochMicrosec(pmEvent.getCommonEventHeader().getLastEpochMicrosec());
73         commonEventHeader.setVersion(pmEvent.getCommonEventHeader().getVersion());
74         commonEventHeader.setVesEventListenerVersion(pmEvent.getCommonEventHeader().getVesEventListenerVersion());
75         commonEventHeader.setTimeZoneOffset(pmEvent.getCommonEventHeader().getTimeZoneOffset());
76         Perf3gppFields perf3gppFields = new Perf3gppFields();
77         perf3gppFields.setPerf3gppFieldsVersion(pmEvent.getPerf3gppFields().getPerf3gppFieldsVersion());
78         MeasDataCollection tmpMeasDataCollection = new MeasDataCollection();
79         tmpMeasDataCollection
80                 .setGranularityPeriod(pmEvent.getPerf3gppFields().getMeasDataCollection().getGranularityPeriod());
81         tmpMeasDataCollection.setMeasuredEntityUserName(
82                 pmEvent.getPerf3gppFields().getMeasDataCollection().getMeasuredEntityUserName());
83         tmpMeasDataCollection
84                 .setMeasuredEntityDn(pmEvent.getPerf3gppFields().getMeasDataCollection().getMeasuredEntityDn());
85         tmpMeasDataCollection.setMeasuredEntitySoftwareVersion(
86                 pmEvent.getPerf3gppFields().getMeasDataCollection().getMeasuredEntitySoftwareVersion());
87         MeasInfoId measInfoId = new MeasInfoId();
88         measInfoId.setMeasInfoId(schemaType.toString());
89         MeasTypes measTypes = new MeasTypes();
90         List<String> measTypesList = new ArrayList<>();
91         String keyOperands = measInfoMap.keySet().stream().findAny().get();
92         measTypesList.add(new StringBuilder().append(measType).append(keyOperands).toString());
93         measTypes.setMeasTypesList(measTypesList);
94         MeasValues measValue = new MeasValues();
95         measValue.setSuspectFlag(false);
96         List<MeasResult> measResults = new ArrayList<>();
97         MeasResult measureMent = new MeasResult();
98         List<BigDecimal> kpiOperands = measInfoMap.values().stream().findAny().get();
99         BigDecimal result = kpiOperands.stream().map(i -> i).reduce(BigDecimal.ZERO, BigDecimal::add);
100
101         measureMent.setPvalue(1);
102         measureMent.setSvalue(result.toString());
103         measResults.add(measureMent);
104         MeasInfo measInfo = new MeasInfo();
105         measValue.setMeasResults(measResults);
106         List<MeasValues> measValuesList = new ArrayList<>();
107         measValuesList.add(measValue);
108         measInfo.setMeasInfoId(measInfoId);
109         measInfo.setMeasTypes(measTypes);
110         measInfo.setMeasValuesList(measValuesList);
111         List<MeasInfo> measInfoList = new ArrayList<>();
112         measInfoList.add(measInfo);
113         tmpMeasDataCollection.setMeasInfoList(measInfoList);
114         perf3gppFields.setMeasDataCollection(tmpMeasDataCollection);
115         VesEvent kpiVesEvent = new VesEvent();
116         PerformanceEvent kpiEvent = new PerformanceEvent();
117         kpiEvent.setCommonEventHeader(commonEventHeader);
118         kpiEvent.setPerf3gppFields(perf3gppFields);
119         kpiVesEvent.setEvent(kpiEvent);
120         logger.info("kpiVesEvent: {}", kpiVesEvent);
121         return kpiVesEvent;
122     }
123
124 }