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 @@ ...@@ -15,6 +15,8 @@
15 */ 15 */
16 package org.onosproject.net.driver; 16 package org.onosproject.net.driver;
17 17
18 +import static com.google.common.base.Preconditions.checkState;
19 +
18 /** 20 /**
19 * Base implementation of device driver behaviour. 21 * Base implementation of device driver behaviour.
20 */ 22 */
...@@ -23,12 +25,13 @@ public class AbstractBehaviour implements Behaviour { ...@@ -23,12 +25,13 @@ public class AbstractBehaviour implements Behaviour {
23 private DriverData data; 25 private DriverData data;
24 26
25 @Override 27 @Override
26 - public void setData(DriverData data) { 28 + public DriverData data() {
27 - this.data = data; 29 + return data;
28 } 30 }
29 31
30 @Override 32 @Override
31 - public DriverData data() { 33 + public void setData(DriverData data) {
32 - return data; 34 + checkState(this.data == null, "Driver data already set");
35 + this.data = data;
33 } 36 }
34 } 37 }
......
1 +/*
2 + * Copyright 2015 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 +package org.onosproject.net.driver;
17 +
18 +import static com.google.common.base.Preconditions.checkState;
19 +
20 +/**
21 + * Base implementation of device driver handler behaviour.
22 + */
23 +public class AbstractHandlerBehaviour
24 + extends AbstractBehaviour implements HandlerBehaviour {
25 +
26 + private DriverHandler handler;
27 +
28 + @Override
29 + public DriverHandler handler() {
30 + return handler;
31 + }
32 +
33 + @Override
34 + public void setHandler(DriverHandler handler) {
35 + checkState(this.handler == null, "Driver handler already set");
36 + this.handler = handler;
37 + }
38 +}
...@@ -23,17 +23,17 @@ package org.onosproject.net.driver; ...@@ -23,17 +23,17 @@ package org.onosproject.net.driver;
23 public interface Behaviour { 23 public interface Behaviour {
24 24
25 /** 25 /**
26 - * Sets the driver data context on this this behaviour should operate. 26 + * Returns the driver data context.
27 * 27 *
28 - * @param data driver data 28 + * @return driver data
29 */ 29 */
30 - void setData(DriverData data); 30 + DriverData data();
31 31
32 /** 32 /**
33 - * Obtains the driver data. 33 + * Sets the driver data context on this this behaviour should operate.
34 * 34 *
35 - * @return driver data 35 + * @param data driver data
36 */ 36 */
37 - DriverData data(); 37 + void setData(DriverData data);
38 38
39 } 39 }
......
...@@ -63,13 +63,6 @@ public class DefaultDriver implements Driver { ...@@ -63,13 +63,6 @@ public class DefaultDriver implements Driver {
63 this.properties = copyOf(checkNotNull(properties, "Properties cannot be null")); 63 this.properties = copyOf(checkNotNull(properties, "Properties cannot be null"));
64 } 64 }
65 65
66 - /**
67 - * Merges the two drivers while giving preference to this driver when
68 - * dealing with conflicts.
69 - *
70 - * @param other other driver
71 - * @return new driver
72 - */
73 @Override 66 @Override
74 public Driver merge(Driver other) { 67 public Driver merge(Driver other) {
75 // Merge the behaviours. 68 // Merge the behaviours.
...@@ -121,19 +114,22 @@ public class DefaultDriver implements Driver { ...@@ -121,19 +114,22 @@ public class DefaultDriver implements Driver {
121 return behaviours.containsKey(behaviourClass); 114 return behaviours.containsKey(behaviourClass);
122 } 115 }
123 116
124 - /** 117 + @Override
125 - * Creates an instance of behaviour primed with the specified driver data.
126 - *
127 - * @param data driver data context
128 - * @param behaviourClass driver behaviour class
129 - * @param handler indicates behaviour is intended for handler context
130 - * @param <T> type of behaviour
131 - * @return behaviour instance
132 - */
133 public <T extends Behaviour> T createBehaviour(DriverData data, 118 public <T extends Behaviour> T createBehaviour(DriverData data,
134 - Class<T> behaviourClass, 119 + Class<T> behaviourClass) {
135 - boolean handler) { 120 + return createBehaviour(data, null, behaviourClass);
136 - checkArgument(handler || !HandlerBehaviour.class.isAssignableFrom(behaviourClass), 121 + }
122 +
123 + @Override
124 + public <T extends Behaviour> T createBehaviour(DriverHandler handler,
125 + Class<T> behaviourClass) {
126 + return createBehaviour(handler.data(), handler, behaviourClass);
127 + }
128 +
129 + // Creates an instance of behaviour primed with the specified driver data.
130 + private <T extends Behaviour> T createBehaviour(DriverData data, DriverHandler handler,
131 + Class<T> behaviourClass) {
132 + checkArgument(handler != null || !HandlerBehaviour.class.isAssignableFrom(behaviourClass),
137 "{} is applicable only to handler context", behaviourClass.getName()); 133 "{} is applicable only to handler context", behaviourClass.getName());
138 134
139 // Locate the implementation of the requested behaviour. 135 // Locate the implementation of the requested behaviour.
...@@ -143,6 +139,11 @@ public class DefaultDriver implements Driver { ...@@ -143,6 +139,11 @@ public class DefaultDriver implements Driver {
143 // Create an instance of the behaviour and apply data as its context. 139 // Create an instance of the behaviour and apply data as its context.
144 T behaviour = createBehaviour(behaviourClass, implementation); 140 T behaviour = createBehaviour(behaviourClass, implementation);
145 behaviour.setData(data); 141 behaviour.setData(data);
142 +
143 + // If this is a handler behaviour, also apply handler as its context.
144 + if (handler != null) {
145 + ((HandlerBehaviour) behaviour).setHandler(handler);
146 + }
146 return behaviour; 147 return behaviour;
147 } 148 }
148 149
......
...@@ -49,7 +49,7 @@ public class DefaultDriverData implements DriverData { ...@@ -49,7 +49,7 @@ public class DefaultDriverData implements DriverData {
49 49
50 @Override 50 @Override
51 public <T extends Behaviour> T behaviour(Class<T> behaviourClass) { 51 public <T extends Behaviour> T behaviour(Class<T> behaviourClass) {
52 - return driver.createBehaviour(this, behaviourClass, false); 52 + return driver.createBehaviour(this, behaviourClass);
53 } 53 }
54 54
55 @Override 55 @Override
......
...@@ -45,7 +45,7 @@ public class DefaultDriverHandler implements DriverHandler { ...@@ -45,7 +45,7 @@ public class DefaultDriverHandler implements DriverHandler {
45 45
46 @Override 46 @Override
47 public <T extends Behaviour> T behaviour(Class<T> behaviourClass) { 47 public <T extends Behaviour> T behaviour(Class<T> behaviourClass) {
48 - return data.driver().createBehaviour(this.data, behaviourClass, true); 48 + return data.driver().createBehaviour(this, behaviourClass);
49 } 49 }
50 50
51 @Override 51 @Override
......
...@@ -84,12 +84,20 @@ public interface Driver extends Annotations { ...@@ -84,12 +84,20 @@ public interface Driver extends Annotations {
84 * 84 *
85 * @param data driver data context 85 * @param data driver data context
86 * @param behaviourClass driver behaviour class 86 * @param behaviourClass driver behaviour class
87 - * @param handler indicates behaviour is intended for handler context
88 * @param <T> type of behaviour 87 * @param <T> type of behaviour
89 * @return behaviour instance 88 * @return behaviour instance
90 */ 89 */
91 - <T extends Behaviour> T createBehaviour(DriverData data, Class<T> behaviourClass, 90 + <T extends Behaviour> T createBehaviour(DriverData data, Class<T> behaviourClass);
92 - boolean handler); 91 +
92 + /**
93 + * Creates an instance of behaviour primed with the specified driver data.
94 + *
95 + * @param handler driver handler context
96 + * @param behaviourClass driver behaviour class
97 + * @param <T> type of behaviour
98 + * @return behaviour instance
99 + */
100 + <T extends Behaviour> T createBehaviour(DriverHandler handler, Class<T> behaviourClass);
93 101
94 /** 102 /**
95 * Returns the set of annotations as map of key/value properties. 103 * Returns the set of annotations as map of key/value properties.
......
...@@ -20,4 +20,19 @@ package org.onosproject.net.driver; ...@@ -20,4 +20,19 @@ package org.onosproject.net.driver;
20 * with a device (in context of {@link org.onosproject.net.driver.DriverHandler}). 20 * with a device (in context of {@link org.onosproject.net.driver.DriverHandler}).
21 */ 21 */
22 public interface HandlerBehaviour extends Behaviour { 22 public interface HandlerBehaviour extends Behaviour {
23 +
24 + /**
25 + * Returns the driver handler context on which this behaviour operates.
26 + *
27 + * @return driver handler context
28 + */
29 + DriverHandler handler();
30 +
31 + /**
32 + * Sets the driver handler context for this behaviour.
33 + *
34 + * @param handler driver handler
35 + */
36 + void setHandler(DriverHandler handler);
37 +
23 } 38 }
......
...@@ -18,5 +18,5 @@ package org.onosproject.net.driver; ...@@ -18,5 +18,5 @@ package org.onosproject.net.driver;
18 /** 18 /**
19 * Test behaviour. 19 * Test behaviour.
20 */ 20 */
21 -public class TestBehaviourTwoImpl extends AbstractBehaviour implements TestBehaviourTwo { 21 +public class TestBehaviourTwoImpl extends AbstractHandlerBehaviour implements TestBehaviourTwo {
22 } 22 }
......
...@@ -67,7 +67,7 @@ public class XmlDriverLoaderTest { ...@@ -67,7 +67,7 @@ public class XmlDriverLoaderTest {
67 InputStream stream = getClass().getResourceAsStream("drivers.noconstructor.xml"); 67 InputStream stream = getClass().getResourceAsStream("drivers.noconstructor.xml");
68 DriverProvider provider = loader.loadDrivers(stream); 68 DriverProvider provider = loader.loadDrivers(stream);
69 Driver driver = provider.getDrivers().iterator().next(); 69 Driver driver = provider.getDrivers().iterator().next();
70 - driver.createBehaviour(new DefaultDriverData(driver), TestBehaviour.class, false); 70 + driver.createBehaviour(new DefaultDriverData(driver), TestBehaviour.class);
71 } 71 }
72 72
73 } 73 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -21,7 +21,7 @@ import org.onosproject.core.DefaultGroupId; ...@@ -21,7 +21,7 @@ import org.onosproject.core.DefaultGroupId;
21 import org.onosproject.net.DeviceId; 21 import org.onosproject.net.DeviceId;
22 import org.onosproject.net.behaviour.Pipeliner; 22 import org.onosproject.net.behaviour.Pipeliner;
23 import org.onosproject.net.behaviour.PipelinerContext; 23 import org.onosproject.net.behaviour.PipelinerContext;
24 -import org.onosproject.net.driver.AbstractBehaviour; 24 +import org.onosproject.net.driver.AbstractHandlerBehaviour;
25 import org.onosproject.net.flow.DefaultFlowRule; 25 import org.onosproject.net.flow.DefaultFlowRule;
26 import org.onosproject.net.flow.FlowRule; 26 import org.onosproject.net.flow.FlowRule;
27 import org.onosproject.net.flow.FlowRuleOperations; 27 import org.onosproject.net.flow.FlowRuleOperations;
...@@ -41,7 +41,7 @@ import static org.slf4j.LoggerFactory.getLogger; ...@@ -41,7 +41,7 @@ import static org.slf4j.LoggerFactory.getLogger;
41 /** 41 /**
42 * Simple single table pipeline abstraction. 42 * Simple single table pipeline abstraction.
43 */ 43 */
44 -public class DefaultSingleTablePipeline extends AbstractBehaviour implements Pipeliner { 44 +public class DefaultSingleTablePipeline extends AbstractHandlerBehaviour implements Pipeliner {
45 45
46 private final Logger log = getLogger(getClass()); 46 private final Logger log = getLogger(getClass());
47 47
...@@ -55,7 +55,6 @@ public class DefaultSingleTablePipeline extends AbstractBehaviour implements Pip ...@@ -55,7 +55,6 @@ public class DefaultSingleTablePipeline extends AbstractBehaviour implements Pip
55 this.deviceId = deviceId; 55 this.deviceId = deviceId;
56 56
57 flowRuleService = serviceDirectory.get(FlowRuleService.class); 57 flowRuleService = serviceDirectory.get(FlowRuleService.class);
58 -
59 } 58 }
60 59
61 @Override 60 @Override
......
...@@ -26,7 +26,7 @@ import org.onosproject.core.CoreService; ...@@ -26,7 +26,7 @@ import org.onosproject.core.CoreService;
26 import org.onosproject.net.DeviceId; 26 import org.onosproject.net.DeviceId;
27 import org.onosproject.net.behaviour.Pipeliner; 27 import org.onosproject.net.behaviour.Pipeliner;
28 import org.onosproject.net.behaviour.PipelinerContext; 28 import org.onosproject.net.behaviour.PipelinerContext;
29 -import org.onosproject.net.driver.AbstractBehaviour; 29 +import org.onosproject.net.driver.AbstractHandlerBehaviour;
30 import org.onosproject.net.flow.DefaultFlowRule; 30 import org.onosproject.net.flow.DefaultFlowRule;
31 import org.onosproject.net.flow.DefaultTrafficSelector; 31 import org.onosproject.net.flow.DefaultTrafficSelector;
32 import org.onosproject.net.flow.DefaultTrafficTreatment; 32 import org.onosproject.net.flow.DefaultTrafficTreatment;
...@@ -52,7 +52,7 @@ import static org.slf4j.LoggerFactory.getLogger; ...@@ -52,7 +52,7 @@ import static org.slf4j.LoggerFactory.getLogger;
52 /** 52 /**
53 * Corsa pipeline handler. 53 * Corsa pipeline handler.
54 */ 54 */
55 -public class OVSCorsaPipeline extends AbstractBehaviour implements Pipeliner { 55 +public class OVSCorsaPipeline extends AbstractHandlerBehaviour implements Pipeliner {
56 56
57 private static final int CONTROLLER_PRIORITY = 255; 57 private static final int CONTROLLER_PRIORITY = 255;
58 private static final int DROP_PRIORITY = 0; 58 private static final int DROP_PRIORITY = 0;
...@@ -79,7 +79,6 @@ public class OVSCorsaPipeline extends AbstractBehaviour implements Pipeliner { ...@@ -79,7 +79,6 @@ public class OVSCorsaPipeline extends AbstractBehaviour implements Pipeliner {
79 "org.onosproject.driver.OVSCorsaPipeline"); 79 "org.onosproject.driver.OVSCorsaPipeline");
80 80
81 pushDefaultRules(); 81 pushDefaultRules();
82 -
83 } 82 }
84 83
85 @Override 84 @Override
......