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