b2375ec7d6a47c23622d9d46cf33fd3bb1c0dbe1
[oam/nf-oam-adopter.git] / ves-nf-oam-adopter / ves-nf-oam-adopter-pm-manager / src / main / java / org / o / ran / oam / nf / oam / adopter / pm / rest / manager / mapper / CommonEventHeaderHandler.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  O-RAN-SC
4  *  ================================================================================
5  *  Copyright © 2021 AT&T Intellectual Property. All rights reserved.
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  *        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  *  ============LICENSE_END=========================================================
18  */
19
20 package org.o.ran.oam.nf.oam.adopter.pm.rest.manager.mapper;
21
22 import java.nio.charset.StandardCharsets;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Optional;
26 import java.util.UUID;
27 import java.util.stream.Collectors;
28 import lombok.AccessLevel;
29 import lombok.NoArgsConstructor;
30 import org.o.ran.oam.nf.oam.adopter.api.CommonEventHeader;
31 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.pojos.CsvConfiguration;
32 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.pojos.VesMappingConfiguration;
33
34 @NoArgsConstructor(access = AccessLevel.PRIVATE)
35 final class CommonEventHeaderHandler {
36     private static final String PM_NOTIFICATIONS = "PM_Notification";
37
38     static CommonEventHeader toCommonEventHeader(final VesMappingConfiguration config, final String hostIp,
39             final CsvConfiguration csv, final Map<String, String> recordMap, final int sequence) {
40         final CommonEventHeader header = new CommonEventHeader();
41         setMandatoryFields(config, hostIp, csv, header, recordMap, sequence);
42         setOptionalFields(config, header);
43         return header;
44     }
45
46     private static void setOptionalFields(final VesMappingConfiguration config, final CommonEventHeader header) {
47         header.setNfVendorName(Optional.ofNullable(config.getNfVendorName()).orElse(null));
48         header.setReportingEntityId(config.getReportingEntityId());
49         header.setNfNamingCode(null); //NOP
50         header.setNfcNamingCode(null); //NOP
51         header.setTimeZoneOffset(null); //NOP
52     }
53
54     private static void setMandatoryFields(final VesMappingConfiguration config, final String hostIp,
55             final CsvConfiguration csv, final CommonEventHeader header, final Map<String, String> recordMap,
56             final int sequence) {
57         header.setDomain(CommonEventHeader.Domain.MEASUREMENT);
58         header.setEventName(CommonEventHeader.Domain.FAULT.name()
59                 + "_" + config.getReportingEntityName()
60                 + "_" + Optional.ofNullable(config.getEventName()).orElse(PM_NOTIFICATIONS));
61         header.setStartEpochMicrosec(System.currentTimeMillis());
62         header.setLastEpochMicrosec(System.currentTimeMillis());
63         header.setPriority(CommonEventHeader.Priority.fromValue(config.getPriority()));
64         header.setReportingEntityName(config.getReportingEntityName());
65         header.setSequence((long) sequence);
66         final String sourceNameField = csv.getSourceName();
67         final String sourceNameRecordValue = Optional.ofNullable(sourceNameField).map(recordMap::get).orElse(hostIp);
68         final Optional<String> optRegex = Optional.ofNullable(csv.getSourceNameRegex());
69         header.setSourceName(optRegex.map(regex -> sourceNameRecordValue.replaceAll(regex, ""))
70             .orElse(sourceNameRecordValue));
71         header.setVersion(CommonEventHeader.Version._4_0);
72         header.setVesEventListenerVersion(Optional.ofNullable(config.getVesEventListenerVersion())
73                 .map(CommonEventHeader.VesEventListenerVersion::fromValue)
74                 .orElse(CommonEventHeader.VesEventListenerVersion._7_1));
75
76         final List<String> eventId = csv.getEventId();
77         final String keyIdConcat =  eventId.stream()
78                 .filter(recordMap::containsKey)
79                 .map(recordMap::get)
80                 .collect(Collectors.joining());
81         header.setEventId(UUID.nameUUIDFromBytes(keyIdConcat.getBytes(StandardCharsets.UTF_8)).toString());
82     }
83 }