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