blob: 03b2ba2ac81d80e3fb3891c0ce7b1b94781ad48b [file] [log] [blame]
Andrea Campanellae72ac552016-04-11 10:04:52 -07001/*
2 * Copyright 2016 Open Networking Laboratory
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
17package org.onosproject.drivers.bti;
18
19import com.btisystems.mibbler.mibs.netsnmp.netsnmp.I_Device;
20import com.btisystems.mibbler.mibs.netsnmp.netsnmp._OidRegistry;
21import com.btisystems.mibbler.mibs.netsnmp.netsnmp.mib_2.interfaces.IfTable;
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 com.google.common.collect.ImmutableList;
27import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
28import org.onosproject.incubator.net.faultmanagement.alarm.AlarmConsumer;
29import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId;
30import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
31import org.onosproject.net.DeviceId;
32import org.onosproject.net.driver.AbstractHandlerBehaviour;
33import org.onosproject.snmp.SnmpController;
34import org.slf4j.Logger;
35
36import java.io.IOException;
37import java.util.ArrayList;
38import java.util.Collections;
39import java.util.List;
40
41import static com.google.common.base.Preconditions.checkNotNull;
42import static org.slf4j.LoggerFactory.getLogger;
43
44/**
45 * Net SNMP specific implementation to provide a list of current alarms.
46 */
47public class NetSnmpAlarmConsumer extends AbstractHandlerBehaviour implements AlarmConsumer {
48 private final Logger log = getLogger(getClass());
49 protected static final IClassRegistry CLASS_REGISTRY =
50 new ClassRegistry(_OidRegistry.oidRegistry, I_Device.class);
51
52 @Override
53 public List<Alarm> consumeAlarms() {
54 SnmpController controller = checkNotNull(handler().get(SnmpController.class));
55 List<Alarm> alarms = new ArrayList<>();
56 ISnmpSession session;
57 DeviceId deviceId = handler().data().deviceId();
58 try {
59 session = controller.getSession(deviceId);
60
61 NetworkDevice networkDevice = new NetworkDevice(CLASS_REGISTRY,
62 session.getAddress()
63 .getHostAddress());
64 session.walkDevice(networkDevice, Collections.singletonList(
65 CLASS_REGISTRY.getClassToOidMap().get(IfTable.class)));
66
67 IfTable interfaceTable = (IfTable) networkDevice.getRootObject()
68 .getEntity(CLASS_REGISTRY.getClassToOidMap().get(IfTable.class));
69 if (interfaceTable != null) {
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -070070 interfaceTable.getEntries().values().forEach((ifEntry) -> {
Andrea Campanellae72ac552016-04-11 10:04:52 -070071 if (ifEntry.getIfAdminStatus() == 1 && ifEntry.getIfOperStatus() == 2) {
72 alarms.add(new DefaultAlarm.Builder(deviceId, "Link Down.",
73 Alarm.SeverityLevel.CRITICAL,
74 System.currentTimeMillis())
75 .forSource(AlarmEntityId
76 .alarmEntityId("port:" + ifEntry.
77 getIfDescr())).build());
78 }
79 log.debug("Interface: " + ifEntry);
80 });
81 }
82 } catch (IOException ex) {
83 log.error("Error reading alarms for device {}.", deviceId, ex);
84 alarms.add(controller.buildWalkFailedAlarm(deviceId));
85 }
86 return ImmutableList.copyOf(alarms);
87 }
88}