[ONOS-4621] Unit Tests for Snmp provider and protocol
Change-Id: If70c701478762201b3739d9bd017c9f7eec75db1
diff --git a/core/api/src/test/java/org/onosproject/net/device/DeviceProviderRegistryAdapter.java b/core/api/src/test/java/org/onosproject/net/device/DeviceProviderRegistryAdapter.java
new file mode 100644
index 0000000..e9cd8a2
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/device/DeviceProviderRegistryAdapter.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.device;
+
+import org.onosproject.net.provider.ProviderId;
+
+import java.util.Set;
+
+/**
+ * Testing adapter for the DeviceProviderRegistry API.
+ */
+public class DeviceProviderRegistryAdapter implements DeviceProviderRegistry {
+ @Override
+ public DeviceProviderService register(DeviceProvider provider) {
+ return null;
+ }
+
+ @Override
+ public void unregister(DeviceProvider provider) {
+
+ }
+
+ @Override
+ public Set<ProviderId> getProviders() {
+ return null;
+ }
+}
diff --git a/core/api/src/test/java/org/onosproject/net/device/DeviceProviderServiceAdapter.java b/core/api/src/test/java/org/onosproject/net/device/DeviceProviderServiceAdapter.java
new file mode 100644
index 0000000..5309e69
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/device/DeviceProviderServiceAdapter.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.device;
+
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.MastershipRole;
+
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * Test Adapter for DeviceProviderService API.
+ */
+public class DeviceProviderServiceAdapter implements DeviceProviderService {
+ @Override
+ public void deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) {
+
+ }
+
+ @Override
+ public void deviceDisconnected(DeviceId deviceId) {
+
+ }
+
+ @Override
+ public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) {
+
+ }
+
+ @Override
+ public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) {
+
+ }
+
+ @Override
+ public void receivedRoleReply(DeviceId deviceId, MastershipRole requested, MastershipRole response) {
+
+ }
+
+ @Override
+ public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) {
+
+ }
+
+ @Override
+ public DeviceProvider provider() {
+ return null;
+ }
+}
diff --git a/core/api/src/test/java/org/onosproject/net/device/DeviceStoreAdapter.java b/core/api/src/test/java/org/onosproject/net/device/DeviceStoreAdapter.java
new file mode 100644
index 0000000..2a109f1
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/device/DeviceStoreAdapter.java
@@ -0,0 +1,125 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.device;
+
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Port;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.provider.ProviderId;
+
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * Test adapter for the DeviceStore API.
+ */
+public class DeviceStoreAdapter implements DeviceStore {
+ @Override
+ public int getDeviceCount() {
+ return 0;
+ }
+
+ @Override
+ public Iterable<Device> getDevices() {
+ return null;
+ }
+
+ @Override
+ public Iterable<Device> getAvailableDevices() {
+ return null;
+ }
+
+ @Override
+ public Device getDevice(DeviceId deviceId) {
+ return null;
+ }
+
+ @Override
+ public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId,
+ DeviceDescription deviceDescription) {
+ return null;
+ }
+
+ @Override
+ public DeviceEvent markOffline(DeviceId deviceId) {
+ return null;
+ }
+
+ @Override
+ public List<DeviceEvent> updatePorts(ProviderId providerId, DeviceId deviceId,
+ List<PortDescription> portDescriptions) {
+ return null;
+ }
+
+ @Override
+ public DeviceEvent updatePortStatus(ProviderId providerId, DeviceId deviceId,
+ PortDescription portDescription) {
+ return null;
+ }
+
+ @Override
+ public List<Port> getPorts(DeviceId deviceId) {
+ return null;
+ }
+
+ @Override
+ public DeviceEvent updatePortStatistics(ProviderId providerId, DeviceId deviceId,
+ Collection<PortStatistics> portStats) {
+ return null;
+ }
+
+ @Override
+ public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
+ return null;
+ }
+
+ @Override
+ public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) {
+ return null;
+ }
+
+ @Override
+ public Port getPort(DeviceId deviceId, PortNumber portNumber) {
+ return null;
+ }
+
+ @Override
+ public boolean isAvailable(DeviceId deviceId) {
+ return false;
+ }
+
+ @Override
+ public DeviceEvent removeDevice(DeviceId deviceId) {
+ return null;
+ }
+
+ @Override
+ public void setDelegate(DeviceStoreDelegate delegate) {
+
+ }
+
+ @Override
+ public void unsetDelegate(DeviceStoreDelegate delegate) {
+
+ }
+
+ @Override
+ public boolean hasDelegate() {
+ return false;
+ }
+}
diff --git a/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpController.java b/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpController.java
index 35556cd..e82a583 100644
--- a/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpController.java
+++ b/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpController.java
@@ -52,10 +52,10 @@
private final Logger log = LoggerFactory
.getLogger(getClass());
- private ISnmpSessionFactory sessionFactory;
+ protected ISnmpSessionFactory sessionFactory;
- private final Map<DeviceId, ISnmpSession> sessionMap = new HashMap<>();
- protected Map<DeviceId, SnmpDevice> snmpDeviceMap = new ConcurrentHashMap<>();
+ protected final Map<DeviceId, ISnmpSession> sessionMap = new HashMap<>();
+ protected final Map<DeviceId, SnmpDevice> snmpDeviceMap = new ConcurrentHashMap<>();
@Activate
public void activate(ComponentContext context) {
@@ -66,6 +66,7 @@
@Deactivate
public void deactivate() {
+ sessionMap.clear();
snmpDeviceMap.clear();
log.info("Stopped");
}
diff --git a/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/DefaultSnmpControllerTest.java b/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/DefaultSnmpControllerTest.java
new file mode 100644
index 0000000..c785dd6
--- /dev/null
+++ b/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/DefaultSnmpControllerTest.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.snmp.ctl;
+
+import com.btisystems.pronx.ems.core.snmp.ISnmpConfiguration;
+import com.btisystems.pronx.ems.core.snmp.ISnmpConfigurationFactory;
+import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
+import com.btisystems.pronx.ems.core.snmp.ISnmpSessionFactory;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
+import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
+
+import java.io.IOException;
+
+import static org.junit.Assert.*;
+
+/**
+ * DefaultSnmpController test class.
+ */
+public class DefaultSnmpControllerTest {
+
+ ISnmpSessionFactory mockSnmpSessionFactory = new MockISnmpSessionFactory();
+
+ DefaultSnmpController snmpController = new DefaultSnmpController();
+
+ DefaultSnmpDevice device = new DefaultSnmpDevice("1.1.1.1", 1, "test", "test");
+
+ ISnmpSession snmpSession = new ISnmpSessionAdapter();
+
+ DefaultAlarm alarm = new DefaultAlarm.Builder(
+ device.deviceId(), "SNMP alarm retrieval failed",
+ Alarm.SeverityLevel.CRITICAL,
+ System.currentTimeMillis()).build();
+
+ @Before
+ public void setUp() {
+ snmpController.sessionFactory = mockSnmpSessionFactory;
+ }
+
+ @Test
+ public void testActivate() {
+ snmpController.activate(null);
+ assertNotNull("Incorrect sessionFactory", snmpController.sessionFactory);
+ }
+
+ @Test
+ public void testDeactivate() {
+ snmpController.deactivate();
+ assertEquals("Device map should be clear", 0, snmpController.getDevices().size());
+ assertEquals("Session map should be clear", 0, snmpController.sessionMap.size());
+ }
+
+ @Test
+ public void addDevice() {
+ snmpController.addDevice(device.deviceId(), device);
+ assertEquals("Controller should contain device", device, snmpController.getDevice(device.deviceId()));
+ }
+
+ /**
+ * tests session creation and get from map if already exists.
+ */
+ @Test
+ public void getNotExistingSession() throws Exception {
+ addDevice();
+ assertEquals("Session should be created", snmpSession, snmpController.getSession(device.deviceId()));
+ assertEquals("Map should contain session", 1, snmpController.snmpDeviceMap.size());
+ assertEquals("Session should be fetched from map", snmpSession, snmpController.getSession(device.deviceId()));
+ }
+
+ @Test
+ public void removeDevice() {
+ addDevice();
+ snmpController.removeDevice(device.deviceId());
+ assertNull("Device shoudl not be present", snmpController.getDevice(device.deviceId()));
+ }
+
+ @Test
+ public void walkFailedAlarm() {
+ assertEquals("Alarms should be equals", alarm, snmpController.buildWalkFailedAlarm(device.deviceId()));
+ }
+
+ public class MockISnmpSessionFactory implements ISnmpSessionFactory {
+
+ @Override
+ public ISnmpSession createSession(ISnmpConfiguration configuration, String ipAddress) throws IOException {
+ new ISnmpSessionAdapter();
+ return snmpSession;
+ }
+
+ @Override
+ public ISnmpSession createSession(String ipAddress, String community)
+ throws IOException {
+ return snmpSession;
+ }
+
+ @Override
+ public ISnmpSession createSession(String ipAddress, String community,
+ String factoryName,
+ ISnmpConfigurationFactory.AccessType accessType)
+ throws IOException {
+ return snmpSession;
+ }
+ }
+}
\ No newline at end of file
diff --git a/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/DefaultSnmpDeviceTest.java b/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/DefaultSnmpDeviceTest.java
new file mode 100644
index 0000000..d62b113
--- /dev/null
+++ b/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/DefaultSnmpDeviceTest.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.snmp.ctl;
+
+import org.junit.Test;
+import org.onosproject.net.DeviceId;
+
+import static org.junit.Assert.*;
+
+/**
+ * Test class for DefaultSnmpDevice.
+ */
+public class DefaultSnmpDeviceTest {
+
+ private final String snmpHost = "1.1.1.1";
+ private int snmpPort = 1;
+ private final String username = "test";
+ private final String community = "test";
+ private final DeviceId deviceId = DeviceId.deviceId("snmp:1.1.1.1:1");
+ private final String deviceInfo = "host: 1.1.1.1. port: 1";
+
+ DefaultSnmpDevice device = new DefaultSnmpDevice("1.1.1.1", 1, "test", "test");
+
+ @Test
+ public void basics() throws Exception {
+ assertTrue("Device should be reachable", device.isReachable());
+ assertEquals("Incorrect host", snmpHost, device.getSnmpHost());
+ assertEquals("Incorrect port", snmpPort, device.getSnmpPort());
+ assertEquals("Incorrect username", username, device.getUsername());
+ assertEquals("Incorrect community", community, device.getCommunity());
+ assertEquals("Incorrect deviceID", deviceId, device.deviceId());
+ assertEquals("Incorrect deviceInfo", deviceInfo, device.deviceInfo());
+ device.disconnect();
+ assertFalse("Device should not be reachable", device.isReachable());
+
+ }
+}
diff --git a/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/ISnmpSessionAdapter.java b/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/ISnmpSessionAdapter.java
new file mode 100644
index 0000000..8bbb93d
--- /dev/null
+++ b/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/ISnmpSessionAdapter.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.snmp.ctl;
+
+import com.btisystems.pronx.ems.core.model.DeviceEntityDescription;
+import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
+import com.btisystems.pronx.ems.core.snmp.IVariableBindingHandler;
+import com.btisystems.pronx.ems.core.snmp.SnmpIoException;
+import com.btisystems.pronx.ems.core.snmp.WalkResponse;
+import org.snmp4j.smi.OID;
+import org.snmp4j.smi.VariableBinding;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * AdapterClass for ISnmpSession.
+ */
+public class ISnmpSessionAdapter implements ISnmpSession {
+ @Override
+ public String identifyDevice() {
+ return null;
+ }
+
+ @Override
+ public String getVariable(String oid) {
+ return null;
+ }
+
+ @Override
+ public Integer getVariableAsInt(String oid) {
+ return null;
+ }
+
+ @Override
+ public WalkResponse walkDevice(IVariableBindingHandler networkDevice, List<OID> oids)
+ throws IOException {
+ return null;
+ }
+
+ @Override
+ public WalkResponse getTableRows(IVariableBindingHandler networkDevice,
+ Map<DeviceEntityDescription, List<OID>> tableIndexes)
+ throws IOException {
+ return null;
+ }
+
+ @Override
+ public InetAddress getAddress() {
+ return null;
+ }
+
+ @Override
+ public void close() throws IOException {
+
+ }
+
+ @Override
+ public void setVariables(VariableBinding[] bindings) {
+
+ }
+
+ @Override
+ public void checkErrorCodeAndDescription() throws SnmpIoException {
+
+ }
+}
\ No newline at end of file
diff --git a/providers/snmp/device/pom.xml b/providers/snmp/device/pom.xml
index bde52f6..c66a9ad 100644
--- a/providers/snmp/device/pom.xml
+++ b/providers/snmp/device/pom.xml
@@ -57,7 +57,6 @@
</exclusion>
</exclusions>
</dependency>
-
<dependency>
<groupId>com.btisystems</groupId>
<artifactId>snmp-core</artifactId>
@@ -80,15 +79,21 @@
<artifactId>onos-core-net</artifactId>
<version>1.6.0-SNAPSHOT</version>
</dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-api</artifactId>
+ <version>1.6.0-SNAPSHOT</version>
+ <classifier>tests</classifier>
+ <scope>test</scope>
+ </dependency>
</dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.felix</groupId>
- <artifactId>maven-scr-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-scr-plugin</artifactId>
+ </plugin>
+ </plugins>
+ </build>
</project>
diff --git a/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceProvider.java b/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceProvider.java
index 4cd809b..ae8da4a 100644
--- a/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceProvider.java
+++ b/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceProvider.java
@@ -74,8 +74,6 @@
private static final String APP_NAME = "org.onosproject.snmp";
private static final String SCHEME = "snmp";
- private DeviceProviderService providerService;
-
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected SnmpController controller;
@@ -94,14 +92,17 @@
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected NetworkConfigRegistry netCfgService;
+ protected DeviceProviderService providerService;
+
+ protected ApplicationId appId;
+
private final ExecutorService deviceBuilderExecutor = Executors
.newFixedThreadPool(5, groupedThreads("onos/snmp", "device-creator", log));
- private final NetworkConfigListener cfgLister = new InternalNetworkConfigListener();
+ protected final NetworkConfigListener cfgLister = new InternalNetworkConfigListener();
- private ApplicationId appId;
- private final ConfigFactory factory =
+ protected final ConfigFactory factory =
new ConfigFactory<ApplicationId, SnmpProviderConfig>(APP_SUBJECT_FACTORY,
SnmpProviderConfig.class,
"devices",
diff --git a/providers/snmp/device/src/main/test/org/onosproject/provider/snmp/device/impl/SnmpControllerAdapter.java b/providers/snmp/device/src/main/test/org/onosproject/provider/snmp/device/impl/SnmpControllerAdapter.java
new file mode 100644
index 0000000..aa40183
--- /dev/null
+++ b/providers/snmp/device/src/main/test/org/onosproject/provider/snmp/device/impl/SnmpControllerAdapter.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.provider.snmp.device.impl;
+
+import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
+import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
+import org.onosproject.net.DeviceId;
+import org.onosproject.snmp.*;
+import org.onosproject.snmp.SnmpDevice;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+
+/**
+ * Test Adapter for SnmpController API.
+ */
+public class SnmpControllerAdapter implements SnmpController {
+
+ protected HashMap<DeviceId, SnmpDevice> devices = new HashMap<>();
+ @Override
+ public Collection<SnmpDevice> getDevices() {
+ return devices.values();
+ }
+
+ @Override
+ public SnmpDevice getDevice(DeviceId deviceId) {
+ return devices.get(deviceId);
+ }
+
+ @Override
+ public void removeDevice(DeviceId deviceId) {
+ devices.remove(deviceId);
+ }
+
+ @Override
+ public void addDevice(DeviceId deviceId, SnmpDevice snmpDevice) {
+ devices.put(deviceId,snmpDevice);
+ }
+
+ @Override
+ public ISnmpSession getSession(DeviceId deviceId) throws IOException {
+ return null;
+ }
+
+ @Override
+ public DefaultAlarm buildWalkFailedAlarm(DeviceId deviceId) {
+ return null;
+ }
+}
diff --git a/providers/snmp/device/src/main/test/org/onosproject/provider/snmp/device/impl/SnmpDeviceProviderTest.java b/providers/snmp/device/src/main/test/org/onosproject/provider/snmp/device/impl/SnmpDeviceProviderTest.java
new file mode 100644
index 0000000..5577fdb
--- /dev/null
+++ b/providers/snmp/device/src/main/test/org/onosproject/provider/snmp/device/impl/SnmpDeviceProviderTest.java
@@ -0,0 +1,253 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.provider.snmp.device.impl;
+
+import com.google.common.collect.ImmutableSet;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpAddress;
+import org.onosproject.TestApplicationId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.core.CoreServiceAdapter;
+import org.onosproject.incubator.net.config.basics.ConfigException;
+import org.onosproject.net.AbstractProjectableModel;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.DefaultDevice;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.config.Config;
+import org.onosproject.net.config.ConfigFactory;
+import org.onosproject.net.config.NetworkConfigEvent;
+import org.onosproject.net.config.NetworkConfigListener;
+import org.onosproject.net.config.NetworkConfigRegistry;
+import org.onosproject.net.config.NetworkConfigRegistryAdapter;
+import org.onosproject.net.config.basics.BasicDeviceConfig;
+import org.onosproject.net.device.DeviceDescription;
+import org.onosproject.net.device.DeviceEvent;
+import org.onosproject.net.device.DeviceProvider;
+import org.onosproject.net.device.DeviceProviderRegistry;
+import org.onosproject.net.device.DeviceProviderRegistryAdapter;
+import org.onosproject.net.device.DeviceProviderService;
+import org.onosproject.net.device.DeviceProviderServiceAdapter;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.device.DeviceServiceAdapter;
+import org.onosproject.net.device.DeviceStore;
+import org.onosproject.net.device.DeviceStoreAdapter;
+import org.onosproject.net.driver.DriverService;
+import org.onosproject.net.driver.DriverServiceAdapter;
+import org.onosproject.net.provider.ProviderId;
+import org.onosproject.snmp.SnmpController;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.junit.Assert.*;
+import static org.onlab.junit.TestTools.assertAfter;
+
+/**
+ * Testing class for SnmpDeviceProvider.
+ */
+public class SnmpDeviceProviderTest {
+
+ private final SnmpDeviceProvider provider = new SnmpDeviceProvider();
+ private final SnmpController controller = new SnmpControllerAdapter();
+ private final DeviceProviderRegistry providerRegistry = new MockDeviceProviderRegistry();
+ private final DeviceService deviceService = new MockDeviceService();
+ private final NetworkConfigRegistry netCfgService = new MockNetworkConfigRegistry();
+ private final DeviceStore deviceStore = new MockDeviceStore();
+ protected CoreService coreService = new MockCoreService();
+ private final DeviceProviderService deviceProviderService = new MockDeviceProviderService();
+ private final TestApplicationId applicationId = new TestApplicationId("TestAppId");
+ private final NetworkConfigEvent deviceAddedEvent =
+ new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED,
+ null, SnmpProviderConfig.class);
+ private final SnmpProviderConfig snmpProviderConfig = new MockSnmpProviderConfig();
+ private final NetworkConfigEvent deviceAddedIrrelevantEvent =
+ new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED,
+ null, BasicDeviceConfig.class);
+ private final DeviceId deviceId = DeviceId.deviceId("snmp:1.1.1.1:1");
+ private final DeviceId wrongDeviceId = DeviceId.deviceId("snmp:2.2.2.2:2");
+
+
+ @Before
+ public void setUp() throws Exception {
+ provider.controller = controller;
+ provider.providerRegistry = providerRegistry;
+ provider.deviceService = deviceService;
+ provider.netCfgService = netCfgService;
+ provider.deviceStore = deviceStore;
+ provider.coreService = coreService;
+ provider.activate(null);
+ }
+
+ @Test
+ public void testActivate() {
+ assertEquals("Incorrect provider service", deviceProviderService, provider.providerService);
+ assertEquals("Incorrect application id", applicationId, provider.appId);
+ assertEquals("Incorrect config factory", cfgFactory, provider.factory);
+ assertTrue("Incorrect network config listener", netCfgListeners.contains(provider.cfgLister));
+
+
+ }
+
+ @Test
+ public void testDeactivate() {
+ this.addDevice();
+ provider.deactivate(null);
+ assertAfter(500, () ->
+ assertNull("Device should be removed", controller.getDevice(deviceId)));
+ assertNull("Network config factory not removed", cfgFactory);
+ assertFalse("Network config listener not removed", netCfgListeners.contains(provider.cfgLister));
+ assertFalse("Provider not unregistered", providerRegistry.getProviders().contains(provider.id()));
+ assertNull("Provider registry not removed", provider.providerService);
+ }
+
+ @Test
+ public void eventNotRelevant() {
+ assertFalse("Event should not be relevant", provider.cfgLister.isRelevant(deviceAddedIrrelevantEvent));
+ assertFalse("Device should not be reachable", provider.isReachable(wrongDeviceId));
+ }
+
+ @Test
+ public void addDevice() {
+ assertTrue("Event should be relevant", provider.cfgLister.isRelevant(deviceAddedEvent));
+ provider.cfgLister.event(deviceAddedEvent);
+ AbstractProjectableModel.setDriverService(null, new MockDriverService());
+ //FIXME this needs sleep
+ assertAfter(500, () ->
+ assertNotNull("Device should be added to controller", controller.getDevice(deviceId)));
+ assertTrue("Device should be reachable", provider.isReachable(deviceId));
+ }
+
+ private class MockDeviceProviderRegistry extends DeviceProviderRegistryAdapter {
+
+ Set<ProviderId> providers = new HashSet<>();
+
+ @Override
+ public DeviceProviderService register(DeviceProvider provider) {
+ providers.add(provider.id());
+ return deviceProviderService;
+ }
+
+ @Override
+ public void unregister(DeviceProvider provider) {
+ providers.remove(provider.id());
+ }
+
+ @Override
+ public Set<ProviderId> getProviders() {
+ return providers;
+ }
+
+ }
+
+ private class MockDeviceService extends DeviceServiceAdapter {
+ @Override
+ public Device getDevice(DeviceId deviceId) {
+ return deviceStore.getDevice(deviceId);
+ }
+ }
+
+ private ConfigFactory cfgFactory;
+ private Set<NetworkConfigListener> netCfgListeners = new HashSet<>();
+
+ private class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter {
+
+ @Override
+ public void registerConfigFactory(ConfigFactory configFactory) {
+ cfgFactory = configFactory;
+ }
+
+ @Override
+ public void unregisterConfigFactory(ConfigFactory configFactory) {
+ cfgFactory = null;
+ }
+
+ @Override
+ public void addListener(NetworkConfigListener listener) {
+ netCfgListeners.add(listener);
+ }
+
+ @Override
+ public void removeListener(NetworkConfigListener listener) {
+ netCfgListeners.remove(listener);
+ }
+
+
+ @Override
+ public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
+ if (configClass.equals(SnmpProviderConfig.class)) {
+ return (C) snmpProviderConfig;
+ } else {
+ return (C) new BasicDeviceConfig();
+ }
+ }
+ }
+
+ private class MockDeviceStore extends DeviceStoreAdapter {
+ protected HashMap<DeviceId, Device> devices = new HashMap<>();
+
+ @Override
+ public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId,
+ DeviceDescription desc) {
+
+ devices.put(deviceId, new DefaultDevice(providerId, deviceId, desc.type(),
+ desc.manufacturer(), desc.hwVersion(),
+ desc.swVersion(), desc.serialNumber(),
+ desc.chassisId(), DefaultAnnotations.builder().build()));
+ return null;
+ }
+
+ @Override
+ public Device getDevice(DeviceId deviceId) {
+ return devices.get(deviceId);
+ }
+
+ }
+
+ private class MockCoreService extends CoreServiceAdapter {
+ @Override
+ public ApplicationId registerApplication(String name) {
+ return applicationId;
+ }
+ }
+
+ private class MockDeviceProviderService extends DeviceProviderServiceAdapter {
+ DeviceStore store = deviceStore;
+
+ @Override
+ public void deviceConnected(DeviceId deviceId, DeviceDescription desc) {
+ store.createOrUpdateDevice(ProviderId.NONE, deviceId, desc);
+ }
+ }
+
+ private class MockSnmpProviderConfig extends SnmpProviderConfig {
+ protected SnmpDeviceInfo deviceInfo = new SnmpDeviceInfo(IpAddress.valueOf("1.1.1.1"), 1, "test", "test");
+
+ @Override
+ public Set<SnmpProviderConfig.SnmpDeviceInfo> getDevicesInfo() throws ConfigException {
+ return ImmutableSet.of(deviceInfo);
+ }
+
+ }
+
+ private class MockDriverService extends DriverServiceAdapter {
+
+ }
+}
\ No newline at end of file