Andrea Campanella
Committed by Gerrit Code Review

[ONOS-4621] Unit Tests for Snmp provider and protocol

Change-Id: If70c701478762201b3739d9bd017c9f7eec75db1
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.net.device;
18 +
19 +import org.onosproject.net.provider.ProviderId;
20 +
21 +import java.util.Set;
22 +
23 +/**
24 + * Testing adapter for the DeviceProviderRegistry API.
25 + */
26 +public class DeviceProviderRegistryAdapter implements DeviceProviderRegistry {
27 + @Override
28 + public DeviceProviderService register(DeviceProvider provider) {
29 + return null;
30 + }
31 +
32 + @Override
33 + public void unregister(DeviceProvider provider) {
34 +
35 + }
36 +
37 + @Override
38 + public Set<ProviderId> getProviders() {
39 + return null;
40 + }
41 +}
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.net.device;
18 +
19 +import org.onosproject.net.DeviceId;
20 +import org.onosproject.net.MastershipRole;
21 +
22 +import java.util.Collection;
23 +import java.util.List;
24 +
25 +/**
26 + * Test Adapter for DeviceProviderService API.
27 + */
28 +public class DeviceProviderServiceAdapter implements DeviceProviderService {
29 + @Override
30 + public void deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) {
31 +
32 + }
33 +
34 + @Override
35 + public void deviceDisconnected(DeviceId deviceId) {
36 +
37 + }
38 +
39 + @Override
40 + public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) {
41 +
42 + }
43 +
44 + @Override
45 + public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) {
46 +
47 + }
48 +
49 + @Override
50 + public void receivedRoleReply(DeviceId deviceId, MastershipRole requested, MastershipRole response) {
51 +
52 + }
53 +
54 + @Override
55 + public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) {
56 +
57 + }
58 +
59 + @Override
60 + public DeviceProvider provider() {
61 + return null;
62 + }
63 +}
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.net.device;
18 +
19 +import org.onosproject.net.Device;
20 +import org.onosproject.net.DeviceId;
21 +import org.onosproject.net.Port;
22 +import org.onosproject.net.PortNumber;
23 +import org.onosproject.net.provider.ProviderId;
24 +
25 +import java.util.Collection;
26 +import java.util.List;
27 +
28 +/**
29 + * Test adapter for the DeviceStore API.
30 + */
31 +public class DeviceStoreAdapter implements DeviceStore {
32 + @Override
33 + public int getDeviceCount() {
34 + return 0;
35 + }
36 +
37 + @Override
38 + public Iterable<Device> getDevices() {
39 + return null;
40 + }
41 +
42 + @Override
43 + public Iterable<Device> getAvailableDevices() {
44 + return null;
45 + }
46 +
47 + @Override
48 + public Device getDevice(DeviceId deviceId) {
49 + return null;
50 + }
51 +
52 + @Override
53 + public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId,
54 + DeviceDescription deviceDescription) {
55 + return null;
56 + }
57 +
58 + @Override
59 + public DeviceEvent markOffline(DeviceId deviceId) {
60 + return null;
61 + }
62 +
63 + @Override
64 + public List<DeviceEvent> updatePorts(ProviderId providerId, DeviceId deviceId,
65 + List<PortDescription> portDescriptions) {
66 + return null;
67 + }
68 +
69 + @Override
70 + public DeviceEvent updatePortStatus(ProviderId providerId, DeviceId deviceId,
71 + PortDescription portDescription) {
72 + return null;
73 + }
74 +
75 + @Override
76 + public List<Port> getPorts(DeviceId deviceId) {
77 + return null;
78 + }
79 +
80 + @Override
81 + public DeviceEvent updatePortStatistics(ProviderId providerId, DeviceId deviceId,
82 + Collection<PortStatistics> portStats) {
83 + return null;
84 + }
85 +
86 + @Override
87 + public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
88 + return null;
89 + }
90 +
91 + @Override
92 + public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) {
93 + return null;
94 + }
95 +
96 + @Override
97 + public Port getPort(DeviceId deviceId, PortNumber portNumber) {
98 + return null;
99 + }
100 +
101 + @Override
102 + public boolean isAvailable(DeviceId deviceId) {
103 + return false;
104 + }
105 +
106 + @Override
107 + public DeviceEvent removeDevice(DeviceId deviceId) {
108 + return null;
109 + }
110 +
111 + @Override
112 + public void setDelegate(DeviceStoreDelegate delegate) {
113 +
114 + }
115 +
116 + @Override
117 + public void unsetDelegate(DeviceStoreDelegate delegate) {
118 +
119 + }
120 +
121 + @Override
122 + public boolean hasDelegate() {
123 + return false;
124 + }
125 +}
...@@ -52,10 +52,10 @@ public class DefaultSnmpController implements SnmpController { ...@@ -52,10 +52,10 @@ public class DefaultSnmpController implements SnmpController {
52 private final Logger log = LoggerFactory 52 private final Logger log = LoggerFactory
53 .getLogger(getClass()); 53 .getLogger(getClass());
54 54
55 - private ISnmpSessionFactory sessionFactory; 55 + protected ISnmpSessionFactory sessionFactory;
56 56
57 - private final Map<DeviceId, ISnmpSession> sessionMap = new HashMap<>(); 57 + protected final Map<DeviceId, ISnmpSession> sessionMap = new HashMap<>();
58 - protected Map<DeviceId, SnmpDevice> snmpDeviceMap = new ConcurrentHashMap<>(); 58 + protected final Map<DeviceId, SnmpDevice> snmpDeviceMap = new ConcurrentHashMap<>();
59 59
60 @Activate 60 @Activate
61 public void activate(ComponentContext context) { 61 public void activate(ComponentContext context) {
...@@ -66,6 +66,7 @@ public class DefaultSnmpController implements SnmpController { ...@@ -66,6 +66,7 @@ public class DefaultSnmpController implements SnmpController {
66 66
67 @Deactivate 67 @Deactivate
68 public void deactivate() { 68 public void deactivate() {
69 + sessionMap.clear();
69 snmpDeviceMap.clear(); 70 snmpDeviceMap.clear();
70 log.info("Stopped"); 71 log.info("Stopped");
71 } 72 }
......
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.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;
26 +import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
27 +
28 +import java.io.IOException;
29 +
30 +import static org.junit.Assert.*;
31 +
32 +/**
33 + * DefaultSnmpController test class.
34 + */
35 +public class DefaultSnmpControllerTest {
36 +
37 + ISnmpSessionFactory mockSnmpSessionFactory = new MockISnmpSessionFactory();
38 +
39 + DefaultSnmpController snmpController = new DefaultSnmpController();
40 +
41 + DefaultSnmpDevice device = new DefaultSnmpDevice("1.1.1.1", 1, "test", "test");
42 +
43 + ISnmpSession snmpSession = new ISnmpSessionAdapter();
44 +
45 + DefaultAlarm alarm = new DefaultAlarm.Builder(
46 + device.deviceId(), "SNMP alarm retrieval failed",
47 + Alarm.SeverityLevel.CRITICAL,
48 + System.currentTimeMillis()).build();
49 +
50 + @Before
51 + public void setUp() {
52 + snmpController.sessionFactory = mockSnmpSessionFactory;
53 + }
54 +
55 + @Test
56 + public void testActivate() {
57 + snmpController.activate(null);
58 + assertNotNull("Incorrect sessionFactory", snmpController.sessionFactory);
59 + }
60 +
61 + @Test
62 + public void testDeactivate() {
63 + snmpController.deactivate();
64 + assertEquals("Device map should be clear", 0, snmpController.getDevices().size());
65 + assertEquals("Session map should be clear", 0, snmpController.sessionMap.size());
66 + }
67 +
68 + @Test
69 + public void addDevice() {
70 + snmpController.addDevice(device.deviceId(), device);
71 + assertEquals("Controller should contain device", device, snmpController.getDevice(device.deviceId()));
72 + }
73 +
74 + /**
75 + * tests session creation and get from map if already exists.
76 + */
77 + @Test
78 + public void getNotExistingSession() throws Exception {
79 + addDevice();
80 + assertEquals("Session should be created", snmpSession, snmpController.getSession(device.deviceId()));
81 + assertEquals("Map should contain session", 1, snmpController.snmpDeviceMap.size());
82 + assertEquals("Session should be fetched from map", snmpSession, snmpController.getSession(device.deviceId()));
83 + }
84 +
85 + @Test
86 + public void removeDevice() {
87 + addDevice();
88 + snmpController.removeDevice(device.deviceId());
89 + assertNull("Device shoudl not be present", snmpController.getDevice(device.deviceId()));
90 + }
91 +
92 + @Test
93 + public void walkFailedAlarm() {
94 + assertEquals("Alarms should be equals", alarm, snmpController.buildWalkFailedAlarm(device.deviceId()));
95 + }
96 +
97 + public class MockISnmpSessionFactory implements ISnmpSessionFactory {
98 +
99 + @Override
100 + public ISnmpSession createSession(ISnmpConfiguration configuration, String ipAddress) throws IOException {
101 + new ISnmpSessionAdapter();
102 + return snmpSession;
103 + }
104 +
105 + @Override
106 + public ISnmpSession createSession(String ipAddress, String community)
107 + throws IOException {
108 + return snmpSession;
109 + }
110 +
111 + @Override
112 + public ISnmpSession createSession(String ipAddress, String community,
113 + String factoryName,
114 + ISnmpConfigurationFactory.AccessType accessType)
115 + throws IOException {
116 + return snmpSession;
117 + }
118 + }
119 +}
...\ No newline at end of file ...\ No newline at end of file
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.snmp.ctl;
18 +
19 +import org.junit.Test;
20 +import org.onosproject.net.DeviceId;
21 +
22 +import static org.junit.Assert.*;
23 +
24 +/**
25 + * Test class for DefaultSnmpDevice.
26 + */
27 +public class DefaultSnmpDeviceTest {
28 +
29 + private final String snmpHost = "1.1.1.1";
30 + private int snmpPort = 1;
31 + private final String username = "test";
32 + private final String community = "test";
33 + private final DeviceId deviceId = DeviceId.deviceId("snmp:1.1.1.1:1");
34 + private final String deviceInfo = "host: 1.1.1.1. port: 1";
35 +
36 + DefaultSnmpDevice device = new DefaultSnmpDevice("1.1.1.1", 1, "test", "test");
37 +
38 + @Test
39 + public void basics() throws Exception {
40 + assertTrue("Device should be reachable", device.isReachable());
41 + assertEquals("Incorrect host", snmpHost, device.getSnmpHost());
42 + assertEquals("Incorrect port", snmpPort, device.getSnmpPort());
43 + assertEquals("Incorrect username", username, device.getUsername());
44 + assertEquals("Incorrect community", community, device.getCommunity());
45 + assertEquals("Incorrect deviceID", deviceId, device.deviceId());
46 + assertEquals("Incorrect deviceInfo", deviceInfo, device.deviceInfo());
47 + device.disconnect();
48 + assertFalse("Device should not be reachable", device.isReachable());
49 +
50 + }
51 +}
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.snmp.ctl;
18 +
19 +import com.btisystems.pronx.ems.core.model.DeviceEntityDescription;
20 +import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
21 +import com.btisystems.pronx.ems.core.snmp.IVariableBindingHandler;
22 +import com.btisystems.pronx.ems.core.snmp.SnmpIoException;
23 +import com.btisystems.pronx.ems.core.snmp.WalkResponse;
24 +import org.snmp4j.smi.OID;
25 +import org.snmp4j.smi.VariableBinding;
26 +
27 +import java.io.IOException;
28 +import java.net.InetAddress;
29 +import java.util.List;
30 +import java.util.Map;
31 +
32 +/**
33 + * AdapterClass for ISnmpSession.
34 + */
35 +public class ISnmpSessionAdapter implements ISnmpSession {
36 + @Override
37 + public String identifyDevice() {
38 + return null;
39 + }
40 +
41 + @Override
42 + public String getVariable(String oid) {
43 + return null;
44 + }
45 +
46 + @Override
47 + public Integer getVariableAsInt(String oid) {
48 + return null;
49 + }
50 +
51 + @Override
52 + public WalkResponse walkDevice(IVariableBindingHandler networkDevice, List<OID> oids)
53 + throws IOException {
54 + return null;
55 + }
56 +
57 + @Override
58 + public WalkResponse getTableRows(IVariableBindingHandler networkDevice,
59 + Map<DeviceEntityDescription, List<OID>> tableIndexes)
60 + throws IOException {
61 + return null;
62 + }
63 +
64 + @Override
65 + public InetAddress getAddress() {
66 + return null;
67 + }
68 +
69 + @Override
70 + public void close() throws IOException {
71 +
72 + }
73 +
74 + @Override
75 + public void setVariables(VariableBinding[] bindings) {
76 +
77 + }
78 +
79 + @Override
80 + public void checkErrorCodeAndDescription() throws SnmpIoException {
81 +
82 + }
83 +}
...\ No newline at end of file ...\ No newline at end of file
...@@ -57,7 +57,6 @@ ...@@ -57,7 +57,6 @@
57 </exclusion> 57 </exclusion>
58 </exclusions> 58 </exclusions>
59 </dependency> 59 </dependency>
60 -
61 <dependency> 60 <dependency>
62 <groupId>com.btisystems</groupId> 61 <groupId>com.btisystems</groupId>
63 <artifactId>snmp-core</artifactId> 62 <artifactId>snmp-core</artifactId>
...@@ -80,15 +79,21 @@ ...@@ -80,15 +79,21 @@
80 <artifactId>onos-core-net</artifactId> 79 <artifactId>onos-core-net</artifactId>
81 <version>1.6.0-SNAPSHOT</version> 80 <version>1.6.0-SNAPSHOT</version>
82 </dependency> 81 </dependency>
82 + <dependency>
83 + <groupId>org.onosproject</groupId>
84 + <artifactId>onos-api</artifactId>
85 + <version>1.6.0-SNAPSHOT</version>
86 + <classifier>tests</classifier>
87 + <scope>test</scope>
88 + </dependency>
83 </dependencies> 89 </dependencies>
84 - <build> 90 + <build>
85 - <plugins> 91 + <plugins>
86 - <plugin> 92 + <plugin>
87 - <groupId>org.apache.felix</groupId> 93 + <groupId>org.apache.felix</groupId>
88 - <artifactId>maven-scr-plugin</artifactId> 94 + <artifactId>maven-scr-plugin</artifactId>
89 - </plugin> 95 + </plugin>
90 - </plugins> 96 + </plugins>
91 - </build> 97 + </build>
92 -
93 </project> 98 </project>
94 99
......
...@@ -74,8 +74,6 @@ public class SnmpDeviceProvider extends AbstractProvider ...@@ -74,8 +74,6 @@ public class SnmpDeviceProvider extends AbstractProvider
74 private static final String APP_NAME = "org.onosproject.snmp"; 74 private static final String APP_NAME = "org.onosproject.snmp";
75 private static final String SCHEME = "snmp"; 75 private static final String SCHEME = "snmp";
76 76
77 - private DeviceProviderService providerService;
78 -
79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 77 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected SnmpController controller; 78 protected SnmpController controller;
81 79
...@@ -94,14 +92,17 @@ public class SnmpDeviceProvider extends AbstractProvider ...@@ -94,14 +92,17 @@ public class SnmpDeviceProvider extends AbstractProvider
94 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
95 protected NetworkConfigRegistry netCfgService; 93 protected NetworkConfigRegistry netCfgService;
96 94
95 + protected DeviceProviderService providerService;
96 +
97 + protected ApplicationId appId;
98 +
97 private final ExecutorService deviceBuilderExecutor = Executors 99 private final ExecutorService deviceBuilderExecutor = Executors
98 .newFixedThreadPool(5, groupedThreads("onos/snmp", "device-creator", log)); 100 .newFixedThreadPool(5, groupedThreads("onos/snmp", "device-creator", log));
99 101
100 - private final NetworkConfigListener cfgLister = new InternalNetworkConfigListener(); 102 + protected final NetworkConfigListener cfgLister = new InternalNetworkConfigListener();
101 103
102 - private ApplicationId appId;
103 104
104 - private final ConfigFactory factory = 105 + protected final ConfigFactory factory =
105 new ConfigFactory<ApplicationId, SnmpProviderConfig>(APP_SUBJECT_FACTORY, 106 new ConfigFactory<ApplicationId, SnmpProviderConfig>(APP_SUBJECT_FACTORY,
106 SnmpProviderConfig.class, 107 SnmpProviderConfig.class,
107 "devices", 108 "devices",
......
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.provider.snmp.device.impl;
18 +
19 +import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
20 +import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
21 +import org.onosproject.net.DeviceId;
22 +import org.onosproject.snmp.*;
23 +import org.onosproject.snmp.SnmpDevice;
24 +
25 +import java.io.IOException;
26 +import java.util.ArrayList;
27 +import java.util.Collection;
28 +import java.util.HashMap;
29 +import java.util.List;
30 +
31 +/**
32 + * Test Adapter for SnmpController API.
33 + */
34 +public class SnmpControllerAdapter implements SnmpController {
35 +
36 + protected HashMap<DeviceId, SnmpDevice> devices = new HashMap<>();
37 + @Override
38 + public Collection<SnmpDevice> getDevices() {
39 + return devices.values();
40 + }
41 +
42 + @Override
43 + public SnmpDevice getDevice(DeviceId deviceId) {
44 + return devices.get(deviceId);
45 + }
46 +
47 + @Override
48 + public void removeDevice(DeviceId deviceId) {
49 + devices.remove(deviceId);
50 + }
51 +
52 + @Override
53 + public void addDevice(DeviceId deviceId, SnmpDevice snmpDevice) {
54 + devices.put(deviceId,snmpDevice);
55 + }
56 +
57 + @Override
58 + public ISnmpSession getSession(DeviceId deviceId) throws IOException {
59 + return null;
60 + }
61 +
62 + @Override
63 + public DefaultAlarm buildWalkFailedAlarm(DeviceId deviceId) {
64 + return null;
65 + }
66 +}
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.provider.snmp.device.impl;
18 +
19 +import com.google.common.collect.ImmutableSet;
20 +import org.junit.Before;
21 +import org.junit.Test;
22 +import org.onlab.packet.IpAddress;
23 +import org.onosproject.TestApplicationId;
24 +import org.onosproject.core.ApplicationId;
25 +import org.onosproject.core.CoreService;
26 +import org.onosproject.core.CoreServiceAdapter;
27 +import org.onosproject.incubator.net.config.basics.ConfigException;
28 +import org.onosproject.net.AbstractProjectableModel;
29 +import org.onosproject.net.DefaultAnnotations;
30 +import org.onosproject.net.DefaultDevice;
31 +import org.onosproject.net.Device;
32 +import org.onosproject.net.DeviceId;
33 +import org.onosproject.net.config.Config;
34 +import org.onosproject.net.config.ConfigFactory;
35 +import org.onosproject.net.config.NetworkConfigEvent;
36 +import org.onosproject.net.config.NetworkConfigListener;
37 +import org.onosproject.net.config.NetworkConfigRegistry;
38 +import org.onosproject.net.config.NetworkConfigRegistryAdapter;
39 +import org.onosproject.net.config.basics.BasicDeviceConfig;
40 +import org.onosproject.net.device.DeviceDescription;
41 +import org.onosproject.net.device.DeviceEvent;
42 +import org.onosproject.net.device.DeviceProvider;
43 +import org.onosproject.net.device.DeviceProviderRegistry;
44 +import org.onosproject.net.device.DeviceProviderRegistryAdapter;
45 +import org.onosproject.net.device.DeviceProviderService;
46 +import org.onosproject.net.device.DeviceProviderServiceAdapter;
47 +import org.onosproject.net.device.DeviceService;
48 +import org.onosproject.net.device.DeviceServiceAdapter;
49 +import org.onosproject.net.device.DeviceStore;
50 +import org.onosproject.net.device.DeviceStoreAdapter;
51 +import org.onosproject.net.driver.DriverService;
52 +import org.onosproject.net.driver.DriverServiceAdapter;
53 +import org.onosproject.net.provider.ProviderId;
54 +import org.onosproject.snmp.SnmpController;
55 +
56 +import java.util.HashMap;
57 +import java.util.HashSet;
58 +import java.util.Set;
59 +
60 +import static org.junit.Assert.*;
61 +import static org.onlab.junit.TestTools.assertAfter;
62 +
63 +/**
64 + * Testing class for SnmpDeviceProvider.
65 + */
66 +public class SnmpDeviceProviderTest {
67 +
68 + private final SnmpDeviceProvider provider = new SnmpDeviceProvider();
69 + private final SnmpController controller = new SnmpControllerAdapter();
70 + private final DeviceProviderRegistry providerRegistry = new MockDeviceProviderRegistry();
71 + private final DeviceService deviceService = new MockDeviceService();
72 + private final NetworkConfigRegistry netCfgService = new MockNetworkConfigRegistry();
73 + private final DeviceStore deviceStore = new MockDeviceStore();
74 + protected CoreService coreService = new MockCoreService();
75 + private final DeviceProviderService deviceProviderService = new MockDeviceProviderService();
76 + private final TestApplicationId applicationId = new TestApplicationId("TestAppId");
77 + private final NetworkConfigEvent deviceAddedEvent =
78 + new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED,
79 + null, SnmpProviderConfig.class);
80 + private final SnmpProviderConfig snmpProviderConfig = new MockSnmpProviderConfig();
81 + private final NetworkConfigEvent deviceAddedIrrelevantEvent =
82 + new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED,
83 + null, BasicDeviceConfig.class);
84 + private final DeviceId deviceId = DeviceId.deviceId("snmp:1.1.1.1:1");
85 + private final DeviceId wrongDeviceId = DeviceId.deviceId("snmp:2.2.2.2:2");
86 +
87 +
88 + @Before
89 + public void setUp() throws Exception {
90 + provider.controller = controller;
91 + provider.providerRegistry = providerRegistry;
92 + provider.deviceService = deviceService;
93 + provider.netCfgService = netCfgService;
94 + provider.deviceStore = deviceStore;
95 + provider.coreService = coreService;
96 + provider.activate(null);
97 + }
98 +
99 + @Test
100 + public void testActivate() {
101 + assertEquals("Incorrect provider service", deviceProviderService, provider.providerService);
102 + assertEquals("Incorrect application id", applicationId, provider.appId);
103 + assertEquals("Incorrect config factory", cfgFactory, provider.factory);
104 + assertTrue("Incorrect network config listener", netCfgListeners.contains(provider.cfgLister));
105 +
106 +
107 + }
108 +
109 + @Test
110 + public void testDeactivate() {
111 + this.addDevice();
112 + provider.deactivate(null);
113 + assertAfter(500, () ->
114 + assertNull("Device should be removed", controller.getDevice(deviceId)));
115 + assertNull("Network config factory not removed", cfgFactory);
116 + assertFalse("Network config listener not removed", netCfgListeners.contains(provider.cfgLister));
117 + assertFalse("Provider not unregistered", providerRegistry.getProviders().contains(provider.id()));
118 + assertNull("Provider registry not removed", provider.providerService);
119 + }
120 +
121 + @Test
122 + public void eventNotRelevant() {
123 + assertFalse("Event should not be relevant", provider.cfgLister.isRelevant(deviceAddedIrrelevantEvent));
124 + assertFalse("Device should not be reachable", provider.isReachable(wrongDeviceId));
125 + }
126 +
127 + @Test
128 + public void addDevice() {
129 + assertTrue("Event should be relevant", provider.cfgLister.isRelevant(deviceAddedEvent));
130 + provider.cfgLister.event(deviceAddedEvent);
131 + AbstractProjectableModel.setDriverService(null, new MockDriverService());
132 + //FIXME this needs sleep
133 + assertAfter(500, () ->
134 + assertNotNull("Device should be added to controller", controller.getDevice(deviceId)));
135 + assertTrue("Device should be reachable", provider.isReachable(deviceId));
136 + }
137 +
138 + private class MockDeviceProviderRegistry extends DeviceProviderRegistryAdapter {
139 +
140 + Set<ProviderId> providers = new HashSet<>();
141 +
142 + @Override
143 + public DeviceProviderService register(DeviceProvider provider) {
144 + providers.add(provider.id());
145 + return deviceProviderService;
146 + }
147 +
148 + @Override
149 + public void unregister(DeviceProvider provider) {
150 + providers.remove(provider.id());
151 + }
152 +
153 + @Override
154 + public Set<ProviderId> getProviders() {
155 + return providers;
156 + }
157 +
158 + }
159 +
160 + private class MockDeviceService extends DeviceServiceAdapter {
161 + @Override
162 + public Device getDevice(DeviceId deviceId) {
163 + return deviceStore.getDevice(deviceId);
164 + }
165 + }
166 +
167 + private ConfigFactory cfgFactory;
168 + private Set<NetworkConfigListener> netCfgListeners = new HashSet<>();
169 +
170 + private class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter {
171 +
172 + @Override
173 + public void registerConfigFactory(ConfigFactory configFactory) {
174 + cfgFactory = configFactory;
175 + }
176 +
177 + @Override
178 + public void unregisterConfigFactory(ConfigFactory configFactory) {
179 + cfgFactory = null;
180 + }
181 +
182 + @Override
183 + public void addListener(NetworkConfigListener listener) {
184 + netCfgListeners.add(listener);
185 + }
186 +
187 + @Override
188 + public void removeListener(NetworkConfigListener listener) {
189 + netCfgListeners.remove(listener);
190 + }
191 +
192 +
193 + @Override
194 + public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
195 + if (configClass.equals(SnmpProviderConfig.class)) {
196 + return (C) snmpProviderConfig;
197 + } else {
198 + return (C) new BasicDeviceConfig();
199 + }
200 + }
201 + }
202 +
203 + private class MockDeviceStore extends DeviceStoreAdapter {
204 + protected HashMap<DeviceId, Device> devices = new HashMap<>();
205 +
206 + @Override
207 + public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId,
208 + DeviceDescription desc) {
209 +
210 + devices.put(deviceId, new DefaultDevice(providerId, deviceId, desc.type(),
211 + desc.manufacturer(), desc.hwVersion(),
212 + desc.swVersion(), desc.serialNumber(),
213 + desc.chassisId(), DefaultAnnotations.builder().build()));
214 + return null;
215 + }
216 +
217 + @Override
218 + public Device getDevice(DeviceId deviceId) {
219 + return devices.get(deviceId);
220 + }
221 +
222 + }
223 +
224 + private class MockCoreService extends CoreServiceAdapter {
225 + @Override
226 + public ApplicationId registerApplication(String name) {
227 + return applicationId;
228 + }
229 + }
230 +
231 + private class MockDeviceProviderService extends DeviceProviderServiceAdapter {
232 + DeviceStore store = deviceStore;
233 +
234 + @Override
235 + public void deviceConnected(DeviceId deviceId, DeviceDescription desc) {
236 + store.createOrUpdateDevice(ProviderId.NONE, deviceId, desc);
237 + }
238 + }
239 +
240 + private class MockSnmpProviderConfig extends SnmpProviderConfig {
241 + protected SnmpDeviceInfo deviceInfo = new SnmpDeviceInfo(IpAddress.valueOf("1.1.1.1"), 1, "test", "test");
242 +
243 + @Override
244 + public Set<SnmpProviderConfig.SnmpDeviceInfo> getDevicesInfo() throws ConfigException {
245 + return ImmutableSet.of(deviceInfo);
246 + }
247 +
248 + }
249 +
250 + private class MockDriverService extends DriverServiceAdapter {
251 +
252 + }
253 +}
...\ No newline at end of file ...\ No newline at end of file