sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 43b5354 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.segmentrouting; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Service; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 24 | import org.onlab.packet.Ethernet; |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 25 | import org.onlab.packet.IPv4; |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 26 | import org.onlab.packet.Ip4Prefix; |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 27 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 28 | import org.onlab.packet.VlanId; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 29 | import org.onlab.util.KryoNamespace; |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 30 | import org.onosproject.cfg.ComponentConfigService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.CoreService; |
| 33 | import org.onosproject.event.Event; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 34 | import org.onosproject.incubator.net.config.basics.McastConfig; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 35 | import org.onosproject.mastership.MastershipService; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 36 | import org.onosproject.net.Device; |
| 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.Link; |
| 39 | import org.onosproject.net.Port; |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 40 | import org.onosproject.net.PortNumber; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 41 | import org.onosproject.net.config.ConfigFactory; |
| 42 | import org.onosproject.net.config.NetworkConfigEvent; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 43 | import org.onosproject.net.config.NetworkConfigListener; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 44 | import org.onosproject.net.config.NetworkConfigRegistry; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 45 | import org.onosproject.net.config.basics.SubjectFactories; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 46 | import org.onosproject.net.device.DeviceEvent; |
| 47 | import org.onosproject.net.device.DeviceListener; |
| 48 | import org.onosproject.net.device.DeviceService; |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 49 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.TrafficSelector; |
| 51 | import org.onosproject.net.flow.TrafficTreatment; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 52 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 53 | import org.onosproject.net.host.HostEvent; |
| 54 | import org.onosproject.net.host.HostListener; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 55 | import org.onosproject.net.mcast.McastEvent; |
| 56 | import org.onosproject.net.mcast.McastListener; |
| 57 | import org.onosproject.net.mcast.MulticastRouteService; |
| 58 | import org.onosproject.net.packet.PacketPriority; |
| 59 | import org.onosproject.net.topology.TopologyService; |
| 60 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 61 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
| 62 | import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig; |
| 63 | import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig; |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame^] | 64 | import org.onosproject.segmentrouting.config.XConnectConfig; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 65 | import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler; |
| 66 | import org.onosproject.segmentrouting.grouphandler.NeighborSet; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 67 | import org.onosproject.segmentrouting.storekey.NeighborSetNextObjectiveStoreKey; |
| 68 | import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 69 | import org.onosproject.net.host.HostService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 70 | import org.onosproject.net.link.LinkEvent; |
| 71 | import org.onosproject.net.link.LinkListener; |
| 72 | import org.onosproject.net.link.LinkService; |
| 73 | import org.onosproject.net.packet.InboundPacket; |
| 74 | import org.onosproject.net.packet.PacketContext; |
| 75 | import org.onosproject.net.packet.PacketProcessor; |
| 76 | import org.onosproject.net.packet.PacketService; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 77 | import org.onosproject.segmentrouting.storekey.SubnetAssignedVidStoreKey; |
| 78 | import org.onosproject.segmentrouting.storekey.SubnetNextObjectiveStoreKey; |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame^] | 79 | import org.onosproject.segmentrouting.storekey.XConnectStoreKey; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 80 | import org.onosproject.store.serializers.KryoNamespaces; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 81 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 82 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; |
| 83 | import org.onosproject.store.service.StorageService; |
| 84 | import org.onosproject.store.service.WallClockTimestamp; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 85 | import org.slf4j.Logger; |
| 86 | import org.slf4j.LoggerFactory; |
| 87 | |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 88 | import java.util.Collections; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 89 | import java.util.HashSet; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 90 | import java.util.List; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 91 | import java.util.Map; |
Charles Chan | de6655c | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 92 | import java.util.Optional; |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 93 | import java.util.Set; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 94 | import java.util.concurrent.ConcurrentHashMap; |
| 95 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 96 | import java.util.concurrent.Executors; |
| 97 | import java.util.concurrent.ScheduledExecutorService; |
| 98 | import java.util.concurrent.ScheduledFuture; |
| 99 | import java.util.concurrent.TimeUnit; |
| 100 | |
Charles Chan | d6d2533 | 2016-02-26 22:19:52 -0800 | [diff] [blame] | 101 | import static com.google.common.base.Preconditions.checkState; |
| 102 | |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 103 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 104 | /** |
| 105 | * Segment routing manager. |
| 106 | */ |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 107 | @Service |
| 108 | @Component(immediate = true) |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 109 | public class SegmentRoutingManager implements SegmentRoutingService { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 110 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 111 | private static Logger log = LoggerFactory |
| 112 | .getLogger(SegmentRoutingManager.class); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 113 | |
| 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 115 | protected CoreService coreService; |
| 116 | |
| 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 118 | protected PacketService packetService; |
| 119 | |
| 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 121 | protected HostService hostService; |
| 122 | |
| 123 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 124 | protected DeviceService deviceService; |
| 125 | |
| 126 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 127 | protected FlowObjectiveService flowObjectiveService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 128 | |
| 129 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 130 | protected LinkService linkService; |
| 131 | |
| 132 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 133 | protected MastershipService mastershipService; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 134 | |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 135 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 136 | protected StorageService storageService; |
| 137 | |
| 138 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 139 | protected NetworkConfigRegistry cfgService; |
| 140 | |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 141 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 142 | protected ComponentConfigService compCfgService; |
| 143 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 144 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 145 | protected MulticastRouteService multicastRouteService; |
| 146 | |
| 147 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 148 | protected TopologyService topologyService; |
| 149 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 150 | protected ArpHandler arpHandler = null; |
| 151 | protected IcmpHandler icmpHandler = null; |
| 152 | protected IpHandler ipHandler = null; |
| 153 | protected RoutingRulePopulator routingRulePopulator = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 154 | protected ApplicationId appId; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 155 | protected DeviceConfiguration deviceConfiguration = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 156 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 157 | protected DefaultRoutingHandler defaultRoutingHandler = null; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 158 | private TunnelHandler tunnelHandler = null; |
| 159 | private PolicyHandler policyHandler = null; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 160 | private InternalPacketProcessor processor = null; |
| 161 | private InternalLinkListener linkListener = null; |
| 162 | private InternalDeviceListener deviceListener = null; |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame^] | 163 | private AppConfigHandler appCfgHandler = null; |
| 164 | protected XConnectHandler xConnectHandler = null; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 165 | private McastHandler mcastHandler = null; |
| 166 | private HostHandler hostHandler = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 167 | private InternalEventHandler eventHandler = new InternalEventHandler(); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 168 | private final InternalHostListener hostListener = new InternalHostListener(); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 169 | private final InternalConfigListener cfgListener = new InternalConfigListener(this); |
| 170 | private final InternalMcastListener mcastListener = new InternalMcastListener(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 171 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 172 | private ScheduledExecutorService executorService = Executors |
| 173 | .newScheduledThreadPool(1); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 174 | |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 175 | @SuppressWarnings("unused") |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 176 | private static ScheduledFuture<?> eventHandlerFuture = null; |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 177 | @SuppressWarnings("rawtypes") |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 178 | private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<Event>(); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 179 | private Map<DeviceId, DefaultGroupHandler> groupHandlerMap = |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 180 | new ConcurrentHashMap<>(); |
| 181 | /** |
| 182 | * Per device next objective ID store with (device id + neighbor set) as key. |
| 183 | */ |
| 184 | public EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer> |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 185 | nsNextObjStore = null; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 186 | /** |
| 187 | * Per device next objective ID store with (device id + subnet) as key. |
| 188 | */ |
| 189 | public EventuallyConsistentMap<SubnetNextObjectiveStoreKey, Integer> |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 190 | subnetNextObjStore = null; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 191 | /** |
| 192 | * Per device next objective ID store with (device id + port) as key. |
| 193 | */ |
| 194 | public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 195 | portNextObjStore = null; |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 196 | // Per device, per-subnet assigned-vlans store, with (device id + subnet |
| 197 | // IPv4 prefix) as key |
| 198 | private EventuallyConsistentMap<SubnetAssignedVidStoreKey, VlanId> |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 199 | subnetVidStore = null; |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 200 | private EventuallyConsistentMap<String, Tunnel> tunnelStore = null; |
| 201 | private EventuallyConsistentMap<String, Policy> policyStore = null; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 202 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 203 | private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory = |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame^] | 204 | new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>( |
| 205 | SubjectFactories.DEVICE_SUBJECT_FACTORY, |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 206 | SegmentRoutingDeviceConfig.class, "segmentrouting") { |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 207 | @Override |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 208 | public SegmentRoutingDeviceConfig createConfig() { |
| 209 | return new SegmentRoutingDeviceConfig(); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 210 | } |
| 211 | }; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 212 | private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory = |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame^] | 213 | new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>( |
| 214 | SubjectFactories.APP_SUBJECT_FACTORY, |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 215 | SegmentRoutingAppConfig.class, "segmentrouting") { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 216 | @Override |
| 217 | public SegmentRoutingAppConfig createConfig() { |
| 218 | return new SegmentRoutingAppConfig(); |
| 219 | } |
| 220 | }; |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame^] | 221 | private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory = |
| 222 | new ConfigFactory<ApplicationId, XConnectConfig>( |
| 223 | SubjectFactories.APP_SUBJECT_FACTORY, |
| 224 | XConnectConfig.class, "xconnect") { |
| 225 | @Override |
| 226 | public XConnectConfig createConfig() { |
| 227 | return new XConnectConfig(); |
| 228 | } |
| 229 | }; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 230 | private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory = |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame^] | 231 | new ConfigFactory<ApplicationId, McastConfig>( |
| 232 | SubjectFactories.APP_SUBJECT_FACTORY, |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 233 | McastConfig.class, "multicast") { |
| 234 | @Override |
| 235 | public McastConfig createConfig() { |
| 236 | return new McastConfig(); |
| 237 | } |
| 238 | }; |
| 239 | |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 240 | private Object threadSchedulerLock = new Object(); |
| 241 | private static int numOfEventsQueued = 0; |
| 242 | private static int numOfEventsExecuted = 0; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 243 | private static int numOfHandlerExecution = 0; |
| 244 | private static int numOfHandlerScheduled = 0; |
| 245 | |
Charles Chan | 1963f4f | 2016-02-18 14:22:42 -0800 | [diff] [blame] | 246 | /** |
| 247 | * Segment Routing App ID. |
| 248 | */ |
| 249 | public static final String SR_APP_ID = "org.onosproject.segmentrouting"; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 250 | /** |
| 251 | * The starting value of per-subnet VLAN ID assignment. |
| 252 | */ |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 253 | private static final short ASSIGNED_VLAN_START = 4093; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 254 | /** |
| 255 | * The default VLAN ID assigned to the interfaces without subnet config. |
| 256 | */ |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 257 | public static final short ASSIGNED_VLAN_NO_SUBNET = 4094; |
| 258 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 259 | @Activate |
| 260 | protected void activate() { |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 261 | appId = coreService.registerApplication(SR_APP_ID); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 262 | |
| 263 | log.debug("Creating EC map nsnextobjectivestore"); |
| 264 | EventuallyConsistentMapBuilder<NeighborSetNextObjectiveStoreKey, Integer> |
| 265 | nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 266 | nsNextObjStore = nsNextObjMapBuilder |
| 267 | .withName("nsnextobjectivestore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 268 | .withSerializer(createSerializer()) |
Madan Jampani | 675ae20 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 269 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 270 | .build(); |
| 271 | log.trace("Current size {}", nsNextObjStore.size()); |
| 272 | |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 273 | log.debug("Creating EC map subnetnextobjectivestore"); |
| 274 | EventuallyConsistentMapBuilder<SubnetNextObjectiveStoreKey, Integer> |
| 275 | subnetNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 276 | subnetNextObjStore = subnetNextObjMapBuilder |
| 277 | .withName("subnetnextobjectivestore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 278 | .withSerializer(createSerializer()) |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 279 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 280 | .build(); |
| 281 | |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 282 | log.debug("Creating EC map subnetnextobjectivestore"); |
| 283 | EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer> |
| 284 | portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
| 285 | portNextObjStore = portNextObjMapBuilder |
| 286 | .withName("portnextobjectivestore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 287 | .withSerializer(createSerializer()) |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 288 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 289 | .build(); |
| 290 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 291 | EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder = |
| 292 | storageService.eventuallyConsistentMapBuilder(); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 293 | tunnelStore = tunnelMapBuilder |
| 294 | .withName("tunnelstore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 295 | .withSerializer(createSerializer()) |
Madan Jampani | 675ae20 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 296 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 297 | .build(); |
| 298 | |
| 299 | EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder = |
| 300 | storageService.eventuallyConsistentMapBuilder(); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 301 | policyStore = policyMapBuilder |
| 302 | .withName("policystore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 303 | .withSerializer(createSerializer()) |
Madan Jampani | 675ae20 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 304 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 305 | .build(); |
| 306 | |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 307 | EventuallyConsistentMapBuilder<SubnetAssignedVidStoreKey, VlanId> |
| 308 | subnetVidStoreMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 309 | subnetVidStore = subnetVidStoreMapBuilder |
| 310 | .withName("subnetvidstore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 311 | .withSerializer(createSerializer()) |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 312 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 313 | .build(); |
| 314 | |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 315 | compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager", |
| 316 | "purgeOnDisconnection", "true"); |
| 317 | compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager", |
| 318 | "purgeOnDisconnection", "true"); |
| 319 | |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 320 | processor = new InternalPacketProcessor(); |
| 321 | linkListener = new InternalLinkListener(); |
| 322 | deviceListener = new InternalDeviceListener(); |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame^] | 323 | appCfgHandler = new AppConfigHandler(this); |
| 324 | xConnectHandler = new XConnectHandler(this); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 325 | mcastHandler = new McastHandler(this); |
| 326 | hostHandler = new HostHandler(this); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 327 | |
Charles Chan | d6d2533 | 2016-02-26 22:19:52 -0800 | [diff] [blame] | 328 | cfgService.addListener(cfgListener); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 329 | cfgService.registerConfigFactory(deviceConfigFactory); |
| 330 | cfgService.registerConfigFactory(appConfigFactory); |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame^] | 331 | cfgService.registerConfigFactory(xConnectConfigFactory); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 332 | cfgService.registerConfigFactory(mcastConfigFactory); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 333 | hostService.addListener(hostListener); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 334 | packetService.addProcessor(processor, PacketProcessor.director(2)); |
| 335 | linkService.addListener(linkListener); |
| 336 | deviceService.addListener(deviceListener); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 337 | multicastRouteService.addListener(mcastListener); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 338 | |
Charles Chan | de6655c | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 339 | // Request ARP packet-in |
| 340 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 341 | selector.matchEthType(Ethernet.TYPE_ARP); |
| 342 | packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId, Optional.empty()); |
| 343 | |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 344 | cfgListener.configureNetwork(); |
| 345 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 346 | log.info("Started"); |
| 347 | } |
| 348 | |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 349 | private KryoNamespace.Builder createSerializer() { |
| 350 | return new KryoNamespace.Builder() |
| 351 | .register(KryoNamespaces.API) |
| 352 | .register(NeighborSetNextObjectiveStoreKey.class, |
| 353 | SubnetNextObjectiveStoreKey.class, |
| 354 | SubnetAssignedVidStoreKey.class, |
| 355 | NeighborSet.class, |
| 356 | Tunnel.class, |
| 357 | DefaultTunnel.class, |
| 358 | Policy.class, |
| 359 | TunnelPolicy.class, |
| 360 | Policy.Type.class, |
| 361 | PortNextObjectiveStoreKey.class, |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame^] | 362 | XConnectStoreKey.class |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 363 | ); |
| 364 | } |
| 365 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 366 | @Deactivate |
| 367 | protected void deactivate() { |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 368 | cfgService.removeListener(cfgListener); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 369 | cfgService.unregisterConfigFactory(deviceConfigFactory); |
| 370 | cfgService.unregisterConfigFactory(appConfigFactory); |
| 371 | cfgService.unregisterConfigFactory(mcastConfigFactory); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 372 | |
Charles Chan | de6655c | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 373 | // Withdraw ARP packet-in |
| 374 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 375 | selector.matchEthType(Ethernet.TYPE_ARP); |
| 376 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId, Optional.empty()); |
| 377 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 378 | packetService.removeProcessor(processor); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 379 | linkService.removeListener(linkListener); |
| 380 | deviceService.removeListener(deviceListener); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 381 | multicastRouteService.removeListener(mcastListener); |
| 382 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 383 | processor = null; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 384 | linkListener = null; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 385 | deviceListener = null; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 386 | groupHandlerMap.clear(); |
| 387 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 388 | nsNextObjStore.destroy(); |
| 389 | subnetNextObjStore.destroy(); |
| 390 | portNextObjStore.destroy(); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 391 | tunnelStore.destroy(); |
| 392 | policyStore.destroy(); |
| 393 | subnetVidStore.destroy(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 394 | log.info("Stopped"); |
| 395 | } |
| 396 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 397 | |
| 398 | @Override |
| 399 | public List<Tunnel> getTunnels() { |
| 400 | return tunnelHandler.getTunnels(); |
| 401 | } |
| 402 | |
| 403 | @Override |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 404 | public TunnelHandler.Result createTunnel(Tunnel tunnel) { |
| 405 | return tunnelHandler.createTunnel(tunnel); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | @Override |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 409 | public TunnelHandler.Result removeTunnel(Tunnel tunnel) { |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 410 | for (Policy policy: policyHandler.getPolicies()) { |
| 411 | if (policy.type() == Policy.Type.TUNNEL_FLOW) { |
| 412 | TunnelPolicy tunnelPolicy = (TunnelPolicy) policy; |
| 413 | if (tunnelPolicy.tunnelId().equals(tunnel.id())) { |
| 414 | log.warn("Cannot remove the tunnel used by a policy"); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 415 | return TunnelHandler.Result.TUNNEL_IN_USE; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | } |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 419 | return tunnelHandler.removeTunnel(tunnel); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | @Override |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 423 | public PolicyHandler.Result removePolicy(Policy policy) { |
| 424 | return policyHandler.removePolicy(policy); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | @Override |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 428 | public PolicyHandler.Result createPolicy(Policy policy) { |
| 429 | return policyHandler.createPolicy(policy); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | @Override |
| 433 | public List<Policy> getPolicies() { |
| 434 | return policyHandler.getPolicies(); |
| 435 | } |
| 436 | |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 437 | @Override |
| 438 | public void rerouteNetwork() { |
| 439 | cfgListener.configureNetwork(); |
| 440 | for (Device device : deviceService.getDevices()) { |
| 441 | defaultRoutingHandler.populatePortAddressingRules(device.id()); |
| 442 | } |
| 443 | defaultRoutingHandler.startPopulationProcess(); |
| 444 | } |
| 445 | |
sangho | 80f1f89 | 2015-05-19 11:57:42 -0700 | [diff] [blame] | 446 | /** |
| 447 | * Returns the tunnel object with the tunnel ID. |
| 448 | * |
| 449 | * @param tunnelId Tunnel ID |
| 450 | * @return Tunnel reference |
| 451 | */ |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 452 | public Tunnel getTunnel(String tunnelId) { |
| 453 | return tunnelHandler.getTunnel(tunnelId); |
| 454 | } |
| 455 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 456 | /** |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 457 | * Returns the vlan-id assigned to the subnet configured for a device. |
| 458 | * If no vlan-id has been assigned, a new one is assigned out of a pool of ids, |
| 459 | * if and only if this controller instance is the master for the device. |
| 460 | * <p> |
| 461 | * USAGE: The assigned vlans are meant to be applied to untagged packets on those |
| 462 | * switches/pipelines that need this functionality. These vids are meant |
| 463 | * to be used internally within a switch, and thus need to be unique only |
| 464 | * on a switch level. Note that packets never go out on the wire with these |
| 465 | * vlans. Currently, vlan ids are assigned from value 4093 down. |
| 466 | * Vlan id 4094 expected to be used for all ports that are not assigned subnets. |
| 467 | * Vlan id 4095 is reserved and unused. Only a single vlan id is assigned |
| 468 | * per subnet. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 469 | * |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 470 | * @param deviceId switch dpid |
| 471 | * @param subnet IPv4 prefix for which assigned vlan is desired |
| 472 | * @return VlanId assigned for the subnet on the device, or |
| 473 | * null if no vlan assignment was found and this instance is not |
| 474 | * the master for the device. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 475 | */ |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 476 | // TODO: We should avoid assigning VLAN IDs that are used by VLAN cross-connection. |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 477 | public VlanId getSubnetAssignedVlanId(DeviceId deviceId, Ip4Prefix subnet) { |
| 478 | VlanId assignedVid = subnetVidStore.get(new SubnetAssignedVidStoreKey( |
| 479 | deviceId, subnet)); |
| 480 | if (assignedVid != null) { |
| 481 | log.debug("Query for subnet:{} on device:{} returned assigned-vlan " |
| 482 | + "{}", subnet, deviceId, assignedVid); |
| 483 | return assignedVid; |
| 484 | } |
| 485 | //check mastership for the right to assign a vlan |
| 486 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 487 | log.warn("This controller instance is not the master for device {}. " |
| 488 | + "Cannot assign vlan-id for subnet {}", deviceId, subnet); |
| 489 | return null; |
| 490 | } |
| 491 | // vlan assignment is expensive but done only once |
Charles Chan | c6ad775 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 492 | Set<Ip4Prefix> configuredSubnets = deviceConfiguration.getSubnets(deviceId); |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 493 | Set<Short> assignedVlans = new HashSet<>(); |
| 494 | Set<Ip4Prefix> unassignedSubnets = new HashSet<>(); |
| 495 | for (Ip4Prefix sub : configuredSubnets) { |
| 496 | VlanId v = subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, |
| 497 | sub)); |
| 498 | if (v != null) { |
| 499 | assignedVlans.add(v.toShort()); |
| 500 | } else { |
| 501 | unassignedSubnets.add(sub); |
| 502 | } |
| 503 | } |
| 504 | short nextAssignedVlan = ASSIGNED_VLAN_START; |
| 505 | if (!assignedVlans.isEmpty()) { |
| 506 | nextAssignedVlan = (short) (Collections.min(assignedVlans) - 1); |
| 507 | } |
| 508 | for (Ip4Prefix unsub : unassignedSubnets) { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 509 | // Special case for default route. Assign default VLAN ID to /32 and /0 subnets |
| 510 | if (unsub.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH || |
| 511 | unsub.prefixLength() == 0) { |
| 512 | subnetVidStore.put(new SubnetAssignedVidStoreKey(deviceId, unsub), |
| 513 | VlanId.vlanId(ASSIGNED_VLAN_NO_SUBNET)); |
| 514 | } else { |
| 515 | subnetVidStore.put(new SubnetAssignedVidStoreKey(deviceId, unsub), |
| 516 | VlanId.vlanId(nextAssignedVlan--)); |
| 517 | log.info("Assigned vlan: {} to subnet: {} on device: {}", |
| 518 | nextAssignedVlan + 1, unsub, deviceId); |
| 519 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 522 | return subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, subnet)); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 523 | } |
| 524 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 525 | /** |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 526 | * Returns the next objective ID for the given NeighborSet. |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 527 | * If the nextObjective does not exist, a new one is created and |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 528 | * its id is returned. |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 529 | * |
sangho | 80f1f89 | 2015-05-19 11:57:42 -0700 | [diff] [blame] | 530 | * @param deviceId Device ID |
| 531 | * @param ns NegighborSet |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 532 | * @param meta metadata passed into the creation of a Next Objective |
| 533 | * @return next objective ID or -1 if an error was encountered during the |
| 534 | * creation of the nextObjective |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 535 | */ |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 536 | public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns, |
| 537 | TrafficSelector meta) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 538 | if (groupHandlerMap.get(deviceId) != null) { |
| 539 | log.trace("getNextObjectiveId query in device {}", deviceId); |
| 540 | return groupHandlerMap |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 541 | .get(deviceId).getNextObjectiveId(ns, meta); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 542 | } else { |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 543 | log.warn("getNextObjectiveId query - groupHandler for device {} " |
| 544 | + "not found", deviceId); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 545 | return -1; |
| 546 | } |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 549 | /** |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 550 | * Returns the next objective ID for the given subnet prefix. It is expected |
| 551 | * that the next-objective has been pre-created from configuration. |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 552 | * |
| 553 | * @param deviceId Device ID |
| 554 | * @param prefix Subnet |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 555 | * @return next objective ID or -1 if it was not found |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 556 | */ |
| 557 | public int getSubnetNextObjectiveId(DeviceId deviceId, IpPrefix prefix) { |
| 558 | if (groupHandlerMap.get(deviceId) != null) { |
| 559 | log.trace("getSubnetNextObjectiveId query in device {}", deviceId); |
| 560 | return groupHandlerMap |
| 561 | .get(deviceId).getSubnetNextObjectiveId(prefix); |
| 562 | } else { |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 563 | log.warn("getSubnetNextObjectiveId query - groupHandler for " |
| 564 | + "device {} not found", deviceId); |
| 565 | return -1; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Returns the next objective ID for the given portNumber, given the treatment. |
| 571 | * There could be multiple different treatments to the same outport, which |
| 572 | * would result in different objectives. If the next object |
| 573 | * does not exist, a new one is created and its id is returned. |
| 574 | * |
| 575 | * @param deviceId Device ID |
| 576 | * @param portNum port number on device for which NextObjective is queried |
| 577 | * @param treatment the actions to apply on the packets (should include outport) |
| 578 | * @param meta metadata passed into the creation of a Next Objective if necessary |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 579 | * @return next objective ID or -1 if an error occurred during retrieval or creation |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 580 | */ |
| 581 | public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum, |
| 582 | TrafficTreatment treatment, |
| 583 | TrafficSelector meta) { |
| 584 | DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId); |
| 585 | if (ghdlr != null) { |
| 586 | return ghdlr.getPortNextObjectiveId(portNum, treatment, meta); |
| 587 | } else { |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 588 | log.warn("getPortNextObjectiveId query - groupHandler for device {}" |
| 589 | + " not found", deviceId); |
| 590 | return -1; |
| 591 | } |
| 592 | } |
| 593 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 594 | private class InternalPacketProcessor implements PacketProcessor { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 595 | @Override |
| 596 | public void process(PacketContext context) { |
| 597 | |
| 598 | if (context.isHandled()) { |
| 599 | return; |
| 600 | } |
| 601 | |
| 602 | InboundPacket pkt = context.inPacket(); |
| 603 | Ethernet ethernet = pkt.parsed(); |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 604 | log.trace("Rcvd pktin: {}", ethernet); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 605 | if (ethernet.getEtherType() == Ethernet.TYPE_ARP) { |
| 606 | arpHandler.processPacketIn(pkt); |
| 607 | } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) { |
| 608 | IPv4 ipPacket = (IPv4) ethernet.getPayload(); |
| 609 | ipHandler.addToPacketBuffer(ipPacket); |
| 610 | if (ipPacket.getProtocol() == IPv4.PROTOCOL_ICMP) { |
| 611 | icmpHandler.processPacketIn(pkt); |
| 612 | } else { |
| 613 | ipHandler.processPacketIn(pkt); |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | private class InternalLinkListener implements LinkListener { |
| 620 | @Override |
| 621 | public void event(LinkEvent event) { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 622 | if (event.type() == LinkEvent.Type.LINK_ADDED |
| 623 | || event.type() == LinkEvent.Type.LINK_REMOVED) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 624 | log.debug("Event {} received from Link Service", event.type()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 625 | scheduleEventHandlerIfNotScheduled(event); |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | private class InternalDeviceListener implements DeviceListener { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 631 | @Override |
| 632 | public void event(DeviceEvent event) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 633 | switch (event.type()) { |
| 634 | case DEVICE_ADDED: |
| 635 | case PORT_REMOVED: |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 636 | case DEVICE_UPDATED: |
| 637 | case DEVICE_AVAILABILITY_CHANGED: |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 638 | log.debug("Event {} received from Device Service", event.type()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 639 | scheduleEventHandlerIfNotScheduled(event); |
| 640 | break; |
| 641 | default: |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 646 | @SuppressWarnings("rawtypes") |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 647 | private void scheduleEventHandlerIfNotScheduled(Event event) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 648 | synchronized (threadSchedulerLock) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 649 | eventQueue.add(event); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 650 | numOfEventsQueued++; |
| 651 | |
| 652 | if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) { |
| 653 | //No pending scheduled event handling threads. So start a new one. |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 654 | eventHandlerFuture = executorService |
| 655 | .schedule(eventHandler, 100, TimeUnit.MILLISECONDS); |
| 656 | numOfHandlerScheduled++; |
| 657 | } |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 658 | log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}", |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 659 | numOfEventsQueued, |
| 660 | numOfHandlerScheduled); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 661 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | private class InternalEventHandler implements Runnable { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 665 | @Override |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 666 | public void run() { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 667 | try { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 668 | while (true) { |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 669 | @SuppressWarnings("rawtypes") |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 670 | Event event = null; |
| 671 | synchronized (threadSchedulerLock) { |
| 672 | if (!eventQueue.isEmpty()) { |
| 673 | event = eventQueue.poll(); |
| 674 | numOfEventsExecuted++; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 675 | } else { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 676 | numOfHandlerExecution++; |
| 677 | log.debug("numOfHandlerExecution {} numOfEventsExecuted {}", |
| 678 | numOfHandlerExecution, numOfEventsExecuted); |
| 679 | break; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 680 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 681 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 682 | if (event.type() == LinkEvent.Type.LINK_ADDED) { |
| 683 | processLinkAdded((Link) event.subject()); |
| 684 | } else if (event.type() == LinkEvent.Type.LINK_REMOVED) { |
| 685 | processLinkRemoved((Link) event.subject()); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 686 | } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED || |
| 687 | event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED || |
| 688 | event.type() == DeviceEvent.Type.DEVICE_UPDATED) { |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 689 | DeviceId deviceId = ((Device) event.subject()).id(); |
| 690 | if (deviceService.isAvailable(deviceId)) { |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 691 | log.info("Processing device event {} for available device {}", |
| 692 | event.type(), ((Device) event.subject()).id()); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 693 | processDeviceAdded((Device) event.subject()); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 694 | } else { |
| 695 | log.info("Processing device event {} for unavailable device {}", |
| 696 | event.type(), ((Device) event.subject()).id()); |
| 697 | processDeviceRemoved((Device) event.subject()); |
| 698 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 699 | } else if (event.type() == DeviceEvent.Type.PORT_REMOVED) { |
| 700 | processPortRemoved((Device) event.subject(), |
| 701 | ((DeviceEvent) event).port()); |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 702 | } else if (event.type() == DeviceEvent.Type.PORT_ADDED || |
| 703 | event.type() == DeviceEvent.Type.PORT_UPDATED) { |
| 704 | log.info("** PORT ADDED OR UPDATED {}/{} -> {}", |
| 705 | (Device) event.subject(), |
| 706 | ((DeviceEvent) event).port(), |
| 707 | event.type()); |
| 708 | /* XXX create method for single port filtering rules |
| 709 | if (defaultRoutingHandler != null) { |
| 710 | defaultRoutingHandler.populatePortAddressingRules( |
| 711 | ((Device) event.subject()).id()); |
| 712 | }*/ |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 713 | } else { |
| 714 | log.warn("Unhandled event type: {}", event.type()); |
| 715 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 716 | } |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 717 | } catch (Exception e) { |
| 718 | log.error("SegmentRouting event handler " |
| 719 | + "thread thrown an exception: {}", e); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 720 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 724 | private void processLinkAdded(Link link) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 725 | log.info("** LINK ADDED {}", link.toString()); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 726 | if (!deviceConfiguration.isConfigured(link.src().deviceId())) { |
| 727 | log.warn("Source device of this link is not configured."); |
| 728 | return; |
| 729 | } |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 730 | //Irrespective whether the local is a MASTER or not for this device, |
| 731 | //create group handler instance and push default TTP flow rules. |
| 732 | //Because in a multi-instance setup, instances can initiate |
| 733 | //groups for any devices. Also the default TTP rules are needed |
| 734 | //to be pushed before inserting any IP table entries for any device |
| 735 | DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src() |
| 736 | .deviceId()); |
| 737 | if (groupHandler != null) { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 738 | groupHandler.linkUp(link, mastershipService.isLocalMaster( |
| 739 | link.src().deviceId())); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 740 | } else { |
| 741 | Device device = deviceService.getDevice(link.src().deviceId()); |
| 742 | if (device != null) { |
| 743 | log.warn("processLinkAdded: Link Added " |
| 744 | + "Notification without Device Added " |
| 745 | + "event, still handling it"); |
| 746 | processDeviceAdded(device); |
| 747 | groupHandler = groupHandlerMap.get(link.src() |
| 748 | .deviceId()); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 749 | groupHandler.linkUp(link, mastershipService.isLocalMaster(device.id())); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 750 | } |
| 751 | } |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 752 | |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 753 | log.trace("Starting optimized route population process"); |
| 754 | defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null); |
| 755 | //log.trace("processLinkAdded: re-starting route population process"); |
| 756 | //defaultRoutingHandler.startPopulationProcess(); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 757 | |
| 758 | mcastHandler.init(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | private void processLinkRemoved(Link link) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 762 | log.info("** LINK REMOVED {}", link.toString()); |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 763 | DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId()); |
| 764 | if (groupHandler != null) { |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 765 | groupHandler.portDown(link.src().port(), |
| 766 | mastershipService.isLocalMaster(link.src().deviceId())); |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 767 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 768 | log.trace("Starting optimized route population process"); |
| 769 | defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link); |
| 770 | //log.trace("processLinkRemoved: re-starting route population process"); |
| 771 | //defaultRoutingHandler.startPopulationProcess(); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 772 | |
| 773 | mcastHandler.processLinkDown(link); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | private void processDeviceAdded(Device device) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 777 | log.info("** DEVICE ADDED with ID {}", device.id()); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 778 | if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) { |
Saurav Das | 8ec0ec4 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 779 | log.warn("Device configuration uploading. Device {} will be " |
| 780 | + "processed after config completes.", device.id()); |
| 781 | return; |
| 782 | } |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 783 | processDeviceAddedInternal(device.id()); |
| 784 | } |
| 785 | |
| 786 | private void processDeviceAddedInternal(DeviceId deviceId) { |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 787 | // Irrespective of whether the local is a MASTER or not for this device, |
| 788 | // we need to create a SR-group-handler instance. This is because in a |
| 789 | // multi-instance setup, any instance can initiate forwarding/next-objectives |
| 790 | // for any switch (even if this instance is a SLAVE or not even connected |
| 791 | // to the switch). To handle this, a default-group-handler instance is necessary |
| 792 | // per switch. |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 793 | log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet()); |
| 794 | if (groupHandlerMap.get(deviceId) == null) { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 795 | DefaultGroupHandler groupHandler; |
| 796 | try { |
| 797 | groupHandler = DefaultGroupHandler. |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 798 | createGroupHandler(deviceId, |
| 799 | appId, |
| 800 | deviceConfiguration, |
| 801 | linkService, |
| 802 | flowObjectiveService, |
| 803 | this); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 804 | } catch (DeviceConfigNotFoundException e) { |
| 805 | log.warn(e.getMessage() + " Aborting processDeviceAdded."); |
| 806 | return; |
| 807 | } |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 808 | log.debug("updating groupHandlerMap with new config for device: {}", |
| 809 | deviceId); |
| 810 | groupHandlerMap.put(deviceId, groupHandler); |
Saurav Das | 8ec0ec4 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 811 | } |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 812 | // Also, in some cases, drivers may need extra |
| 813 | // information to process rules (eg. Router IP/MAC); and so, we send |
| 814 | // port addressing rules to the driver as well irrespective of whether |
| 815 | // this instance is the master or not. |
| 816 | defaultRoutingHandler.populatePortAddressingRules(deviceId); |
| 817 | |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 818 | if (mastershipService.isLocalMaster(deviceId)) { |
| 819 | hostHandler.readInitialHosts(deviceId); |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame^] | 820 | xConnectHandler.init(deviceId); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 821 | DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 822 | groupHandler.createGroupsFromSubnetConfig(); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 823 | routingRulePopulator.populateSubnetBroadcastRule(deviceId); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 824 | } |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 825 | |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame^] | 826 | appCfgHandler.initVRouters(deviceId); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 829 | private void processDeviceRemoved(Device device) { |
| 830 | nsNextObjStore.entrySet().stream() |
| 831 | .filter(entry -> entry.getKey().deviceId().equals(device.id())) |
| 832 | .forEach(entry -> { |
| 833 | nsNextObjStore.remove(entry.getKey()); |
| 834 | }); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 835 | subnetNextObjStore.entrySet().stream() |
| 836 | .filter(entry -> entry.getKey().deviceId().equals(device.id())) |
| 837 | .forEach(entry -> { |
| 838 | subnetNextObjStore.remove(entry.getKey()); |
| 839 | }); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 840 | portNextObjStore.entrySet().stream() |
| 841 | .filter(entry -> entry.getKey().deviceId().equals(device.id())) |
| 842 | .forEach(entry -> { |
| 843 | portNextObjStore.remove(entry.getKey()); |
| 844 | }); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 845 | subnetVidStore.entrySet().stream() |
| 846 | .filter(entry -> entry.getKey().deviceId().equals(device.id())) |
| 847 | .forEach(entry -> { |
| 848 | subnetVidStore.remove(entry.getKey()); |
| 849 | }); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 850 | groupHandlerMap.remove(device.id()); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 851 | defaultRoutingHandler.purgeEcmpGraph(device.id()); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 852 | mcastHandler.removeDevice(device.id()); |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame^] | 853 | xConnectHandler.removeDevice(device.id()); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 854 | } |
| 855 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 856 | private void processPortRemoved(Device device, Port port) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 857 | log.info("Port {} was removed", port.toString()); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 858 | DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 859 | if (groupHandler != null) { |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 860 | groupHandler.portDown(port.number(), |
| 861 | mastershipService.isLocalMaster(device.id())); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 862 | } |
| 863 | } |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 864 | |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 865 | private class InternalConfigListener implements NetworkConfigListener { |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 866 | SegmentRoutingManager segmentRoutingManager; |
| 867 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 868 | /** |
| 869 | * Constructs the internal network config listener. |
| 870 | * |
| 871 | * @param srMgr segment routing manager |
| 872 | */ |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 873 | public InternalConfigListener(SegmentRoutingManager srMgr) { |
| 874 | this.segmentRoutingManager = srMgr; |
| 875 | } |
| 876 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 877 | /** |
| 878 | * Reads network config and initializes related data structure accordingly. |
| 879 | */ |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 880 | public void configureNetwork() { |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 881 | deviceConfiguration = new DeviceConfiguration(appId, |
| 882 | segmentRoutingManager.cfgService); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 883 | |
| 884 | arpHandler = new ArpHandler(segmentRoutingManager); |
| 885 | icmpHandler = new IcmpHandler(segmentRoutingManager); |
| 886 | ipHandler = new IpHandler(segmentRoutingManager); |
| 887 | routingRulePopulator = new RoutingRulePopulator(segmentRoutingManager); |
| 888 | defaultRoutingHandler = new DefaultRoutingHandler(segmentRoutingManager); |
| 889 | |
| 890 | tunnelHandler = new TunnelHandler(linkService, deviceConfiguration, |
| 891 | groupHandlerMap, tunnelStore); |
| 892 | policyHandler = new PolicyHandler(appId, deviceConfiguration, |
| 893 | flowObjectiveService, |
| 894 | tunnelHandler, policyStore); |
| 895 | |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 896 | for (Device device : deviceService.getDevices()) { |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 897 | processDeviceAddedInternal(device.id()); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | defaultRoutingHandler.startPopulationProcess(); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 901 | mcastHandler.init(); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 904 | @Override |
| 905 | public void event(NetworkConfigEvent event) { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 906 | // TODO move this part to NetworkConfigEventHandler |
| 907 | if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) { |
| 908 | switch (event.type()) { |
| 909 | case CONFIG_ADDED: |
| 910 | log.info("Segment Routing Config added."); |
| 911 | configureNetwork(); |
| 912 | break; |
| 913 | case CONFIG_UPDATED: |
| 914 | log.info("Segment Routing Config updated."); |
| 915 | // TODO support dynamic configuration |
| 916 | break; |
| 917 | default: |
| 918 | break; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 919 | } |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 920 | } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) { |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame^] | 921 | checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized"); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 922 | switch (event.type()) { |
| 923 | case CONFIG_ADDED: |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame^] | 924 | appCfgHandler.processAppConfigAdded(event); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 925 | break; |
| 926 | case CONFIG_UPDATED: |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame^] | 927 | appCfgHandler.processAppConfigUpdated(event); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 928 | break; |
| 929 | case CONFIG_REMOVED: |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame^] | 930 | appCfgHandler.processAppConfigRemoved(event); |
| 931 | break; |
| 932 | default: |
| 933 | break; |
| 934 | } |
| 935 | } else if (event.configClass().equals(XConnectConfig.class)) { |
| 936 | checkState(xConnectHandler != null, "XConnectHandler is not initialized"); |
| 937 | switch (event.type()) { |
| 938 | case CONFIG_ADDED: |
| 939 | xConnectHandler.processXConnectConfigAdded(event); |
| 940 | break; |
| 941 | case CONFIG_UPDATED: |
| 942 | xConnectHandler.processXConnectConfigUpdated(event); |
| 943 | break; |
| 944 | case CONFIG_REMOVED: |
| 945 | xConnectHandler.processXConnectConfigRemoved(event); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 946 | break; |
| 947 | default: |
| 948 | break; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 949 | } |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | } |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 953 | |
| 954 | private class InternalHostListener implements HostListener { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 955 | @Override |
| 956 | public void event(HostEvent event) { |
| 957 | // Do not proceed without mastership |
| 958 | DeviceId deviceId = event.subject().location().deviceId(); |
| 959 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 960 | return; |
| 961 | } |
| 962 | |
| 963 | switch (event.type()) { |
| 964 | case HOST_ADDED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 965 | hostHandler.processHostAddedEvent(event); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 966 | break; |
| 967 | case HOST_MOVED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 968 | hostHandler.processHostMovedEvent(event); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 969 | break; |
| 970 | case HOST_REMOVED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 971 | hostHandler.processHostRemoveEvent(event); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 972 | break; |
| 973 | case HOST_UPDATED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 974 | hostHandler.processHostUpdatedEvent(event); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 975 | break; |
| 976 | default: |
| 977 | log.warn("Unsupported host event type: {}", event.type()); |
| 978 | break; |
| 979 | } |
| 980 | } |
| 981 | } |
| 982 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 983 | private class InternalMcastListener implements McastListener { |
| 984 | @Override |
| 985 | public void event(McastEvent event) { |
| 986 | switch (event.type()) { |
| 987 | case SOURCE_ADDED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 988 | mcastHandler.processSourceAdded(event); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 989 | break; |
| 990 | case SINK_ADDED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 991 | mcastHandler.processSinkAdded(event); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 992 | break; |
| 993 | case SINK_REMOVED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 994 | mcastHandler.processSinkRemoved(event); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 995 | break; |
| 996 | case ROUTE_ADDED: |
| 997 | case ROUTE_REMOVED: |
| 998 | default: |
| 999 | break; |
| 1000 | } |
| 1001 | } |
| 1002 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1003 | } |