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