kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | * you may not use this file except in compliance with the License. |
| 4 | * You may obtain a copy of the License at |
| 5 | * |
| 6 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | * |
| 8 | * Unless required by applicable law or agreed to in writing, software |
| 9 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | * See the License for the specific language governing permissions and |
| 12 | * limitations under the License. |
| 13 | */ |
| 14 | package org.onosproject.provider.snmp.alarm.impl; |
| 15 | |
| 16 | import com.btisystems.mibbler.mibs.netsnmp.interfaces.mib_2.interfaces.iftable.IIfEntry; |
| 17 | import com.btisystems.mibbler.mibs.netsnmp.netsnmp.mib_2.interfaces.IfTable; |
| 18 | import com.btisystems.pronx.ems.core.model.NetworkDevice; |
| 19 | import com.btisystems.pronx.ems.core.snmp.ISnmpSession; |
| 20 | import java.io.IOException; |
| 21 | import java.net.InetAddress; |
| 22 | import java.net.UnknownHostException; |
| 23 | import java.util.Arrays; |
| 24 | import java.util.Collection; |
| 25 | import org.easymock.Capture; |
| 26 | import static org.easymock.EasyMock.capture; |
| 27 | import static org.easymock.EasyMock.createMock; |
| 28 | import static org.easymock.EasyMock.eq; |
| 29 | import static org.easymock.EasyMock.expect; |
| 30 | import static org.easymock.EasyMock.isA; |
| 31 | import static org.easymock.EasyMock.replay; |
| 32 | import static org.easymock.EasyMock.verify; |
| 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
| 35 | import static org.junit.Assert.*; |
| 36 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
| 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.snmp4j.smi.OID; |
| 39 | |
| 40 | |
| 41 | public class NetSnmpSnmpAlarmProviderTest { |
| 42 | private NetSnmpAlarmProvider alarmProvider; |
| 43 | private ISnmpSession mockSession; |
| 44 | private IfTable interfaceTable; |
| 45 | |
| 46 | public NetSnmpSnmpAlarmProviderTest() { |
| 47 | } |
| 48 | |
| 49 | @Before |
| 50 | public void setUp() { |
| 51 | mockSession = createMock(ISnmpSession.class); |
| 52 | |
| 53 | alarmProvider = new NetSnmpAlarmProvider(); |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | public void shouldWalkDevice() throws UnknownHostException, IOException { |
| 58 | expect(mockSession.getAddress()).andReturn(InetAddress.getLoopbackAddress()); |
| 59 | expect(mockSession.walkDevice(isA(NetworkDevice.class), |
| 60 | eq(Arrays.asList(new OID[]{ |
| 61 | NetSnmpAlarmProvider.CLASS_REGISTRY.getClassToOidMap().get(IfTable.class)})))) |
| 62 | .andReturn(null); |
| 63 | |
| 64 | replay(mockSession); |
| 65 | |
| 66 | assertNotNull(alarmProvider.getAlarms(mockSession, DeviceId.deviceId("snmp:1.1.1.1"))); |
| 67 | |
| 68 | verify(mockSession); |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void shouldFindAlarms() throws UnknownHostException, IOException { |
| 73 | interfaceTable = new IfTable(); |
| 74 | interfaceTable.createEntry("1"); |
| 75 | IIfEntry entry = interfaceTable.getEntry("1"); |
| 76 | entry.setIfDescr("eth1"); |
| 77 | entry.setIfAdminStatus(1); |
| 78 | entry.setIfOperStatus(2); |
| 79 | |
| 80 | Capture<NetworkDevice> networkDeviceCapture = new Capture<>(); |
| 81 | |
| 82 | expect(mockSession.getAddress()).andReturn(InetAddress.getLoopbackAddress()); |
| 83 | expect(mockSession.walkDevice(capture(networkDeviceCapture), |
| 84 | eq(Arrays.asList(new OID[]{ |
| 85 | NetSnmpAlarmProvider.CLASS_REGISTRY.getClassToOidMap().get(IfTable.class)})))) |
| 86 | .andAnswer(() -> { |
| 87 | networkDeviceCapture.getValue().getRootObject().setObject(interfaceTable); |
| 88 | return null; |
| 89 | }); |
| 90 | |
| 91 | replay(mockSession); |
| 92 | |
| 93 | Collection<Alarm> alarms = alarmProvider.getAlarms(mockSession, DeviceId.deviceId("snmp:1.1.1.1")); |
| 94 | assertEquals(1, alarms.size()); |
| 95 | assertEquals("Link Down.", alarms.iterator().next().description()); |
| 96 | verify(mockSession); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void shouldHandleException() throws UnknownHostException, IOException { |
| 101 | expect(mockSession.getAddress()).andReturn(InetAddress.getLoopbackAddress()); |
| 102 | expect(mockSession.walkDevice(isA(NetworkDevice.class), |
| 103 | eq(Arrays.asList(new OID[]{ |
| 104 | NetSnmpAlarmProvider.CLASS_REGISTRY.getClassToOidMap().get(IfTable.class)})))) |
| 105 | .andThrow(new IOException()); |
| 106 | |
| 107 | replay(mockSession); |
| 108 | |
| 109 | assertNotNull(alarmProvider.getAlarms(mockSession, DeviceId.deviceId("snmp:1.1.1.1"))); |
| 110 | |
| 111 | verify(mockSession); |
| 112 | } |
| 113 | |
| 114 | } |