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