blob: 9940d6e2e2df0c5e5d4e68820ce4be4e5451f6b0 [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.netsnmp.netsnmp.I_Device;
18import com.btisystems.mibbler.mibs.netsnmp.netsnmp._OidRegistry;
19import com.btisystems.mibbler.mibs.netsnmp.netsnmp.mib_2.interfaces.IfTable;
20import com.btisystems.pronx.ems.core.model.ClassRegistry;
21import com.btisystems.pronx.ems.core.model.IClassRegistry;
22import com.btisystems.pronx.ems.core.model.NetworkDevice;
23import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
24import java.io.IOException;
25import java.util.Arrays;
26import java.util.Collection;
27import java.util.HashSet;
28import java.util.Set;
29import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
30import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId;
31import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
32import org.onosproject.net.DeviceId;
33import org.slf4j.Logger;
34import static org.slf4j.LoggerFactory.getLogger;
35import org.snmp4j.smi.OID;
36
37/**
38 * Net SNMP specific implementation to provide a list of current alarms.
39 */
40public class NetSnmpAlarmProvider implements SnmpDeviceAlarmProvider {
41 private final Logger log = getLogger(getClass());
42 protected static final IClassRegistry CLASS_REGISTRY =
43 new ClassRegistry(_OidRegistry.oidRegistry, I_Device.class);
44 @Override
45 public Collection<Alarm> getAlarms(ISnmpSession session, DeviceId deviceId) {
46 Set<Alarm> alarms = new HashSet<>();
47
48 NetworkDevice networkDevice = new NetworkDevice(CLASS_REGISTRY,
49 session.getAddress().getHostAddress());
50 try {
51 session.walkDevice(networkDevice, Arrays.asList(new OID[]{
52 CLASS_REGISTRY.getClassToOidMap().get(IfTable.class)}));
53
54 IfTable interfaceTable = (IfTable) networkDevice.getRootObject()
55 .getEntity(CLASS_REGISTRY.getClassToOidMap().get(IfTable.class));
56 if (interfaceTable != null) {
57 interfaceTable.getEntries().values().stream().forEach((ifEntry) -> {
58 //TODO will raise alarm for each interface as a demo.
59 // if (ifEntry.getIfAdminStatus() == 1 && ifEntry.getIfOperStatus() == 2){
60 alarms.add(new DefaultAlarm.Builder(deviceId, "Link Down.",
61 Alarm.SeverityLevel.CRITICAL, System.currentTimeMillis())
62 .forSource(AlarmEntityId.alarmEntityId("port:" + ifEntry.getIfDescr())).build());
63 // }
64 log.info("Interface: " + ifEntry);
65 });
66 }
67 } catch (IOException ex) {
68 log.error("Error reading alarms for device {}.", deviceId, ex);
69 }
70 return alarms;
71 }
72}