2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.o.ran.oam.nf.oam.adopter.pm.rest.manager.mapper;
22 import java.nio.charset.StandardCharsets;
23 import java.util.List;
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;
34 @NoArgsConstructor(access = AccessLevel.PRIVATE)
35 final class CommonEventHeaderHandler {
36 private static final String PM_NOTIFICATIONS = "PM_Notification";
38 static CommonEventHeader toCommonEventHeader(final VesMappingConfiguration config, final String hostIp,
39 final CsvConfiguration csv, final Map<String, String> record, final int sequence) {
40 final CommonEventHeader header = new CommonEventHeader();
41 setMandatoryFields(config, hostIp, csv, header, record, sequence);
42 setOptionalFields(config, header);
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
54 private static void setMandatoryFields(final VesMappingConfiguration config, final String hostIp,
55 final CsvConfiguration csv, final CommonEventHeader header, final Map<String, String> record,
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(record::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));
76 final List<String> eventId = csv.getEventId();
77 final String keyIdConcat = eventId.stream()
78 .filter(record::containsKey)
80 .collect(Collectors.joining());
81 header.setEventId(UUID.nameUUIDFromBytes(keyIdConcat.getBytes(StandardCharsets.UTF_8)).toString());