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