blob: 27cd34fb09cb6928572fa6570a9accab81e50000 [file] [log] [blame]
Andrea Campanella58454b92016-04-01 15:19:00 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Andrea Campanella58454b92016-04-01 15:19:00 -07003 *
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.snmp.ctl;
18
19import com.btisystems.pronx.ems.core.snmp.ISnmpConfiguration;
20import com.btisystems.pronx.ems.core.snmp.ISnmpConfigurationFactory;
21import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
22import com.btisystems.pronx.ems.core.snmp.ISnmpSessionFactory;
23import org.junit.Before;
24import org.junit.Test;
25import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
Ray Milkey0a1a0912018-02-15 11:35:58 -080026import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId;
Andrea Campanella58454b92016-04-01 15:19:00 -070027import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
28
29import java.io.IOException;
30
31import static org.junit.Assert.*;
32
33/**
34 * DefaultSnmpController test class.
35 */
36public 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 Milkey0a1a0912018-02-15 11:35:58 -080046 long time = System.currentTimeMillis();
Andrea Campanella58454b92016-04-01 15:19:00 -070047 DefaultAlarm alarm = new DefaultAlarm.Builder(
Ray Milkey0a1a0912018-02-15 11:35:58 -080048 AlarmId.alarmId(device.deviceId(), Long.toString(time)),
Andrea Campanella58454b92016-04-01 15:19:00 -070049 device.deviceId(), "SNMP alarm retrieval failed",
50 Alarm.SeverityLevel.CRITICAL,
Ray Milkey0a1a0912018-02-15 11:35:58 -080051 time).build();
Andrea Campanella58454b92016-04-01 15:19:00 -070052
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 Milkey6b1acfc2018-09-05 16:59:17 -070073 snmpController.addDevice(device);
Andrea Campanella58454b92016-04-01 15:19:00 -070074 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 Papp9655b182017-01-31 13:01:45 -0800122}