alshabib
Committed by Gerrit Code Review

WIP: default driver ignition

Change-Id: Ia37de8dcaee2ff2be0908fa12c567acf99ef3a13
...@@ -31,6 +31,8 @@ import org.onosproject.mastership.MastershipService; ...@@ -31,6 +31,8 @@ import org.onosproject.mastership.MastershipService;
31 import org.onosproject.net.Device; 31 import org.onosproject.net.Device;
32 import org.onosproject.net.DeviceId; 32 import org.onosproject.net.DeviceId;
33 import org.onosproject.net.behaviour.Pipeliner; 33 import org.onosproject.net.behaviour.Pipeliner;
34 +import org.onosproject.net.device.DeviceEvent;
35 +import org.onosproject.net.device.DeviceListener;
34 import org.onosproject.net.device.DeviceService; 36 import org.onosproject.net.device.DeviceService;
35 import org.onosproject.net.driver.Driver; 37 import org.onosproject.net.driver.Driver;
36 import org.onosproject.net.driver.DriverHandler; 38 import org.onosproject.net.driver.DriverHandler;
...@@ -73,18 +75,23 @@ public class FlowObjectiveManager implements FlowObjectiveService { ...@@ -73,18 +75,23 @@ public class FlowObjectiveManager implements FlowObjectiveService {
73 75
74 private MastershipListener mastershipListener = new InnerMastershipListener(); 76 private MastershipListener mastershipListener = new InnerMastershipListener();
75 77
78 + private DeviceListener deviceListener = new InnerDeviceListener();
79 +
76 private Map<DeviceId, DriverHandler> driverHandlers = 80 private Map<DeviceId, DriverHandler> driverHandlers =
77 Maps.newConcurrentMap(); 81 Maps.newConcurrentMap();
78 82
79 @Activate 83 @Activate
80 protected void activate() { 84 protected void activate() {
81 mastershipService.addListener(mastershipListener); 85 mastershipService.addListener(mastershipListener);
86 + deviceService.addListener(deviceListener);
87 + deviceService.getDevices().forEach(device -> setupDriver(device.id()));
82 log.info("Started"); 88 log.info("Started");
83 } 89 }
84 90
85 @Deactivate 91 @Deactivate
86 protected void deactivate() { 92 protected void deactivate() {
87 mastershipService.removeListener(mastershipListener); 93 mastershipService.removeListener(mastershipListener);
94 + deviceService.removeListener(deviceListener);
88 log.info("Stopped"); 95 log.info("Stopped");
89 } 96 }
90 97
...@@ -121,43 +128,82 @@ public class FlowObjectiveManager implements FlowObjectiveService { ...@@ -121,43 +128,82 @@ public class FlowObjectiveManager implements FlowObjectiveService {
121 return pipe.next(nextObjectives); 128 return pipe.next(nextObjectives);
122 } 129 }
123 130
131 +
132 +
124 private class InnerMastershipListener implements MastershipListener { 133 private class InnerMastershipListener implements MastershipListener {
125 @Override 134 @Override
126 public void event(MastershipEvent event) { 135 public void event(MastershipEvent event) {
127 switch (event.type()) { 136 switch (event.type()) {
128 137
129 case MASTER_CHANGED: 138 case MASTER_CHANGED:
130 - //TODO: refactor this into a method 139 + setupDriver(event.subject());
131 - if (event.roleInfo().master().equals( 140 +
132 - clusterService.getLocalNode().id())) { 141 + break;
133 - DriverHandler handler = lookupDriver(event.subject()); 142 + case BACKUPS_CHANGED:
134 - if (handler != null) { 143 + break;
135 - Pipeliner pipe = handler.behaviour(Pipeliner.class); 144 + default:
136 - pipe.init(event.subject(), serviceDirectory); 145 + log.warn("Unknown mastership type {}", event.type());
137 - driverHandlers.put(event.subject(), handler);
138 - log.info("Driver {} bound to device {}",
139 - handler.data().type().name(), event.subject());
140 - } else {
141 - log.error("No driver for device {}", event.subject());
142 } 146 }
147 + }
148 +
143 149
144 } 150 }
145 151
152 + private class InnerDeviceListener implements DeviceListener {
153 + @Override
154 + public void event(DeviceEvent event) {
155 + switch (event.type()) {
156 + case DEVICE_ADDED:
157 + case DEVICE_AVAILABILITY_CHANGED:
158 + setupDriver(event.subject().id());
146 break; 159 break;
147 - case BACKUPS_CHANGED: 160 + case DEVICE_UPDATED:
161 + break;
162 + case DEVICE_REMOVED:
163 + break;
164 + case DEVICE_SUSPENDED:
165 + break;
166 + case PORT_ADDED:
167 + break;
168 + case PORT_UPDATED:
169 + break;
170 + case PORT_REMOVED:
148 break; 171 break;
149 default: 172 default:
150 - log.warn("Unknown mastership type {}", event.type()); 173 + log.warn("Unknown event type {}", event.type());
174 + }
151 } 175 }
152 } 176 }
153 177
178 + private void setupDriver(DeviceId deviceId) {
179 + //TODO: Refactor this to make it nicer and use a cache.
180 + if (mastershipService.getMasterFor(
181 + deviceId).equals(clusterService.getLocalNode().id())) {
182 +
183 + DriverHandler handler = lookupDriver(deviceId);
184 + if (handler != null) {
185 + Pipeliner pipe = handler.behaviour(Pipeliner.class);
186 + pipe.init(deviceId, serviceDirectory);
187 + driverHandlers.put(deviceId, handler);
188 + log.info("Driver {} bound to device {}",
189 + handler.data().type().name(), deviceId);
190 + } else {
191 + log.error("No driver for device {}", deviceId);
192 + }
193 + }
194 + }
195 +
196 +
154 private DriverHandler lookupDriver(DeviceId deviceId) { 197 private DriverHandler lookupDriver(DeviceId deviceId) {
155 Device device = deviceService.getDevice(deviceId); 198 Device device = deviceService.getDevice(deviceId);
156 - 199 + if (device == null) {
200 + log.warn("Device is null!");
201 + return null;
202 + }
157 Driver driver = driverService.getDriver(device.manufacturer(), 203 Driver driver = driverService.getDriver(device.manufacturer(),
158 device.hwVersion(), device.swVersion()); 204 device.hwVersion(), device.swVersion());
159 205
160 return driverService.createHandler(driver.name(), deviceId); 206 return driverService.createHandler(driver.name(), deviceId);
161 } 207 }
162 - } 208 +
163 } 209 }
......
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.driver.pipeline;
17 +
18 +import org.onlab.osgi.ServiceDirectory;
19 +import org.onlab.packet.Ethernet;
20 +import org.onlab.packet.MacAddress;
21 +import org.onlab.packet.VlanId;
22 +import org.onosproject.core.ApplicationId;
23 +import org.onosproject.core.CoreService;
24 +import org.onosproject.net.DeviceId;
25 +import org.onosproject.net.behaviour.Pipeliner;
26 +import org.onosproject.net.driver.DriverData;
27 +import org.onosproject.net.flow.DefaultFlowRule;
28 +import org.onosproject.net.flow.DefaultTrafficSelector;
29 +import org.onosproject.net.flow.DefaultTrafficTreatment;
30 +import org.onosproject.net.flow.FlowRule;
31 +import org.onosproject.net.flow.FlowRuleOperations;
32 +import org.onosproject.net.flow.FlowRuleOperationsContext;
33 +import org.onosproject.net.flow.FlowRuleService;
34 +import org.onosproject.net.flow.TrafficSelector;
35 +import org.onosproject.net.flow.TrafficTreatment;
36 +import org.onosproject.net.flowobjective.FilteringObjective;
37 +import org.onosproject.net.flowobjective.ForwardingObjective;
38 +import org.onosproject.net.flowobjective.NextObjective;
39 +import org.slf4j.Logger;
40 +
41 +import java.util.Collection;
42 +import java.util.concurrent.Future;
43 +
44 +import static org.slf4j.LoggerFactory.getLogger;
45 +
46 +/**
47 + * Created by ash on 07/04/15.
48 + */
49 +public class OVSCorsaPipeline implements Pipeliner {
50 +
51 + private static final int CONTROLLER_PRIORITY = 255;
52 + private static final int DROP_PRIORITY = 0;
53 + private static final int HIGHEST_PRIORITY = 0xffff;
54 +
55 + private final Logger log = getLogger(getClass());
56 +
57 + private ServiceDirectory serviceDirectory;
58 + private FlowRuleService flowRuleService;
59 + private CoreService coreService;
60 + private DeviceId deviceId;
61 + private ApplicationId appId;
62 +
63 + @Override
64 + public void init(DeviceId deviceId, ServiceDirectory serviceDirectory) {
65 + this.serviceDirectory = serviceDirectory;
66 + this.deviceId = deviceId;
67 +
68 +
69 + coreService = serviceDirectory.get(CoreService.class);
70 + flowRuleService = serviceDirectory.get(FlowRuleService.class);
71 +
72 + appId = coreService.registerApplication(
73 + "org.onosproject.driver.OVSCorsaPipeline");
74 +
75 + pushDefaultRules();
76 +
77 + }
78 +
79 + @Override
80 + public Future<Boolean> filter(Collection<FilteringObjective> filteringObjectives) {
81 + return null;
82 + }
83 +
84 + @Override
85 + public Future<Boolean> forward(Collection<ForwardingObjective> forwardObjectives) {
86 + return null;
87 + }
88 +
89 + @Override
90 + public Future<Boolean> next(Collection<NextObjective> nextObjectives) {
91 + return null;
92 + }
93 +
94 + @Override
95 + public void setData(DriverData data) {
96 +
97 + }
98 +
99 +
100 + private void pushDefaultRules() {
101 + boolean install = true;
102 + processTableZero(install);
103 + processTableOne(install);
104 + processTableTwo(install);
105 + processTableFour(install);
106 + processTableFive(install);
107 + processTableSix(install);
108 + processTableNine(install);
109 + }
110 +
111 + private void processTableZero(boolean install) {
112 + TrafficSelector.Builder selector;
113 + TrafficTreatment.Builder treatment;
114 +
115 + // Bcast rule
116 + selector = DefaultTrafficSelector.builder();
117 + treatment = DefaultTrafficTreatment.builder();
118 +
119 + selector.matchEthDst(MacAddress.BROADCAST);
120 + treatment.transition(FlowRule.Type.VLAN_MPLS);
121 +
122 + FlowRule rule = new DefaultFlowRule(deviceId, selector.build(),
123 + treatment.build(),
124 + CONTROLLER_PRIORITY, appId, 0,
125 + true, FlowRule.Type.FIRST);
126 +
127 + FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
128 +
129 + ops = install ? ops.add(rule) : ops.remove(rule);
130 +
131 +
132 +
133 + //Drop rule
134 + selector = DefaultTrafficSelector.builder();
135 + treatment = DefaultTrafficTreatment.builder();
136 +
137 + treatment.drop();
138 +
139 + rule = new DefaultFlowRule(deviceId, selector.build(),
140 + treatment.build(), DROP_PRIORITY, appId,
141 + 0, true, FlowRule.Type.FIRST);
142 +
143 + ops = install ? ops.add(rule) : ops.remove(rule);
144 +
145 + flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
146 + @Override
147 + public void onSuccess(FlowRuleOperations ops) {
148 + log.info("Provisioned default table for bgp router");
149 + }
150 +
151 + @Override
152 + public void onError(FlowRuleOperations ops) {
153 + log.info("Failed to provision default table for bgp router");
154 + }
155 + }));
156 +
157 + }
158 +
159 + private void processTableOne(boolean install) {
160 + TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
161 + TrafficTreatment.Builder treatment = DefaultTrafficTreatment
162 + .builder();
163 + FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
164 + FlowRule rule;
165 +
166 + selector.matchVlanId(VlanId.ANY);
167 + treatment.transition(FlowRule.Type.VLAN);
168 +
169 + rule = new DefaultFlowRule(deviceId, selector.build(),
170 + treatment.build(), CONTROLLER_PRIORITY,
171 + appId, 0, true, FlowRule.Type.VLAN_MPLS);
172 +
173 + ops = install ? ops.add(rule) : ops.remove(rule);
174 +
175 + flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
176 + @Override
177 + public void onSuccess(FlowRuleOperations ops) {
178 + log.info("Provisioned vlan/mpls table for bgp router");
179 + }
180 +
181 + @Override
182 + public void onError(FlowRuleOperations ops) {
183 + log.info(
184 + "Failed to provision vlan/mpls table for bgp router");
185 + }
186 + }));
187 +
188 + }
189 +
190 + private void processTableTwo(boolean install) {
191 + TrafficSelector.Builder selector;
192 + TrafficTreatment.Builder treatment;
193 + FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
194 + FlowRule rule;
195 +
196 +
197 +
198 + //Drop rule
199 + selector = DefaultTrafficSelector.builder();
200 + treatment = DefaultTrafficTreatment.builder();
201 +
202 + treatment.drop();
203 +
204 + rule = new DefaultFlowRule(deviceId, selector.build(),
205 + treatment.build(), DROP_PRIORITY, appId,
206 + 0, true, FlowRule.Type.VLAN);
207 +
208 + ops = install ? ops.add(rule) : ops.remove(rule);
209 +
210 + flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
211 + @Override
212 + public void onSuccess(FlowRuleOperations ops) {
213 + log.info("Provisioned vlan table for bgp router");
214 + }
215 +
216 + @Override
217 + public void onError(FlowRuleOperations ops) {
218 + log.info("Failed to provision vlan table for bgp router");
219 + }
220 + }));
221 + }
222 +
223 + private void processTableFour(boolean install) {
224 + TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
225 + TrafficTreatment.Builder treatment = DefaultTrafficTreatment
226 + .builder();
227 + FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
228 + FlowRule rule;
229 +
230 + selector.matchEthType(Ethernet.TYPE_ARP);
231 + treatment.punt();
232 +
233 + rule = new DefaultFlowRule(deviceId, selector.build(),
234 + treatment.build(), CONTROLLER_PRIORITY,
235 + appId, 0, true, FlowRule.Type.ETHER);
236 +
237 + ops = install ? ops.add(rule) : ops.remove(rule);
238 +
239 + selector = DefaultTrafficSelector.builder();
240 + treatment = DefaultTrafficTreatment.builder();
241 +
242 + selector.matchEthType(Ethernet.TYPE_IPV4);
243 + treatment.transition(FlowRule.Type.COS);
244 +
245 + rule = new DefaultFlowRule(deviceId, selector.build(),
246 + treatment.build(), CONTROLLER_PRIORITY,
247 + appId, 0, true, FlowRule.Type.ETHER);
248 +
249 + ops = install ? ops.add(rule) : ops.remove(rule);
250 +
251 + //Drop rule
252 + selector = DefaultTrafficSelector.builder();
253 + treatment = DefaultTrafficTreatment.builder();
254 +
255 + treatment.drop();
256 +
257 + rule = new DefaultFlowRule(deviceId, selector.build(),
258 + treatment.build(), DROP_PRIORITY, appId,
259 + 0, true, FlowRule.Type.ETHER);
260 +
261 + ops = install ? ops.add(rule) : ops.remove(rule);
262 +
263 + flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
264 + @Override
265 + public void onSuccess(FlowRuleOperations ops) {
266 + log.info("Provisioned ether table for bgp router");
267 + }
268 +
269 + @Override
270 + public void onError(FlowRuleOperations ops) {
271 + log.info("Failed to provision ether table for bgp router");
272 + }
273 + }));
274 +
275 + }
276 +
277 + private void processTableFive(boolean install) {
278 + TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
279 + TrafficTreatment.Builder treatment = DefaultTrafficTreatment
280 + .builder();
281 + FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
282 + FlowRule rule;
283 +
284 + treatment.transition(FlowRule.Type.IP);
285 +
286 + rule = new DefaultFlowRule(deviceId, selector.build(),
287 + treatment.build(), DROP_PRIORITY, appId,
288 + 0, true, FlowRule.Type.COS);
289 +
290 + ops = install ? ops.add(rule) : ops.remove(rule);
291 +
292 + flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
293 + @Override
294 + public void onSuccess(FlowRuleOperations ops) {
295 + log.info("Provisioned cos table for bgp router");
296 + }
297 +
298 + @Override
299 + public void onError(FlowRuleOperations ops) {
300 + log.info("Failed to provision cos table for bgp router");
301 + }
302 + }));
303 +
304 + }
305 +
306 + private void processTableSix(boolean install) {
307 + TrafficSelector.Builder selector;
308 + TrafficTreatment.Builder treatment;
309 + FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
310 + FlowRule rule;
311 +
312 + //Drop rule
313 + selector = DefaultTrafficSelector.builder();
314 + treatment = DefaultTrafficTreatment.builder();
315 +
316 + treatment.drop();
317 +
318 + rule = new DefaultFlowRule(deviceId, selector.build(),
319 + treatment.build(), DROP_PRIORITY, appId,
320 + 0, true, FlowRule.Type.IP);
321 +
322 + ops = install ? ops.add(rule) : ops.remove(rule);
323 +
324 + flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
325 + @Override
326 + public void onSuccess(FlowRuleOperations ops) {
327 + log.info("Provisioned FIB table for bgp router");
328 + }
329 +
330 + @Override
331 + public void onError(FlowRuleOperations ops) {
332 + log.info("Failed to provision FIB table for bgp router");
333 + }
334 + }));
335 + }
336 +
337 + private void processTableNine(boolean install) {
338 + TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
339 + TrafficTreatment.Builder treatment = DefaultTrafficTreatment
340 + .builder();
341 + FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
342 + FlowRule rule;
343 +
344 + treatment.punt();
345 +
346 + rule = new DefaultFlowRule(deviceId, selector.build(),
347 + treatment.build(), CONTROLLER_PRIORITY,
348 + appId, 0, true, FlowRule.Type.DEFAULT);
349 +
350 + ops = install ? ops.add(rule) : ops.remove(rule);
351 +
352 + flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
353 + @Override
354 + public void onSuccess(FlowRuleOperations ops) {
355 + log.info("Provisioned Local table for bgp router");
356 + }
357 +
358 + @Override
359 + public void onError(FlowRuleOperations ops) {
360 + log.info("Failed to provision Local table for bgp router");
361 + }
362 + }));
363 + }
364 +
365 +}
...@@ -19,4 +19,8 @@ ...@@ -19,4 +19,8 @@
19 <behaviour api="org.onosproject.net.behaviour.Pipeliner" 19 <behaviour api="org.onosproject.net.behaviour.Pipeliner"
20 impl="org.onosproject.driver.pipeline.DefaultSingleTablePipeline"/> 20 impl="org.onosproject.driver.pipeline.DefaultSingleTablePipeline"/>
21 </driver> 21 </driver>
22 + <driver name="ovs-corsa" manufacturer="Nicira, Inc." hwVersion="Open vSwitch" swVersion="2.3.0">
23 + <behaviour api="org.onosproject.net.behaviour.Pipeliner"
24 + impl="org.onosproject.driver.pipeline.OVSCorsaPipeline"/>
25 + </driver>
22 </drivers> 26 </drivers>
...\ No newline at end of file ...\ No newline at end of file
......