ONOS-7647 - add mock tests for ciena driver
Change-Id: I6e5b0d1333c5d138c2fb3bcdbf9b9ccecc86ce54
diff --git a/drivers/netconf/src/test/java/org/onosproject/drivers/netconf/MockDriverHandler.java b/drivers/netconf/src/test/java/org/onosproject/drivers/netconf/MockDriverHandler.java
index bfa6d51..d898158 100644
--- a/drivers/netconf/src/test/java/org/onosproject/drivers/netconf/MockDriverHandler.java
+++ b/drivers/netconf/src/test/java/org/onosproject/drivers/netconf/MockDriverHandler.java
@@ -15,12 +15,25 @@
*/
package org.onosproject.drivers.netconf;
+import static org.junit.Assert.fail;
+
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathFactory;
+
+import org.apache.commons.io.IOUtils;
import org.onosproject.core.CoreService;
import org.onosproject.mastership.MastershipService;
import org.onosproject.net.DeviceId;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.AbstractDriverLoader;
import org.onosproject.net.driver.Behaviour;
import org.onosproject.net.driver.DefaultDriver;
import org.onosproject.net.driver.DefaultDriverData;
@@ -30,33 +43,78 @@
import org.onosproject.net.flow.FlowRuleProgrammable;
import org.onosproject.netconf.NetconfController;
import org.onosproject.netconf.NetconfException;
+import org.w3c.dom.Document;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import com.google.common.io.Resources;
public class MockDriverHandler implements DriverHandler {
private DriverData mockDriverData;
-
private NetconfController ncc;
private CoreService coreService;
+ private DeviceService deviceService;
private MastershipService mastershipService;
- public MockDriverHandler() throws NetconfException {
- Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours =
- new HashMap<Class<? extends Behaviour>, Class<? extends Behaviour>>();
- behaviours.put(FlowRuleProgrammable.class, FlowRuleProgrammable.class);
-
+ // Centralize some initialization.
+ private void init(Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours, DeviceId mockDeviceId,
+ CoreService mockCoreService, DeviceService mockDeviceService) throws NetconfException {
Map<String, String> properties = new HashMap<String, String>();
- Driver mockDriver =
- new DefaultDriver("mockDriver", null, "ONOSProject", "1.0.0", "1.0.0", behaviours, properties);
- DeviceId mockDeviceId = DeviceId.deviceId("netconf:1.2.3.4:830");
+ Driver mockDriver = new DefaultDriver("mockDriver", null, "ONOSProject", "1.0.0", "1.0.0", behaviours,
+ properties);
mockDriverData = new DefaultDriverData(mockDriver, mockDeviceId);
-
ncc = new MockNetconfController();
ncc.connectDevice(mockDeviceId);
-
- coreService = new MockCoreService();
+ coreService = mockCoreService;
mastershipService = new MockMastershipService();
+ deviceService = mockDeviceService;
+ }
+
+ @SuppressWarnings("unchecked")
+ public MockDriverHandler(Class<? extends AbstractDriverLoader> loaderClass, String behaviorSpec,
+ DeviceId mockDeviceId, CoreService mockCoreService, DeviceService mockDeviceService) {
+
+ // Had to split into declaration and initialization to make stylecheck happy
+ // else line was considered too long
+ // and auto format couldn't be tweak to make it correct
+ Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours;
+ behaviours = new HashMap<Class<? extends Behaviour>, Class<? extends Behaviour>>();
+
+ try {
+ String data = Resources.toString(Resources.getResource(loaderClass, behaviorSpec), StandardCharsets.UTF_8);
+ InputStream resp = IOUtils.toInputStream(data, StandardCharsets.UTF_8);
+ DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder builder = builderFactory.newDocumentBuilder();
+ Document document = builder.parse(resp);
+
+ XPath xp = XPathFactory.newInstance().newXPath();
+ NodeList list = (NodeList) xp.evaluate("//behaviour", document, XPathConstants.NODESET);
+ for (int i = 0; i < list.getLength(); i += 1) {
+ Node node = list.item(i);
+ NamedNodeMap attrs = node.getAttributes();
+ Class<? extends Behaviour> api = (Class<? extends Behaviour>) Class
+ .forName(attrs.getNamedItem("api").getNodeValue());
+ Class<? extends Behaviour> impl = (Class<? extends Behaviour>) Class
+ .forName(attrs.getNamedItem("impl").getNodeValue());
+ behaviours.put(api, impl);
+ }
+ init(behaviours, mockDeviceId, mockCoreService, mockDeviceService);
+ } catch (Exception e) {
+ fail(e.toString());
+ }
+ }
+
+ public MockDriverHandler() throws NetconfException {
+ Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours;
+ behaviours = new HashMap<Class<? extends Behaviour>, Class<? extends Behaviour>>();
+ behaviours.put(FlowRuleProgrammable.class, FlowRuleProgrammable.class);
+ DeviceId mockDeviceId = DeviceId.deviceId("netconf:1.2.3.4:830");
+ coreService = new MockCoreService();
+ init(behaviours, mockDeviceId, coreService, null);
}
@Override
@@ -75,17 +133,17 @@
return null;
}
+ @SuppressWarnings("unchecked")
@Override
public <T> T get(Class<T> serviceClass) {
if (serviceClass.equals(NetconfController.class)) {
return (T) ncc;
-
} else if (serviceClass.equals(CoreService.class)) {
return (T) coreService;
-
} else if (serviceClass.equals(MastershipService.class)) {
return (T) mastershipService;
-
+ } else if (serviceClass.equals(DeviceService.class)) {
+ return (T) deviceService;
}
return null;