Laszlo Papp | 85f0ace | 2018-06-15 13:09:20 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.drivers.polatis.snmp; |
| 18 | |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame^] | 21 | import org.onosproject.alarm.Alarm; |
| 22 | import org.onosproject.alarm.AlarmId; |
| 23 | import org.onosproject.alarm.DefaultAlarm; |
| 24 | import org.onosproject.alarm.DeviceAlarmConfig; |
Laszlo Papp | 85f0ace | 2018-06-15 13:09:20 +0100 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 27 | |
| 28 | import org.snmp4j.CommandResponderEvent; |
| 29 | import org.snmp4j.PDU; |
| 30 | import org.snmp4j.smi.OID; |
| 31 | |
| 32 | import org.slf4j.Logger; |
| 33 | |
| 34 | import java.util.HashSet; |
| 35 | import java.util.List; |
| 36 | import java.util.Set; |
| 37 | |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame^] | 38 | import static org.onosproject.alarm.Alarm.SeverityLevel; |
Laszlo Papp | 85f0ace | 2018-06-15 13:09:20 +0100 | [diff] [blame] | 39 | import static org.slf4j.LoggerFactory.getLogger; |
| 40 | |
| 41 | /** |
| 42 | * Polatis specific implementation to provide asynchronous alarms via SNMP. |
| 43 | */ |
| 44 | public class PolatisAlarmConfig extends AbstractHandlerBehaviour implements DeviceAlarmConfig { |
| 45 | private final Logger log = getLogger(getClass()); |
| 46 | |
| 47 | private DeviceId deviceId; |
| 48 | private static final OID SNMP_TRAP_OID = new OID(".1.3.6.1.6.3.1.1.4.1"); |
| 49 | private static final OID OPM_ALARM_OID = new OID(".1.3.6.1.4.1.26592.2.3.3.0.3"); |
| 50 | private static final OID ALARM_STATUS_OID = new OID(".1.3.6.1.4.1.26592.2.6.2.3.3"); |
| 51 | private static final OID ALARM_ID_OID = new OID(".1.3.6.1.4.1.26592.2.6.2.3.2"); |
| 52 | private static final OID ALARM_PORT_OID = new OID(".1.3.6.1.4.1.26592.2.3.3.1.1"); |
| 53 | private static final OID ALARM_PORT_LABEL_OID = new OID(".1.3.6.1.4.1.26592.2.3.3.1.2"); |
| 54 | private static final OID SYSUPTIME_OID = new OID(".1.3.6.1.2.1.1.3"); |
| 55 | private static final String CLEARED = "cleared"; |
| 56 | |
| 57 | @Override |
| 58 | public boolean configureDevice(IpAddress address, int port, String protocol) { |
| 59 | // TODO: Implement me |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public <T> Set<Alarm> translateAlarms(List<T> unparsedAlarms) { |
| 65 | deviceId = handler().data().deviceId(); |
| 66 | Set<Alarm> alarms = new HashSet<>(); |
| 67 | for (T alarm : unparsedAlarms) { |
| 68 | if (alarm instanceof CommandResponderEvent) { |
| 69 | CommandResponderEvent alarmEvent = (CommandResponderEvent) alarm; |
| 70 | PDU pdu = alarmEvent.getPDU(); |
| 71 | if (pdu != null) { |
| 72 | String alarmType = pdu.getVariable(SNMP_TRAP_OID).toString(); |
| 73 | if (alarmType.equals(OPM_ALARM_OID.toString())) { |
| 74 | String label = pdu.getVariable(ALARM_PORT_LABEL_OID).toString(); |
| 75 | int port = pdu.getVariable(ALARM_PORT_OID).toInt(); |
| 76 | String uniqueIdentifier = "LOS" + port; |
| 77 | String status = pdu.getVariable(ALARM_STATUS_OID).toString(); |
| 78 | String alarmMessage = "Loss of Service alarm " + status + " for fibre " + port; |
| 79 | SeverityLevel alarmLevel = SeverityLevel.MAJOR; |
| 80 | long timeRaised = 0; |
| 81 | DefaultAlarm.Builder alarmBuilder = new DefaultAlarm.Builder( |
| 82 | AlarmId.alarmId(deviceId, uniqueIdentifier), |
| 83 | deviceId, alarmMessage, alarmLevel, timeRaised); |
| 84 | if (status.equals(CLEARED)) { |
| 85 | long now = System.currentTimeMillis(); |
| 86 | alarmBuilder.clear().withTimeUpdated(now).withTimeCleared(now); |
| 87 | } |
| 88 | alarms.add(alarmBuilder.build()); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | return alarms; |
| 94 | } |
| 95 | } |