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.snmp.manager.mapper;
22 import java.util.Optional;
23 import lombok.AccessLevel;
24 import lombok.NoArgsConstructor;
25 import org.o.ran.oam.nf.oam.adopter.api.CommonEventHeader;
26 import org.o.ran.oam.nf.oam.adopter.snmp.manager.pojos.TrapsMappingConfiguration;
27 import org.o.ran.oam.nf.oam.adopter.snmp.manager.pojos.VesMappingConfiguration;
28 import org.snmp4j.PDU;
29 import org.snmp4j.smi.OID;
30 import org.snmp4j.smi.UdpAddress;
31 import org.snmp4j.smi.Variable;
35 * https://docs.onap.org/projects/onap-vnfrqts-requirements/en/latest/Chapter8/ves7_1spec.html#datatype-commoneventheader
37 @NoArgsConstructor(access = AccessLevel.PRIVATE)
38 final class CommonEventHeaderHandler {
39 static CommonEventHeader toCommonEventHeader(final UdpAddress peerAddress,
40 final VesMappingConfiguration vesMappingConfig, final TrapsMappingConfiguration mappingConfiguration,
41 final PDU pdu, final String timeZone) {
42 final var header = new CommonEventHeader();
43 setMandatoryFields(header, peerAddress, vesMappingConfig, mappingConfiguration, pdu);
44 setOptionalFields(header, vesMappingConfig, mappingConfiguration, pdu, timeZone);
48 private static void setOptionalFields(final CommonEventHeader header,
49 final VesMappingConfiguration vesMappingConfig, final TrapsMappingConfiguration mappingConfiguration,
50 final PDU pdu, final String timeZone) {
51 header.setNfVendorName(vesMappingConfig.getNfVendorName());
52 final String oidEntityId = mappingConfiguration.getOidReportingEntityID();
53 if (oidEntityId != null) {
54 final var uuid = pdu.getVariable(new OID(oidEntityId));
55 header.setReportingEntityId(uuid == null ? null : uuid.toString());
57 header.setNfNamingCode(null); //NOP
58 header.setNfcNamingCode(null); //NOP
59 if (timeZone != null) {
60 header.setTimeZoneOffset(timeZone);
64 private static void setMandatoryFields(final CommonEventHeader header, final UdpAddress peerAddress,
65 final VesMappingConfiguration vesMappingConfig, final TrapsMappingConfiguration mappingConfiguration,
67 header.setDomain(CommonEventHeader.Domain.FAULT);
69 final Optional<OID> eventIdOid = Optional.ofNullable(mappingConfiguration.getOidEventId()).map(OID::new);
70 final Optional<Variable> eventIdValue = eventIdOid.map(pdu::getVariable);
71 final String eventId = eventIdValue.map(Variable::toString).orElse(pdu.getRequestID().toString());
72 header.setEventId(eventId);
74 CommonEventHeader.Domain.FAULT.name() + "_" + vesMappingConfig.getReportingEntityName() + "_"
75 + mappingConfiguration.getName());
77 final String oidStartEpoch = mappingConfiguration.getEventStartEpochMicrosec();
78 if (oidStartEpoch != null) {
79 final var uuid = pdu.getVariable(new OID(oidStartEpoch));
80 header.setStartEpochMicrosec(uuid == null ? null : Long.valueOf(uuid.toString()));
82 header.setStartEpochMicrosec(System.currentTimeMillis());
85 final String oidLastEpoch = mappingConfiguration.getEventLastEpochMicrosec();
86 if (oidLastEpoch != null) {
87 final var uuid = pdu.getVariable(new OID(oidLastEpoch));
88 header.setLastEpochMicrosec(uuid == null ? null : Long.valueOf(uuid.toString()));
90 header.setLastEpochMicrosec(System.currentTimeMillis());
92 header.setPriority(CommonEventHeader.Priority.HIGH);
93 header.setReportingEntityName(vesMappingConfig.getReportingEntityName());
94 header.setReportingEntityId(vesMappingConfig.getReportingEntityId());
95 header.setSequence(extractEventSequence(mappingConfiguration, pdu));
96 final String oidSourceName = mappingConfiguration.getOidSourceName();
97 if (oidSourceName != null) {
98 final var sourceName = pdu.getVariable(new OID(oidSourceName));
99 header.setSourceName(sourceName.toString());
101 header.setSourceName(peerAddress.getInetAddress().getHostAddress());
103 header.setVersion(CommonEventHeader.Version._4_0);
104 header.setVesEventListenerVersion(Optional.ofNullable(vesMappingConfig.getVesEventListenerVersion())
105 .map(CommonEventHeader.VesEventListenerVersion::fromValue)
106 .orElse(CommonEventHeader.VesEventListenerVersion._7_1));
109 private static long extractEventSequence(final TrapsMappingConfiguration mappingConfiguration, final PDU pdu) {
110 final Optional<String> optEventSequenceOid = Optional.ofNullable(mappingConfiguration.getOidEventSequence());
111 if (optEventSequenceOid.isPresent()) {
112 final String eventSequenceOid = optEventSequenceOid.get();
113 if (!eventSequenceOid.contains(".")) {
114 return Long.parseLong(eventSequenceOid);
116 final Optional<Variable> optValue = Optional.ofNullable(pdu.getVariable(new OID(eventSequenceOid)));
117 if (optValue.isPresent()) {
118 return optValue.get().toLong();