Andrea Campanella
Committed by Gerrit Code Review

ONOS-3650 Device driver multiple inheritance

Change-Id: Ib7b72d44533d4e63c4122662b50485243562aa21
......@@ -16,8 +16,11 @@
package org.onosproject.net.driver;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
......@@ -26,14 +29,17 @@ import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableMap.copyOf;
import static org.slf4j.LoggerFactory.getLogger;
/**
* Default implementation of extensible driver.
*/
public class DefaultDriver implements Driver {
private final Logger log = getLogger(getClass());
private final String name;
private final Driver parent;
private final List<Driver> parents;
private final String manufacturer;
private final String hwVersion;
......@@ -53,12 +59,37 @@ public class DefaultDriver implements Driver {
* @param behaviours device behaviour classes
* @param properties properties for configuration of device behaviour classes
*/
@Deprecated
public DefaultDriver(String name, Driver parent, String manufacturer,
String hwVersion, String swVersion,
Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours,
Map<String, String> properties) {
this.name = checkNotNull(name, "Name cannot be null");
this.parent = parent;
this.parents = parent == null ? null : Lists.newArrayList(parent);
this.manufacturer = checkNotNull(manufacturer, "Manufacturer cannot be null");
this.hwVersion = checkNotNull(hwVersion, "HW version cannot be null");
this.swVersion = checkNotNull(swVersion, "SW version cannot be null");
this.behaviours = copyOf(checkNotNull(behaviours, "Behaviours cannot be null"));
this.properties = copyOf(checkNotNull(properties, "Properties cannot be null"));
}
/**
* Creates a driver with the specified name.
*
* @param name driver name
* @param parents optional parent drivers
* @param manufacturer device manufacturer
* @param hwVersion device hardware version
* @param swVersion device software version
* @param behaviours device behaviour classes
* @param properties properties for configuration of device behaviour classes
*/
public DefaultDriver(String name, List<Driver> parents, String manufacturer,
String hwVersion, String swVersion,
Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours,
Map<String, String> properties) {
this.name = checkNotNull(name, "Name cannot be null");
this.parents = parents == null || parents.isEmpty() ? null : parents;
this.manufacturer = checkNotNull(manufacturer, "Manufacturer cannot be null");
this.hwVersion = checkNotNull(hwVersion, "HW version cannot be null");
this.swVersion = checkNotNull(swVersion, "SW version cannot be null");
......@@ -68,7 +99,7 @@ public class DefaultDriver implements Driver {
@Override
public Driver merge(Driver other) {
checkArgument(parent == null || Objects.equals(parent, other.parent()),
checkArgument(parents == null || Objects.equals(parent(), other.parent()),
"Parent drivers are not the same");
// Merge the behaviours.
......@@ -81,7 +112,8 @@ public class DefaultDriver implements Driver {
ImmutableMap.Builder<String, String> properties = ImmutableMap.builder();
properties.putAll(this.properties).putAll(other.properties());
return new DefaultDriver(name, other.parent(), manufacturer, hwVersion, swVersion,
return new DefaultDriver(name, other.parents(),
manufacturer, hwVersion, swVersion,
ImmutableMap.copyOf(behaviours), properties.build());
}
......@@ -107,7 +139,12 @@ public class DefaultDriver implements Driver {
@Override
public Driver parent() {
return parent;
return parents == null ? null : parents.get(0);
}
@Override
public List<Driver> parents() {
return parents;
}
@Override
......@@ -123,7 +160,8 @@ public class DefaultDriver implements Driver {
@Override
public boolean hasBehaviour(Class<? extends Behaviour> behaviourClass) {
return behaviours.containsKey(behaviourClass) ||
(parent != null && parent.hasBehaviour(behaviourClass));
(parents != null && parents.stream()
.filter(parent -> parent.hasBehaviour(behaviourClass)).count() > 0);
}
@Override
......@@ -132,8 +170,14 @@ public class DefaultDriver implements Driver {
T behaviour = createBehaviour(data, null, behaviourClass);
if (behaviour != null) {
return behaviour;
} else if (parent != null) {
return parent.createBehaviour(data, behaviourClass);
} else if (parents != null) {
for (Driver parent : Lists.reverse(parents)) {
try {
return parent.createBehaviour(data, behaviourClass);
} catch (IllegalArgumentException e) {
log.debug("Parent {} does not support behaviour {}", parent, behaviourClass);
}
}
}
throw new IllegalArgumentException(behaviourClass.getName() + " not supported");
}
......@@ -144,8 +188,14 @@ public class DefaultDriver implements Driver {
T behaviour = createBehaviour(handler.data(), handler, behaviourClass);
if (behaviour != null) {
return behaviour;
} else if (parent != null) {
return parent.createBehaviour(handler, behaviourClass);
} else if (parents != null && !parents.isEmpty()) {
for (Driver parent : Lists.reverse(parents)) {
try {
return parent.createBehaviour(handler, behaviourClass);
} catch (IllegalArgumentException e) {
log.debug("Parent {} does not support behaviour {}", parent, behaviourClass);
}
}
}
throw new IllegalArgumentException(behaviourClass.getName() + " not supported");
}
......@@ -202,7 +252,7 @@ public class DefaultDriver implements Driver {
public String toString() {
return toStringHelper(this)
.add("name", name)
.add("parent", parent)
.add("parents", parents)
.add("manufacturer", manufacturer)
.add("hwVersion", hwVersion)
.add("swVersion", swVersion)
......
......@@ -17,6 +17,7 @@ package org.onosproject.net.driver;
import org.onosproject.net.Annotations;
import java.util.List;
import java.util.Map;
import java.util.Set;
......@@ -40,9 +41,18 @@ public interface Driver extends Annotations {
*
* @return parent driver; null if driver has no parent
*/
@Deprecated
Driver parent();
/**
* Returns all the parent drivers from which this driver inherits behaviours
* and properties.
*
* @return list of parent drivers; null if driver has no parent
*/
List<Driver> parents();
/**
* Returns the device manufacturer name.
*
* @return manufacturer name
......
......@@ -16,6 +16,7 @@
package org.onosproject.net.driver;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalConfiguration;
......@@ -23,7 +24,10 @@ import org.apache.commons.configuration.XMLConfiguration;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Utility capable of reading driver configuration XML resources and producing
......@@ -126,13 +130,22 @@ public class XmlDriverLoader {
public DefaultDriver loadDriver(HierarchicalConfiguration driverCfg,
DriverResolver resolver) {
String name = driverCfg.getString(NAME);
String parentName = driverCfg.getString(EXTENDS);
String parentsString = driverCfg.getString(EXTENDS, "");
List<Driver> parents = Lists.newArrayList();
if (!parentsString.equals("")) {
List<String> parentsNames;
if (parentsString.contains(",")) {
parentsNames = Arrays.asList(parentsString.replace(" ", "").split(","));
} else {
parentsNames = Lists.newArrayList(parentsString);
}
parents = parentsNames.stream().map(parent -> (parent != null) ?
resolve(parent, resolver) : null).collect(Collectors.toList());
}
String manufacturer = driverCfg.getString(MFG, "");
String hwVersion = driverCfg.getString(HW, "");
String swVersion = driverCfg.getString(SW, "");
Driver parent = parentName != null ? resolve(parentName, resolver) : null;
return new DefaultDriver(name, parent, manufacturer, hwVersion, swVersion,
return new DefaultDriver(name, parents, manufacturer, hwVersion, swVersion,
parseBehaviours(driverCfg),
parseProperties(driverCfg));
}
......
......@@ -20,6 +20,8 @@ import org.junit.Before;
import org.junit.Test;
import org.onosproject.net.DeviceId;
import java.util.ArrayList;
import static org.junit.Assert.*;
import static org.onosproject.net.DeviceId.deviceId;
......@@ -32,7 +34,7 @@ public class DefaultDriverDataTest {
@Before
public void setUp() {
ddc = new DefaultDriver("foo.bar", null, "Circus", "lux", "1.2a",
ddc = new DefaultDriver("foo.bar", new ArrayList<>(), "Circus", "lux", "1.2a",
ImmutableMap.of(TestBehaviour.class,
TestBehaviourImpl.class),
ImmutableMap.of("foo", "bar"));
......
......@@ -19,6 +19,8 @@ import com.google.common.collect.ImmutableMap;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
......@@ -30,7 +32,7 @@ public class DefaultDriverHandlerTest {
@Before
public void setUp() {
ddc = new DefaultDriver("foo.bar", null, "Circus", "lux", "1.2a",
ddc = new DefaultDriver("foo.bar", new ArrayList<>(), "Circus", "lux", "1.2a",
ImmutableMap.of(TestBehaviour.class,
TestBehaviourImpl.class,
TestBehaviourTwo.class,
......
......@@ -18,6 +18,7 @@ package org.onosproject.net.driver;
import com.google.common.collect.ImmutableMap;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Set;
import static com.google.common.collect.ImmutableSet.of;
......@@ -28,15 +29,15 @@ public class DefaultDriverProviderTest {
@Test
public void basics() {
DefaultDriverProvider ddp = new DefaultDriverProvider();
DefaultDriver one = new DefaultDriver("foo.bar", null, "Circus", "lux", "1.2a",
DefaultDriver one = new DefaultDriver("foo.bar", new ArrayList<>(), "Circus", "lux", "1.2a",
ImmutableMap.of(TestBehaviour.class,
TestBehaviourImpl.class),
ImmutableMap.of("foo", "bar"));
DefaultDriver two = new DefaultDriver("foo.bar", null, "", "", "",
DefaultDriver two = new DefaultDriver("foo.bar", new ArrayList<>(), "", "", "",
ImmutableMap.of(TestBehaviourTwo.class,
TestBehaviourTwoImpl.class),
ImmutableMap.of("goo", "wee"));
DefaultDriver three = new DefaultDriver("goo.foo", null, "BigTop", "better", "2.2",
DefaultDriver three = new DefaultDriver("goo.foo", new ArrayList<>(), "BigTop", "better", "2.2",
ImmutableMap.of(TestBehaviourTwo.class,
TestBehaviourTwoImpl.class),
ImmutableMap.of("goo", "gee"));
......
......@@ -18,6 +18,8 @@ package org.onosproject.net.driver;
import com.google.common.collect.ImmutableMap;
import org.junit.Test;
import java.util.ArrayList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.onosproject.net.driver.DefaultDriverDataTest.DEVICE_ID;
......@@ -26,7 +28,7 @@ public class DefaultDriverTest {
@Test
public void basics() {
DefaultDriver ddp = new DefaultDriver("foo.base", null, "Circus", "lux", "1.2a",
DefaultDriver ddp = new DefaultDriver("foo.base", new ArrayList<>(), "Circus", "lux", "1.2a",
ImmutableMap.of(TestBehaviour.class,
TestBehaviourImpl.class,
TestBehaviourTwo.class,
......@@ -62,12 +64,12 @@ public class DefaultDriverTest {
@Test
public void merge() {
DefaultDriver one = new DefaultDriver("foo.bar", null, "Circus", "lux", "1.2a",
DefaultDriver one = new DefaultDriver("foo.bar", new ArrayList<>(), "Circus", "lux", "1.2a",
ImmutableMap.of(TestBehaviour.class,
TestBehaviourImpl.class),
ImmutableMap.of("foo", "bar"));
Driver ddc =
one.merge(new DefaultDriver("foo.bar", null, "", "", "",
one.merge(new DefaultDriver("foo.bar", new ArrayList<>(), "", "", "",
ImmutableMap.of(TestBehaviourTwo.class,
TestBehaviourTwoImpl.class),
ImmutableMap.of("goo", "wee")));
......
/*
* 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.driver;
/**
* Test behaviour.
*/
public class TestBehaviourImpl2 extends AbstractHandlerBehaviour implements TestBehaviour {
}
/*
* 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.driver;
/**
* Test behaviour.
*/
public interface TestBehaviourThree extends HandlerBehaviour {
}
/*
* 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.driver;
/**
* Test behaviour.
*/
public class TestBehaviourThreeImpl extends AbstractHandlerBehaviour implements TestBehaviourThree {
}
......@@ -16,6 +16,7 @@
package org.onosproject.net.driver;
import org.junit.Test;
import org.onosproject.net.DeviceId;
import java.io.IOException;
import java.io.InputStream;
......@@ -77,4 +78,38 @@ public class XmlDriverLoaderTest {
driver.createBehaviour(new DefaultDriverData(driver, DEVICE_ID), TestBehaviour.class);
}
@Test
public void multipleDrivers() throws IOException {
XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader());
InputStream stream = getClass().getResourceAsStream("drivers.multipleInheritance.xml");
DriverProvider provider = loader.loadDrivers(stream, null);
Iterator<Driver> iterator = provider.getDrivers().iterator();
Driver driver;
do {
driver = iterator.next();
} while (!driver.name().equals("foo.2"));
assertTrue("incorrect multiple behaviour inheritance", driver.hasBehaviour(TestBehaviour.class));
assertTrue("incorrect multiple behaviour inheritance", driver.hasBehaviour(TestBehaviourTwo.class));
}
@Test
public void multipleDriversSameBehaviors() throws IOException {
XmlDriverLoader loader = new XmlDriverLoader(getClass().getClassLoader());
InputStream stream = getClass().getResourceAsStream("drivers.sameMultipleInheritance.xml");
DriverProvider provider = loader.loadDrivers(stream, null);
Iterator<Driver> iterator = provider.getDrivers().iterator();
Driver driver;
do {
driver = iterator.next();
} while (!driver.name().equals("foo.2"));
assertTrue("incorrect multiple behaviour inheritance", driver.hasBehaviour(TestBehaviour.class));
Behaviour b2 = driver.createBehaviour(new DefaultDriverHandler(
new DefaultDriverData(
driver, DeviceId.deviceId("test_device"))),
TestBehaviour.class);
assertTrue("incorrect multiple same behaviour inheritance", b2.getClass()
.getSimpleName().equals("TestBehaviourImpl2"));
assertTrue("incorrect multiple behaviour inheritance", driver.hasBehaviour(TestBehaviourTwo.class));
}
}
......
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<drivers>
<driver name="foo.0" manufacturer="Circus" hwVersion="1.2" swVersion="2.0">
<behaviour api="org.onosproject.net.driver.TestBehaviour"
impl="org.onosproject.net.driver.TestBehaviourImpl"/>
</driver>
<driver name="foo.1" extends="foo.0" manufacturer="Circus" hwVersion="1.2a" swVersion="2.2">
<fingerprint>ding</fingerprint>
<fingerprint>bat</fingerprint>
<behaviour api="org.onosproject.net.driver.TestBehaviourTwo"
impl="org.onosproject.net.driver.TestBehaviourTwoImpl"/>
<property name="p1">v1</property>
<property name="p2">v2</property>
</driver>
<driver name="foo.2" extends="foo.0,foo.1" manufacturer="Circus" hwVersion="1.2" swVersion="2.0">
<behaviour api="org.onosproject.net.driver.TestBehaviourThree"
impl="org.onosproject.net.driver.TestBehaviourThreeImpl"/>
</driver>
</drivers>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<drivers>
<driver name="foo.0" manufacturer="Circus" hwVersion="1.2" swVersion="2.0">
<behaviour api="org.onosproject.net.driver.TestBehaviour"
impl="org.onosproject.net.driver.TestBehaviourImpl"/>
</driver>
<driver name="foo.1" extends="foo.0" manufacturer="Circus" hwVersion="1.2a" swVersion="2.2">
<fingerprint>ding</fingerprint>
<fingerprint>bat</fingerprint>
<behaviour api="org.onosproject.net.driver.TestBehaviour"
impl="org.onosproject.net.driver.TestBehaviourImpl2"/>
<behaviour api="org.onosproject.net.driver.TestBehaviourTwo"
impl="org.onosproject.net.driver.TestBehaviourTwoImpl"/>
<property name="p1">v1</property>
<property name="p2">v2</property>
</driver>
<driver name="foo.2" extends="foo.0,foo.1" manufacturer="Circus" hwVersion="1.2" swVersion="2.0">
<behaviour api="org.onosproject.net.driver.TestBehaviourThree"
impl="org.onosproject.net.driver.TestBehaviourThreeImpl"/>
</driver>
</drivers>
\ No newline at end of file
......@@ -16,6 +16,7 @@
package org.onosproject.codec.impl;
import java.util.ArrayList;
import java.util.Map;
import org.junit.Test;
......@@ -48,7 +49,7 @@ public class DriverCodecTest {
Map<String, String> properties =
ImmutableMap.of("key1", "value1", "key2", "value2");
DefaultDriver parent = new DefaultDriver("parent", null, "Acme",
DefaultDriver parent = new DefaultDriver("parent", new ArrayList<>(), "Acme",
"HW1.2.3", "SW1.2.3",
behaviours,
properties);
......
......@@ -46,7 +46,7 @@
impl="org.onosproject.driver.query.FullMplsAvailable" />
</driver>
<!--This driver is for simulated NETCONF devices through of-config tool on top og OVSDB-->
<driver name="ovs-netconf" extends="default"
<driver name="ovs-netconf" extends="default,ovs"
manufacturer="" hwVersion="" swVersion="">
<behaviour api="org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver"
impl="org.onosproject.driver.handshaker.NiciraSwitchHandshaker"/>
......
......@@ -28,6 +28,8 @@ import org.onosproject.net.driver.DefaultDriverHandler;
import org.onosproject.ovsdb.controller.driver.OvsdbClientServiceAdapter;
import org.onosproject.ovsdb.controller.driver.OvsdbControllerAdapter;
import java.util.ArrayList;
/**
* Created by Andrea on 10/7/15.
*/
......@@ -51,7 +53,7 @@ public class OvsdbControllerConfigTest {
public void setUp() {
controllerConfig = new OvsdbControllerConfig();
ddc = new DefaultDriver("foo.bar", null, "Circus", "lux", "1.2a",
ddc = new DefaultDriver("foo.bar", new ArrayList<>(), "Circus", "lux", "1.2a",
ImmutableMap.of(ControllerConfig.class,
OvsdbControllerConfig.class),
ImmutableMap.of("foo", "bar"));
......
......@@ -15,6 +15,7 @@
*/
package org.onosproject.openflow;
import java.util.List;
import java.util.Map;
import java.util.Set;
......@@ -39,6 +40,11 @@ public class DriverAdapter implements Driver {
}
@Override
public List<Driver> parents() {
return null;
}
@Override
public String manufacturer() {
return null;
}
......