c2d052cb0980549b6eb0f961c40777e8eb497c51
[oam/nf-oam-adopter.git] / ves-nf-oam-adopter / ves-nf-oam-adopter-snmp-manager / src / main / java / org / o / ran / oam / nf / oam / adopter / snmp / 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.snmp.manager.mapper;
21
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;
32
33 /**
34  * Follows
35  * https://docs.onap.org/projects/onap-vnfrqts-requirements/en/latest/Chapter8/ves7_1spec.html#datatype-commoneventheader
36  */
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 CommonEventHeader header = new CommonEventHeader();
43         setMandatoryFields(header, peerAddress, vesMappingConfig, mappingConfiguration, pdu);
44         setOptionalFields(header, vesMappingConfig, mappingConfiguration, pdu, timeZone);
45         return header;
46     }
47
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 Variable uuid = pdu.getVariable(new OID(oidEntityId));
55             header.setReportingEntityId(uuid == null ? null : uuid.toString());
56         }
57         header.setNfNamingCode(null); //NOP
58         header.setNfcNamingCode(null); //NOP
59         if (timeZone != null) {
60             header.setTimeZoneOffset(timeZone);
61         }
62     }
63
64     private static void setMandatoryFields(final CommonEventHeader header, final UdpAddress peerAddress,
65             final VesMappingConfiguration vesMappingConfig, final TrapsMappingConfiguration mappingConfiguration,
66             final PDU pdu) {
67         header.setDomain(CommonEventHeader.Domain.FAULT);
68
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);
73         header.setEventName(
74                 CommonEventHeader.Domain.FAULT.name() + "_" + vesMappingConfig.getReportingEntityName() + "_"
75                         + mappingConfiguration.getName());
76
77         final String oidStartEpoch = mappingConfiguration.getEventStartEpochMicrosec();
78         if (oidStartEpoch != null) {
79             final Variable uuid = pdu.getVariable(new OID(oidStartEpoch));
80             header.setStartEpochMicrosec(uuid == null ? null : Long.valueOf(uuid.toString()));
81         } else {
82             header.setStartEpochMicrosec(System.currentTimeMillis());
83         }
84
85         final String oidLastEpoch = mappingConfiguration.getEventLastEpochMicrosec();
86         if (oidLastEpoch != null) {
87             final Variable uuid = pdu.getVariable(new OID(oidLastEpoch));
88             header.setLastEpochMicrosec(uuid == null ? null : Long.valueOf(uuid.toString()));
89         } else {
90             header.setLastEpochMicrosec(System.currentTimeMillis());
91         }
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 Variable sourceName = pdu.getVariable(new OID(oidSourceName));
99             header.setSourceName(sourceName.toString());
100         } else {
101             header.setSourceName(peerAddress.getInetAddress().getHostAddress());
102         }
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));
107     }
108
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);
115             }
116             final Optional<Variable> optValue = Optional.ofNullable(pdu.getVariable(new OID(eventSequenceOid)));
117             if (optValue.isPresent()) {
118                 return optValue.get().toLong();
119             }
120         }
121         return 0L;
122     }
123 }