Fix Sonar complains
[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 / FaultFieldsHandler.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 static org.o.ran.oam.nf.oam.adopter.snmp.manager.mapper.SnmpMapperImpl.DEFAULT;
23
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Optional;
27 import java.util.stream.Collectors;
28 import lombok.AccessLevel;
29 import lombok.NoArgsConstructor;
30 import org.o.ran.oam.nf.oam.adopter.api.FaultFields;
31 import org.o.ran.oam.nf.oam.adopter.snmp.manager.pojos.TrapsMappingConfiguration;
32 import org.snmp4j.PDU;
33 import org.snmp4j.smi.OID;
34 import org.snmp4j.smi.VariableBinding;
35
36 @NoArgsConstructor(access = AccessLevel.PRIVATE)
37 final class FaultFieldsHandler {
38     private static final String SNMP_FAULT = "SNMP_Fault";
39     private static final String SNMP_UNKNOWN = "Unknown";
40
41     public static FaultFields toFaultFields(final TrapsMappingConfiguration trapsDescription, final PDU pdu) {
42         final var faultFields = new FaultFields();
43         setMandatoryFields(faultFields, trapsDescription, pdu);
44         setOptionalFields(faultFields, trapsDescription, pdu);
45         return faultFields;
46     }
47
48     private static void setMandatoryFields(final FaultFields faultFields,
49             final TrapsMappingConfiguration trapsDescription, final PDU pdu) {
50         faultFields.setAlarmCondition(trapsDescription.getName());
51         faultFields.setEventSeverity(FaultFields.EventSeverity.fromValue(trapsDescription.getOidEventSeverity()));
52         faultFields.setEventSourceType(Optional.ofNullable(trapsDescription.getEventSourceType()).orElse(SNMP_UNKNOWN));
53         faultFields.setFaultFieldsVersion(FaultFields.FaultFieldsVersion._4_0);
54         final String descOid = trapsDescription.getOidSpecificProblemDesc();
55         faultFields.setSpecificProblem(SNMP_FAULT);
56         if (descOid != null && !DEFAULT.equals(descOid)) {
57             final var desc = pdu.getVariable(new OID(descOid));
58             faultFields.setSpecificProblem(desc == null ? SNMP_FAULT : desc.toString());
59         }
60         faultFields.setVfStatus(FaultFields.VfStatus.ACTIVE);
61     }
62
63     private static void setOptionalFields(final FaultFields faultFields,
64             final TrapsMappingConfiguration trapsDescription, final PDU pdu) {
65         final List<? extends VariableBinding> variables = pdu.getVariableBindings();
66         final Map<String, String> map = variables.stream()
67                 .collect(Collectors.toMap(x -> x.getOid().toString(), x -> x.getVariable().toString()));
68
69         faultFields.setAlarmAdditionalInformation(map.isEmpty() ? null : map);
70         final String interfaceOid = trapsDescription.getOidAlarmInterfaceName();
71         if (interfaceOid != null) {
72             final var desc = pdu.getVariable(new OID(interfaceOid));
73             faultFields.setAlarmInterfaceA(desc == null ? SNMP_FAULT : desc.toString());
74         }
75         final String eCategory = trapsDescription.getEventCategory();
76         faultFields.setEventCategory(eCategory);
77     }
78 }