alshabib

added FlowObjectiveService to act as an objective manager between applications and drivers

Change-Id: I4dc44db8bafae8a55b7663895c87b1b08645637f
...@@ -17,8 +17,10 @@ package org.onosproject.net.behaviour; ...@@ -17,8 +17,10 @@ package org.onosproject.net.behaviour;
17 17
18 import org.onlab.osgi.ServiceDirectory; 18 import org.onlab.osgi.ServiceDirectory;
19 import org.onosproject.net.DeviceId; 19 import org.onosproject.net.DeviceId;
20 +import org.onosproject.net.driver.HandlerBehaviour;
20 import org.onosproject.net.flowobjective.FilteringObjective; 21 import org.onosproject.net.flowobjective.FilteringObjective;
21 import org.onosproject.net.flowobjective.ForwardingObjective; 22 import org.onosproject.net.flowobjective.ForwardingObjective;
23 +import org.onosproject.net.flowobjective.NextObjective;
22 24
23 import java.util.Collection; 25 import java.util.Collection;
24 import java.util.concurrent.Future; 26 import java.util.concurrent.Future;
...@@ -26,7 +28,7 @@ import java.util.concurrent.Future; ...@@ -26,7 +28,7 @@ import java.util.concurrent.Future;
26 /** 28 /**
27 * Behaviour for handling various pipelines. 29 * Behaviour for handling various pipelines.
28 */ 30 */
29 -public interface Pipeliner { 31 +public interface Pipeliner extends HandlerBehaviour {
30 32
31 /** 33 /**
32 * Injecting the service directory into the driver. 34 * Injecting the service directory into the driver.
...@@ -39,17 +41,24 @@ public interface Pipeliner { ...@@ -39,17 +41,24 @@ public interface Pipeliner {
39 /** 41 /**
40 * Installs the filtering rules onto the device. 42 * Installs the filtering rules onto the device.
41 * 43 *
42 - * @param filters the collection of filters 44 + * @param filteringObjectives the collection of filters
43 * @return a future indicating the success of the operation 45 * @return a future indicating the success of the operation
44 */ 46 */
45 - Future<Boolean> filter(Collection<FilteringObjective> filters); 47 + Future<Boolean> filter(Collection<FilteringObjective> filteringObjectives);
46 48
47 /** 49 /**
48 * Installs the forwarding rules onto the device. 50 * Installs the forwarding rules onto the device.
49 * 51 *
50 - * @param forwardings the collection of forwarding objectives 52 + * @param forwardObjectives the collection of forwarding objectives
51 * @return a future indicating the success of the operation 53 * @return a future indicating the success of the operation
52 */ 54 */
53 - Future<Boolean> forward(Collection<ForwardingObjective> forwardings); 55 + Future<Boolean> forward(Collection<ForwardingObjective> forwardObjectives);
54 56
57 + /**
58 + * Installs the next hop elements into the device.
59 + *
60 + * @param nextObjectives the collection of next objectives
61 + * @return a future indicating the success of the operation
62 + */
63 + Future<Boolean> next(Collection<NextObjective> nextObjectives);
55 } 64 }
......
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.flowobjective;
17 +
18 +import org.onosproject.net.DeviceId;
19 +
20 +import java.util.Collection;
21 +import java.util.concurrent.Future;
22 +
23 +/**
24 + * Created by ash on 07/04/15.
25 + */
26 +public interface FlowObjectiveService {
27 +
28 + Future<Boolean> filter(DeviceId deviceId, Collection<FilteringObjective> filterObjectives);
29 +
30 + Future<Boolean> forward(DeviceId deviceId, Collection<ForwardingObjective> forwardingObjectives);
31 +
32 + Future<Boolean> next(DeviceId deviceId, Collection<NextObjective> nextObjectives);
33 +
34 +}
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.impl;
17 +
18 +import com.google.common.collect.ImmutableSet;
19 +import com.google.common.collect.Maps;
20 +import com.google.common.collect.Sets;
21 +import org.apache.felix.scr.annotations.Activate;
22 +import org.apache.felix.scr.annotations.Component;
23 +import org.apache.felix.scr.annotations.Deactivate;
24 +import org.apache.felix.scr.annotations.Service;
25 +import org.onosproject.net.DeviceId;
26 +import org.onosproject.net.driver.Behaviour;
27 +import org.onosproject.net.driver.DefaultDriverData;
28 +import org.onosproject.net.driver.DefaultDriverHandler;
29 +import org.onosproject.net.driver.Driver;
30 +import org.onosproject.net.driver.DriverAdminService;
31 +import org.onosproject.net.driver.DriverData;
32 +import org.onosproject.net.driver.DriverHandler;
33 +import org.onosproject.net.driver.DriverProvider;
34 +import org.slf4j.Logger;
35 +import org.slf4j.LoggerFactory;
36 +
37 +import java.util.Map;
38 +import java.util.Set;
39 +
40 +
41 +@Component(immediate = true)
42 +@Service
43 +public class DriverManager implements DriverAdminService {
44 +
45 + private final Logger log = LoggerFactory.getLogger(getClass());
46 +
47 + private Set<DriverProvider> providers = Sets.newConcurrentHashSet();
48 + private Map<String, Driver> driverByName = Maps.newConcurrentMap();
49 + private Map<String, Driver> driverByKey = Maps.newConcurrentMap();
50 +
51 + @Activate
52 + protected void activate() {
53 + log.info("Started");
54 + }
55 +
56 + @Deactivate
57 + protected void deactivate() {
58 + log.info("Stopped");
59 + }
60 +
61 +
62 + @Override
63 + public Set<DriverProvider> getProviders() {
64 + return ImmutableSet.copyOf(providers);
65 + }
66 +
67 + @Override
68 + public void registerProvider(DriverProvider provider) {
69 + provider.getDrivers().forEach(driver -> {
70 + driverByName.put(driver.name(), driver);
71 + driverByKey.put(key(driver.manufacturer(),
72 + driver.hwVersion(),
73 + driver.swVersion()), driver);
74 + });
75 + providers.add(provider);
76 + }
77 +
78 + @Override
79 + public void unregisterProvider(DriverProvider provider) {
80 + provider.getDrivers().forEach(driver -> {
81 + driverByName.remove(driver.name());
82 + driverByKey.remove(key(driver.manufacturer(),
83 + driver.hwVersion(),
84 + driver.swVersion()));
85 + });
86 + providers.remove(provider);
87 + }
88 +
89 + @Override
90 + public Set<Driver> getDrivers(Class<? extends Behaviour>... withBehaviours) {
91 + //TODO
92 + return null;
93 + }
94 +
95 + @Override
96 + public Driver getDriver(String driverName) {
97 + //TODO: replace with fallback driver.
98 + return driverByName.getOrDefault(driverName, driverByName.get("default"));
99 + }
100 +
101 + @Override
102 + public Driver getDriver(String mfr, String hw, String sw) {
103 + return driverByKey.getOrDefault(key(mfr, hw, sw), driverByName.get("default"));
104 + }
105 +
106 + @Override
107 + public DriverHandler createHandler(String driverName, DeviceId deviceId, String... credentials) {
108 + Driver driver = driverByName.get(driverName);
109 + return new DefaultDriverHandler(new DefaultDriverData(driver));
110 + }
111 +
112 + @Override
113 + public DriverHandler createHandler(DriverData data, DeviceId deviceId, String... credentials) {
114 + return null;
115 + }
116 +
117 + private String key(String mfr, String hw, String sw) {
118 + return String.format("%s-%s-%s", mfr, hw, sw);
119 + }
120 +
121 +}
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.flowobjective.impl;
17 +
18 +import com.google.common.collect.Maps;
19 +import org.apache.felix.scr.annotations.Activate;
20 +import org.apache.felix.scr.annotations.Component;
21 +import org.apache.felix.scr.annotations.Deactivate;
22 +import org.apache.felix.scr.annotations.Reference;
23 +import org.apache.felix.scr.annotations.ReferenceCardinality;
24 +import org.apache.felix.scr.annotations.Service;
25 +import org.onlab.osgi.DefaultServiceDirectory;
26 +import org.onlab.osgi.ServiceDirectory;
27 +import org.onosproject.cluster.ClusterService;
28 +import org.onosproject.mastership.MastershipEvent;
29 +import org.onosproject.mastership.MastershipListener;
30 +import org.onosproject.mastership.MastershipService;
31 +import org.onosproject.net.Device;
32 +import org.onosproject.net.DeviceId;
33 +import org.onosproject.net.behaviour.Pipeliner;
34 +import org.onosproject.net.device.DeviceService;
35 +import org.onosproject.net.driver.Driver;
36 +import org.onosproject.net.driver.DriverHandler;
37 +import org.onosproject.net.driver.DriverService;
38 +import org.onosproject.net.flowobjective.FilteringObjective;
39 +import org.onosproject.net.flowobjective.FlowObjectiveService;
40 +import org.onosproject.net.flowobjective.ForwardingObjective;
41 +import org.onosproject.net.flowobjective.NextObjective;
42 +import org.slf4j.Logger;
43 +import org.slf4j.LoggerFactory;
44 +
45 +import java.util.Collection;
46 +import java.util.Map;
47 +import java.util.concurrent.Future;
48 +
49 +import static com.google.common.base.Preconditions.checkState;
50 +
51 +/**
52 + * Created by ash on 07/04/15.
53 + */
54 +@Component(immediate = true)
55 +@Service
56 +public class FlowObjectiveManager implements FlowObjectiveService {
57 +
58 + private final Logger log = LoggerFactory.getLogger(getClass());
59 +
60 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 + protected DriverService driverService;
62 +
63 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 + protected DeviceService deviceService;
65 +
66 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 + protected MastershipService mastershipService;
68 +
69 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 + protected ClusterService clusterService;
71 +
72 + protected ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
73 +
74 + private MastershipListener mastershipListener = new InnerMastershipListener();
75 +
76 + private Map<DeviceId, DriverHandler> driverHandlers =
77 + Maps.newConcurrentMap();
78 +
79 + @Activate
80 + protected void activate() {
81 + mastershipService.addListener(mastershipListener);
82 + log.info("Started");
83 + }
84 +
85 + @Deactivate
86 + protected void deactivate() {
87 + mastershipService.removeListener(mastershipListener);
88 + log.info("Stopped");
89 + }
90 +
91 + @Override
92 + public Future<Boolean> filter(DeviceId deviceId,
93 + Collection<FilteringObjective> filterObjectives) {
94 + DriverHandler handler = driverHandlers.get(deviceId);
95 + checkState(handler != null, "Driver not initialized");
96 +
97 + Pipeliner pipe = handler.behaviour(Pipeliner.class);
98 +
99 + return pipe.filter(filterObjectives);
100 + }
101 +
102 + @Override
103 + public Future<Boolean> forward(DeviceId deviceId,
104 + Collection<ForwardingObjective> forwardingObjectives) {
105 + DriverHandler handler = driverHandlers.get(deviceId);
106 + checkState(handler != null, "Driver not initialized");
107 +
108 + Pipeliner pipe = handler.behaviour(Pipeliner.class);
109 +
110 + return pipe.forward(forwardingObjectives);
111 + }
112 +
113 + @Override
114 + public Future<Boolean> next(DeviceId deviceId,
115 + Collection<NextObjective> nextObjectives) {
116 + DriverHandler handler = driverHandlers.get(deviceId);
117 + checkState(handler != null, "Driver not initialized");
118 +
119 + Pipeliner pipe = handler.behaviour(Pipeliner.class);
120 +
121 + return pipe.next(nextObjectives);
122 + }
123 +
124 + private class InnerMastershipListener implements MastershipListener {
125 + @Override
126 + public void event(MastershipEvent event) {
127 + switch (event.type()) {
128 +
129 + case MASTER_CHANGED:
130 + //TODO: refactor this into a method
131 + if (event.roleInfo().master().equals(
132 + clusterService.getLocalNode().id())) {
133 + DriverHandler handler = lookupDriver(event.subject());
134 + if (handler != null) {
135 + Pipeliner pipe = handler.behaviour(Pipeliner.class);
136 + pipe.init(event.subject(), serviceDirectory);
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 + }
143 +
144 + }
145 +
146 + break;
147 + case BACKUPS_CHANGED:
148 + break;
149 + default:
150 + log.warn("Unknown mastership type {}", event.type());
151 + }
152 + }
153 +
154 + private DriverHandler lookupDriver(DeviceId deviceId) {
155 + Device device = deviceService.getDevice(deviceId);
156 +
157 + Driver driver = driverService.getDriver(device.manufacturer(),
158 + device.hwVersion(), device.swVersion());
159 +
160 + return driverService.createHandler(driver.name(), deviceId);
161 + }
162 + }
163 +}
...@@ -49,12 +49,21 @@ ...@@ -49,12 +49,21 @@
49 <scope>test</scope> 49 <scope>test</scope>
50 </dependency> 50 </dependency>
51 51
52 + <dependency>
53 + <groupId>org.apache.felix</groupId>
54 + <artifactId>org.apache.felix.scr.annotations</artifactId>
55 + </dependency>
56 +
52 </dependencies> 57 </dependencies>
53 58
54 <build> 59 <build>
55 <plugins> 60 <plugins>
56 <plugin> 61 <plugin>
57 <groupId>org.apache.felix</groupId> 62 <groupId>org.apache.felix</groupId>
63 + <artifactId>maven-scr-plugin</artifactId>
64 + </plugin>
65 + <plugin>
66 + <groupId>org.apache.felix</groupId>
58 <artifactId>maven-bundle-plugin</artifactId> 67 <artifactId>maven-bundle-plugin</artifactId>
59 </plugin> 68 </plugin>
60 69
......
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.apache.felix.scr.annotations.Activate;
19 +import org.apache.felix.scr.annotations.Deactivate;
20 +import org.apache.felix.scr.annotations.Reference;
21 +import org.apache.felix.scr.annotations.ReferenceCardinality;
22 +import org.onosproject.net.driver.DriverAdminService;
23 +import org.onosproject.net.driver.DriverProvider;
24 +import org.onosproject.net.driver.XmlDriverLoader;
25 +import org.apache.felix.scr.annotations.Component;
26 +import org.slf4j.Logger;
27 +import org.slf4j.LoggerFactory;
28 +
29 +import java.io.IOException;
30 +
31 +/**
32 + * Bootstrap for built in drivers.
33 + */
34 +@Component(immediate = true)
35 +public class DefaultDrivers {
36 +
37 + private final Logger log = LoggerFactory.getLogger(getClass());
38 +
39 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
40 + protected DriverAdminService driverService;
41 +
42 + private DriverProvider provider;
43 +
44 + @Activate
45 + protected void activate() {
46 + XmlDriverLoader xmlDriverLoader =
47 + new XmlDriverLoader(getClass().getClassLoader());
48 + try {
49 + provider = xmlDriverLoader.loadDrivers(
50 + getClass().getResourceAsStream("/default.xml"));
51 + driverService.registerProvider(
52 + provider);
53 + } catch (IOException e) {
54 + log.warn("Unable to load drivers");
55 + }
56 +
57 + log.info("Started");
58 + }
59 +
60 + @Deactivate
61 + protected void deactivate() {
62 + driverService.unregisterProvider(provider);
63 + log.info("Stopped");
64 + }
65 +
66 +
67 +}
...@@ -20,6 +20,7 @@ import org.onlab.osgi.ServiceDirectory; ...@@ -20,6 +20,7 @@ import org.onlab.osgi.ServiceDirectory;
20 import org.onosproject.core.DefaultGroupId; 20 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.driver.DriverData;
23 import org.onosproject.net.flow.DefaultFlowRule; 24 import org.onosproject.net.flow.DefaultFlowRule;
24 import org.onosproject.net.flow.FlowRule; 25 import org.onosproject.net.flow.FlowRule;
25 import org.onosproject.net.flow.FlowRuleOperations; 26 import org.onosproject.net.flow.FlowRuleOperations;
...@@ -28,6 +29,7 @@ import org.onosproject.net.flow.FlowRuleService; ...@@ -28,6 +29,7 @@ import org.onosproject.net.flow.FlowRuleService;
28 import org.onosproject.net.flow.TrafficSelector; 29 import org.onosproject.net.flow.TrafficSelector;
29 import org.onosproject.net.flowobjective.FilteringObjective; 30 import org.onosproject.net.flowobjective.FilteringObjective;
30 import org.onosproject.net.flowobjective.ForwardingObjective; 31 import org.onosproject.net.flowobjective.ForwardingObjective;
32 +import org.onosproject.net.flowobjective.NextObjective;
31 import org.slf4j.Logger; 33 import org.slf4j.Logger;
32 34
33 import java.util.Collection; 35 import java.util.Collection;
...@@ -106,4 +108,14 @@ public class DefaultSingleTablePipeline implements Pipeliner { ...@@ -106,4 +108,14 @@ public class DefaultSingleTablePipeline implements Pipeliner {
106 })); 108 }));
107 return future; 109 return future;
108 } 110 }
111 +
112 + @Override
113 + public Future<Boolean> next(Collection<NextObjective> nextObjectives) {
114 + throw new UnsupportedOperationException("Single table does not next hop.");
115 + }
116 +
117 + @Override
118 + public void setData(DriverData data) {
119 +
120 + }
109 } 121 }
......
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<!--
3 + ~ Copyright 2015 Open Networking Laboratory
4 + ~
5 + ~ Licensed under the Apache License, Version 2.0 (the "License");
6 + ~ you may not use this file except in compliance with the License.
7 + ~ You may obtain a copy of the License at
8 + ~
9 + ~ http://www.apache.org/licenses/LICENSE-2.0
10 + ~
11 + ~ Unless required by applicable law or agreed to in writing, software
12 + ~ distributed under the License is distributed on an "AS IS" BASIS,
13 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 + ~ See the License for the specific language governing permissions and
15 + ~ limitations under the License.
16 + -->
17 +<drivers>
18 + <driver name="default" manufacturer="ON.Lab" hwVersion="0.0.1" swVersion="0.0.1">
19 + <behaviour api="org.onosproject.net.behaviour.Pipeliner"
20 + impl="org.onosproject.driver.pipeline.DefaultSingleTablePipeline"/>
21 + </driver>
22 +</drivers>
...\ No newline at end of file ...\ No newline at end of file
...@@ -147,6 +147,7 @@ ...@@ -147,6 +147,7 @@
147 <bundle>mvn:org.onosproject/onos-of-api/@ONOS-VERSION</bundle> 147 <bundle>mvn:org.onosproject/onos-of-api/@ONOS-VERSION</bundle>
148 <bundle>mvn:org.onosproject/onos-of-drivers/@ONOS-VERSION</bundle> 148 <bundle>mvn:org.onosproject/onos-of-drivers/@ONOS-VERSION</bundle>
149 <bundle>mvn:org.onosproject/onos-of-ctl/@ONOS-VERSION</bundle> 149 <bundle>mvn:org.onosproject/onos-of-ctl/@ONOS-VERSION</bundle>
150 + <bundle>mvn:org.onosproject/onos-drivers/@ONOS-VERSION</bundle>
150 151
151 <bundle>mvn:org.onosproject/onos-lldp-provider/@ONOS-VERSION</bundle> 152 <bundle>mvn:org.onosproject/onos-lldp-provider/@ONOS-VERSION</bundle>
152 <bundle>mvn:org.onosproject/onos-host-provider/@ONOS-VERSION</bundle> 153 <bundle>mvn:org.onosproject/onos-host-provider/@ONOS-VERSION</bundle>
......