alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 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; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 34 | import org.onosproject.net.device.DeviceEvent; |
| 35 | import org.onosproject.net.device.DeviceListener; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 36 | import org.onosproject.net.device.DeviceService; |
| 37 | import org.onosproject.net.driver.Driver; |
| 38 | import org.onosproject.net.driver.DriverHandler; |
| 39 | import org.onosproject.net.driver.DriverService; |
| 40 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 41 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 42 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 43 | import org.onosproject.net.flowobjective.NextObjective; |
| 44 | import org.slf4j.Logger; |
| 45 | import org.slf4j.LoggerFactory; |
| 46 | |
| 47 | import java.util.Collection; |
| 48 | import java.util.Map; |
| 49 | import java.util.concurrent.Future; |
| 50 | |
| 51 | import static com.google.common.base.Preconditions.checkState; |
| 52 | |
| 53 | /** |
| 54 | * Created by ash on 07/04/15. |
| 55 | */ |
| 56 | @Component(immediate = true) |
| 57 | @Service |
| 58 | public class FlowObjectiveManager implements FlowObjectiveService { |
| 59 | |
| 60 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected DriverService driverService; |
| 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected DeviceService deviceService; |
| 67 | |
| 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | protected MastershipService mastershipService; |
| 70 | |
| 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 72 | protected ClusterService clusterService; |
| 73 | |
| 74 | protected ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
| 75 | |
| 76 | private MastershipListener mastershipListener = new InnerMastershipListener(); |
| 77 | |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 78 | private DeviceListener deviceListener = new InnerDeviceListener(); |
| 79 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 80 | private Map<DeviceId, DriverHandler> driverHandlers = |
| 81 | Maps.newConcurrentMap(); |
| 82 | |
| 83 | @Activate |
| 84 | protected void activate() { |
| 85 | mastershipService.addListener(mastershipListener); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 86 | deviceService.addListener(deviceListener); |
| 87 | deviceService.getDevices().forEach(device -> setupDriver(device.id())); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 88 | log.info("Started"); |
| 89 | } |
| 90 | |
| 91 | @Deactivate |
| 92 | protected void deactivate() { |
| 93 | mastershipService.removeListener(mastershipListener); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 94 | deviceService.removeListener(deviceListener); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 95 | log.info("Stopped"); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public Future<Boolean> filter(DeviceId deviceId, |
| 100 | Collection<FilteringObjective> filterObjectives) { |
| 101 | DriverHandler handler = driverHandlers.get(deviceId); |
| 102 | checkState(handler != null, "Driver not initialized"); |
| 103 | |
| 104 | Pipeliner pipe = handler.behaviour(Pipeliner.class); |
| 105 | |
| 106 | return pipe.filter(filterObjectives); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public Future<Boolean> forward(DeviceId deviceId, |
| 111 | Collection<ForwardingObjective> forwardingObjectives) { |
| 112 | DriverHandler handler = driverHandlers.get(deviceId); |
| 113 | checkState(handler != null, "Driver not initialized"); |
| 114 | |
| 115 | Pipeliner pipe = handler.behaviour(Pipeliner.class); |
| 116 | |
| 117 | return pipe.forward(forwardingObjectives); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public Future<Boolean> next(DeviceId deviceId, |
| 122 | Collection<NextObjective> nextObjectives) { |
| 123 | DriverHandler handler = driverHandlers.get(deviceId); |
| 124 | checkState(handler != null, "Driver not initialized"); |
| 125 | |
| 126 | Pipeliner pipe = handler.behaviour(Pipeliner.class); |
| 127 | |
| 128 | return pipe.next(nextObjectives); |
| 129 | } |
| 130 | |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 131 | |
| 132 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 133 | private class InnerMastershipListener implements MastershipListener { |
| 134 | @Override |
| 135 | public void event(MastershipEvent event) { |
| 136 | switch (event.type()) { |
| 137 | |
| 138 | case MASTER_CHANGED: |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 139 | setupDriver(event.subject()); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 140 | |
| 141 | break; |
| 142 | case BACKUPS_CHANGED: |
| 143 | break; |
| 144 | default: |
| 145 | log.warn("Unknown mastership type {}", event.type()); |
| 146 | } |
| 147 | } |
| 148 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 149 | |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 150 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 151 | |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 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()); |
| 159 | break; |
| 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: |
| 171 | break; |
| 172 | default: |
| 173 | log.warn("Unknown event type {}", event.type()); |
| 174 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 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 | |
| 197 | private DriverHandler lookupDriver(DeviceId deviceId) { |
| 198 | Device device = deviceService.getDevice(deviceId); |
| 199 | if (device == null) { |
| 200 | log.warn("Device is null!"); |
| 201 | return null; |
| 202 | } |
| 203 | Driver driver = driverService.getDriver(device.manufacturer(), |
| 204 | device.hwVersion(), device.swVersion()); |
| 205 | |
| 206 | return driverService.createHandler(driver.name(), deviceId); |
| 207 | } |
| 208 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 209 | } |