Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 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.ofagent.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
Claudine Chiu | ce8ccc6 | 2017-08-16 10:06:20 -0400 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 20 | import io.netty.channel.ChannelOutboundInvoker; |
| 21 | import io.netty.channel.nio.NioEventLoopGroup; |
| 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.apache.felix.scr.annotations.Service; |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 28 | import org.onosproject.cluster.ClusterService; |
| 29 | import org.onosproject.cluster.LeadershipService; |
| 30 | import org.onosproject.cluster.NodeId; |
| 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.CoreService; |
| 33 | import org.onosproject.incubator.net.virtual.NetworkId; |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 34 | import org.onosproject.incubator.net.virtual.VirtualNetworkEvent; |
| 35 | import org.onosproject.incubator.net.virtual.VirtualNetworkListener; |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 36 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
Claudine Chiu | 785ef2d | 2017-07-04 13:13:28 -0400 | [diff] [blame] | 37 | import org.onosproject.incubator.net.virtual.VirtualPort; |
Claudine Chiu | 5c184e1 | 2017-08-08 21:21:38 -0400 | [diff] [blame] | 38 | import org.onosproject.net.ConnectPoint; |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 39 | import org.onosproject.net.Device; |
| 40 | import org.onosproject.net.DeviceId; |
Claudine Chiu | 5c184e1 | 2017-08-08 21:21:38 -0400 | [diff] [blame] | 41 | import org.onosproject.net.Link; |
Claudine Chiu | 785ef2d | 2017-07-04 13:13:28 -0400 | [diff] [blame] | 42 | import org.onosproject.net.Port; |
Claudine Chiu | 5c184e1 | 2017-08-08 21:21:38 -0400 | [diff] [blame] | 43 | import org.onosproject.net.PortNumber; |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 44 | import org.onosproject.net.device.DeviceEvent; |
| 45 | import org.onosproject.net.device.DeviceListener; |
| 46 | import org.onosproject.net.device.DeviceService; |
Claudine Chiu | 2729ffd | 2017-07-31 21:38:27 -0400 | [diff] [blame] | 47 | import org.onosproject.net.device.PortStatistics; |
Claudine Chiu | ce8ccc6 | 2017-08-16 10:06:20 -0400 | [diff] [blame] | 48 | import org.onosproject.net.flow.FlowEntry; |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 49 | import org.onosproject.net.flow.FlowRuleEvent; |
| 50 | import org.onosproject.net.flow.FlowRuleListener; |
| 51 | import org.onosproject.net.flow.FlowRuleService; |
Claudine Chiu | ce8ccc6 | 2017-08-16 10:06:20 -0400 | [diff] [blame] | 52 | import org.onosproject.net.flow.TableStatisticsEntry; |
| 53 | import org.onosproject.net.group.Group; |
| 54 | import org.onosproject.net.group.GroupService; |
Claudine Chiu | 5c184e1 | 2017-08-08 21:21:38 -0400 | [diff] [blame] | 55 | import org.onosproject.net.link.LinkService; |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 56 | import org.onosproject.net.packet.PacketContext; |
| 57 | import org.onosproject.net.packet.PacketProcessor; |
| 58 | import org.onosproject.net.packet.PacketService; |
| 59 | import org.onosproject.ofagent.api.OFAgent; |
| 60 | import org.onosproject.ofagent.api.OFAgentEvent; |
| 61 | import org.onosproject.ofagent.api.OFAgentListener; |
| 62 | import org.onosproject.ofagent.api.OFAgentService; |
| 63 | import org.onosproject.ofagent.api.OFController; |
| 64 | import org.onosproject.ofagent.api.OFSwitch; |
| 65 | import org.onosproject.ofagent.api.OFSwitchCapabilities; |
| 66 | import org.onosproject.ofagent.api.OFSwitchService; |
| 67 | import org.projectfloodlight.openflow.types.DatapathId; |
| 68 | import org.slf4j.Logger; |
| 69 | import org.slf4j.LoggerFactory; |
| 70 | |
| 71 | import java.net.InetSocketAddress; |
| 72 | import java.net.SocketAddress; |
Claudine Chiu | ce8ccc6 | 2017-08-16 10:06:20 -0400 | [diff] [blame] | 73 | import java.util.ArrayList; |
Claudine Chiu | 2729ffd | 2017-07-31 21:38:27 -0400 | [diff] [blame] | 74 | import java.util.List; |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 75 | import java.util.Objects; |
| 76 | import java.util.Set; |
| 77 | import java.util.concurrent.ConcurrentHashMap; |
| 78 | import java.util.concurrent.ExecutorService; |
| 79 | import java.util.stream.Collectors; |
| 80 | |
| 81 | import static com.google.common.base.Preconditions.checkArgument; |
| 82 | import static org.onlab.util.BoundedThreadPool.newSingleThreadExecutor; |
| 83 | import static org.onlab.util.Tools.groupedThreads; |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 84 | import static org.onosproject.ofagent.api.OFAgent.State.STARTED; |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 85 | import static org.onosproject.ofagent.api.OFAgentService.APPLICATION_NAME; |
| 86 | |
| 87 | /** |
| 88 | * Manages OF switches. |
| 89 | */ |
| 90 | @Component(immediate = true) |
| 91 | @Service |
| 92 | public class OFSwitchManager implements OFSwitchService { |
| 93 | |
| 94 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 95 | |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 96 | private static final OFSwitchCapabilities DEFAULT_CAPABILITIES = |
| 97 | DefaultOFSwitchCapabilities.builder() |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 98 | .flowStats() |
| 99 | .tableStats() |
| 100 | .portStats() |
| 101 | .groupStats() |
| 102 | .queueStats() |
| 103 | .ipReasm() |
| 104 | .portBlocked() |
| 105 | .build(); |
| 106 | |
| 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 108 | protected CoreService coreService; |
| 109 | |
| 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 111 | protected LeadershipService leadershipService; |
| 112 | |
| 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 114 | protected ClusterService clusterService; |
| 115 | |
| 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 117 | protected VirtualNetworkService virtualNetService; |
| 118 | |
| 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 120 | protected OFAgentService ofAgentService; |
| 121 | |
| 122 | private final ConcurrentHashMap<DeviceId, OFSwitch> ofSwitchMap = new ConcurrentHashMap<>(); |
| 123 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 124 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 125 | private final OFAgentListener ofAgentListener = new InternalOFAgentListener(); |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 126 | private final VirtualNetworkListener vNetworkListener = new InternalVirtualNetworkListener(); |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 127 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 128 | private final FlowRuleListener flowRuleListener = new InternalFlowRuleListener(); |
| 129 | private final PacketProcessor packetProcessor = new InternalPacketProcessor(); |
| 130 | |
| 131 | private NioEventLoopGroup ioWorker; |
| 132 | private ApplicationId appId; |
| 133 | private NodeId localId; |
| 134 | |
| 135 | @Activate |
| 136 | protected void activate() { |
| 137 | appId = coreService.registerApplication(APPLICATION_NAME); |
| 138 | localId = clusterService.getLocalNode().id(); |
| 139 | ioWorker = new NioEventLoopGroup(); |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 140 | |
| 141 | ofAgentService.agents().forEach(this::processOFAgentCreated); |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 142 | ofAgentService.addListener(ofAgentListener); |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 143 | virtualNetService.addListener(vNetworkListener); |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 144 | |
| 145 | log.info("Started"); |
| 146 | } |
| 147 | |
| 148 | @Deactivate |
| 149 | protected void deactivate() { |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 150 | virtualNetService.removeListener(vNetworkListener); |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 151 | ofAgentService.removeListener(ofAgentListener); |
| 152 | ofAgentService.agents().forEach(this::processOFAgentStopped); |
| 153 | |
| 154 | ioWorker.shutdownGracefully(); |
| 155 | eventExecutor.shutdown(); |
| 156 | |
| 157 | log.info("Stopped"); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Set<OFSwitch> ofSwitches() { |
| 162 | return ImmutableSet.copyOf(ofSwitchMap.values()); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public Set<OFSwitch> ofSwitches(NetworkId networkId) { |
| 167 | Set<OFSwitch> ofSwitches = devices(networkId).stream() |
| 168 | .map(ofSwitchMap::get) |
| 169 | .filter(Objects::nonNull) |
| 170 | .collect(Collectors.toSet()); |
| 171 | return ImmutableSet.copyOf(ofSwitches); |
| 172 | } |
| 173 | |
Claudine Chiu | 785ef2d | 2017-07-04 13:13:28 -0400 | [diff] [blame] | 174 | @Override |
Claudine Chiu | 2729ffd | 2017-07-31 21:38:27 -0400 | [diff] [blame] | 175 | public OFSwitch ofSwitch(NetworkId networkId, DeviceId deviceId) { |
| 176 | return ofSwitchMap.get(deviceId); |
| 177 | } |
| 178 | |
| 179 | @Override |
Claudine Chiu | 785ef2d | 2017-07-04 13:13:28 -0400 | [diff] [blame] | 180 | public Set<Port> ports(NetworkId networkId, DeviceId deviceId) { |
| 181 | Set<Port> ports = virtualNetService.getVirtualPorts(networkId, deviceId) |
| 182 | .stream() |
| 183 | .collect(Collectors.toSet()); |
| 184 | return ImmutableSet.copyOf(ports); |
| 185 | } |
| 186 | |
Claudine Chiu | 2729ffd | 2017-07-31 21:38:27 -0400 | [diff] [blame] | 187 | @Override |
| 188 | public List<PortStatistics> getPortStatistics(NetworkId networkId, DeviceId deviceId) { |
| 189 | DeviceService deviceService = virtualNetService.get(networkId, DeviceService.class); |
| 190 | List<PortStatistics> portStatistics = deviceService.getPortStatistics(deviceId); |
| 191 | return portStatistics; |
| 192 | } |
| 193 | |
Claudine Chiu | 5c184e1 | 2017-08-08 21:21:38 -0400 | [diff] [blame] | 194 | @Override |
| 195 | public ConnectPoint neighbour(NetworkId networkId, DeviceId deviceId, PortNumber portNumber) { |
| 196 | ConnectPoint cp = new ConnectPoint(deviceId, portNumber); |
| 197 | LinkService linkService = virtualNetService.get(networkId, LinkService.class); |
| 198 | Set<Link> links = linkService.getEgressLinks(cp); |
| 199 | log.trace("neighbour cp {} egressLinks {}", cp, links); |
| 200 | if (links != null && links.size() > 0) { |
| 201 | Link link = links.iterator().next(); |
| 202 | return link.src(); |
| 203 | } |
| 204 | return null; |
| 205 | } |
| 206 | |
Claudine Chiu | ce8ccc6 | 2017-08-16 10:06:20 -0400 | [diff] [blame] | 207 | @Override |
Claudine Chiu | b211b87 | 2017-09-05 17:27:00 -0400 | [diff] [blame] | 208 | public ApplicationId appId() { |
| 209 | return appId; |
| 210 | } |
| 211 | |
| 212 | @Override |
Claudine Chiu | ce8ccc6 | 2017-08-16 10:06:20 -0400 | [diff] [blame] | 213 | public List<FlowEntry> getFlowEntries(NetworkId networkId, DeviceId deviceId) { |
| 214 | FlowRuleService flowRuleService = virtualNetService.get(networkId, FlowRuleService.class); |
| 215 | Iterable<FlowEntry> entries = flowRuleService.getFlowEntries(deviceId); |
| 216 | return Lists.newArrayList(entries); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public List<TableStatisticsEntry> getFlowTableStatistics(NetworkId networkId, DeviceId deviceId) { |
| 221 | FlowRuleService flowRuleService = virtualNetService.get(networkId, FlowRuleService.class); |
| 222 | Iterable<TableStatisticsEntry> entries = flowRuleService.getFlowTableStatistics(deviceId); |
| 223 | if (entries == null) { |
| 224 | entries = new ArrayList<>(); |
| 225 | } |
| 226 | return Lists.newArrayList(entries); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public List<Group> getGroups(NetworkId networkId, DeviceId deviceId) { |
| 231 | GroupService groupService = virtualNetService.get(networkId, GroupService.class); |
| 232 | Iterable<Group> entries = groupService.getGroups(deviceId); |
| 233 | return Lists.newArrayList(entries); |
| 234 | } |
| 235 | |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 236 | private void addOFSwitch(NetworkId networkId, DeviceId deviceId) { |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 237 | OFSwitch ofSwitch = DefaultOFSwitch.of( |
| 238 | dpidWithDeviceId(deviceId), |
Claudine Chiu | 785ef2d | 2017-07-04 13:13:28 -0400 | [diff] [blame] | 239 | DEFAULT_CAPABILITIES, networkId, deviceId, |
| 240 | virtualNetService.getServiceDirectory()); |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 241 | ofSwitchMap.put(deviceId, ofSwitch); |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 242 | log.info("Added virtual OF switch for {}", deviceId); |
| 243 | |
| 244 | OFAgent ofAgent = ofAgentService.agent(networkId); |
Eric Tang | df833c8 | 2017-06-04 22:55:49 +0800 | [diff] [blame] | 245 | if (ofAgent == null) { |
| 246 | log.error("OFAgent for network {} does not exist", networkId); |
| 247 | return; |
| 248 | } |
| 249 | |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 250 | if (ofAgent.state() == STARTED) { |
| 251 | connectController(ofSwitch, ofAgent.controllers()); |
| 252 | } |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | private void deleteOFSwitch(DeviceId deviceId) { |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 256 | OFSwitch ofSwitch = ofSwitchMap.get(deviceId); |
| 257 | ofSwitch.controllerChannels().forEach(ChannelOutboundInvoker::disconnect); |
| 258 | |
| 259 | ofSwitchMap.remove(deviceId); |
| 260 | log.info("Removed virtual OFSwitch for {}", deviceId); |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | private void connectController(OFSwitch ofSwitch, Set<OFController> controllers) { |
| 264 | controllers.forEach(controller -> { |
| 265 | OFConnectionHandler connectionHandler = new OFConnectionHandler( |
| 266 | ofSwitch, |
| 267 | controller, |
| 268 | ioWorker); |
| 269 | connectionHandler.connect(); |
| 270 | }); |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | private void disconnectController(OFSwitch ofSwitch, Set<OFController> controllers) { |
| 274 | Set<SocketAddress> controllerAddrs = controllers.stream() |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 275 | .map(ctrl -> new InetSocketAddress( |
| 276 | ctrl.ip().toInetAddress(), ctrl.port().toInt())) |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 277 | .collect(Collectors.toSet()); |
| 278 | |
| 279 | ofSwitch.controllerChannels().stream() |
| 280 | .filter(channel -> controllerAddrs.contains(channel.remoteAddress())) |
| 281 | .forEach(ChannelOutboundInvoker::disconnect); |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | private Set<DeviceId> devices(NetworkId networkId) { |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 285 | Set<DeviceId> deviceIds = virtualNetService.getVirtualDevices(networkId) |
| 286 | .stream() |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 287 | .map(Device::id) |
| 288 | .collect(Collectors.toSet()); |
| 289 | return ImmutableSet.copyOf(deviceIds); |
| 290 | } |
| 291 | |
| 292 | private DatapathId dpidWithDeviceId(DeviceId deviceId) { |
| 293 | String strDeviceId = deviceId.toString().split(":")[1]; |
| 294 | checkArgument(strDeviceId.length() == 16, "Invalid device ID " + strDeviceId); |
| 295 | |
| 296 | String resultedHexString = ""; |
| 297 | for (int i = 0; i < 8; i++) { |
| 298 | resultedHexString = resultedHexString + strDeviceId.charAt(2 * i) |
| 299 | + strDeviceId.charAt(2 * i + 1); |
| 300 | if (i != 7) { |
| 301 | resultedHexString += ":"; |
| 302 | } |
| 303 | } |
| 304 | return DatapathId.of(resultedHexString); |
| 305 | } |
| 306 | |
| 307 | private void processOFAgentCreated(OFAgent ofAgent) { |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 308 | devices(ofAgent.networkId()).forEach(deviceId -> { |
| 309 | addOFSwitch(ofAgent.networkId(), deviceId); |
| 310 | }); |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | private void processOFAgentRemoved(OFAgent ofAgent) { |
| 314 | devices(ofAgent.networkId()).forEach(this::deleteOFSwitch); |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | private void processOFAgentStarted(OFAgent ofAgent) { |
| 318 | devices(ofAgent.networkId()).forEach(deviceId -> { |
| 319 | OFSwitch ofSwitch = ofSwitchMap.get(deviceId); |
| 320 | if (ofSwitch != null) { |
| 321 | connectController(ofSwitch, ofAgent.controllers()); |
| 322 | } |
| 323 | }); |
| 324 | |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 325 | DeviceService deviceService = virtualNetService.get( |
| 326 | ofAgent.networkId(), |
| 327 | DeviceService.class); |
| 328 | deviceService.addListener(deviceListener); |
| 329 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 330 | PacketService packetService = virtualNetService.get( |
| 331 | ofAgent.networkId(), |
| 332 | PacketService.class); |
| 333 | packetService.addProcessor(packetProcessor, PacketProcessor.director(0)); |
| 334 | |
| 335 | FlowRuleService flowRuleService = virtualNetService.get( |
| 336 | ofAgent.networkId(), |
| 337 | FlowRuleService.class); |
| 338 | flowRuleService.addListener(flowRuleListener); |
| 339 | } |
| 340 | |
| 341 | private void processOFAgentStopped(OFAgent ofAgent) { |
| 342 | devices(ofAgent.networkId()).forEach(deviceId -> { |
| 343 | OFSwitch ofSwitch = ofSwitchMap.get(deviceId); |
| 344 | if (ofSwitch != null) { |
| 345 | disconnectController(ofSwitch, ofAgent.controllers()); |
| 346 | } |
| 347 | }); |
| 348 | |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 349 | DeviceService deviceService = virtualNetService.get( |
| 350 | ofAgent.networkId(), |
| 351 | DeviceService.class); |
| 352 | deviceService.removeListener(deviceListener); |
| 353 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 354 | PacketService packetService = virtualNetService.get( |
| 355 | ofAgent.networkId(), |
| 356 | PacketService.class); |
| 357 | packetService.removeProcessor(packetProcessor); |
| 358 | |
| 359 | FlowRuleService flowRuleService = virtualNetService.get( |
| 360 | ofAgent.networkId(), |
| 361 | FlowRuleService.class); |
| 362 | flowRuleService.removeListener(flowRuleListener); |
| 363 | } |
| 364 | |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 365 | private class InternalVirtualNetworkListener implements VirtualNetworkListener { |
| 366 | |
| 367 | @Override |
| 368 | public void event(VirtualNetworkEvent event) { |
Claudine Chiu | 785ef2d | 2017-07-04 13:13:28 -0400 | [diff] [blame] | 369 | log.trace("Vnet event {}", event); |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 370 | switch (event.type()) { |
| 371 | case VIRTUAL_DEVICE_ADDED: |
| 372 | eventExecutor.execute(() -> { |
| 373 | log.debug("Virtual device {} added to network {}", |
| 374 | event.virtualDevice().id(), |
| 375 | event.subject()); |
| 376 | addOFSwitch(event.subject(), event.virtualDevice().id()); |
| 377 | }); |
| 378 | break; |
| 379 | case VIRTUAL_DEVICE_UPDATED: |
| 380 | // TODO handle device availability updates |
| 381 | break; |
| 382 | case VIRTUAL_DEVICE_REMOVED: |
| 383 | eventExecutor.execute(() -> { |
| 384 | log.debug("Virtual device {} removed from network {}", |
| 385 | event.virtualDevice().id(), |
| 386 | event.subject()); |
| 387 | deleteOFSwitch(event.virtualDevice().id()); |
| 388 | }); |
| 389 | break; |
| 390 | case NETWORK_UPDATED: |
| 391 | case NETWORK_REMOVED: |
| 392 | case NETWORK_ADDED: |
Claudine Chiu | 785ef2d | 2017-07-04 13:13:28 -0400 | [diff] [blame] | 393 | break; |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 394 | case VIRTUAL_PORT_ADDED: |
Claudine Chiu | 785ef2d | 2017-07-04 13:13:28 -0400 | [diff] [blame] | 395 | eventExecutor.execute(() -> { |
| 396 | OFSwitch ofSwitch = ofSwitch(event.virtualPort()); |
| 397 | if (ofSwitch != null) { |
| 398 | ofSwitch.processPortAdded(event.virtualPort()); |
| 399 | log.debug("Virtual port {} added to network {}", |
| 400 | event.virtualPort(), |
| 401 | event.subject()); |
| 402 | } |
| 403 | }); |
| 404 | break; |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 405 | case VIRTUAL_PORT_UPDATED: |
Claudine Chiu | e92efea | 2017-10-31 13:58:44 -0400 | [diff] [blame] | 406 | eventExecutor.execute(() -> { |
| 407 | OFSwitch ofSwitch = ofSwitch(event.virtualPort()); |
| 408 | if (ofSwitch != null) { |
| 409 | if (event.virtualPort().isEnabled()) { |
| 410 | ofSwitch.processPortUp(event.virtualPort()); |
| 411 | } else { |
| 412 | ofSwitch.processPortDown(event.virtualPort()); |
| 413 | } |
| 414 | log.debug("Virtual port {} updated in network {}", |
| 415 | event.virtualPort(), |
| 416 | event.subject()); |
| 417 | } |
| 418 | }); |
Claudine Chiu | 785ef2d | 2017-07-04 13:13:28 -0400 | [diff] [blame] | 419 | break; |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 420 | case VIRTUAL_PORT_REMOVED: |
Claudine Chiu | 785ef2d | 2017-07-04 13:13:28 -0400 | [diff] [blame] | 421 | eventExecutor.execute(() -> { |
| 422 | OFSwitch ofSwitch = ofSwitch(event.virtualPort()); |
| 423 | if (ofSwitch != null) { |
| 424 | ofSwitch.processPortRemoved(event.virtualPort()); |
| 425 | log.debug("Virtual port {} removed from network {}", |
| 426 | event.virtualPort(), |
| 427 | event.subject()); |
| 428 | } |
| 429 | }); |
| 430 | break; |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 431 | default: |
| 432 | // do nothing |
| 433 | break; |
| 434 | } |
| 435 | } |
Claudine Chiu | 785ef2d | 2017-07-04 13:13:28 -0400 | [diff] [blame] | 436 | |
| 437 | private OFSwitch ofSwitch(VirtualPort virtualPort) { |
| 438 | OFSwitch ofSwitch = ofSwitchMap.get(virtualPort.element().id()); |
| 439 | if (ofSwitch == null) { |
| 440 | log.warn("Switch does not exist for port {}", virtualPort); |
| 441 | } else { |
| 442 | log.trace("Switch exists for port {}", virtualPort); |
| 443 | } |
| 444 | return ofSwitch; |
| 445 | } |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 446 | } |
| 447 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 448 | private class InternalOFAgentListener implements OFAgentListener { |
| 449 | |
| 450 | @Override |
| 451 | public boolean isRelevant(OFAgentEvent event) { |
| 452 | return Objects.equals(localId, leadershipService.getLeader(appId.name())); |
| 453 | } |
| 454 | |
| 455 | @Override |
| 456 | public void event(OFAgentEvent event) { |
| 457 | switch (event.type()) { |
| 458 | case OFAGENT_CREATED: |
| 459 | eventExecutor.execute(() -> { |
| 460 | OFAgent ofAgent = event.subject(); |
| 461 | log.debug("Processing OFAgent created: {}", ofAgent); |
| 462 | processOFAgentCreated(ofAgent); |
| 463 | }); |
| 464 | break; |
| 465 | case OFAGENT_REMOVED: |
| 466 | eventExecutor.execute(() -> { |
| 467 | OFAgent ofAgent = event.subject(); |
| 468 | log.debug("Processing OFAgent removed: {}", ofAgent); |
| 469 | processOFAgentRemoved(ofAgent); |
| 470 | }); |
| 471 | break; |
| 472 | case OFAGENT_CONTROLLER_ADDED: |
| 473 | // TODO handle additional controller |
| 474 | break; |
| 475 | case OFAGENT_CONTROLLER_REMOVED: |
| 476 | // TODO handle removed controller |
| 477 | break; |
| 478 | case OFAGENT_STARTED: |
| 479 | eventExecutor.execute(() -> { |
| 480 | OFAgent ofAgent = event.subject(); |
| 481 | log.debug("Processing OFAgent started: {}", ofAgent); |
| 482 | processOFAgentStarted(ofAgent); |
| 483 | }); |
| 484 | break; |
| 485 | case OFAGENT_STOPPED: |
| 486 | eventExecutor.execute(() -> { |
| 487 | OFAgent ofAgent = event.subject(); |
| 488 | log.debug("Processing OFAgent stopped: {}", ofAgent); |
| 489 | processOFAgentStopped(ofAgent); |
| 490 | }); |
| 491 | break; |
| 492 | default: |
| 493 | // do nothing |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | private class InternalDeviceListener implements DeviceListener { |
| 500 | |
| 501 | @Override |
| 502 | public void event(DeviceEvent event) { |
| 503 | switch (event.type()) { |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 504 | case DEVICE_AVAILABILITY_CHANGED: |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 505 | case DEVICE_ADDED: |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 506 | case DEVICE_UPDATED: |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 507 | case DEVICE_REMOVED: |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 508 | case DEVICE_SUSPENDED: |
| 509 | case PORT_ADDED: |
| 510 | // TODO handle event |
| 511 | case PORT_REMOVED: |
| 512 | // TODO handle event |
| 513 | case PORT_STATS_UPDATED: |
| 514 | case PORT_UPDATED: |
| 515 | default: |
| 516 | break; |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | private class InternalPacketProcessor implements PacketProcessor { |
| 522 | |
| 523 | @Override |
| 524 | public void process(PacketContext context) { |
| 525 | // TODO handle packet-in |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | private class InternalFlowRuleListener implements FlowRuleListener { |
| 530 | |
| 531 | @Override |
| 532 | public void event(FlowRuleEvent event) { |
| 533 | // TODO handle flow rule event |
| 534 | } |
| 535 | } |
| 536 | } |