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