Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.drivers.bti; |
| 18 | |
| 19 | import com.btisystems.mibbler.mibs.netsnmp.netsnmp.I_Device; |
| 20 | import com.btisystems.mibbler.mibs.netsnmp.netsnmp._OidRegistry; |
| 21 | import com.btisystems.mibbler.mibs.netsnmp.netsnmp.mib_2.interfaces.IfTable; |
| 22 | import com.btisystems.pronx.ems.core.model.ClassRegistry; |
| 23 | import com.btisystems.pronx.ems.core.model.IClassRegistry; |
| 24 | import com.btisystems.pronx.ems.core.model.NetworkDevice; |
| 25 | import com.btisystems.pronx.ems.core.snmp.ISnmpSession; |
| 26 | import com.google.common.collect.ImmutableList; |
| 27 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
| 28 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmConsumer; |
| 29 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId; |
| 30 | import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm; |
| 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 33 | import org.onosproject.snmp.SnmpController; |
| 34 | import org.slf4j.Logger; |
| 35 | |
| 36 | import java.io.IOException; |
| 37 | import java.util.ArrayList; |
| 38 | import java.util.Collections; |
| 39 | import java.util.List; |
| 40 | |
| 41 | import static com.google.common.base.Preconditions.checkNotNull; |
| 42 | import static org.slf4j.LoggerFactory.getLogger; |
| 43 | |
| 44 | /** |
| 45 | * Net SNMP specific implementation to provide a list of current alarms. |
| 46 | */ |
| 47 | public 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 SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame^] | 70 | interfaceTable.getEntries().values().forEach((ifEntry) -> { |
Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame] | 71 | 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 | } |