sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0947d7e | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 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 | |
Ray Milkey | ae0068a | 2017-08-15 11:02:29 -0700 | [diff] [blame] | 18 | import java.util.List; |
| 19 | import java.util.Map; |
| 20 | import java.util.Map.Entry; |
| 21 | import java.util.Optional; |
| 22 | import java.util.Set; |
| 23 | import java.util.concurrent.ConcurrentHashMap; |
| 24 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 25 | import java.util.concurrent.Executors; |
| 26 | import java.util.concurrent.ScheduledExecutorService; |
| 27 | import java.util.concurrent.ScheduledFuture; |
| 28 | import java.util.concurrent.TimeUnit; |
| 29 | import java.util.concurrent.atomic.AtomicBoolean; |
| 30 | import java.util.stream.Collectors; |
| 31 | |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 32 | import com.google.common.collect.Sets; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 33 | import org.apache.felix.scr.annotations.Activate; |
| 34 | import org.apache.felix.scr.annotations.Component; |
| 35 | import org.apache.felix.scr.annotations.Deactivate; |
| 36 | import org.apache.felix.scr.annotations.Reference; |
| 37 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 38 | import org.apache.felix.scr.annotations.Service; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 39 | import org.onlab.packet.Ethernet; |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 40 | import org.onlab.packet.ICMP6; |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 41 | import org.onlab.packet.IPv4; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 42 | import org.onlab.packet.IPv6; |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 43 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 44 | import org.onlab.packet.VlanId; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 45 | import org.onlab.util.KryoNamespace; |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 46 | import org.onosproject.cfg.ComponentConfigService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 47 | import org.onosproject.core.ApplicationId; |
| 48 | import org.onosproject.core.CoreService; |
| 49 | import org.onosproject.event.Event; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 50 | import org.onosproject.mastership.MastershipService; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 51 | import org.onosproject.net.ConnectPoint; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 52 | import org.onosproject.net.Device; |
| 53 | import org.onosproject.net.DeviceId; |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 54 | import org.onosproject.net.Host; |
| 55 | import org.onosproject.net.HostId; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 56 | import org.onosproject.net.Link; |
| 57 | import org.onosproject.net.Port; |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 58 | import org.onosproject.net.PortNumber; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 59 | import org.onosproject.net.config.ConfigFactory; |
| 60 | import org.onosproject.net.config.NetworkConfigEvent; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 61 | import org.onosproject.net.config.NetworkConfigListener; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 62 | import org.onosproject.net.config.NetworkConfigRegistry; |
Ray Milkey | ae0068a | 2017-08-15 11:02:29 -0700 | [diff] [blame] | 63 | import org.onosproject.net.config.basics.InterfaceConfig; |
| 64 | import org.onosproject.net.config.basics.McastConfig; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 65 | import org.onosproject.net.config.basics.SubjectFactories; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 66 | import org.onosproject.net.device.DeviceEvent; |
| 67 | import org.onosproject.net.device.DeviceListener; |
| 68 | import org.onosproject.net.device.DeviceService; |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 69 | import org.onosproject.net.flow.TrafficSelector; |
| 70 | import org.onosproject.net.flow.TrafficTreatment; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 71 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 72 | import org.onosproject.net.host.HostEvent; |
| 73 | import org.onosproject.net.host.HostListener; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 74 | import org.onosproject.net.host.HostService; |
Ray Milkey | ae0068a | 2017-08-15 11:02:29 -0700 | [diff] [blame] | 75 | import org.onosproject.net.intf.Interface; |
| 76 | import org.onosproject.net.intf.InterfaceService; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 77 | import org.onosproject.net.link.LinkEvent; |
| 78 | import org.onosproject.net.link.LinkListener; |
| 79 | import org.onosproject.net.link.LinkService; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 80 | import org.onosproject.net.mcast.McastEvent; |
| 81 | import org.onosproject.net.mcast.McastListener; |
| 82 | import org.onosproject.net.mcast.MulticastRouteService; |
Ray Milkey | ae0068a | 2017-08-15 11:02:29 -0700 | [diff] [blame] | 83 | import org.onosproject.net.neighbour.NeighbourResolutionService; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 84 | import org.onosproject.net.packet.InboundPacket; |
| 85 | import org.onosproject.net.packet.PacketContext; |
| 86 | import org.onosproject.net.packet.PacketProcessor; |
| 87 | import org.onosproject.net.packet.PacketService; |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 88 | import org.onosproject.net.topology.PathService; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 89 | import org.onosproject.net.topology.TopologyService; |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 90 | import org.onosproject.routeservice.ResolvedRoute; |
Ray Milkey | ae0068a | 2017-08-15 11:02:29 -0700 | [diff] [blame] | 91 | import org.onosproject.routeservice.RouteEvent; |
| 92 | import org.onosproject.routeservice.RouteListener; |
| 93 | import org.onosproject.routeservice.RouteService; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 94 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 95 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 96 | import org.onosproject.segmentrouting.config.PwaasConfig; |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 97 | import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig; |
Ray Milkey | ae0068a | 2017-08-15 11:02:29 -0700 | [diff] [blame] | 98 | import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig; |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 99 | import org.onosproject.segmentrouting.config.XConnectConfig; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 100 | import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 101 | import org.onosproject.segmentrouting.grouphandler.DestinationSet; |
| 102 | import org.onosproject.segmentrouting.grouphandler.NextNeighbors; |
Ray Milkey | ae0068a | 2017-08-15 11:02:29 -0700 | [diff] [blame] | 103 | import org.onosproject.segmentrouting.pwaas.L2TunnelHandler; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 104 | import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 105 | import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey; |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 106 | import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey; |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 107 | import org.onosproject.segmentrouting.storekey.XConnectStoreKey; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 108 | import org.onosproject.store.serializers.KryoNamespaces; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 109 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 110 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; |
| 111 | import org.onosproject.store.service.StorageService; |
| 112 | import org.onosproject.store.service.WallClockTimestamp; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 113 | import org.slf4j.Logger; |
| 114 | import org.slf4j.LoggerFactory; |
| 115 | |
Ray Milkey | ae0068a | 2017-08-15 11:02:29 -0700 | [diff] [blame] | 116 | import com.google.common.collect.HashMultimap; |
| 117 | import com.google.common.collect.ImmutableMap; |
| 118 | import com.google.common.collect.Maps; |
| 119 | import com.google.common.collect.Multimap; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 120 | |
Charles Chan | d6d2533 | 2016-02-26 22:19:52 -0800 | [diff] [blame] | 121 | import static com.google.common.base.Preconditions.checkState; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 122 | import static org.onlab.packet.Ethernet.TYPE_ARP; |
Yuta HIGUCHI | ebee2f1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 123 | import static org.onlab.util.Tools.groupedThreads; |
Charles Chan | d6d2533 | 2016-02-26 22:19:52 -0800 | [diff] [blame] | 124 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 125 | /** |
| 126 | * Segment routing manager. |
| 127 | */ |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 128 | @Service |
| 129 | @Component(immediate = true) |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 130 | public class SegmentRoutingManager implements SegmentRoutingService { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 131 | |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 132 | private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class); |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame^] | 133 | private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore."; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 134 | |
| 135 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 136 | private ComponentConfigService compCfgService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 137 | |
| 138 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 139 | private NeighbourResolutionService neighbourResolutionService; |
| 140 | |
| 141 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 142 | public PathService pathService; |
| 143 | |
| 144 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 145 | CoreService coreService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 146 | |
| 147 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 148 | PacketService packetService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 149 | |
| 150 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 151 | HostService hostService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 152 | |
| 153 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 154 | DeviceService deviceService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 155 | |
| 156 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 157 | public FlowObjectiveService flowObjectiveService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 158 | |
| 159 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 160 | LinkService linkService; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 161 | |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 162 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 163 | public MastershipService mastershipService; |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 164 | |
| 165 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 166 | public StorageService storageService; |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 167 | |
| 168 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 169 | MulticastRouteService multicastRouteService; |
| 170 | |
| 171 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 172 | TopologyService topologyService; |
| 173 | |
| 174 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 175 | RouteService routeService; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 176 | |
| 177 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 178 | public NetworkConfigRegistry cfgService; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 179 | |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 180 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 181 | public InterfaceService interfaceService; |
| 182 | |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 183 | ArpHandler arpHandler = null; |
| 184 | IcmpHandler icmpHandler = null; |
| 185 | IpHandler ipHandler = null; |
| 186 | RoutingRulePopulator routingRulePopulator = null; |
Ray Milkey | b85de08 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 187 | ApplicationId appId; |
| 188 | DeviceConfiguration deviceConfiguration = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 189 | |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 190 | DefaultRoutingHandler defaultRoutingHandler = null; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 191 | private TunnelHandler tunnelHandler = null; |
| 192 | private PolicyHandler policyHandler = null; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 193 | private InternalPacketProcessor processor = null; |
| 194 | private InternalLinkListener linkListener = null; |
| 195 | private InternalDeviceListener deviceListener = null; |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 196 | private AppConfigHandler appCfgHandler = null; |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 197 | XConnectHandler xConnectHandler = null; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 198 | private McastHandler mcastHandler = null; |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 199 | private HostHandler hostHandler = null; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 200 | private RouteHandler routeHandler = null; |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 201 | private SegmentRoutingNeighbourDispatcher neighbourHandler = null; |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 202 | private L2TunnelHandler l2TunnelHandler = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 203 | private InternalEventHandler eventHandler = new InternalEventHandler(); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 204 | private final InternalHostListener hostListener = new InternalHostListener(); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 205 | private final InternalConfigListener cfgListener = new InternalConfigListener(this); |
| 206 | private final InternalMcastListener mcastListener = new InternalMcastListener(); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 207 | private final InternalRouteEventListener routeListener = new InternalRouteEventListener(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 208 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 209 | private ScheduledExecutorService executorService = Executors |
Yuta HIGUCHI | ebee2f1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 210 | .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log)); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 211 | |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 212 | @SuppressWarnings("unused") |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 213 | private static ScheduledFuture<?> eventHandlerFuture = null; |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 214 | @SuppressWarnings("rawtypes") |
Yuta HIGUCHI | ebee2f1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 215 | private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>(); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 216 | private Map<DeviceId, DefaultGroupHandler> groupHandlerMap = |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 217 | new ConcurrentHashMap<>(); |
| 218 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 219 | * Per device next objective ID store with (device id + destination set) as key. |
Charles Chan | 53de91f | 2017-08-22 15:07:34 -0700 | [diff] [blame] | 220 | * Used to keep track on MPLS group information. |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 221 | */ |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 222 | EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> |
| 223 | dsNextObjStore = null; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 224 | /** |
Charles Chan | 53de91f | 2017-08-22 15:07:34 -0700 | [diff] [blame] | 225 | * Per device next objective ID store with (device id + vlanid) as key. |
| 226 | * Used to keep track on L2 flood group information. |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 227 | */ |
Ray Milkey | b85de08 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 228 | EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 229 | vlanNextObjStore = null; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 230 | /** |
Charles Chan | 53de91f | 2017-08-22 15:07:34 -0700 | [diff] [blame] | 231 | * Per device next objective ID store with (device id + port + treatment + meta) as key. |
| 232 | * Used to keep track on L2 interface group and L3 unicast group information. |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 233 | */ |
Ray Milkey | b85de08 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 234 | EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 235 | portNextObjStore = null; |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 236 | |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 237 | // Local store for all links seen and their present status, used for |
| 238 | // optimized routing. The existence of the link in the keys is enough to know |
| 239 | // if the link has been "seen-before" by this instance of the controller. |
| 240 | // The boolean value indicates if the link is currently up or not. |
| 241 | // XXX Currently the optimized routing logic depends on "forgetting" a link |
| 242 | // when a switch goes down, but "remembering" it when only the link goes down. |
| 243 | // Consider changing this logic so we can use the Link Service instead of |
| 244 | // a local cache. |
| 245 | private Map<Link, Boolean> seenLinks = new ConcurrentHashMap<>(); |
| 246 | |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 247 | private EventuallyConsistentMap<String, Tunnel> tunnelStore = null; |
| 248 | private EventuallyConsistentMap<String, Policy> policyStore = null; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 249 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 250 | private AtomicBoolean programmingScheduled = new AtomicBoolean(); |
| 251 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 252 | private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory = |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 253 | new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>( |
| 254 | SubjectFactories.DEVICE_SUBJECT_FACTORY, |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 255 | SegmentRoutingDeviceConfig.class, "segmentrouting") { |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 256 | @Override |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 257 | public SegmentRoutingDeviceConfig createConfig() { |
| 258 | return new SegmentRoutingDeviceConfig(); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 259 | } |
| 260 | }; |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 261 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 262 | private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory = |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 263 | new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>( |
| 264 | SubjectFactories.APP_SUBJECT_FACTORY, |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 265 | SegmentRoutingAppConfig.class, "segmentrouting") { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 266 | @Override |
| 267 | public SegmentRoutingAppConfig createConfig() { |
| 268 | return new SegmentRoutingAppConfig(); |
| 269 | } |
| 270 | }; |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 271 | |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 272 | private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory = |
| 273 | new ConfigFactory<ApplicationId, XConnectConfig>( |
| 274 | SubjectFactories.APP_SUBJECT_FACTORY, |
| 275 | XConnectConfig.class, "xconnect") { |
| 276 | @Override |
| 277 | public XConnectConfig createConfig() { |
| 278 | return new XConnectConfig(); |
| 279 | } |
| 280 | }; |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 281 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 282 | private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory = |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 283 | new ConfigFactory<ApplicationId, McastConfig>( |
| 284 | SubjectFactories.APP_SUBJECT_FACTORY, |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 285 | McastConfig.class, "multicast") { |
| 286 | @Override |
| 287 | public McastConfig createConfig() { |
| 288 | return new McastConfig(); |
| 289 | } |
| 290 | }; |
| 291 | |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 292 | private final ConfigFactory<ApplicationId, PwaasConfig> pwaasConfigFactory = |
| 293 | new ConfigFactory<ApplicationId, PwaasConfig>( |
| 294 | SubjectFactories.APP_SUBJECT_FACTORY, |
| 295 | PwaasConfig.class, "pwaas") { |
| 296 | @Override |
| 297 | public PwaasConfig createConfig() { |
| 298 | return new PwaasConfig(); |
| 299 | } |
| 300 | }; |
| 301 | |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 302 | private static final Object THREAD_SCHED_LOCK = new Object(); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 303 | private static int numOfEventsQueued = 0; |
| 304 | private static int numOfEventsExecuted = 0; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 305 | private static int numOfHandlerExecution = 0; |
| 306 | private static int numOfHandlerScheduled = 0; |
| 307 | |
Charles Chan | 1963f4f | 2016-02-18 14:22:42 -0800 | [diff] [blame] | 308 | /** |
| 309 | * Segment Routing App ID. |
| 310 | */ |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 311 | public static final String APP_NAME = "org.onosproject.segmentrouting"; |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 312 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 313 | /** |
| 314 | * The default VLAN ID assigned to the interfaces without subnet config. |
| 315 | */ |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 316 | public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094); |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 317 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 318 | @Activate |
| 319 | protected void activate() { |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 320 | appId = coreService.registerApplication(APP_NAME); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 321 | |
| 322 | log.debug("Creating EC map nsnextobjectivestore"); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 323 | EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors> |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 324 | nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 325 | dsNextObjStore = nsNextObjMapBuilder |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 326 | .withName("nsnextobjectivestore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 327 | .withSerializer(createSerializer()) |
Madan Jampani | 675ae20 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 328 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 329 | .build(); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 330 | log.trace("Current size {}", dsNextObjStore.size()); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 331 | |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 332 | log.debug("Creating EC map vlannextobjectivestore"); |
| 333 | EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer> |
| 334 | vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
| 335 | vlanNextObjStore = vlanNextObjMapBuilder |
| 336 | .withName("vlannextobjectivestore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 337 | .withSerializer(createSerializer()) |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 338 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 339 | .build(); |
| 340 | |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 341 | log.debug("Creating EC map subnetnextobjectivestore"); |
| 342 | EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer> |
| 343 | portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
| 344 | portNextObjStore = portNextObjMapBuilder |
| 345 | .withName("portnextobjectivestore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 346 | .withSerializer(createSerializer()) |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 347 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 348 | .build(); |
| 349 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 350 | EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder = |
| 351 | storageService.eventuallyConsistentMapBuilder(); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 352 | tunnelStore = tunnelMapBuilder |
| 353 | .withName("tunnelstore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 354 | .withSerializer(createSerializer()) |
Madan Jampani | 675ae20 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 355 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 356 | .build(); |
| 357 | |
| 358 | EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder = |
| 359 | storageService.eventuallyConsistentMapBuilder(); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 360 | policyStore = policyMapBuilder |
| 361 | .withName("policystore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 362 | .withSerializer(createSerializer()) |
Madan Jampani | 675ae20 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 363 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 364 | .build(); |
| 365 | |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 366 | compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager", |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 367 | "purgeOnDisconnection", "true"); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 368 | compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager", |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 369 | "purgeOnDisconnection", "true"); |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 370 | compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider", |
| 371 | "requestInterceptsEnabled", "false"); |
Charles Chan | c1909e1 | 2017-08-08 15:13:37 -0700 | [diff] [blame] | 372 | compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager", |
Pier Luigi | bc976df | 2017-01-12 22:46:39 -0800 | [diff] [blame] | 373 | "requestInterceptsEnabled", "false"); |
Charles Chan | 9597f8d | 2017-07-24 15:56:10 -0700 | [diff] [blame] | 374 | compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager", |
Pier Luigi | bc976df | 2017-01-12 22:46:39 -0800 | [diff] [blame] | 375 | "arpEnabled", "false"); |
Pier Luigi | 0ebeb31 | 2017-02-02 22:31:34 -0800 | [diff] [blame] | 376 | compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager", |
| 377 | "greedyLearningIpv6", "true"); |
Charles Chan | cf8ea47 | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 378 | compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager", |
| 379 | "forceUnprovision", "true"); |
Charles Chan | c7b73c7 | 2017-08-10 16:57:28 -0700 | [diff] [blame] | 380 | compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl", |
Charles Chan | 4c95c0d | 2017-07-20 16:16:25 -0700 | [diff] [blame] | 381 | "distributed", "true"); |
Charles Chan | 804772f | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 382 | compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider", |
| 383 | "multihomingEnabled", "true"); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 384 | |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 385 | processor = new InternalPacketProcessor(); |
| 386 | linkListener = new InternalLinkListener(); |
| 387 | deviceListener = new InternalDeviceListener(); |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 388 | appCfgHandler = new AppConfigHandler(this); |
| 389 | xConnectHandler = new XConnectHandler(this); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 390 | mcastHandler = new McastHandler(this); |
| 391 | hostHandler = new HostHandler(this); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 392 | routeHandler = new RouteHandler(this); |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 393 | neighbourHandler = new SegmentRoutingNeighbourDispatcher(this); |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 394 | l2TunnelHandler = new L2TunnelHandler(this); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 395 | |
Charles Chan | d6d2533 | 2016-02-26 22:19:52 -0800 | [diff] [blame] | 396 | cfgService.addListener(cfgListener); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 397 | cfgService.registerConfigFactory(deviceConfigFactory); |
| 398 | cfgService.registerConfigFactory(appConfigFactory); |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 399 | cfgService.registerConfigFactory(xConnectConfigFactory); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 400 | cfgService.registerConfigFactory(mcastConfigFactory); |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 401 | cfgService.registerConfigFactory(pwaasConfigFactory); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 402 | hostService.addListener(hostListener); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 403 | packetService.addProcessor(processor, PacketProcessor.director(2)); |
| 404 | linkService.addListener(linkListener); |
| 405 | deviceService.addListener(deviceListener); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 406 | multicastRouteService.addListener(mcastListener); |
Charles Chan | fd48c22 | 2017-06-19 00:43:31 -0700 | [diff] [blame] | 407 | routeService.addListener(routeListener); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 408 | |
| 409 | cfgListener.configureNetwork(); |
| 410 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 411 | log.info("Started"); |
| 412 | } |
| 413 | |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 414 | private KryoNamespace.Builder createSerializer() { |
| 415 | return new KryoNamespace.Builder() |
| 416 | .register(KryoNamespaces.API) |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 417 | .register(DestinationSetNextObjectiveStoreKey.class, |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 418 | VlanNextObjectiveStoreKey.class, |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 419 | DestinationSet.class, |
| 420 | NextNeighbors.class, |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 421 | Tunnel.class, |
| 422 | DefaultTunnel.class, |
| 423 | Policy.class, |
| 424 | TunnelPolicy.class, |
| 425 | Policy.Type.class, |
| 426 | PortNextObjectiveStoreKey.class, |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 427 | XConnectStoreKey.class |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 428 | ); |
| 429 | } |
| 430 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 431 | @Deactivate |
| 432 | protected void deactivate() { |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 433 | cfgService.removeListener(cfgListener); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 434 | cfgService.unregisterConfigFactory(deviceConfigFactory); |
| 435 | cfgService.unregisterConfigFactory(appConfigFactory); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 436 | cfgService.unregisterConfigFactory(xConnectConfigFactory); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 437 | cfgService.unregisterConfigFactory(mcastConfigFactory); |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 438 | cfgService.unregisterConfigFactory(pwaasConfigFactory); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 439 | |
Charles Chan | fd48c22 | 2017-06-19 00:43:31 -0700 | [diff] [blame] | 440 | hostService.removeListener(hostListener); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 441 | packetService.removeProcessor(processor); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 442 | linkService.removeListener(linkListener); |
| 443 | deviceService.removeListener(deviceListener); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 444 | multicastRouteService.removeListener(mcastListener); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 445 | routeService.removeListener(routeListener); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 446 | |
Charles Chan | 2e71ef3 | 2017-02-23 15:44:08 -0800 | [diff] [blame] | 447 | neighbourResolutionService.unregisterNeighbourHandlers(appId); |
| 448 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 449 | processor = null; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 450 | linkListener = null; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 451 | deviceListener = null; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 452 | groupHandlerMap.clear(); |
| 453 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 454 | dsNextObjStore.destroy(); |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 455 | vlanNextObjStore.destroy(); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 456 | portNextObjStore.destroy(); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 457 | tunnelStore.destroy(); |
| 458 | policyStore.destroy(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 459 | log.info("Stopped"); |
| 460 | } |
| 461 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 462 | @Override |
| 463 | public List<Tunnel> getTunnels() { |
| 464 | return tunnelHandler.getTunnels(); |
| 465 | } |
| 466 | |
| 467 | @Override |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 468 | public TunnelHandler.Result createTunnel(Tunnel tunnel) { |
| 469 | return tunnelHandler.createTunnel(tunnel); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | @Override |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 473 | public TunnelHandler.Result removeTunnel(Tunnel tunnel) { |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 474 | for (Policy policy: policyHandler.getPolicies()) { |
| 475 | if (policy.type() == Policy.Type.TUNNEL_FLOW) { |
| 476 | TunnelPolicy tunnelPolicy = (TunnelPolicy) policy; |
| 477 | if (tunnelPolicy.tunnelId().equals(tunnel.id())) { |
| 478 | log.warn("Cannot remove the tunnel used by a policy"); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 479 | return TunnelHandler.Result.TUNNEL_IN_USE; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | } |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 483 | return tunnelHandler.removeTunnel(tunnel); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | @Override |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 487 | public PolicyHandler.Result removePolicy(Policy policy) { |
| 488 | return policyHandler.removePolicy(policy); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | @Override |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 492 | public PolicyHandler.Result createPolicy(Policy policy) { |
| 493 | return policyHandler.createPolicy(policy); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | @Override |
| 497 | public List<Policy> getPolicies() { |
| 498 | return policyHandler.getPolicies(); |
| 499 | } |
| 500 | |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 501 | @Override |
| 502 | public void rerouteNetwork() { |
| 503 | cfgListener.configureNetwork(); |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Charles Chan | d7844e5 | 2016-10-20 17:02:44 -0700 | [diff] [blame] | 506 | @Override |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 507 | public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() { |
| 508 | Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap(); |
Charles Chan | d7844e5 | 2016-10-20 17:02:44 -0700 | [diff] [blame] | 509 | deviceService.getAvailableDevices().forEach(device -> { |
| 510 | deviceSubnetMap.put(device.id(), deviceConfiguration.getSubnets(device.id())); |
| 511 | }); |
| 512 | return deviceSubnetMap; |
| 513 | } |
| 514 | |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 515 | |
| 516 | @Override |
| 517 | public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() { |
| 518 | if (defaultRoutingHandler != null) { |
| 519 | return defaultRoutingHandler.getCurrentEmcpSpgMap(); |
| 520 | } else { |
| 521 | return null; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | @Override |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 526 | public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() { |
| 527 | if (dsNextObjStore != null) { |
| 528 | return ImmutableMap.copyOf(dsNextObjStore.entrySet()); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 529 | } else { |
| 530 | return ImmutableMap.of(); |
| 531 | } |
| 532 | } |
| 533 | |
Saurav Das | fbe7457 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 534 | @Override |
| 535 | public void verifyGroups(DeviceId id) { |
| 536 | DefaultGroupHandler gh = groupHandlerMap.get(id); |
| 537 | if (gh != null) { |
| 538 | gh.triggerBucketCorrector(); |
| 539 | } |
| 540 | } |
| 541 | |
sangho | 80f1f89 | 2015-05-19 11:57:42 -0700 | [diff] [blame] | 542 | /** |
Ray Milkey | b85de08 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 543 | * Extracts the application ID from the manager. |
| 544 | * |
| 545 | * @return application ID |
| 546 | */ |
| 547 | public ApplicationId appId() { |
| 548 | return appId; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Returns the device configuration. |
| 553 | * |
| 554 | * @return device configuration |
| 555 | */ |
| 556 | public DeviceConfiguration deviceConfiguration() { |
| 557 | return deviceConfiguration; |
| 558 | } |
| 559 | |
| 560 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 561 | * Per device next objective ID store with (device id + destination set) as key. |
Charles Chan | 53de91f | 2017-08-22 15:07:34 -0700 | [diff] [blame] | 562 | * Used to keep track on MPLS group information. |
Ray Milkey | b85de08 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 563 | * |
| 564 | * @return next objective ID store |
| 565 | */ |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 566 | public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> |
| 567 | dsNextObjStore() { |
| 568 | return dsNextObjStore; |
Ray Milkey | b85de08 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /** |
Charles Chan | 53de91f | 2017-08-22 15:07:34 -0700 | [diff] [blame] | 572 | * Per device next objective ID store with (device id + vlanid) as key. |
| 573 | * Used to keep track on L2 flood group information. |
Ray Milkey | b85de08 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 574 | * |
| 575 | * @return vlan next object store |
| 576 | */ |
| 577 | public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() { |
| 578 | return vlanNextObjStore; |
| 579 | } |
| 580 | |
| 581 | /** |
Charles Chan | 53de91f | 2017-08-22 15:07:34 -0700 | [diff] [blame] | 582 | * Per device next objective ID store with (device id + port + treatment + meta) as key. |
| 583 | * Used to keep track on L2 interface group and L3 unicast group information. |
Ray Milkey | b85de08 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 584 | * |
| 585 | * @return port next object store. |
| 586 | */ |
| 587 | public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() { |
| 588 | return portNextObjStore; |
| 589 | } |
| 590 | |
| 591 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 592 | * Returns the MPLS-ECMP configuration which indicates whether ECMP on |
| 593 | * labeled packets should be programmed or not. |
Pier Ventre | 7a78de2 | 2016-10-31 15:00:01 -0700 | [diff] [blame] | 594 | * |
| 595 | * @return MPLS-ECMP value |
| 596 | */ |
| 597 | public boolean getMplsEcmp() { |
| 598 | SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService |
| 599 | .getConfig(this.appId, SegmentRoutingAppConfig.class); |
| 600 | return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp(); |
| 601 | } |
| 602 | |
| 603 | /** |
sangho | 80f1f89 | 2015-05-19 11:57:42 -0700 | [diff] [blame] | 604 | * Returns the tunnel object with the tunnel ID. |
| 605 | * |
| 606 | * @param tunnelId Tunnel ID |
| 607 | * @return Tunnel reference |
| 608 | */ |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 609 | public Tunnel getTunnel(String tunnelId) { |
| 610 | return tunnelHandler.getTunnel(tunnelId); |
| 611 | } |
| 612 | |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 613 | // TODO Consider moving these to InterfaceService |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 614 | /** |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 615 | * Returns untagged VLAN configured on given connect point. |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 616 | * <p> |
| 617 | * Only returns the first match if there are multiple untagged VLAN configured |
| 618 | * on the connect point. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 619 | * |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 620 | * @param connectPoint connect point |
| 621 | * @return untagged VLAN or null if not configured |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 622 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 623 | VlanId getUntaggedVlanId(ConnectPoint connectPoint) { |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 624 | return interfaceService.getInterfacesByPort(connectPoint).stream() |
| 625 | .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE)) |
| 626 | .map(Interface::vlanUntagged) |
| 627 | .findFirst().orElse(null); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 628 | } |
| 629 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 630 | /** |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 631 | * Returns tagged VLAN configured on given connect point. |
| 632 | * <p> |
| 633 | * Returns all matches if there are multiple tagged VLAN configured |
| 634 | * on the connect point. |
| 635 | * |
| 636 | * @param connectPoint connect point |
| 637 | * @return tagged VLAN or empty set if not configured |
| 638 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 639 | Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 640 | Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint); |
| 641 | return interfaces.stream() |
| 642 | .map(Interface::vlanTagged) |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 643 | .flatMap(Set::stream) |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 644 | .collect(Collectors.toSet()); |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Returns native VLAN configured on given connect point. |
| 649 | * <p> |
| 650 | * Only returns the first match if there are multiple native VLAN configured |
| 651 | * on the connect point. |
| 652 | * |
| 653 | * @param connectPoint connect point |
| 654 | * @return native VLAN or null if not configured |
| 655 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 656 | VlanId getNativeVlanId(ConnectPoint connectPoint) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 657 | Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint); |
| 658 | return interfaces.stream() |
| 659 | .filter(intf -> !intf.vlanNative().equals(VlanId.NONE)) |
| 660 | .map(Interface::vlanNative) |
| 661 | .findFirst() |
| 662 | .orElse(null); |
| 663 | } |
| 664 | |
| 665 | /** |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 666 | * Returns internal VLAN for untagged hosts on given connect point. |
| 667 | * <p> |
| 668 | * The internal VLAN is either vlan-untagged for an access port, |
| 669 | * or vlan-native for a trunk port. |
| 670 | * |
| 671 | * @param connectPoint connect point |
| 672 | * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined |
| 673 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 674 | VlanId getInternalVlanId(ConnectPoint connectPoint) { |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 675 | VlanId untaggedVlanId = getUntaggedVlanId(connectPoint); |
| 676 | VlanId nativeVlanId = getNativeVlanId(connectPoint); |
| 677 | return untaggedVlanId != null ? untaggedVlanId : nativeVlanId; |
| 678 | } |
| 679 | |
| 680 | /** |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 681 | * Returns optional pair device ID of given device. |
| 682 | * |
| 683 | * @param deviceId device ID |
| 684 | * @return optional pair device ID. Might be empty if pair device is not configured |
| 685 | */ |
| 686 | Optional<DeviceId> getPairDeviceId(DeviceId deviceId) { |
| 687 | SegmentRoutingDeviceConfig deviceConfig = |
| 688 | cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class); |
| 689 | return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId); |
| 690 | } |
| 691 | /** |
| 692 | * Returns optional pair device local port of given device. |
| 693 | * |
| 694 | * @param deviceId device ID |
| 695 | * @return optional pair device ID. Might be empty if pair device is not configured |
| 696 | */ |
| 697 | Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) { |
| 698 | SegmentRoutingDeviceConfig deviceConfig = |
| 699 | cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class); |
| 700 | return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort); |
| 701 | } |
| 702 | |
| 703 | /** |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame^] | 704 | * Determine if current instance is the master of given connect point. |
| 705 | * |
| 706 | * @param cp connect point |
| 707 | * @return true if current instance is the master of given connect point |
| 708 | */ |
| 709 | boolean isMasterOf(ConnectPoint cp) { |
| 710 | boolean isMaster = mastershipService.isLocalMaster(cp.deviceId()); |
| 711 | if (!isMaster) { |
| 712 | log.debug(NOT_MASTER, cp); |
| 713 | } |
| 714 | return isMaster; |
| 715 | } |
| 716 | |
| 717 | /** |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 718 | * Returns locations of given resolved route. |
| 719 | * |
| 720 | * @param resolvedRoute resolved route |
| 721 | * @return locations of nexthop. Might be empty if next hop is not found |
| 722 | */ |
| 723 | Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) { |
| 724 | HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan()); |
| 725 | return Optional.ofNullable(hostService.getHost(hostId)) |
| 726 | .map(Host::locations).orElse(Sets.newHashSet()) |
| 727 | .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet()); |
| 728 | } |
| 729 | |
| 730 | /** |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 731 | * Returns vlan port map of given device. |
| 732 | * |
| 733 | * @param deviceId device id |
| 734 | * @return vlan-port multimap |
| 735 | */ |
| 736 | public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) { |
| 737 | HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create(); |
| 738 | |
| 739 | interfaceService.getInterfaces().stream() |
| 740 | .filter(intf -> intf.connectPoint().deviceId().equals(deviceId)) |
| 741 | .forEach(intf -> { |
| 742 | vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port()); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 743 | intf.vlanTagged().forEach(vlanTagged -> |
| 744 | vlanPortMap.put(vlanTagged, intf.connectPoint().port()) |
| 745 | ); |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 746 | vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port()); |
| 747 | }); |
| 748 | vlanPortMap.removeAll(VlanId.NONE); |
| 749 | |
| 750 | return vlanPortMap; |
| 751 | } |
| 752 | |
| 753 | /** |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 754 | * Returns the next objective ID for the given subnet prefix. It is expected |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 755 | * Returns the next objective ID for the given vlan id. It is expected |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 756 | * that the next-objective has been pre-created from configuration. |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 757 | * |
| 758 | * @param deviceId Device ID |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 759 | * @param vlanId VLAN ID |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 760 | * @return next objective ID or -1 if it was not found |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 761 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 762 | int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) { |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 763 | if (groupHandlerMap.get(deviceId) != null) { |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 764 | log.trace("getVlanNextObjectiveId query in device {}", deviceId); |
| 765 | return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 766 | } else { |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 767 | log.warn("getVlanNextObjectiveId query - groupHandler for " |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 768 | + "device {} not found", deviceId); |
| 769 | return -1; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * Returns the next objective ID for the given portNumber, given the treatment. |
| 775 | * There could be multiple different treatments to the same outport, which |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 776 | * would result in different objectives. If the next object does not exist, |
| 777 | * and should be created, a new one is created and its id is returned. |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 778 | * |
| 779 | * @param deviceId Device ID |
| 780 | * @param portNum port number on device for which NextObjective is queried |
| 781 | * @param treatment the actions to apply on the packets (should include outport) |
| 782 | * @param meta metadata passed into the creation of a Next Objective if necessary |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 783 | * @param createIfMissing true if a next object should be created if not found |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 784 | * @return next objective ID or -1 if an error occurred during retrieval or creation |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 785 | */ |
| 786 | public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum, |
| 787 | TrafficTreatment treatment, |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 788 | TrafficSelector meta, |
| 789 | boolean createIfMissing) { |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 790 | DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId); |
| 791 | if (ghdlr != null) { |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 792 | return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing); |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 793 | } else { |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 794 | log.warn("getPortNextObjectiveId query - groupHandler for device {}" |
| 795 | + " not found", deviceId); |
| 796 | return -1; |
| 797 | } |
| 798 | } |
| 799 | |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 800 | /** |
| 801 | * Returns the group handler object for the specified device id. |
| 802 | * |
| 803 | * @param devId the device identifier |
| 804 | * @return the groupHandler object for the device id, or null if not found |
| 805 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 806 | DefaultGroupHandler getGroupHandler(DeviceId devId) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 807 | return groupHandlerMap.get(devId); |
| 808 | } |
| 809 | |
| 810 | /** |
Saurav Das | fbe7457 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 811 | * Returns the default routing handler object. |
| 812 | * |
| 813 | * @return the default routing handler object |
| 814 | */ |
| 815 | public DefaultRoutingHandler getRoutingHandler() { |
| 816 | return defaultRoutingHandler; |
| 817 | } |
| 818 | |
| 819 | /** |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 820 | * Returns true if this controller instance has seen this link before. The |
| 821 | * link may not be currently up, but as long as the link had been seen before |
| 822 | * this method will return true. The one exception is when the link was |
| 823 | * indeed seen before, but this controller instance was forced to forget it |
| 824 | * by a call to purgeSeenLink method. |
| 825 | * |
| 826 | * @param link the infrastructure link being queried |
| 827 | * @return true if this controller instance has seen this link before |
| 828 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 829 | boolean isSeenLink(Link link) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 830 | return seenLinks.containsKey(link); |
| 831 | } |
| 832 | |
| 833 | /** |
| 834 | * Updates the seen link store. Updates can be for links that are currently |
| 835 | * available or not. |
| 836 | * |
| 837 | * @param link the link to update in the seen-link local store |
| 838 | * @param up the status of the link, true if up, false if down |
| 839 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 840 | void updateSeenLink(Link link, boolean up) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 841 | seenLinks.put(link, up); |
| 842 | } |
| 843 | |
| 844 | /** |
| 845 | * Returns the status of a seen-link (up or down). If the link has not |
| 846 | * been seen-before, a null object is returned. |
| 847 | * |
| 848 | * @param link the infrastructure link being queried |
| 849 | * @return null if the link was not seen-before; |
| 850 | * true if the seen-link is up; |
| 851 | * false if the seen-link is down |
| 852 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 853 | Boolean isSeenLinkUp(Link link) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 854 | return seenLinks.get(link); |
| 855 | } |
| 856 | |
| 857 | /** |
| 858 | * Makes this controller instance forget a previously seen before link. |
| 859 | * |
| 860 | * @param link the infrastructure link to purge |
| 861 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 862 | private void purgeSeenLink(Link link) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 863 | seenLinks.remove(link); |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * Returns the status of a link as parallel link. A parallel link |
| 868 | * is defined as a link which has common src and dst switches as another |
| 869 | * seen-link that is currently enabled. It is not necessary for the link being |
| 870 | * queried to be a seen-link. |
| 871 | * |
| 872 | * @param link the infrastructure link being queried |
| 873 | * @return true if a seen-link exists that is up, and shares the |
| 874 | * same src and dst switches as the link being queried |
| 875 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 876 | private boolean isParallelLink(Link link) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 877 | for (Entry<Link, Boolean> seen : seenLinks.entrySet()) { |
| 878 | Link seenLink = seen.getKey(); |
| 879 | if (seenLink.equals(link)) { |
| 880 | continue; |
| 881 | } |
| 882 | if (seenLink.src().deviceId().equals(link.src().deviceId()) && |
| 883 | seenLink.dst().deviceId().equals(link.dst().deviceId()) && |
| 884 | seen.getValue()) { |
| 885 | return true; |
| 886 | } |
| 887 | } |
| 888 | return false; |
| 889 | } |
| 890 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 891 | /** |
| 892 | * Returns true if the link being queried is a bidirectional link. A bidi |
| 893 | * link is defined as a link, whose reverse link - ie. the link in the reverse |
| 894 | * direction - has been seen-before and is up. It is not necessary for the link |
| 895 | * being queried to be a seen-link. |
| 896 | * |
| 897 | * @param link the infrastructure link being queried |
| 898 | * @return true if another unidirectional link exists in the reverse direction, |
| 899 | * has been seen-before and is up |
| 900 | */ |
| 901 | public boolean isBidirectional(Link link) { |
| 902 | Link reverseLink = linkService.getLink(link.dst(), link.src()); |
| 903 | if (reverseLink == null) { |
| 904 | return false; |
| 905 | } |
| 906 | Boolean result = isSeenLinkUp(reverseLink); |
| 907 | if (result == null) { |
| 908 | return false; |
| 909 | } |
| 910 | return result.booleanValue(); |
| 911 | } |
| 912 | |
| 913 | /** |
| 914 | * Determines if the given link should be avoided in routing calculations |
| 915 | * by policy or design. |
| 916 | * |
| 917 | * @param link the infrastructure link being queried |
| 918 | * @return true if link should be avoided |
| 919 | */ |
| 920 | public boolean avoidLink(Link link) { |
| 921 | // XXX currently only avoids all pair-links. In the future can be |
| 922 | // extended to avoid any generic link |
| 923 | DeviceId src = link.src().deviceId(); |
| 924 | PortNumber srcPort = link.src().port(); |
| 925 | if (deviceConfiguration == null || !deviceConfiguration.isConfigured(src)) { |
| 926 | log.warn("Device {} not configured..cannot avoid link {}", src, link); |
| 927 | return false; |
| 928 | } |
| 929 | DeviceId pairDev; |
| 930 | PortNumber pairLocalPort, pairRemotePort = null; |
| 931 | try { |
| 932 | pairDev = deviceConfiguration.getPairDeviceId(src); |
| 933 | pairLocalPort = deviceConfiguration.getPairLocalPort(src); |
| 934 | if (pairDev != null) { |
| 935 | pairRemotePort = deviceConfiguration.getPairLocalPort(pairDev); |
| 936 | } |
| 937 | } catch (DeviceConfigNotFoundException e) { |
| 938 | log.warn("Pair dev for dev {} not configured..cannot avoid link {}", |
| 939 | src, link); |
| 940 | return false; |
| 941 | } |
| 942 | |
| 943 | if (srcPort.equals(pairLocalPort) && |
| 944 | link.dst().deviceId().equals(pairDev) && |
| 945 | link.dst().port().equals(pairRemotePort)) { |
| 946 | return true; |
| 947 | } |
| 948 | return false; |
| 949 | } |
| 950 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 951 | private class InternalPacketProcessor implements PacketProcessor { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 952 | @Override |
| 953 | public void process(PacketContext context) { |
| 954 | |
| 955 | if (context.isHandled()) { |
| 956 | return; |
| 957 | } |
| 958 | |
| 959 | InboundPacket pkt = context.inPacket(); |
| 960 | Ethernet ethernet = pkt.parsed(); |
Pier Luigi | 37a3543 | 2017-02-01 13:50:04 -0800 | [diff] [blame] | 961 | |
| 962 | if (ethernet == null) { |
| 963 | return; |
| 964 | } |
| 965 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 966 | log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(), |
| 967 | ethernet); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 968 | if (ethernet.getEtherType() == TYPE_ARP) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 969 | log.warn("Received unexpected ARP packet on {}", |
| 970 | context.inPacket().receivedFrom()); |
Saurav Das | 368cf21 | 2017-03-15 15:15:14 -0700 | [diff] [blame] | 971 | log.trace("{}", ethernet); |
Pier Ventre | 6b2c1b3 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 972 | return; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 973 | } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) { |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 974 | IPv4 ipv4Packet = (IPv4) ethernet.getPayload(); |
| 975 | //ipHandler.addToPacketBuffer(ipv4Packet); |
| 976 | if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) { |
| 977 | icmpHandler.processIcmp(ethernet, pkt.receivedFrom()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 978 | } else { |
Charles Chan | d041ad8 | 2017-01-13 17:20:44 -0800 | [diff] [blame] | 979 | // NOTE: We don't support IP learning at this moment so this |
| 980 | // is not necessary. Also it causes duplication of DHCP packets. |
Pier Ventre | 6b2c1b3 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 981 | // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 982 | } |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 983 | } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) { |
| 984 | IPv6 ipv6Packet = (IPv6) ethernet.getPayload(); |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 985 | //ipHandler.addToPacketBuffer(ipv6Packet); |
Pier Luigi | 37a3543 | 2017-02-01 13:50:04 -0800 | [diff] [blame] | 986 | // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 987 | if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) { |
| 988 | ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload(); |
| 989 | if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST || |
| 990 | icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) { |
| 991 | icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom()); |
| 992 | } else { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 993 | log.trace("Received ICMPv6 0x{} - not handled", |
Charles Chan | d3727b7 | 2017-03-13 13:10:30 -0700 | [diff] [blame] | 994 | Integer.toHexString(icmp6Packet.getIcmpType() & 0xff)); |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 995 | } |
| 996 | } else { |
| 997 | // NOTE: We don't support IP learning at this moment so this |
| 998 | // is not necessary. Also it causes duplication of DHCPv6 packets. |
| 999 | // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom()); |
| 1000 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | private class InternalLinkListener implements LinkListener { |
| 1006 | @Override |
| 1007 | public void event(LinkEvent event) { |
Charles Chan | 0cff8aa | 2017-03-29 16:39:05 -0700 | [diff] [blame] | 1008 | if (event.type() == LinkEvent.Type.LINK_ADDED || |
| 1009 | event.type() == LinkEvent.Type.LINK_UPDATED || |
| 1010 | event.type() == LinkEvent.Type.LINK_REMOVED) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1011 | log.debug("Event {} received from Link Service", event.type()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1012 | scheduleEventHandlerIfNotScheduled(event); |
| 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | private class InternalDeviceListener implements DeviceListener { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1018 | @Override |
| 1019 | public void event(DeviceEvent event) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1020 | switch (event.type()) { |
| 1021 | case DEVICE_ADDED: |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1022 | case PORT_UPDATED: |
| 1023 | case PORT_ADDED: |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1024 | case DEVICE_UPDATED: |
| 1025 | case DEVICE_AVAILABILITY_CHANGED: |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1026 | log.trace("Event {} received from Device Service", event.type()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1027 | scheduleEventHandlerIfNotScheduled(event); |
| 1028 | break; |
| 1029 | default: |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1034 | @SuppressWarnings("rawtypes") |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1035 | private void scheduleEventHandlerIfNotScheduled(Event event) { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1036 | synchronized (THREAD_SCHED_LOCK) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1037 | eventQueue.add(event); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1038 | numOfEventsQueued++; |
| 1039 | |
| 1040 | if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) { |
| 1041 | //No pending scheduled event handling threads. So start a new one. |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1042 | eventHandlerFuture = executorService |
| 1043 | .schedule(eventHandler, 100, TimeUnit.MILLISECONDS); |
| 1044 | numOfHandlerScheduled++; |
| 1045 | } |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 1046 | log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}", |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1047 | numOfEventsQueued, |
| 1048 | numOfHandlerScheduled); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1049 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | private class InternalEventHandler implements Runnable { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 1053 | @Override |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1054 | public void run() { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1055 | try { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1056 | while (true) { |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1057 | @SuppressWarnings("rawtypes") |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1058 | Event event; |
| 1059 | synchronized (THREAD_SCHED_LOCK) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1060 | if (!eventQueue.isEmpty()) { |
| 1061 | event = eventQueue.poll(); |
| 1062 | numOfEventsExecuted++; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1063 | } else { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1064 | numOfHandlerExecution++; |
| 1065 | log.debug("numOfHandlerExecution {} numOfEventsExecuted {}", |
| 1066 | numOfHandlerExecution, numOfEventsExecuted); |
| 1067 | break; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1068 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1069 | } |
Charles Chan | 0cff8aa | 2017-03-29 16:39:05 -0700 | [diff] [blame] | 1070 | if (event.type() == LinkEvent.Type.LINK_ADDED || |
| 1071 | event.type() == LinkEvent.Type.LINK_UPDATED) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1072 | // Note: do not update seenLinks here, otherwise every |
| 1073 | // link, even one seen for the first time, will be appear |
| 1074 | // to be a previously seen link |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1075 | processLinkAdded((Link) event.subject()); |
| 1076 | } else if (event.type() == LinkEvent.Type.LINK_REMOVED) { |
Pier Ventre | 6d59389 | 2016-09-13 21:33:40 -0700 | [diff] [blame] | 1077 | Link linkRemoved = (Link) event.subject(); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1078 | if (linkRemoved.type() == Link.Type.DIRECT) { |
| 1079 | updateSeenLink(linkRemoved, false); |
| 1080 | } |
| 1081 | // device availability check helps to ensure that |
| 1082 | // multiple link-removed events are actually treated as a |
| 1083 | // single switch removed event. purgeSeenLink is necessary |
| 1084 | // so we do rerouting (instead of rehashing) when switch |
| 1085 | // comes back. |
Pier Ventre | 6d59389 | 2016-09-13 21:33:40 -0700 | [diff] [blame] | 1086 | if (linkRemoved.src().elementId() instanceof DeviceId && |
| 1087 | !deviceService.isAvailable(linkRemoved.src().deviceId())) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1088 | purgeSeenLink(linkRemoved); |
Pier Ventre | 6d59389 | 2016-09-13 21:33:40 -0700 | [diff] [blame] | 1089 | continue; |
| 1090 | } |
| 1091 | if (linkRemoved.dst().elementId() instanceof DeviceId && |
| 1092 | !deviceService.isAvailable(linkRemoved.dst().deviceId())) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1093 | purgeSeenLink(linkRemoved); |
Pier Ventre | 6d59389 | 2016-09-13 21:33:40 -0700 | [diff] [blame] | 1094 | continue; |
| 1095 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1096 | processLinkRemoved((Link) event.subject()); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1097 | } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED || |
| 1098 | event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED || |
| 1099 | event.type() == DeviceEvent.Type.DEVICE_UPDATED) { |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1100 | DeviceId deviceId = ((Device) event.subject()).id(); |
| 1101 | if (deviceService.isAvailable(deviceId)) { |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 1102 | log.info("Processing device event {} for available device {}", |
| 1103 | event.type(), ((Device) event.subject()).id()); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1104 | processDeviceAdded((Device) event.subject()); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 1105 | } else { |
| 1106 | log.info("Processing device event {} for unavailable device {}", |
| 1107 | event.type(), ((Device) event.subject()).id()); |
| 1108 | processDeviceRemoved((Device) event.subject()); |
| 1109 | } |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1110 | } else if (event.type() == DeviceEvent.Type.PORT_ADDED) { |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1111 | // typically these calls come when device is added first time |
| 1112 | // so port filtering rules are handled at the device_added event. |
| 1113 | // port added calls represent all ports on the device, |
| 1114 | // enabled or not. |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1115 | log.trace("** PORT ADDED {}/{} -> {}", |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1116 | ((DeviceEvent) event).subject().id(), |
| 1117 | ((DeviceEvent) event).port().number(), |
| 1118 | event.type()); |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1119 | } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) { |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1120 | // these calls happen for every subsequent event |
| 1121 | // ports enabled, disabled, switch goes away, comes back |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1122 | log.info("** PORT UPDATED {}/{} -> {}", |
| 1123 | event.subject(), |
| 1124 | ((DeviceEvent) event).port(), |
| 1125 | event.type()); |
| 1126 | processPortUpdated(((Device) event.subject()), |
| 1127 | ((DeviceEvent) event).port()); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1128 | } else { |
| 1129 | log.warn("Unhandled event type: {}", event.type()); |
| 1130 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1131 | } |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1132 | } catch (Exception e) { |
| 1133 | log.error("SegmentRouting event handler " |
| 1134 | + "thread thrown an exception: {}", e); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1135 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1139 | private void processLinkAdded(Link link) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 1140 | log.info("** LINK ADDED {}", link.toString()); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1141 | if (link.type() != Link.Type.DIRECT) { |
| 1142 | // NOTE: A DIRECT link might be transiently marked as INDIRECT |
| 1143 | // if BDDP is received before LLDP. We can safely ignore that |
| 1144 | // until the LLDP is received and the link is marked as DIRECT. |
| 1145 | log.info("Ignore link {}->{}. Link type is {} instead of DIRECT.", |
| 1146 | link.src(), link.dst(), link.type()); |
| 1147 | return; |
| 1148 | } |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1149 | if (!deviceConfiguration.isConfigured(link.src().deviceId())) { |
| 1150 | updateSeenLink(link, true); |
| 1151 | // XXX revisit - what about devicePortMap |
| 1152 | log.warn("Source device of this link is not configured.. " |
| 1153 | + "not processing further"); |
| 1154 | return; |
| 1155 | } |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1156 | |
| 1157 | //Irrespective of whether the local is a MASTER or not for this device, |
| 1158 | //create group handler instance and push default TTP flow rules if needed, |
| 1159 | //as in a multi-instance setup, instances can initiate groups for any device. |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1160 | DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src() |
| 1161 | .deviceId()); |
| 1162 | if (groupHandler != null) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1163 | groupHandler.portUpForLink(link); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1164 | } else { |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1165 | // XXX revisit/cleanup |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1166 | Device device = deviceService.getDevice(link.src().deviceId()); |
| 1167 | if (device != null) { |
| 1168 | log.warn("processLinkAdded: Link Added " |
| 1169 | + "Notification without Device Added " |
| 1170 | + "event, still handling it"); |
| 1171 | processDeviceAdded(device); |
| 1172 | groupHandler = groupHandlerMap.get(link.src() |
| 1173 | .deviceId()); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1174 | groupHandler.portUpForLink(link); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1175 | } |
| 1176 | } |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1177 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1178 | /*// process link only if it is bidirectional |
| 1179 | if (!isBidirectional(link)) { |
| 1180 | log.debug("Link not bidirectional.. waiting for other direction " |
| 1181 | + "src {} --> dst {} ", link.dst(), link.src()); |
| 1182 | // note that if we are not processing for routing, it should at least |
| 1183 | // be considered a seen-link |
| 1184 | updateSeenLink(link, true); |
| 1185 | return; |
| 1186 | } |
| 1187 | TO DO this ensure that rehash is still done correctly even if link is |
| 1188 | not processed for rerouting - perhaps rehash in both directions when |
| 1189 | it ultimately becomes bidi? |
| 1190 | */ |
| 1191 | |
| 1192 | log.debug("Starting optimized route population process for link " |
| 1193 | + "{} --> {}", link.src(), link.dst()); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1194 | boolean seenBefore = isSeenLink(link); |
| 1195 | defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null, link, null); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1196 | |
| 1197 | // It's possible that linkUp causes no route-path change as ECMP graph does |
| 1198 | // not change if the link is a parallel link (same src-dst as another link. |
| 1199 | // However we still need to update ECMP hash groups to include new buckets |
| 1200 | // for the link that has come up. |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1201 | if (mastershipService.isLocalMaster(link.src().deviceId())) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1202 | if (!seenBefore && isParallelLink(link)) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1203 | // if link seen first time, we need to ensure hash-groups have all ports |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1204 | log.debug("Attempting retryHash for paralled first-time link {}", link); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1205 | groupHandler.retryHash(link, false, true); |
| 1206 | } else { |
| 1207 | //seen before-link |
| 1208 | if (isParallelLink(link)) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1209 | log.debug("Attempting retryHash for paralled seen-before " |
| 1210 | + "link {}", link); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1211 | groupHandler.retryHash(link, false, false); |
| 1212 | } |
| 1213 | } |
| 1214 | } |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1215 | |
| 1216 | mcastHandler.init(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | private void processLinkRemoved(Link link) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 1220 | log.info("** LINK REMOVED {}", link.toString()); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1221 | defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link, null, null); |
| 1222 | |
| 1223 | // update local groupHandler stores |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 1224 | DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId()); |
| 1225 | if (groupHandler != null) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1226 | if (mastershipService.isLocalMaster(link.src().deviceId()) && |
| 1227 | isParallelLink(link)) { |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1228 | log.debug("* retrying hash for parallel link removed:{}", link); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1229 | groupHandler.retryHash(link, true, false); |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1230 | } else { |
| 1231 | log.debug("Not attempting retry-hash for link removed: {} .. {}", link, |
| 1232 | (mastershipService.isLocalMaster(link.src().deviceId())) |
| 1233 | ? "not parallel" : "not master"); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1234 | } |
| 1235 | // ensure local stores are updated |
| 1236 | groupHandler.portDown(link.src().port()); |
| 1237 | } else { |
| 1238 | log.warn("group handler not found for dev:{} when removing link: {}", |
| 1239 | link.src().deviceId(), link); |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 1240 | } |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1241 | |
| 1242 | mcastHandler.processLinkDown(link); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | private void processDeviceAdded(Device device) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 1246 | log.info("** DEVICE ADDED with ID {}", device.id()); |
Charles Chan | 9bd6aea | 2017-06-27 18:48:32 -0700 | [diff] [blame] | 1247 | |
| 1248 | // NOTE: Punt ARP/NDP even when the device is not configured. |
| 1249 | // Host learning without network config is required for CORD config generator. |
| 1250 | routingRulePopulator.populateIpPunts(device.id()); |
| 1251 | routingRulePopulator.populateArpNdpPunts(device.id()); |
| 1252 | |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 1253 | if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1254 | log.warn("Device configuration unavailable. Device {} will be " |
| 1255 | + "processed after configuration.", device.id()); |
Saurav Das | 8ec0ec4 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 1256 | return; |
| 1257 | } |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1258 | processDeviceAddedInternal(device.id()); |
| 1259 | } |
| 1260 | |
| 1261 | private void processDeviceAddedInternal(DeviceId deviceId) { |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 1262 | // Irrespective of whether the local is a MASTER or not for this device, |
| 1263 | // we need to create a SR-group-handler instance. This is because in a |
| 1264 | // multi-instance setup, any instance can initiate forwarding/next-objectives |
| 1265 | // for any switch (even if this instance is a SLAVE or not even connected |
| 1266 | // to the switch). To handle this, a default-group-handler instance is necessary |
| 1267 | // per switch. |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1268 | log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet()); |
| 1269 | if (groupHandlerMap.get(deviceId) == null) { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 1270 | DefaultGroupHandler groupHandler; |
| 1271 | try { |
| 1272 | groupHandler = DefaultGroupHandler. |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1273 | createGroupHandler(deviceId, |
| 1274 | appId, |
| 1275 | deviceConfiguration, |
| 1276 | linkService, |
| 1277 | flowObjectiveService, |
| 1278 | this); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 1279 | } catch (DeviceConfigNotFoundException e) { |
| 1280 | log.warn(e.getMessage() + " Aborting processDeviceAdded."); |
| 1281 | return; |
| 1282 | } |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1283 | log.debug("updating groupHandlerMap with new config for device: {}", |
| 1284 | deviceId); |
| 1285 | groupHandlerMap.put(deviceId, groupHandler); |
Saurav Das | 8ec0ec4 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 1286 | } |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 1287 | |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1288 | if (mastershipService.isLocalMaster(deviceId)) { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1289 | defaultRoutingHandler.populatePortAddressingRules(deviceId); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 1290 | hostHandler.init(deviceId); |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1291 | xConnectHandler.init(deviceId); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1292 | DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId); |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1293 | groupHandler.createGroupsFromVlanConfig(); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1294 | routingRulePopulator.populateSubnetBroadcastRule(deviceId); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 1295 | } |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 1296 | |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 1297 | appCfgHandler.init(deviceId); |
| 1298 | routeHandler.init(deviceId); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 1301 | private void processDeviceRemoved(Device device) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1302 | dsNextObjStore.entrySet().stream() |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 1303 | .filter(entry -> entry.getKey().deviceId().equals(device.id())) |
| 1304 | .forEach(entry -> { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1305 | dsNextObjStore.remove(entry.getKey()); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 1306 | }); |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1307 | vlanNextObjStore.entrySet().stream() |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 1308 | .filter(entry -> entry.getKey().deviceId().equals(device.id())) |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1309 | .forEach(entry -> vlanNextObjStore.remove(entry.getKey())); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 1310 | portNextObjStore.entrySet().stream() |
| 1311 | .filter(entry -> entry.getKey().deviceId().equals(device.id())) |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1312 | .forEach(entry -> portNextObjStore.remove(entry.getKey())); |
Charles Chan | 17d75d8 | 2017-06-15 00:44:51 -0700 | [diff] [blame] | 1313 | |
| 1314 | seenLinks.keySet().removeIf(key -> key.src().deviceId().equals(device.id()) || |
| 1315 | key.dst().deviceId().equals(device.id())); |
| 1316 | |
Saurav Das | fbe7457 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1317 | DefaultGroupHandler gh = groupHandlerMap.remove(device.id()); |
| 1318 | if (gh != null) { |
| 1319 | gh.shutdown(); |
| 1320 | } |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 1321 | defaultRoutingHandler.purgeEcmpGraph(device.id()); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1322 | // Note that a switch going down is associated with all of its links |
| 1323 | // going down as well, but it is treated as a single switch down event |
| 1324 | // while the link-downs are ignored. |
| 1325 | defaultRoutingHandler |
| 1326 | .populateRoutingRulesForLinkStatusChange(null, null, device.id()); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1327 | mcastHandler.removeDevice(device.id()); |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1328 | xConnectHandler.removeDevice(device.id()); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1331 | private void processPortUpdated(Device device, Port port) { |
| 1332 | if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) { |
| 1333 | log.warn("Device configuration uploading. Not handling port event for" |
| 1334 | + "dev: {} port: {}", device.id(), port.number()); |
| 1335 | return; |
| 1336 | } |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1337 | |
| 1338 | if (!mastershipService.isLocalMaster(device.id())) { |
| 1339 | log.debug("Not master for dev:{} .. not handling port updated event" |
| 1340 | + "for port {}", device.id(), port.number()); |
| 1341 | return; |
| 1342 | } |
| 1343 | |
| 1344 | // first we handle filtering rules associated with the port |
| 1345 | if (port.isEnabled()) { |
| 1346 | log.info("Switchport {}/{} enabled..programming filters", |
| 1347 | device.id(), port.number()); |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 1348 | routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true); |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1349 | } else { |
| 1350 | log.info("Switchport {}/{} disabled..removing filters", |
| 1351 | device.id(), port.number()); |
Charles Chan | 43be46b | 2017-02-26 22:59:35 -0800 | [diff] [blame] | 1352 | routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false); |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1353 | } |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1354 | |
| 1355 | // portUpdated calls are for ports that have gone down or up. For switch |
| 1356 | // to switch ports, link-events should take care of any re-routing or |
| 1357 | // group editing necessary for port up/down. Here we only process edge ports |
| 1358 | // that are already configured. |
Saurav Das | 3fb2827 | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 1359 | ConnectPoint cp = new ConnectPoint(device.id(), port.number()); |
| 1360 | VlanId untaggedVlan = getUntaggedVlanId(cp); |
| 1361 | VlanId nativeVlan = getNativeVlanId(cp); |
| 1362 | Set<VlanId> taggedVlans = getTaggedVlanId(cp); |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1363 | |
Saurav Das | 3fb2827 | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 1364 | if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1365 | log.debug("Not handling port updated event for non-edge port (unconfigured) " |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1366 | + "dev/port: {}/{}", device.id(), port.number()); |
| 1367 | return; |
| 1368 | } |
Saurav Das | 3fb2827 | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 1369 | if (untaggedVlan != null) { |
| 1370 | processEdgePort(device, port, untaggedVlan, true); |
| 1371 | } |
| 1372 | if (nativeVlan != null) { |
| 1373 | processEdgePort(device, port, nativeVlan, true); |
| 1374 | } |
| 1375 | if (!taggedVlans.isEmpty()) { |
| 1376 | taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false)); |
| 1377 | } |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1378 | } |
| 1379 | |
Saurav Das | 3fb2827 | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 1380 | private void processEdgePort(Device device, Port port, VlanId vlanId, |
| 1381 | boolean popVlan) { |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1382 | boolean portUp = port.isEnabled(); |
| 1383 | if (portUp) { |
Saurav Das | 3fb2827 | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 1384 | log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(), |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1385 | port.number(), vlanId); |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1386 | } else { |
Saurav Das | 3fb2827 | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 1387 | log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(), |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1388 | port.number(), vlanId); |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1389 | } |
| 1390 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 1391 | DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1392 | if (groupHandler != null) { |
Saurav Das | 3fb2827 | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 1393 | groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp); |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1394 | } else { |
| 1395 | log.warn("Group handler not found for dev:{}. Not handling edge port" |
| 1396 | + " {} event for port:{}", device.id(), |
| 1397 | (portUp) ? "UP" : "DOWN", port.number()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1398 | } |
| 1399 | } |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1400 | |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 1401 | /** |
| 1402 | * Registers the given connect point with the NRS, this is necessary |
| 1403 | * to receive the NDP and ARP packets from the NRS. |
| 1404 | * |
| 1405 | * @param portToRegister connect point to register |
| 1406 | */ |
| 1407 | public void registerConnectPoint(ConnectPoint portToRegister) { |
Charles Chan | 2e71ef3 | 2017-02-23 15:44:08 -0800 | [diff] [blame] | 1408 | neighbourResolutionService.registerNeighbourHandler( |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 1409 | portToRegister, |
| 1410 | neighbourHandler, |
| 1411 | appId |
| 1412 | ); |
| 1413 | } |
| 1414 | |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 1415 | private class InternalConfigListener implements NetworkConfigListener { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1416 | private static final long PROGRAM_DELAY = 2; |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 1417 | SegmentRoutingManager srManager; |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 1418 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 1419 | /** |
| 1420 | * Constructs the internal network config listener. |
| 1421 | * |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 1422 | * @param srManager segment routing manager |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 1423 | */ |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 1424 | InternalConfigListener(SegmentRoutingManager srManager) { |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 1425 | this.srManager = srManager; |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 1428 | /** |
| 1429 | * Reads network config and initializes related data structure accordingly. |
| 1430 | */ |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 1431 | public void configureNetwork() { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1432 | if (deviceConfiguration == null) { |
| 1433 | deviceConfiguration = new DeviceConfiguration(srManager); |
| 1434 | } else { |
| 1435 | deviceConfiguration.updateConfig(); |
| 1436 | } |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 1437 | |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 1438 | arpHandler = new ArpHandler(srManager); |
| 1439 | icmpHandler = new IcmpHandler(srManager); |
| 1440 | ipHandler = new IpHandler(srManager); |
| 1441 | routingRulePopulator = new RoutingRulePopulator(srManager); |
| 1442 | defaultRoutingHandler = new DefaultRoutingHandler(srManager); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 1443 | |
| 1444 | tunnelHandler = new TunnelHandler(linkService, deviceConfiguration, |
| 1445 | groupHandlerMap, tunnelStore); |
| 1446 | policyHandler = new PolicyHandler(appId, deviceConfiguration, |
| 1447 | flowObjectiveService, |
| 1448 | tunnelHandler, policyStore); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1449 | // add a small delay to absorb multiple network config added notifications |
| 1450 | if (!programmingScheduled.get()) { |
| 1451 | programmingScheduled.set(true); |
| 1452 | executorService.schedule(new ConfigChange(), PROGRAM_DELAY, |
| 1453 | TimeUnit.SECONDS); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 1454 | } |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1455 | mcastHandler.init(); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 1458 | @Override |
| 1459 | public void event(NetworkConfigEvent event) { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 1460 | // TODO move this part to NetworkConfigEventHandler |
| 1461 | if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) { |
| 1462 | switch (event.type()) { |
| 1463 | case CONFIG_ADDED: |
Charles Chan | 68363b1 | 2017-06-26 15:25:09 -0700 | [diff] [blame] | 1464 | log.info("Segment Routing Device Config added for {}", event.subject()); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 1465 | configureNetwork(); |
| 1466 | break; |
| 1467 | case CONFIG_UPDATED: |
Charles Chan | 68363b1 | 2017-06-26 15:25:09 -0700 | [diff] [blame] | 1468 | log.info("Segment Routing Config updated for {}", event.subject()); |
| 1469 | // TODO support dynamic configuration |
| 1470 | break; |
| 1471 | default: |
| 1472 | break; |
| 1473 | } |
| 1474 | } else if (event.configClass().equals(InterfaceConfig.class)) { |
| 1475 | switch (event.type()) { |
| 1476 | case CONFIG_ADDED: |
| 1477 | log.info("Interface Config added for {}", event.subject()); |
| 1478 | configureNetwork(); |
| 1479 | break; |
| 1480 | case CONFIG_UPDATED: |
| 1481 | log.info("Interface Config updated for {}", event.subject()); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 1482 | // TODO support dynamic configuration |
| 1483 | break; |
| 1484 | default: |
| 1485 | break; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 1486 | } |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 1487 | } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) { |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1488 | checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized"); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 1489 | switch (event.type()) { |
| 1490 | case CONFIG_ADDED: |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1491 | appCfgHandler.processAppConfigAdded(event); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 1492 | break; |
| 1493 | case CONFIG_UPDATED: |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1494 | appCfgHandler.processAppConfigUpdated(event); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 1495 | break; |
| 1496 | case CONFIG_REMOVED: |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1497 | appCfgHandler.processAppConfigRemoved(event); |
| 1498 | break; |
| 1499 | default: |
| 1500 | break; |
| 1501 | } |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 1502 | configureNetwork(); |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1503 | } else if (event.configClass().equals(XConnectConfig.class)) { |
| 1504 | checkState(xConnectHandler != null, "XConnectHandler is not initialized"); |
| 1505 | switch (event.type()) { |
| 1506 | case CONFIG_ADDED: |
| 1507 | xConnectHandler.processXConnectConfigAdded(event); |
| 1508 | break; |
| 1509 | case CONFIG_UPDATED: |
| 1510 | xConnectHandler.processXConnectConfigUpdated(event); |
| 1511 | break; |
| 1512 | case CONFIG_REMOVED: |
| 1513 | xConnectHandler.processXConnectConfigRemoved(event); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 1514 | break; |
| 1515 | default: |
| 1516 | break; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 1517 | } |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 1518 | } else if (event.configClass().equals(PwaasConfig.class)) { |
| 1519 | checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized"); |
| 1520 | switch (event.type()) { |
| 1521 | case CONFIG_ADDED: |
| 1522 | l2TunnelHandler.processPwaasConfigAdded(event); |
| 1523 | break; |
| 1524 | case CONFIG_UPDATED: |
| 1525 | l2TunnelHandler.processPwaasConfigUpdated(event); |
| 1526 | break; |
| 1527 | case CONFIG_REMOVED: |
| 1528 | l2TunnelHandler.processPwaasConfigRemoved(event); |
| 1529 | break; |
| 1530 | default: |
| 1531 | break; |
| 1532 | } |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 1533 | } |
| 1534 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1535 | |
| 1536 | private final class ConfigChange implements Runnable { |
| 1537 | @Override |
| 1538 | public void run() { |
| 1539 | programmingScheduled.set(false); |
| 1540 | for (Device device : deviceService.getDevices()) { |
| 1541 | processDeviceAdded(device); |
| 1542 | } |
| 1543 | defaultRoutingHandler.startPopulationProcess(); |
| 1544 | } |
| 1545 | } |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 1546 | } |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1547 | |
| 1548 | private class InternalHostListener implements HostListener { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1549 | @Override |
| 1550 | public void event(HostEvent event) { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1551 | switch (event.type()) { |
| 1552 | case HOST_ADDED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 1553 | hostHandler.processHostAddedEvent(event); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1554 | break; |
| 1555 | case HOST_MOVED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 1556 | hostHandler.processHostMovedEvent(event); |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame^] | 1557 | routeHandler.processHostMovedEvent(event); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1558 | break; |
| 1559 | case HOST_REMOVED: |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 1560 | hostHandler.processHostRemovedEvent(event); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1561 | break; |
| 1562 | case HOST_UPDATED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 1563 | hostHandler.processHostUpdatedEvent(event); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 1564 | break; |
| 1565 | default: |
| 1566 | log.warn("Unsupported host event type: {}", event.type()); |
| 1567 | break; |
| 1568 | } |
| 1569 | } |
| 1570 | } |
| 1571 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1572 | private class InternalMcastListener implements McastListener { |
| 1573 | @Override |
| 1574 | public void event(McastEvent event) { |
| 1575 | switch (event.type()) { |
| 1576 | case SOURCE_ADDED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 1577 | mcastHandler.processSourceAdded(event); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1578 | break; |
| 1579 | case SINK_ADDED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 1580 | mcastHandler.processSinkAdded(event); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1581 | break; |
| 1582 | case SINK_REMOVED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 1583 | mcastHandler.processSinkRemoved(event); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1584 | break; |
| 1585 | case ROUTE_ADDED: |
| 1586 | case ROUTE_REMOVED: |
| 1587 | default: |
| 1588 | break; |
| 1589 | } |
| 1590 | } |
| 1591 | } |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 1592 | |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 1593 | private class InternalRouteEventListener implements RouteListener { |
| 1594 | @Override |
| 1595 | public void event(RouteEvent event) { |
| 1596 | switch (event.type()) { |
| 1597 | case ROUTE_ADDED: |
| 1598 | routeHandler.processRouteAdded(event); |
| 1599 | break; |
| 1600 | case ROUTE_UPDATED: |
| 1601 | routeHandler.processRouteUpdated(event); |
| 1602 | break; |
| 1603 | case ROUTE_REMOVED: |
| 1604 | routeHandler.processRouteRemoved(event); |
| 1605 | break; |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame^] | 1606 | case ALTERNATIVE_ROUTES_CHANGED: |
| 1607 | routeHandler.processAlternativeRoutesChanged(event); |
| 1608 | break; |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 1609 | default: |
| 1610 | break; |
| 1611 | } |
| 1612 | } |
| 1613 | } |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1614 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1615 | } |