blob: 9b743340b707d8e6e5794daf35346f6d3760fff3 [file] [log] [blame]
kmcpeakeb172d5f2015-12-10 11:30:43 +00001/*
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15package org.onosproject.provider.snmp.alarm.impl;
16
17import com.btisystems.mibbler.mibs.bti7000.bti7000_13_2_0.I_Device;
18import com.btisystems.mibbler.mibs.bti7000.bti7000_13_2_0._OidRegistry;
19import com.btisystems.mibbler.mibs.bti7000.bti7000_13_2_0.btisystems.btiproducts.bti7000.objects.conditions.ActAlarmTable;
20import com.btisystems.mibbler.mibs.bti7000.interfaces.btisystems.btiproducts.bti7000.objects.conditions.IActAlarmTable;
21import com.btisystems.pronx.ems.core.model.ClassRegistry;
22import com.btisystems.pronx.ems.core.model.IClassRegistry;
23import com.btisystems.pronx.ems.core.model.NetworkDevice;
24import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
25import java.io.IOException;
26import java.util.Arrays;
27import java.util.Calendar;
28import java.util.Collection;
29import java.util.Date;
30import java.util.GregorianCalendar;
31import java.util.HashSet;
32import java.util.Set;
33import java.util.TimeZone;
34import org.apache.commons.lang.StringUtils;
35import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
36import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId;
37import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
38import org.onosproject.net.DeviceId;
39import org.slf4j.Logger;
40import static org.slf4j.LoggerFactory.getLogger;
41import org.snmp4j.smi.OID;
42import org.snmp4j.smi.OctetString;
43
44/**
45 * BTI 7000 specific implementation to provide a list of current alarms.
46 */
47public class Bti7000SnmpAlarmProvider implements SnmpDeviceAlarmProvider {
48 private final Logger log = getLogger(getClass());
49 protected static final IClassRegistry CLASS_REGISTRY = new ClassRegistry(_OidRegistry.oidRegistry, I_Device.class);
50
51 static final int ALARM_SEVERITY_MINOR = 2;
52 static final int ALARM_SEVERITY_MAJOR = 3;
53 static final int ALARM_SEVERITY_CRITICAL = 4;
54
55 @Override
56 public Collection<Alarm> getAlarms(ISnmpSession session, DeviceId deviceID) {
57 log.info("Getting alarms for BTI 7000 device at {}", deviceID);
58 Set<Alarm> alarms = new HashSet<>();
59 NetworkDevice networkDevice = new NetworkDevice(CLASS_REGISTRY,
60 session.getAddress().getHostAddress());
61
62 try {
63 session.walkDevice(networkDevice, Arrays.asList(
64 new OID[]{CLASS_REGISTRY.getClassToOidMap().get(ActAlarmTable.class)}));
65
66 IActAlarmTable deviceAlarms = (IActAlarmTable) networkDevice.getRootObject()
67 .getEntity(CLASS_REGISTRY.getClassToOidMap().get(ActAlarmTable.class));
68 if ((deviceAlarms != null) && (deviceAlarms.getActAlarmEntry() != null)
69 && (!deviceAlarms.getActAlarmEntry().isEmpty())) {
70
71 deviceAlarms.getActAlarmEntry().values().stream().forEach((alarm) -> {
72 DefaultAlarm.Builder alarmBuilder = new DefaultAlarm.Builder(
73 deviceID, alarm.getActAlarmDescription(),
74 mapAlarmSeverity(alarm.getActAlarmSeverity()),
75 getLocalDateAndTime(alarm.getActAlarmDateAndTime(), null, null).getTime())
76 .forSource(AlarmEntityId.alarmEntityId("other:" + alarm.getActAlarmInstanceIdx()));
77 alarms.add(alarmBuilder.build());
78 });
79
80 }
81 log.info("Conditions retrieved: {}", deviceAlarms);
82
83 } catch (IOException ex) {
84 log.error("Error reading alarms for device {}.", deviceID, ex);
85 }
86
87 return alarms;
88 }
89
90 private Alarm.SeverityLevel mapAlarmSeverity(int intAlarmSeverity) {
91 Alarm.SeverityLevel mappedSeverity;
92 switch (intAlarmSeverity) {
93 case ALARM_SEVERITY_MINOR:
94 mappedSeverity = Alarm.SeverityLevel.MINOR;
95 break;
96 case ALARM_SEVERITY_MAJOR:
97 mappedSeverity = Alarm.SeverityLevel.MAJOR;
98 break;
99 case ALARM_SEVERITY_CRITICAL:
100 mappedSeverity = Alarm.SeverityLevel.CRITICAL;
101 break;
102 default:
103 mappedSeverity = Alarm.SeverityLevel.MINOR;
104 log.warn("Unexpected alarm severity: {}", intAlarmSeverity);
105 }
106 return mappedSeverity;
107 }
108 /**
109 * Converts an SNMP string representation into a {@link Date} object,
110 * and applies time zone conversion to provide the time on the local machine, ie PSM server.
111 *
112 * @param actAlarmDateAndTime MIB-II DateAndTime formatted. May optionally contain
113 * a timezone offset in 3 extra bytes
114 * @param sysInfoTimeZone Must be supplied if actAlarmDateAndTime is just local time (with no timezone)
115 * @param swVersion Must be supplied if actAlarmDateAndTime is just local time (with no timezone)
116 * @return adjusted {@link Date} or a simple conversion if other fields are null.
117 */
118 public static Date getLocalDateAndTime(String actAlarmDateAndTime, String sysInfoTimeZone,
119 String swVersion) {
120 if (StringUtils.isBlank(actAlarmDateAndTime)) {
121 return null;
122 }
123
124 GregorianCalendar decodedDateAndTimeCal = btiMakeCalendar(OctetString.fromHexString(actAlarmDateAndTime));
125 if ((sysInfoTimeZone == null) || (swVersion == null)) {
126 return decodedDateAndTimeCal.getTime();
127 }
128
129 TimeZone javaTimeZone = getTimeZone();
130 decodedDateAndTimeCal.setTimeZone(javaTimeZone);
131
132 GregorianCalendar localTime = new GregorianCalendar();
133 localTime.setTimeInMillis(decodedDateAndTimeCal.getTimeInMillis());
134
135 return localTime.getTime();
136 }
137
138 /**
139 * This method is similar to SNMP4J approach with some fixes for the 11-bytes version (ie the one with timezone
140 * offset).
141 *
142 * For original makeCalendar refer @see http://www.snmp4j.org/agent/doc/org/snmp4j/agent/mo/snmp/DateAndTime.html
143 *
144 * Creates a <code>GregorianCalendar</code> from a properly formatted SNMP4J DateAndTime <code>OctetString</code>.
145 *
146 * @param dateAndTimeValue an OctetString conforming to the DateAndTime TC.
147 * @return the corresponding <code>GregorianCalendar</code> instance.
148 *
149 */
150 public static GregorianCalendar btiMakeCalendar(OctetString dateAndTimeValue) {
151 int year = (dateAndTimeValue.get(0) & 0xFF) * 256
152 + (dateAndTimeValue.get(1) & 0xFF);
153 int month = (dateAndTimeValue.get(2) & 0xFF);
154 int date = (dateAndTimeValue.get(3) & 0xFF);
155 int hour = (dateAndTimeValue.get(4) & 0xFF);
156 int minute = (dateAndTimeValue.get(5) & 0xFF);
157 int second = (dateAndTimeValue.get(6) & 0xFF);
158 int deci = (dateAndTimeValue.get(7) & 0xFF);
159 GregorianCalendar gc =
160 new GregorianCalendar(year, month - 1, date, hour, minute, second);
161 gc.set(Calendar.MILLISECOND, deci * 100);
162
163 if (dateAndTimeValue.length() == 11) {
164 char directionOfOffset = (char) dateAndTimeValue.get(8);
165 int hoursOffset = directionOfOffset == '+'
166 ? dateAndTimeValue.get(9) : -dateAndTimeValue.get(9);
167 org.joda.time.DateTimeZone offset =
168 org.joda.time.DateTimeZone.forOffsetHoursMinutes(hoursOffset, dateAndTimeValue.get(10));
169 org.joda.time.DateTime dt =
170 new org.joda.time.DateTime(year, month, date, hour, minute, second, offset);
171 return dt.toGregorianCalendar();
172 }
173 return gc;
174 }
175
176 private static TimeZone getTimeZone() {
177 return Calendar.getInstance().getTimeZone();
178 }
179}