Andrea Campanella | 58454b9 | 2016-04-01 15:19:00 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Andrea Campanella | 58454b9 | 2016-04-01 15:19:00 -0700 | [diff] [blame] | 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.snmp.ctl; |
| 18 | |
| 19 | import com.btisystems.pronx.ems.core.snmp.ISnmpConfiguration; |
| 20 | import com.btisystems.pronx.ems.core.snmp.ISnmpConfigurationFactory; |
| 21 | import com.btisystems.pronx.ems.core.snmp.ISnmpSession; |
| 22 | import com.btisystems.pronx.ems.core.snmp.ISnmpSessionFactory; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
Ray Milkey | 0a1a091 | 2018-02-15 11:35:58 -0800 | [diff] [blame] | 26 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId; |
Andrea Campanella | 58454b9 | 2016-04-01 15:19:00 -0700 | [diff] [blame] | 27 | import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm; |
| 28 | |
| 29 | import java.io.IOException; |
| 30 | |
| 31 | import static org.junit.Assert.*; |
| 32 | |
| 33 | /** |
| 34 | * DefaultSnmpController test class. |
| 35 | */ |
| 36 | public class DefaultSnmpControllerTest { |
| 37 | |
| 38 | ISnmpSessionFactory mockSnmpSessionFactory = new MockISnmpSessionFactory(); |
| 39 | |
| 40 | DefaultSnmpController snmpController = new DefaultSnmpController(); |
| 41 | |
| 42 | DefaultSnmpDevice device = new DefaultSnmpDevice("1.1.1.1", 1, "test", "test"); |
| 43 | |
| 44 | ISnmpSession snmpSession = new ISnmpSessionAdapter(); |
| 45 | |
Ray Milkey | 0a1a091 | 2018-02-15 11:35:58 -0800 | [diff] [blame] | 46 | long time = System.currentTimeMillis(); |
Andrea Campanella | 58454b9 | 2016-04-01 15:19:00 -0700 | [diff] [blame] | 47 | DefaultAlarm alarm = new DefaultAlarm.Builder( |
Ray Milkey | 0a1a091 | 2018-02-15 11:35:58 -0800 | [diff] [blame] | 48 | AlarmId.alarmId(device.deviceId(), Long.toString(time)), |
Andrea Campanella | 58454b9 | 2016-04-01 15:19:00 -0700 | [diff] [blame] | 49 | device.deviceId(), "SNMP alarm retrieval failed", |
| 50 | Alarm.SeverityLevel.CRITICAL, |
Ray Milkey | 0a1a091 | 2018-02-15 11:35:58 -0800 | [diff] [blame] | 51 | time).build(); |
Andrea Campanella | 58454b9 | 2016-04-01 15:19:00 -0700 | [diff] [blame] | 52 | |
| 53 | @Before |
| 54 | public void setUp() { |
| 55 | snmpController.sessionFactory = mockSnmpSessionFactory; |
| 56 | } |
| 57 | |
| 58 | @Test |
| 59 | public void testActivate() { |
| 60 | snmpController.activate(null); |
| 61 | assertNotNull("Incorrect sessionFactory", snmpController.sessionFactory); |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | public void testDeactivate() { |
| 66 | snmpController.deactivate(); |
| 67 | assertEquals("Device map should be clear", 0, snmpController.getDevices().size()); |
| 68 | assertEquals("Session map should be clear", 0, snmpController.sessionMap.size()); |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void addDevice() { |
Ray Milkey | 6b1acfc | 2018-09-05 16:59:17 -0700 | [diff] [blame] | 73 | snmpController.addDevice(device); |
Andrea Campanella | 58454b9 | 2016-04-01 15:19:00 -0700 | [diff] [blame] | 74 | assertEquals("Controller should contain device", device, snmpController.getDevice(device.deviceId())); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * tests session creation and get from map if already exists. |
| 79 | */ |
| 80 | @Test |
| 81 | public void getNotExistingSession() throws Exception { |
| 82 | addDevice(); |
| 83 | assertEquals("Session should be created", snmpSession, snmpController.getSession(device.deviceId())); |
| 84 | assertEquals("Map should contain session", 1, snmpController.snmpDeviceMap.size()); |
| 85 | assertEquals("Session should be fetched from map", snmpSession, snmpController.getSession(device.deviceId())); |
| 86 | } |
| 87 | |
| 88 | @Test |
| 89 | public void removeDevice() { |
| 90 | addDevice(); |
| 91 | snmpController.removeDevice(device.deviceId()); |
| 92 | assertNull("Device shoudl not be present", snmpController.getDevice(device.deviceId())); |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void walkFailedAlarm() { |
| 97 | assertEquals("Alarms should be equals", alarm, snmpController.buildWalkFailedAlarm(device.deviceId())); |
| 98 | } |
| 99 | |
| 100 | public class MockISnmpSessionFactory implements ISnmpSessionFactory { |
| 101 | |
| 102 | @Override |
| 103 | public ISnmpSession createSession(ISnmpConfiguration configuration, String ipAddress) throws IOException { |
| 104 | new ISnmpSessionAdapter(); |
| 105 | return snmpSession; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public ISnmpSession createSession(String ipAddress, String community) |
| 110 | throws IOException { |
| 111 | return snmpSession; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public ISnmpSession createSession(String ipAddress, String community, |
| 116 | String factoryName, |
| 117 | ISnmpConfigurationFactory.AccessType accessType) |
| 118 | throws IOException { |
| 119 | return snmpSession; |
| 120 | } |
| 121 | } |
Laszlo Papp | 9655b18 | 2017-01-31 13:01:45 -0800 | [diff] [blame] | 122 | } |