sangho | 80f11cb | 2015-04-01 13:05:26 -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.segmentrouting; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Service; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 24 | import org.onlab.packet.Ethernet; |
| 25 | import org.onlab.packet.IPv4; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 26 | import org.onlab.util.KryoNamespace; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
| 29 | import org.onosproject.event.Event; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 30 | import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler; |
| 31 | import org.onosproject.segmentrouting.grouphandler.NeighborSet; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 32 | import org.onosproject.segmentrouting.grouphandler.NeighborSetNextObjectiveStoreKey; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 33 | import org.onosproject.mastership.MastershipService; |
| 34 | import org.onosproject.net.Device; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.Link; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 37 | import org.onosproject.net.Port; |
| 38 | import org.onosproject.net.device.DeviceEvent; |
| 39 | import org.onosproject.net.device.DeviceListener; |
| 40 | import org.onosproject.net.device.DeviceService; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 41 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 42 | import org.onosproject.net.group.GroupKey; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 43 | import org.onosproject.net.host.HostService; |
| 44 | import org.onosproject.net.intent.IntentService; |
| 45 | import org.onosproject.net.link.LinkEvent; |
| 46 | import org.onosproject.net.link.LinkListener; |
| 47 | import org.onosproject.net.link.LinkService; |
| 48 | import org.onosproject.net.packet.InboundPacket; |
| 49 | import org.onosproject.net.packet.PacketContext; |
| 50 | import org.onosproject.net.packet.PacketProcessor; |
| 51 | import org.onosproject.net.packet.PacketService; |
| 52 | import org.onosproject.net.topology.TopologyService; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 53 | import org.onosproject.segmentrouting.config.NetworkConfigManager; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 54 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 55 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; |
| 56 | import org.onosproject.store.service.StorageService; |
| 57 | import org.onosproject.store.service.WallClockTimestamp; |
| 58 | import org.onosproject.store.service.WallclockClockManager; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 59 | import org.slf4j.Logger; |
| 60 | import org.slf4j.LoggerFactory; |
| 61 | |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 62 | import java.net.URI; |
| 63 | import java.util.HashSet; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 64 | import java.util.List; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 65 | import java.util.Map; |
| 66 | import java.util.concurrent.ConcurrentHashMap; |
| 67 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 68 | import java.util.concurrent.Executors; |
| 69 | import java.util.concurrent.ScheduledExecutorService; |
| 70 | import java.util.concurrent.ScheduledFuture; |
| 71 | import java.util.concurrent.TimeUnit; |
| 72 | |
| 73 | @SuppressWarnings("ALL") |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 74 | @Service |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 75 | @Component(immediate = true) |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 76 | public class SegmentRoutingManager implements SegmentRoutingService { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 77 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 78 | private static Logger log = LoggerFactory |
| 79 | .getLogger(SegmentRoutingManager.class); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 80 | |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 82 | protected CoreService coreService; |
| 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 85 | protected TopologyService topologyService; |
| 86 | |
| 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 88 | protected PacketService packetService; |
| 89 | |
| 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 91 | protected IntentService intentService; |
| 92 | |
| 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 94 | protected HostService hostService; |
| 95 | |
| 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected DeviceService deviceService; |
| 98 | |
| 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 100 | protected FlowObjectiveService flowObjectiveService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 101 | |
| 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 103 | protected LinkService linkService; |
| 104 | |
| 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 106 | protected MastershipService mastershipService; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 107 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 108 | protected ArpHandler arpHandler = null; |
| 109 | protected IcmpHandler icmpHandler = null; |
| 110 | protected IpHandler ipHandler = null; |
| 111 | protected RoutingRulePopulator routingRulePopulator = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 112 | protected ApplicationId appId; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 113 | protected DeviceConfiguration deviceConfiguration = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 114 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 115 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 116 | private DefaultRoutingHandler defaultRoutingHandler = null; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 117 | private TunnelHandler tunnelHandler = null; |
| 118 | private PolicyHandler policyHandler = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 119 | private InternalPacketProcessor processor = new InternalPacketProcessor(); |
| 120 | private InternalEventHandler eventHandler = new InternalEventHandler(); |
| 121 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 122 | private ScheduledExecutorService executorService = Executors |
| 123 | .newScheduledThreadPool(1); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 124 | |
| 125 | private static ScheduledFuture<?> eventHandlerFuture = null; |
| 126 | private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<Event>(); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 127 | private Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<DeviceId, DefaultGroupHandler>(); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 128 | // Per device next objective ID store with (device id + neighbor set) as key |
| 129 | private EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, |
| 130 | Integer> nsNextObjStore = null; |
| 131 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 132 | protected StorageService storageService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 133 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 134 | private NetworkConfigManager networkConfigService = new NetworkConfigManager();; |
| 135 | |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 136 | private Object threadSchedulerLock = new Object(); |
| 137 | private static int numOfEventsQueued = 0; |
| 138 | private static int numOfEventsExecuted = 0; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 139 | private static int numOfHandlerExecution = 0; |
| 140 | private static int numOfHandlerScheduled = 0; |
| 141 | |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 142 | private KryoNamespace.Builder kryoBuilder = null; |
| 143 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 144 | @Activate |
| 145 | protected void activate() { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 146 | appId = coreService |
| 147 | .registerApplication("org.onosproject.segmentrouting"); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 148 | |
| 149 | kryoBuilder = new KryoNamespace.Builder() |
| 150 | .register(NeighborSetNextObjectiveStoreKey.class, |
| 151 | NeighborSet.class, |
| 152 | DeviceId.class, |
| 153 | URI.class, |
| 154 | WallClockTimestamp.class, |
| 155 | org.onosproject.cluster.NodeId.class, |
| 156 | HashSet.class |
| 157 | ); |
| 158 | |
| 159 | log.debug("Creating EC map nsnextobjectivestore"); |
| 160 | EventuallyConsistentMapBuilder<NeighborSetNextObjectiveStoreKey, Integer> |
| 161 | nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
| 162 | |
| 163 | nsNextObjStore = nsNextObjMapBuilder |
| 164 | .withName("nsnextobjectivestore") |
| 165 | .withSerializer(kryoBuilder) |
| 166 | .withClockService(new WallclockClockManager<>()) |
| 167 | .build(); |
| 168 | log.trace("Current size {}", nsNextObjStore.size()); |
| 169 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 170 | networkConfigService.init(); |
| 171 | deviceConfiguration = new DeviceConfiguration(networkConfigService); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 172 | arpHandler = new ArpHandler(this); |
| 173 | icmpHandler = new IcmpHandler(this); |
| 174 | ipHandler = new IpHandler(this); |
| 175 | routingRulePopulator = new RoutingRulePopulator(this); |
| 176 | defaultRoutingHandler = new DefaultRoutingHandler(this); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 177 | tunnelHandler = new TunnelHandler(); |
| 178 | policyHandler = new PolicyHandler(); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 179 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 180 | packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 2); |
| 181 | linkService.addListener(new InternalLinkListener()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 182 | deviceService.addListener(new InternalDeviceListener()); |
| 183 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 184 | for (Device device : deviceService.getDevices()) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 185 | //Irrespective whether the local is a MASTER or not for this device, |
| 186 | //create group handler instance and push default TTP flow rules. |
| 187 | //Because in a multi-instance setup, instances can initiate |
| 188 | //groups for any devices. Also the default TTP rules are needed |
| 189 | //to be pushed before inserting any IP table entries for any device |
| 190 | DefaultGroupHandler groupHandler = DefaultGroupHandler |
| 191 | .createGroupHandler(device.id(), appId, |
| 192 | deviceConfiguration, linkService, |
| 193 | flowObjectiveService, |
| 194 | nsNextObjStore); |
| 195 | groupHandlerMap.put(device.id(), groupHandler); |
| 196 | defaultRoutingHandler.populateTtpRules(device.id()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 197 | } |
| 198 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 199 | defaultRoutingHandler.startPopulationProcess(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 200 | log.info("Started"); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 201 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Deactivate |
| 205 | protected void deactivate() { |
| 206 | packetService.removeProcessor(processor); |
| 207 | processor = null; |
| 208 | log.info("Stopped"); |
| 209 | } |
| 210 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 211 | |
| 212 | @Override |
| 213 | public List<Tunnel> getTunnels() { |
| 214 | return tunnelHandler.getTunnels(); |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void createTunnel(Tunnel tunnel) { |
| 219 | tunnelHandler.createTunnel(tunnel); |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public void removeTunnel(Tunnel tunnel) { |
| 224 | for (Policy policy: policyHandler.getPolicies()) { |
| 225 | if (policy.type() == Policy.Type.TUNNEL_FLOW) { |
| 226 | TunnelPolicy tunnelPolicy = (TunnelPolicy) policy; |
| 227 | if (tunnelPolicy.tunnelId().equals(tunnel.id())) { |
| 228 | log.warn("Cannot remove the tunnel used by a policy"); |
| 229 | return; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | tunnelHandler.removeTunnel(tunnel); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public void removePolicy(Policy policy) { |
| 238 | policyHandler.removePolicy(policy); |
| 239 | |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public void createPolicy(Policy policy) { |
| 244 | policyHandler.createPolicy(policy); |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public List<Policy> getPolicies() { |
| 249 | return policyHandler.getPolicies(); |
| 250 | } |
| 251 | |
sangho | 80f1f89 | 2015-05-19 11:57:42 -0700 | [diff] [blame^] | 252 | /** |
| 253 | * Returns the tunnel object with the tunnel ID. |
| 254 | * |
| 255 | * @param tunnelId Tunnel ID |
| 256 | * @return Tunnel reference |
| 257 | */ |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 258 | public Tunnel getTunnel(String tunnelId) { |
| 259 | return tunnelHandler.getTunnel(tunnelId); |
| 260 | } |
| 261 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 262 | /** |
| 263 | * Returns the GrouopKey object for the device and the NighborSet given. |
| 264 | * |
| 265 | * @param ns NeightborSet object for the GroupKey |
| 266 | * @return GroupKey object for the NeighborSet |
| 267 | */ |
| 268 | public GroupKey getGroupKey(NeighborSet ns) { |
| 269 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 270 | for (DefaultGroupHandler groupHandler : groupHandlerMap.values()) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 271 | return groupHandler.getGroupKey(ns); |
| 272 | } |
| 273 | |
| 274 | return null; |
| 275 | } |
| 276 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 277 | /** |
sangho | 80f1f89 | 2015-05-19 11:57:42 -0700 | [diff] [blame^] | 278 | * Returns the next objective ID for the NeighborSet given. If the nextObjectiveID does not exist, |
| 279 | * a new one is created and returned. |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 280 | * |
sangho | 80f1f89 | 2015-05-19 11:57:42 -0700 | [diff] [blame^] | 281 | * @param deviceId Device ID |
| 282 | * @param ns NegighborSet |
| 283 | * @return next objective ID |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 284 | */ |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 285 | public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns) { |
| 286 | |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 287 | if (groupHandlerMap.get(deviceId) != null) { |
| 288 | log.trace("getNextObjectiveId query in device {}", deviceId); |
| 289 | return groupHandlerMap |
| 290 | .get(deviceId).getNextObjectiveId(ns); |
| 291 | } else { |
| 292 | log.warn("getNextObjectiveId query in device {} not found", deviceId); |
| 293 | return -1; |
| 294 | } |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 295 | } |
| 296 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 297 | /** |
sangho | 80f1f89 | 2015-05-19 11:57:42 -0700 | [diff] [blame^] | 298 | * Removes the next objective ID. |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 299 | * |
sangho | 80f1f89 | 2015-05-19 11:57:42 -0700 | [diff] [blame^] | 300 | * @param deviceId Device ID |
| 301 | * @param objectiveId next objective ID to remove |
| 302 | * @return true, if succeeds, false otherwise |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 303 | */ |
| 304 | public boolean removeNextObjective(DeviceId deviceId, int objectiveId) { |
| 305 | return groupHandlerMap.get(deviceId).removeGroup(objectiveId); |
| 306 | } |
| 307 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 308 | private class InternalPacketProcessor implements PacketProcessor { |
| 309 | |
| 310 | @Override |
| 311 | public void process(PacketContext context) { |
| 312 | |
| 313 | if (context.isHandled()) { |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | InboundPacket pkt = context.inPacket(); |
| 318 | Ethernet ethernet = pkt.parsed(); |
| 319 | |
| 320 | if (ethernet.getEtherType() == Ethernet.TYPE_ARP) { |
| 321 | arpHandler.processPacketIn(pkt); |
| 322 | } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) { |
| 323 | IPv4 ipPacket = (IPv4) ethernet.getPayload(); |
| 324 | ipHandler.addToPacketBuffer(ipPacket); |
| 325 | if (ipPacket.getProtocol() == IPv4.PROTOCOL_ICMP) { |
| 326 | icmpHandler.processPacketIn(pkt); |
| 327 | } else { |
| 328 | ipHandler.processPacketIn(pkt); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | private class InternalLinkListener implements LinkListener { |
| 335 | @Override |
| 336 | public void event(LinkEvent event) { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 337 | if (event.type() == LinkEvent.Type.LINK_ADDED |
| 338 | || event.type() == LinkEvent.Type.LINK_REMOVED) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 339 | log.debug("Event {} received from Link Service", event.type()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 340 | scheduleEventHandlerIfNotScheduled(event); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | private class InternalDeviceListener implements DeviceListener { |
| 346 | |
| 347 | @Override |
| 348 | public void event(DeviceEvent event) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 349 | /*if (mastershipService.getLocalRole(event.subject().id()) != MastershipRole.MASTER) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 350 | log.debug("Local role {} is not MASTER for device {}", |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 351 | mastershipService.getLocalRole(event.subject().id()), |
| 352 | event.subject().id()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 353 | return; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 354 | }*/ |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 355 | |
| 356 | switch (event.type()) { |
| 357 | case DEVICE_ADDED: |
| 358 | case PORT_REMOVED: |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 359 | case DEVICE_UPDATED: |
| 360 | case DEVICE_AVAILABILITY_CHANGED: |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 361 | log.debug("Event {} received from Device Service", event.type()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 362 | scheduleEventHandlerIfNotScheduled(event); |
| 363 | break; |
| 364 | default: |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 369 | private void scheduleEventHandlerIfNotScheduled(Event event) { |
| 370 | |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 371 | synchronized (threadSchedulerLock) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 372 | eventQueue.add(event); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 373 | numOfEventsQueued++; |
| 374 | |
| 375 | if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) { |
| 376 | //No pending scheduled event handling threads. So start a new one. |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 377 | eventHandlerFuture = executorService |
| 378 | .schedule(eventHandler, 100, TimeUnit.MILLISECONDS); |
| 379 | numOfHandlerScheduled++; |
| 380 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 381 | log.trace("numOfEventsQueued {}, numOfEventHanlderScheduled {}", |
| 382 | numOfEventsQueued, |
| 383 | numOfHandlerScheduled); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 384 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | private class InternalEventHandler implements Runnable { |
| 388 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 389 | @Override |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 390 | public void run() { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 391 | try { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 392 | while (true) { |
| 393 | Event event = null; |
| 394 | synchronized (threadSchedulerLock) { |
| 395 | if (!eventQueue.isEmpty()) { |
| 396 | event = eventQueue.poll(); |
| 397 | numOfEventsExecuted++; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 398 | } else { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 399 | numOfHandlerExecution++; |
| 400 | log.debug("numOfHandlerExecution {} numOfEventsExecuted {}", |
| 401 | numOfHandlerExecution, numOfEventsExecuted); |
| 402 | break; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 403 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 404 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 405 | if (event.type() == LinkEvent.Type.LINK_ADDED) { |
| 406 | processLinkAdded((Link) event.subject()); |
| 407 | } else if (event.type() == LinkEvent.Type.LINK_REMOVED) { |
| 408 | processLinkRemoved((Link) event.subject()); |
| 409 | //} else if (event.type() == GroupEvent.Type.GROUP_ADDED) { |
| 410 | // processGroupAdded((Group) event.subject()); |
| 411 | } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED || |
| 412 | event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED || |
| 413 | event.type() == DeviceEvent.Type.DEVICE_UPDATED) { |
| 414 | if (deviceService.isAvailable(((Device) event.subject()).id())) { |
| 415 | processDeviceAdded((Device) event.subject()); |
| 416 | } |
| 417 | } else if (event.type() == DeviceEvent.Type.PORT_REMOVED) { |
| 418 | processPortRemoved((Device) event.subject(), |
| 419 | ((DeviceEvent) event).port()); |
| 420 | } else { |
| 421 | log.warn("Unhandled event type: {}", event.type()); |
| 422 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 423 | } |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 424 | } catch (Exception e) { |
| 425 | log.error("SegmentRouting event handler " |
| 426 | + "thread thrown an exception: {}", e); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 427 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 431 | private void processLinkAdded(Link link) { |
| 432 | log.debug("A new link {} was added", link.toString()); |
| 433 | |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 434 | //Irrespective whether the local is a MASTER or not for this device, |
| 435 | //create group handler instance and push default TTP flow rules. |
| 436 | //Because in a multi-instance setup, instances can initiate |
| 437 | //groups for any devices. Also the default TTP rules are needed |
| 438 | //to be pushed before inserting any IP table entries for any device |
| 439 | DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src() |
| 440 | .deviceId()); |
| 441 | if (groupHandler != null) { |
| 442 | groupHandler.linkUp(link); |
| 443 | } else { |
| 444 | Device device = deviceService.getDevice(link.src().deviceId()); |
| 445 | if (device != null) { |
| 446 | log.warn("processLinkAdded: Link Added " |
| 447 | + "Notification without Device Added " |
| 448 | + "event, still handling it"); |
| 449 | processDeviceAdded(device); |
| 450 | groupHandler = groupHandlerMap.get(link.src() |
| 451 | .deviceId()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 452 | groupHandler.linkUp(link); |
| 453 | } |
| 454 | } |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 455 | |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 456 | log.trace("Starting optimized route population process"); |
| 457 | defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null); |
| 458 | //log.trace("processLinkAdded: re-starting route population process"); |
| 459 | //defaultRoutingHandler.startPopulationProcess(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | private void processLinkRemoved(Link link) { |
| 463 | log.debug("A link {} was removed", link.toString()); |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 464 | DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId()); |
| 465 | if (groupHandler != null) { |
| 466 | groupHandler.portDown(link.src().port()); |
| 467 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 468 | log.trace("Starting optimized route population process"); |
| 469 | defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link); |
| 470 | //log.trace("processLinkRemoved: re-starting route population process"); |
| 471 | //defaultRoutingHandler.startPopulationProcess(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | private void processDeviceAdded(Device device) { |
| 475 | log.debug("A new device with ID {} was added", device.id()); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 476 | //Irrespective whether the local is a MASTER or not for this device, |
| 477 | //create group handler instance and push default TTP flow rules. |
| 478 | //Because in a multi-instance setup, instances can initiate |
| 479 | //groups for any devices. Also the default TTP rules are needed |
| 480 | //to be pushed before inserting any IP table entries for any device |
| 481 | DefaultGroupHandler dgh = DefaultGroupHandler. |
| 482 | createGroupHandler(device.id(), |
| 483 | appId, |
| 484 | deviceConfiguration, |
| 485 | linkService, |
| 486 | flowObjectiveService, |
| 487 | nsNextObjStore); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 488 | groupHandlerMap.put(device.id(), dgh); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 489 | defaultRoutingHandler.populateTtpRules(device.id()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | private void processPortRemoved(Device device, Port port) { |
| 493 | log.debug("Port {} was removed", port.toString()); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 494 | DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 495 | if (groupHandler != null) { |
| 496 | groupHandler.portDown(port.number()); |
| 497 | } |
| 498 | } |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 499 | |
| 500 | |
| 501 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 502 | } |