alshabib | eff0054 | 2015-09-23 13:22:33 -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 | */ |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 16 | package org.onosproject.igmp; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 17 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Property; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 24 | import org.onlab.packet.EthType; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 25 | import org.onlab.packet.Ethernet; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 26 | import org.onlab.packet.IGMP; |
Jonathan Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame^] | 27 | import org.onlab.packet.IGMPMembership; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 28 | import org.onlab.packet.IPv4; |
| 29 | import org.onlab.packet.Ip4Address; |
| 30 | import org.onlab.packet.IpAddress; |
| 31 | import org.onlab.packet.IpPrefix; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 32 | import org.onosproject.core.ApplicationId; |
| 33 | import org.onosproject.core.CoreService; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 34 | import org.onosproject.net.ConnectPoint; |
| 35 | import org.onosproject.net.DeviceId; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 36 | import org.onosproject.net.Port; |
| 37 | import org.onosproject.net.PortNumber; |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 38 | import org.onosproject.net.config.ConfigFactory; |
| 39 | import org.onosproject.net.config.NetworkConfigEvent; |
| 40 | import org.onosproject.net.config.NetworkConfigListener; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 41 | import org.onosproject.net.config.NetworkConfigRegistry; |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 42 | import org.onosproject.net.config.basics.SubjectFactories; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 43 | import org.onosproject.net.device.DeviceEvent; |
| 44 | import org.onosproject.net.device.DeviceListener; |
| 45 | import org.onosproject.net.device.DeviceService; |
| 46 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 47 | import org.onosproject.net.flow.criteria.Criteria; |
| 48 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 49 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 50 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 51 | import org.onosproject.net.flowobjective.Objective; |
| 52 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 53 | import org.onosproject.net.flowobjective.ObjectiveError; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 54 | import org.onosproject.net.mcast.McastRoute; |
| 55 | import org.onosproject.net.mcast.MulticastRouteService; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 56 | import org.onosproject.net.packet.InboundPacket; |
| 57 | import org.onosproject.net.packet.PacketContext; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 58 | import org.onosproject.net.packet.PacketProcessor; |
| 59 | import org.onosproject.net.packet.PacketService; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 60 | import org.onosproject.olt.AccessDeviceConfig; |
| 61 | import org.onosproject.olt.AccessDeviceData; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 62 | import org.slf4j.Logger; |
| 63 | |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 64 | import java.util.List; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 65 | import java.util.Map; |
| 66 | import java.util.concurrent.ConcurrentHashMap; |
| 67 | |
| 68 | import static org.slf4j.LoggerFactory.getLogger; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 69 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 70 | /** |
| 71 | * Internet Group Management Protocol. |
| 72 | */ |
| 73 | @Component(immediate = true) |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 74 | public class IgmpSnoop { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 75 | private final Logger log = getLogger(getClass()); |
| 76 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 77 | private static final String DEFAULT_MCAST_ADDR = "224.0.0.0/4"; |
| 78 | |
| 79 | @Property(name = "multicastAddress", |
Jonathan Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame^] | 80 | label = "Define the multicast base range to listen to") |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 81 | private String multicastAddress = DEFAULT_MCAST_ADDR; |
| 82 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 84 | protected FlowObjectiveService flowObjectiveService; |
| 85 | |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 87 | protected PacketService packetService; |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected CoreService coreService; |
| 91 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 93 | protected NetworkConfigRegistry networkConfig; |
| 94 | |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | protected MulticastRouteService multicastService; |
| 97 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 99 | protected DeviceService deviceService; |
| 100 | |
| 101 | private Map<DeviceId, AccessDeviceData> oltData = new ConcurrentHashMap<>(); |
| 102 | |
| 103 | private DeviceListener deviceListener = new InternalDeviceListener(); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 104 | private IgmpPacketProcessor processor = new IgmpPacketProcessor(); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 105 | private static ApplicationId appId; |
| 106 | |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 107 | private InternalNetworkConfigListener configListener = |
| 108 | new InternalNetworkConfigListener(); |
| 109 | |
| 110 | private static final Class<AccessDeviceConfig> CONFIG_CLASS = |
| 111 | AccessDeviceConfig.class; |
| 112 | |
| 113 | private ConfigFactory<DeviceId, AccessDeviceConfig> configFactory = |
| 114 | new ConfigFactory<DeviceId, AccessDeviceConfig>( |
| 115 | SubjectFactories.DEVICE_SUBJECT_FACTORY, CONFIG_CLASS, "accessDevice") { |
| 116 | @Override |
| 117 | public AccessDeviceConfig createConfig() { |
| 118 | return new AccessDeviceConfig(); |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 123 | @Activate |
| 124 | public void activate() { |
| 125 | appId = coreService.registerApplication("org.onosproject.igmp"); |
| 126 | |
| 127 | packetService.addProcessor(processor, PacketProcessor.director(1)); |
| 128 | |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 129 | networkConfig.registerConfigFactory(configFactory); |
| 130 | networkConfig.addListener(configListener); |
| 131 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 132 | networkConfig.getSubjects(DeviceId.class, AccessDeviceConfig.class).forEach( |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 133 | subject -> { |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 134 | AccessDeviceConfig config = networkConfig.getConfig(subject, |
| 135 | AccessDeviceConfig.class); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 136 | if (config != null) { |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 137 | AccessDeviceData data = config.getOlt(); |
| 138 | oltData.put(data.deviceId(), data); |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 139 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | ); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 143 | |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 144 | oltData.keySet().stream() |
| 145 | .flatMap(did -> deviceService.getPorts(did).stream()) |
| 146 | .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number())) |
| 147 | .filter(p -> p.isEnabled()) |
| 148 | .forEach(p -> processFilterObjective((DeviceId) p.element().id(), p, false)); |
| 149 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 150 | deviceService.addListener(deviceListener); |
| 151 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 152 | log.info("Started"); |
| 153 | } |
| 154 | |
| 155 | @Deactivate |
| 156 | public void deactivate() { |
| 157 | packetService.removeProcessor(processor); |
| 158 | processor = null; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 159 | deviceService.removeListener(deviceListener); |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 160 | networkConfig.removeListener(configListener); |
| 161 | networkConfig.unregisterConfigFactory(configFactory); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 162 | log.info("Stopped"); |
| 163 | } |
| 164 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 165 | private void processFilterObjective(DeviceId devId, Port port, boolean remove) { |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 166 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 167 | //TODO migrate to packet requests when packet service uses filtering objectives |
| 168 | DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder(); |
| 169 | |
| 170 | builder = remove ? builder.deny() : builder.permit(); |
| 171 | |
| 172 | FilteringObjective igmp = builder |
| 173 | .withKey(Criteria.matchInPort(port.number())) |
| 174 | .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType())) |
| 175 | .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP)) |
| 176 | .withMeta(DefaultTrafficTreatment.builder() |
| 177 | .setOutput(PortNumber.CONTROLLER).build()) |
| 178 | .fromApp(appId) |
| 179 | .withPriority(1000) |
| 180 | .add(new ObjectiveContext() { |
| 181 | @Override |
| 182 | public void onSuccess(Objective objective) { |
| 183 | log.info("Igmp filter for {} on {} installed.", |
| 184 | devId, port); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void onError(Objective objective, ObjectiveError error) { |
| 189 | log.info("Igmp filter for {} on {} failed because {}.", |
| 190 | devId, port, error); |
| 191 | } |
| 192 | }); |
| 193 | |
| 194 | flowObjectiveService.filter(devId, igmp); |
| 195 | } |
| 196 | |
| 197 | private void processQuery(IGMP pkt, ConnectPoint location) { |
Jonathan Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame^] | 198 | // TODO is this the right thing to do for a query? |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 199 | pkt.getGroups().forEach(group -> group.getSources().forEach(src -> { |
| 200 | |
| 201 | McastRoute route = new McastRoute(src, |
| 202 | group.getGaddr(), |
| 203 | McastRoute.Type.IGMP); |
| 204 | multicastService.add(route); |
| 205 | multicastService.addSink(route, location); |
| 206 | |
| 207 | })); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Jonathan Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame^] | 210 | private void processMembership(IGMP pkt, ConnectPoint location) { |
| 211 | pkt.getGroups().forEach(group -> { |
| 212 | |
| 213 | if (!(group instanceof IGMPMembership)) { |
| 214 | log.warn("Wrong group type in IGMP membership"); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | IGMPMembership membership = (IGMPMembership) group; |
| 219 | |
| 220 | McastRoute route = new McastRoute(IpAddress.valueOf("0.0.0.0"), |
| 221 | group.getGaddr(), |
| 222 | McastRoute.Type.IGMP); |
| 223 | |
| 224 | if (membership.getRecordType() == IGMPMembership.MODE_IS_INCLUDE || |
| 225 | membership.getRecordType() == IGMPMembership.CHANGE_TO_INCLUDE_MODE) { |
| 226 | |
| 227 | |
| 228 | multicastService.add(route); |
| 229 | multicastService.addSink(route, location); |
| 230 | |
| 231 | } else if (membership.getRecordType() == IGMPMembership.MODE_IS_EXCLUDE || |
| 232 | membership.getRecordType() == IGMPMembership.CHANGE_TO_EXCLUDE_MODE) { |
| 233 | multicastService.removeSink(route, location); |
| 234 | // TODO remove route if all sinks are gone |
| 235 | } |
| 236 | |
| 237 | }); |
| 238 | } |
| 239 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 240 | /** |
| 241 | * Packet processor responsible for handling IGMP packets. |
| 242 | */ |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 243 | private class IgmpPacketProcessor implements PacketProcessor { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 244 | |
| 245 | @Override |
| 246 | public void process(PacketContext context) { |
| 247 | // Stop processing if the packet has been handled, since we |
| 248 | // can't do any more to it. |
| 249 | if (context.isHandled()) { |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | InboundPacket pkt = context.inPacket(); |
| 254 | Ethernet ethPkt = pkt.parsed(); |
| 255 | if (ethPkt == null) { |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * IPv6 MLD packets are handled by ICMP6. We'll only deal |
| 261 | * with IPv4. |
| 262 | */ |
| 263 | if (ethPkt.getEtherType() != Ethernet.TYPE_IPV4) { |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | IPv4 ip = (IPv4) ethPkt.getPayload(); |
| 268 | IpAddress gaddr = IpAddress.valueOf(ip.getDestinationAddress()); |
| 269 | IpAddress saddr = Ip4Address.valueOf(ip.getSourceAddress()); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 270 | log.debug("Packet ({}, {}) -> ingress port: {}", saddr, gaddr, |
| 271 | context.inPacket().receivedFrom()); |
| 272 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 273 | |
| 274 | if (ip.getProtocol() != IPv4.PROTOCOL_IGMP) { |
Rusty Eddy | 158d5d8 | 2015-10-12 16:59:04 -0700 | [diff] [blame] | 275 | log.debug("IGMP Picked up a non IGMP packet."); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 276 | return; |
| 277 | } |
| 278 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 279 | IpPrefix mcast = IpPrefix.valueOf(DEFAULT_MCAST_ADDR); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 280 | if (!mcast.contains(gaddr)) { |
Rusty Eddy | 158d5d8 | 2015-10-12 16:59:04 -0700 | [diff] [blame] | 281 | log.debug("IGMP Picked up a non multicast packet."); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 282 | return; |
| 283 | } |
| 284 | |
| 285 | if (mcast.contains(saddr)) { |
Rusty Eddy | 158d5d8 | 2015-10-12 16:59:04 -0700 | [diff] [blame] | 286 | log.debug("IGMP Picked up a packet with a multicast source address."); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 287 | return; |
| 288 | } |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 289 | |
| 290 | IGMP igmp = (IGMP) ip.getPayload(); |
| 291 | switch (igmp.getIgmpType()) { |
| 292 | |
| 293 | case IGMP.TYPE_IGMPV3_MEMBERSHIP_REPORT: |
Jonathan Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame^] | 294 | processMembership(igmp, pkt.receivedFrom()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 295 | break; |
| 296 | |
| 297 | case IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY: |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 298 | processQuery(igmp, pkt.receivedFrom()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 299 | break; |
| 300 | |
| 301 | case IGMP.TYPE_IGMPV1_MEMBERSHIP_REPORT: |
| 302 | case IGMP.TYPE_IGMPV2_MEMBERSHIP_REPORT: |
| 303 | case IGMP.TYPE_IGMPV2_LEAVE_GROUP: |
Jonathan Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame^] | 304 | log.debug("IGMP version 1 & 2 message types are not currently supported. Message type: {}", |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 305 | igmp.getIgmpType()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 306 | break; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 307 | default: |
Jonathan Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame^] | 308 | log.debug("Unknown IGMP message type: {}", igmp.getIgmpType()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 309 | break; |
| 310 | } |
| 311 | } |
| 312 | } |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 313 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 314 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 315 | private class InternalDeviceListener implements DeviceListener { |
| 316 | @Override |
| 317 | public void event(DeviceEvent event) { |
| 318 | switch (event.type()) { |
| 319 | |
| 320 | case DEVICE_ADDED: |
| 321 | case DEVICE_UPDATED: |
| 322 | case DEVICE_REMOVED: |
| 323 | case DEVICE_SUSPENDED: |
| 324 | case DEVICE_AVAILABILITY_CHANGED: |
| 325 | case PORT_STATS_UPDATED: |
| 326 | break; |
| 327 | case PORT_ADDED: |
| 328 | if (event.port().isEnabled()) { |
| 329 | processFilterObjective(event.subject().id(), event.port(), false); |
| 330 | } |
| 331 | break; |
| 332 | case PORT_UPDATED: |
| 333 | if (event.port().isEnabled()) { |
| 334 | processFilterObjective(event.subject().id(), event.port(), false); |
| 335 | } else { |
| 336 | processFilterObjective(event.subject().id(), event.port(), true); |
| 337 | } |
| 338 | break; |
| 339 | case PORT_REMOVED: |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 340 | processFilterObjective(event.subject().id(), event.port(), true); |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 341 | break; |
| 342 | default: |
| 343 | log.warn("Unknown device event {}", event.type()); |
| 344 | break; |
| 345 | } |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | @Override |
| 349 | public boolean isRelevant(DeviceEvent event) { |
| 350 | return oltData.containsKey(event.subject().id()); |
| 351 | } |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 352 | } |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 353 | |
| 354 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 355 | @Override |
| 356 | public void event(NetworkConfigEvent event) { |
| 357 | switch (event.type()) { |
| 358 | |
| 359 | case CONFIG_ADDED: |
| 360 | case CONFIG_UPDATED: |
| 361 | if (event.configClass().equals(CONFIG_CLASS)) { |
| 362 | AccessDeviceConfig config = |
| 363 | networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS); |
| 364 | if (config != null) { |
| 365 | oltData.put(config.getOlt().deviceId(), config.getOlt()); |
| 366 | provisionDefaultFlows((DeviceId) event.subject()); |
| 367 | } |
| 368 | } |
| 369 | break; |
| 370 | case CONFIG_UNREGISTERED: |
| 371 | case CONFIG_REMOVED: |
| 372 | default: |
| 373 | break; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | private void provisionDefaultFlows(DeviceId deviceId) { |
| 379 | List<Port> ports = deviceService.getPorts(deviceId); |
| 380 | |
| 381 | ports.stream() |
| 382 | .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number())) |
| 383 | .filter(p -> p.isEnabled()) |
| 384 | .forEach(p -> processFilterObjective((DeviceId) p.element().id(), p, false)); |
| 385 | |
| 386 | } |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 387 | } |