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; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 27 | import org.onlab.util.ItemNotFoundException; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 28 | import org.onosproject.cluster.ClusterService; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 29 | import org.onosproject.cluster.NodeId; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 30 | import org.onosproject.mastership.MastershipEvent; |
| 31 | import org.onosproject.mastership.MastershipListener; |
| 32 | import org.onosproject.mastership.MastershipService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.behaviour.Pipeliner; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 35 | import org.onosproject.net.behaviour.PipelinerContext; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 36 | import org.onosproject.net.device.DeviceEvent; |
| 37 | import org.onosproject.net.device.DeviceListener; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 38 | import org.onosproject.net.device.DeviceService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 39 | import org.onosproject.net.driver.DriverHandler; |
| 40 | import org.onosproject.net.driver.DriverService; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 41 | import org.onosproject.net.flow.FlowRuleService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 42 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 43 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 44 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 45 | import org.onosproject.net.flowobjective.NextObjective; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 46 | import org.onosproject.net.group.GroupService; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 47 | import org.slf4j.Logger; |
| 48 | import org.slf4j.LoggerFactory; |
| 49 | |
| 50 | import java.util.Collection; |
| 51 | import java.util.Map; |
| 52 | import java.util.concurrent.Future; |
| 53 | |
| 54 | import static com.google.common.base.Preconditions.checkState; |
| 55 | |
| 56 | /** |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 57 | * Provides implementation of the flow objective programming service. |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 58 | */ |
| 59 | @Component(immediate = true) |
| 60 | @Service |
| 61 | public class FlowObjectiveManager implements FlowObjectiveService { |
| 62 | |
| 63 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 64 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 65 | public static final String NOT_INITIALIZED = "Driver not initialized"; |
| 66 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected DriverService driverService; |
| 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected DeviceService deviceService; |
| 72 | |
| 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 74 | protected MastershipService mastershipService; |
| 75 | |
| 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 77 | protected ClusterService clusterService; |
| 78 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 79 | // Note: The following dependencies are added on behalf of the pipeline |
| 80 | // driver behaviours to assure these services are available for their |
| 81 | // initialization. |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected FlowRuleService flowRuleService; |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected GroupService groupService; |
| 87 | |
| 88 | |
| 89 | private final Map<DeviceId, DriverHandler> driverHandlers = Maps.newConcurrentMap(); |
| 90 | |
| 91 | private final PipelinerContext context = new InnerPipelineContext(); |
| 92 | private final MastershipListener mastershipListener = new InnerMastershipListener(); |
| 93 | private final DeviceListener deviceListener = new InnerDeviceListener(); |
| 94 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 95 | protected ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 96 | private NodeId localNode; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 97 | |
| 98 | @Activate |
| 99 | protected void activate() { |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 100 | localNode = clusterService.getLocalNode().id(); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 101 | mastershipService.addListener(mastershipListener); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 102 | deviceService.addListener(deviceListener); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 103 | deviceService.getDevices().forEach(device -> setupPipelineHandler(device.id())); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 104 | log.info("Started"); |
| 105 | } |
| 106 | |
| 107 | @Deactivate |
| 108 | protected void deactivate() { |
| 109 | mastershipService.removeListener(mastershipListener); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 110 | deviceService.removeListener(deviceListener); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 111 | log.info("Stopped"); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public Future<Boolean> filter(DeviceId deviceId, |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 116 | Collection<FilteringObjective> filteringObjectives) { |
| 117 | return getDevicePipeliner(deviceId).filter(filteringObjectives); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public Future<Boolean> forward(DeviceId deviceId, |
| 122 | Collection<ForwardingObjective> forwardingObjectives) { |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 123 | return getDevicePipeliner(deviceId).forward(forwardingObjectives); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public Future<Boolean> next(DeviceId deviceId, |
| 128 | Collection<NextObjective> nextObjectives) { |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 129 | return getDevicePipeliner(deviceId).next(nextObjectives); |
| 130 | } |
| 131 | |
| 132 | // Retrieves the device handler pipeline behaviour from the cache. |
| 133 | private Pipeliner getDevicePipeliner(DeviceId deviceId) { |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 134 | DriverHandler handler = driverHandlers.get(deviceId); |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 135 | checkState(handler != null, NOT_INITIALIZED); |
| 136 | return handler != null ? handler.behaviour(Pipeliner.class) : null; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 137 | } |
| 138 | |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 139 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 140 | // Triggers driver setup when the local node becomes a device master. |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 141 | private class InnerMastershipListener implements MastershipListener { |
| 142 | @Override |
| 143 | public void event(MastershipEvent event) { |
| 144 | switch (event.type()) { |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 145 | case MASTER_CHANGED: |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 146 | setupPipelineHandler(event.subject()); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 147 | break; |
| 148 | default: |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 149 | break; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 152 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 153 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 154 | // Triggers driver setup when a device is (re)detected. |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 155 | private class InnerDeviceListener implements DeviceListener { |
| 156 | @Override |
| 157 | public void event(DeviceEvent event) { |
| 158 | switch (event.type()) { |
| 159 | case DEVICE_ADDED: |
| 160 | case DEVICE_AVAILABILITY_CHANGED: |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 161 | setupPipelineHandler(event.subject().id()); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 162 | break; |
| 163 | default: |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 164 | break; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 165 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 168 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 169 | private void setupPipelineHandler(DeviceId deviceId) { |
| 170 | if (localNode.equals(mastershipService.getMasterFor(deviceId))) { |
| 171 | // Attempt to lookup the handler in the cache |
| 172 | DriverHandler handler = driverHandlers.get(deviceId); |
| 173 | if (handler == null) { |
| 174 | try { |
| 175 | // Otherwise create it and if it has pipeline behaviour, cache it |
| 176 | handler = driverService.createHandler(deviceId); |
| 177 | if (!handler.driver().hasBehaviour(Pipeliner.class)) { |
| 178 | log.warn("Pipeline behaviour not supported for device {}", |
| 179 | deviceId); |
| 180 | return; |
| 181 | } |
| 182 | } catch (ItemNotFoundException e) { |
| 183 | log.warn("No applicable driver for device {}", deviceId); |
| 184 | return; |
| 185 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 186 | driverHandlers.put(deviceId, handler); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 187 | } |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 188 | |
| 189 | // Always (re)initialize the pipeline behaviour |
| 190 | handler.behaviour(Pipeliner.class).init(deviceId, context); |
| 191 | log.info("Driver {} bound to device {}", handler.driver().name(), deviceId); |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame^] | 195 | // Processing context for initializing pipeline driver behaviours. |
| 196 | private class InnerPipelineContext implements PipelinerContext { |
| 197 | @Override |
| 198 | public ServiceDirectory directory() { |
| 199 | return serviceDirectory; |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 200 | } |
alshabib | aebe775 | 2015-04-07 17:45:42 -0700 | [diff] [blame] | 201 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 202 | } |