kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 15 | package org.onosproject.provider.snmp.alarm.impl; |
| 16 | |
| 17 | import com.btisystems.mibbler.mibs.netsnmp.netsnmp.I_Device; |
| 18 | import com.btisystems.mibbler.mibs.netsnmp.netsnmp._OidRegistry; |
| 19 | import com.btisystems.mibbler.mibs.netsnmp.netsnmp.mib_2.interfaces.IfTable; |
| 20 | import com.btisystems.pronx.ems.core.model.ClassRegistry; |
| 21 | import com.btisystems.pronx.ems.core.model.IClassRegistry; |
| 22 | import com.btisystems.pronx.ems.core.model.NetworkDevice; |
| 23 | import com.btisystems.pronx.ems.core.snmp.ISnmpSession; |
| 24 | import java.io.IOException; |
| 25 | import java.util.Arrays; |
| 26 | import java.util.Collection; |
| 27 | import java.util.HashSet; |
| 28 | import java.util.Set; |
| 29 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
| 30 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId; |
| 31 | import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.slf4j.Logger; |
| 34 | import static org.slf4j.LoggerFactory.getLogger; |
| 35 | import org.snmp4j.smi.OID; |
| 36 | |
| 37 | /** |
| 38 | * Net SNMP specific implementation to provide a list of current alarms. |
| 39 | */ |
| 40 | public 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 | } |