kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | 7cbbbb7 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 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 | * |
Brian O'Connor | 7cbbbb7 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 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 | */ |
Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame] | 16 | |
| 17 | package org.onosproject.drivers.bti; |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 18 | |
| 19 | import com.btisystems.mibbler.mibs.bti7000.bti7000_13_2_0.I_Device; |
| 20 | import com.btisystems.mibbler.mibs.bti7000.bti7000_13_2_0._OidRegistry; |
| 21 | import com.btisystems.mibbler.mibs.bti7000.bti7000_13_2_0.btisystems.btiproducts.bti7000.objects.conditions.ActAlarmTable; |
| 22 | import com.btisystems.mibbler.mibs.bti7000.interfaces.btisystems.btiproducts.bti7000.objects.conditions.IActAlarmTable; |
| 23 | import com.btisystems.pronx.ems.core.model.ClassRegistry; |
| 24 | import com.btisystems.pronx.ems.core.model.IClassRegistry; |
| 25 | import com.btisystems.pronx.ems.core.model.NetworkDevice; |
| 26 | import com.btisystems.pronx.ems.core.snmp.ISnmpSession; |
Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame] | 27 | import com.google.common.collect.ImmutableList; |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 28 | import org.apache.commons.lang.StringUtils; |
| 29 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame] | 30 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmConsumer; |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 31 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId; |
| 32 | import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm; |
| 33 | import org.onosproject.net.DeviceId; |
Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame] | 34 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 35 | import org.onosproject.snmp.SnmpController; |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 36 | import org.slf4j.Logger; |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 37 | import org.snmp4j.smi.OctetString; |
| 38 | |
Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame] | 39 | import java.io.IOException; |
| 40 | import java.util.ArrayList; |
| 41 | import java.util.Calendar; |
| 42 | import java.util.Collections; |
| 43 | import java.util.Date; |
| 44 | import java.util.GregorianCalendar; |
| 45 | import java.util.List; |
| 46 | import java.util.TimeZone; |
| 47 | |
| 48 | import static com.google.common.base.Preconditions.checkNotNull; |
| 49 | import static org.slf4j.LoggerFactory.getLogger; |
| 50 | |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 51 | /** |
| 52 | * BTI 7000 specific implementation to provide a list of current alarms. |
| 53 | */ |
Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame] | 54 | public class Bti7000SnmpAlarmConsumer extends AbstractHandlerBehaviour implements AlarmConsumer { |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 55 | private final Logger log = getLogger(getClass()); |
| 56 | protected static final IClassRegistry CLASS_REGISTRY = new ClassRegistry(_OidRegistry.oidRegistry, I_Device.class); |
| 57 | |
| 58 | static final int ALARM_SEVERITY_MINOR = 2; |
| 59 | static final int ALARM_SEVERITY_MAJOR = 3; |
| 60 | static final int ALARM_SEVERITY_CRITICAL = 4; |
| 61 | |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 62 | |
| 63 | private Alarm.SeverityLevel mapAlarmSeverity(int intAlarmSeverity) { |
| 64 | Alarm.SeverityLevel mappedSeverity; |
| 65 | switch (intAlarmSeverity) { |
| 66 | case ALARM_SEVERITY_MINOR: |
| 67 | mappedSeverity = Alarm.SeverityLevel.MINOR; |
| 68 | break; |
| 69 | case ALARM_SEVERITY_MAJOR: |
| 70 | mappedSeverity = Alarm.SeverityLevel.MAJOR; |
| 71 | break; |
| 72 | case ALARM_SEVERITY_CRITICAL: |
| 73 | mappedSeverity = Alarm.SeverityLevel.CRITICAL; |
| 74 | break; |
| 75 | default: |
| 76 | mappedSeverity = Alarm.SeverityLevel.MINOR; |
| 77 | log.warn("Unexpected alarm severity: {}", intAlarmSeverity); |
| 78 | } |
| 79 | return mappedSeverity; |
| 80 | } |
Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame] | 81 | |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 82 | /** |
| 83 | * Converts an SNMP string representation into a {@link Date} object, |
| 84 | * and applies time zone conversion to provide the time on the local machine, ie PSM server. |
| 85 | * |
| 86 | * @param actAlarmDateAndTime MIB-II DateAndTime formatted. May optionally contain |
Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame] | 87 | * a timezone offset in 3 extra bytes |
| 88 | * @param sysInfoTimeZone Must be supplied if actAlarmDateAndTime is just local time (with no timezone) |
| 89 | * @param swVersion Must be supplied if actAlarmDateAndTime is just local time (with no timezone) |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 90 | * @return adjusted {@link Date} or a simple conversion if other fields are null. |
| 91 | */ |
| 92 | public static Date getLocalDateAndTime(String actAlarmDateAndTime, String sysInfoTimeZone, |
Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame] | 93 | String swVersion) { |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 94 | if (StringUtils.isBlank(actAlarmDateAndTime)) { |
| 95 | return null; |
| 96 | } |
| 97 | |
| 98 | GregorianCalendar decodedDateAndTimeCal = btiMakeCalendar(OctetString.fromHexString(actAlarmDateAndTime)); |
| 99 | if ((sysInfoTimeZone == null) || (swVersion == null)) { |
| 100 | return decodedDateAndTimeCal.getTime(); |
| 101 | } |
| 102 | |
| 103 | TimeZone javaTimeZone = getTimeZone(); |
| 104 | decodedDateAndTimeCal.setTimeZone(javaTimeZone); |
| 105 | |
| 106 | GregorianCalendar localTime = new GregorianCalendar(); |
| 107 | localTime.setTimeInMillis(decodedDateAndTimeCal.getTimeInMillis()); |
| 108 | |
| 109 | return localTime.getTime(); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * This method is similar to SNMP4J approach with some fixes for the 11-bytes version (ie the one with timezone |
| 114 | * offset). |
Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame] | 115 | * <p> |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 116 | * For original makeCalendar refer @see http://www.snmp4j.org/agent/doc/org/snmp4j/agent/mo/snmp/DateAndTime.html |
Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame] | 117 | * <p> |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 118 | * Creates a <code>GregorianCalendar</code> from a properly formatted SNMP4J DateAndTime <code>OctetString</code>. |
| 119 | * |
| 120 | * @param dateAndTimeValue an OctetString conforming to the DateAndTime TC. |
| 121 | * @return the corresponding <code>GregorianCalendar</code> instance. |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 122 | */ |
| 123 | public static GregorianCalendar btiMakeCalendar(OctetString dateAndTimeValue) { |
| 124 | int year = (dateAndTimeValue.get(0) & 0xFF) * 256 |
| 125 | + (dateAndTimeValue.get(1) & 0xFF); |
| 126 | int month = (dateAndTimeValue.get(2) & 0xFF); |
| 127 | int date = (dateAndTimeValue.get(3) & 0xFF); |
| 128 | int hour = (dateAndTimeValue.get(4) & 0xFF); |
| 129 | int minute = (dateAndTimeValue.get(5) & 0xFF); |
| 130 | int second = (dateAndTimeValue.get(6) & 0xFF); |
| 131 | int deci = (dateAndTimeValue.get(7) & 0xFF); |
| 132 | GregorianCalendar gc = |
| 133 | new GregorianCalendar(year, month - 1, date, hour, minute, second); |
| 134 | gc.set(Calendar.MILLISECOND, deci * 100); |
| 135 | |
| 136 | if (dateAndTimeValue.length() == 11) { |
| 137 | char directionOfOffset = (char) dateAndTimeValue.get(8); |
| 138 | int hoursOffset = directionOfOffset == '+' |
| 139 | ? dateAndTimeValue.get(9) : -dateAndTimeValue.get(9); |
| 140 | org.joda.time.DateTimeZone offset = |
| 141 | org.joda.time.DateTimeZone.forOffsetHoursMinutes(hoursOffset, dateAndTimeValue.get(10)); |
| 142 | org.joda.time.DateTime dt = |
| 143 | new org.joda.time.DateTime(year, month, date, hour, minute, second, offset); |
| 144 | return dt.toGregorianCalendar(); |
| 145 | } |
| 146 | return gc; |
| 147 | } |
| 148 | |
| 149 | private static TimeZone getTimeZone() { |
| 150 | return Calendar.getInstance().getTimeZone(); |
| 151 | } |
Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame] | 152 | |
| 153 | @Override |
| 154 | public List<Alarm> consumeAlarms() { |
| 155 | SnmpController controller = checkNotNull(handler().get(SnmpController.class)); |
| 156 | ISnmpSession session; |
| 157 | List<Alarm> alarms = new ArrayList<>(); |
| 158 | DeviceId deviceId = handler().data().deviceId(); |
| 159 | try { |
| 160 | session = controller.getSession(deviceId); |
| 161 | log.debug("Getting alarms for BTI 7000 device at {}", deviceId); |
| 162 | NetworkDevice networkDevice = new NetworkDevice(CLASS_REGISTRY, |
| 163 | session.getAddress().getHostAddress()); |
| 164 | session.walkDevice(networkDevice, Collections.singletonList( |
| 165 | CLASS_REGISTRY.getClassToOidMap().get(ActAlarmTable.class))); |
| 166 | |
| 167 | IActAlarmTable deviceAlarms = (IActAlarmTable) networkDevice.getRootObject() |
| 168 | .getEntity(CLASS_REGISTRY.getClassToOidMap().get(ActAlarmTable.class)); |
| 169 | if ((deviceAlarms != null) && (deviceAlarms.getActAlarmEntry() != null) |
| 170 | && (!deviceAlarms.getActAlarmEntry().isEmpty())) { |
| 171 | |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 172 | deviceAlarms.getActAlarmEntry().values().forEach((alarm) -> { |
Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame] | 173 | DefaultAlarm.Builder alarmBuilder = new DefaultAlarm.Builder( |
| 174 | deviceId, alarm.getActAlarmDescription(), |
| 175 | mapAlarmSeverity(alarm.getActAlarmSeverity()), |
| 176 | getLocalDateAndTime(alarm.getActAlarmDateAndTime(), null, null).getTime()) |
| 177 | .forSource(AlarmEntityId.alarmEntityId("other:" + alarm.getActAlarmInstanceIdx())); |
| 178 | alarms.add(alarmBuilder.build()); |
| 179 | }); |
| 180 | |
| 181 | } |
| 182 | log.debug("Conditions retrieved: {}", deviceAlarms); |
| 183 | |
| 184 | } catch (IOException ex) { |
| 185 | log.error("Error reading alarms for device {}.", deviceId, ex); |
| 186 | alarms.add(controller.buildWalkFailedAlarm(deviceId)); |
| 187 | |
| 188 | } |
| 189 | |
| 190 | return ImmutableList.copyOf(alarms); |
| 191 | } |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 192 | } |