Thomas Vachuska
Committed by Gerrit Code Review

Enhancing the driver subsystem to allow retrieving originating data/handler cont…

…exts from the behaviours.

Change-Id: I973888190d569e7e147376b5ae4da9d2f2d9c620
......@@ -15,6 +15,8 @@
*/
package org.onosproject.net.driver;
import static com.google.common.base.Preconditions.checkState;
/**
* Base implementation of device driver behaviour.
*/
......@@ -23,12 +25,13 @@ public class AbstractBehaviour implements Behaviour {
private DriverData data;
@Override
public void setData(DriverData data) {
this.data = data;
public DriverData data() {
return data;
}
@Override
public DriverData data() {
return data;
public void setData(DriverData data) {
checkState(this.data == null, "Driver data already set");
this.data = data;
}
}
......
/*
* Copyright 2015 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;
import static com.google.common.base.Preconditions.checkState;
/**
* Base implementation of device driver handler behaviour.
*/
public class AbstractHandlerBehaviour
extends AbstractBehaviour implements HandlerBehaviour {
private DriverHandler handler;
@Override
public DriverHandler handler() {
return handler;
}
@Override
public void setHandler(DriverHandler handler) {
checkState(this.handler == null, "Driver handler already set");
this.handler = handler;
}
}
......@@ -23,17 +23,17 @@ package org.onosproject.net.driver;
public interface Behaviour {
/**
* Sets the driver data context on this this behaviour should operate.
* Returns the driver data context.
*
* @param data driver data
* @return driver data
*/
void setData(DriverData data);
DriverData data();
/**
* Obtains the driver data.
* Sets the driver data context on this this behaviour should operate.
*
* @return driver data
* @param data driver data
*/
DriverData data();
void setData(DriverData data);
}
......
......@@ -63,13 +63,6 @@ public class DefaultDriver implements Driver {
this.properties = copyOf(checkNotNull(properties, "Properties cannot be null"));
}
/**
* Merges the two drivers while giving preference to this driver when
* dealing with conflicts.
*
* @param other other driver
* @return new driver
*/
@Override
public Driver merge(Driver other) {
// Merge the behaviours.
......@@ -121,19 +114,22 @@ public class DefaultDriver implements Driver {
return behaviours.containsKey(behaviourClass);
}
/**
* Creates an instance of behaviour primed with the specified driver data.
*
* @param data driver data context
* @param behaviourClass driver behaviour class
* @param handler indicates behaviour is intended for handler context
* @param <T> type of behaviour
* @return behaviour instance
*/
@Override
public <T extends Behaviour> T createBehaviour(DriverData data,
Class<T> behaviourClass,
boolean handler) {
checkArgument(handler || !HandlerBehaviour.class.isAssignableFrom(behaviourClass),
Class<T> behaviourClass) {
return createBehaviour(data, null, behaviourClass);
}
@Override
public <T extends Behaviour> T createBehaviour(DriverHandler handler,
Class<T> behaviourClass) {
return createBehaviour(handler.data(), handler, behaviourClass);
}
// Creates an instance of behaviour primed with the specified driver data.
private <T extends Behaviour> T createBehaviour(DriverData data, DriverHandler handler,
Class<T> behaviourClass) {
checkArgument(handler != null || !HandlerBehaviour.class.isAssignableFrom(behaviourClass),
"{} is applicable only to handler context", behaviourClass.getName());
// Locate the implementation of the requested behaviour.
......@@ -143,6 +139,11 @@ public class DefaultDriver implements Driver {
// Create an instance of the behaviour and apply data as its context.
T behaviour = createBehaviour(behaviourClass, implementation);
behaviour.setData(data);
// If this is a handler behaviour, also apply handler as its context.
if (handler != null) {
((HandlerBehaviour) behaviour).setHandler(handler);
}
return behaviour;
}
......
......@@ -49,7 +49,7 @@ public class DefaultDriverData implements DriverData {
@Override
public <T extends Behaviour> T behaviour(Class<T> behaviourClass) {
return driver.createBehaviour(this, behaviourClass, false);
return driver.createBehaviour(this, behaviourClass);
}
@Override
......
......@@ -45,7 +45,7 @@ public class DefaultDriverHandler implements DriverHandler {
@Override
public <T extends Behaviour> T behaviour(Class<T> behaviourClass) {
return data.driver().createBehaviour(this.data, behaviourClass, true);
return data.driver().createBehaviour(this, behaviourClass);
}
@Override
......
......@@ -84,12 +84,20 @@ public interface Driver extends Annotations {
*
* @param data driver data context
* @param behaviourClass driver behaviour class
* @param handler indicates behaviour is intended for handler context
* @param <T> type of behaviour
* @return behaviour instance
*/
<T extends Behaviour> T createBehaviour(DriverData data, Class<T> behaviourClass,
boolean handler);
<T extends Behaviour> T createBehaviour(DriverData data, Class<T> behaviourClass);
/**
* Creates an instance of behaviour primed with the specified driver data.
*
* @param handler driver handler context
* @param behaviourClass driver behaviour class
* @param <T> type of behaviour
* @return behaviour instance
*/
<T extends Behaviour> T createBehaviour(DriverHandler handler, Class<T> behaviourClass);
/**
* Returns the set of annotations as map of key/value properties.
......
......@@ -20,4 +20,19 @@ package org.onosproject.net.driver;
* with a device (in context of {@link org.onosproject.net.driver.DriverHandler}).
*/
public interface HandlerBehaviour extends Behaviour {
/**
* Returns the driver handler context on which this behaviour operates.
*
* @return driver handler context
*/
DriverHandler handler();
/**
* Sets the driver handler context for this behaviour.
*
* @param handler driver handler
*/
void setHandler(DriverHandler handler);
}
......
......@@ -18,5 +18,5 @@ package org.onosproject.net.driver;
/**
* Test behaviour.
*/
public class TestBehaviourTwoImpl extends AbstractBehaviour implements TestBehaviourTwo {
public class TestBehaviourTwoImpl extends AbstractHandlerBehaviour implements TestBehaviourTwo {
}
......
......@@ -67,7 +67,7 @@ public class XmlDriverLoaderTest {
InputStream stream = getClass().getResourceAsStream("drivers.noconstructor.xml");
DriverProvider provider = loader.loadDrivers(stream);
Driver driver = provider.getDrivers().iterator().next();
driver.createBehaviour(new DefaultDriverData(driver), TestBehaviour.class, false);
driver.createBehaviour(new DefaultDriverData(driver), TestBehaviour.class);
}
}
\ No newline at end of file
......
......@@ -21,7 +21,7 @@ import org.onosproject.core.DefaultGroupId;
import org.onosproject.net.DeviceId;
import org.onosproject.net.behaviour.Pipeliner;
import org.onosproject.net.behaviour.PipelinerContext;
import org.onosproject.net.driver.AbstractBehaviour;
import org.onosproject.net.driver.AbstractHandlerBehaviour;
import org.onosproject.net.flow.DefaultFlowRule;
import org.onosproject.net.flow.FlowRule;
import org.onosproject.net.flow.FlowRuleOperations;
......@@ -41,7 +41,7 @@ import static org.slf4j.LoggerFactory.getLogger;
/**
* Simple single table pipeline abstraction.
*/
public class DefaultSingleTablePipeline extends AbstractBehaviour implements Pipeliner {
public class DefaultSingleTablePipeline extends AbstractHandlerBehaviour implements Pipeliner {
private final Logger log = getLogger(getClass());
......@@ -55,7 +55,6 @@ public class DefaultSingleTablePipeline extends AbstractBehaviour implements Pip
this.deviceId = deviceId;
flowRuleService = serviceDirectory.get(FlowRuleService.class);
}
@Override
......
......@@ -26,7 +26,7 @@ import org.onosproject.core.CoreService;
import org.onosproject.net.DeviceId;
import org.onosproject.net.behaviour.Pipeliner;
import org.onosproject.net.behaviour.PipelinerContext;
import org.onosproject.net.driver.AbstractBehaviour;
import org.onosproject.net.driver.AbstractHandlerBehaviour;
import org.onosproject.net.flow.DefaultFlowRule;
import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.DefaultTrafficTreatment;
......@@ -52,7 +52,7 @@ import static org.slf4j.LoggerFactory.getLogger;
/**
* Corsa pipeline handler.
*/
public class OVSCorsaPipeline extends AbstractBehaviour implements Pipeliner {
public class OVSCorsaPipeline extends AbstractHandlerBehaviour implements Pipeliner {
private static final int CONTROLLER_PRIORITY = 255;
private static final int DROP_PRIORITY = 0;
......@@ -79,7 +79,6 @@ public class OVSCorsaPipeline extends AbstractBehaviour implements Pipeliner {
"org.onosproject.driver.OVSCorsaPipeline");
pushDefaultRules();
}
@Override
......