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; |
| 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 | } |