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