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