blob: a2eea7cc388efff4cbd3851ba96640d9d776b4af [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor43b53542016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sangho80f11cb2015-04-01 13:05:26 -07003 *
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 */
16package org.onosproject.segmentrouting;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho27462c62015-05-14 00:39:53 -070023import org.apache.felix.scr.annotations.Service;
sangho80f11cb2015-04-01 13:05:26 -070024import org.onlab.packet.Ethernet;
Charles Chan77277672015-10-20 16:24:19 -070025import org.onlab.packet.IPv4;
Charles Chan77277672015-10-20 16:24:19 -070026import org.onlab.packet.Ip4Prefix;
Charles Chan77277672015-10-20 16:24:19 -070027import org.onlab.packet.IpPrefix;
Jonathan Hart54541d12016-04-12 15:39:44 -070028import org.onlab.packet.VlanId;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070029import org.onlab.util.KryoNamespace;
Saurav Dasc3604f12016-03-23 11:22:49 -070030import org.onosproject.cfg.ComponentConfigService;
sangho80f11cb2015-04-01 13:05:26 -070031import org.onosproject.core.ApplicationId;
32import org.onosproject.core.CoreService;
33import org.onosproject.event.Event;
Charles Chanc91c8782016-03-30 17:54:24 -070034import org.onosproject.incubator.net.config.basics.McastConfig;
Jonathan Hart54541d12016-04-12 15:39:44 -070035import org.onosproject.mastership.MastershipService;
Jonathan Hart54541d12016-04-12 15:39:44 -070036import org.onosproject.net.Device;
37import org.onosproject.net.DeviceId;
38import org.onosproject.net.Link;
39import org.onosproject.net.Port;
Charles Chanf4586112015-11-09 16:37:23 -080040import org.onosproject.net.PortNumber;
Charles Chan72f556a2015-10-05 17:50:33 -070041import org.onosproject.net.config.ConfigFactory;
42import org.onosproject.net.config.NetworkConfigEvent;
Charles Chan72f556a2015-10-05 17:50:33 -070043import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070044import org.onosproject.net.config.NetworkConfigRegistry;
Charles Chan72f556a2015-10-05 17:50:33 -070045import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hart54541d12016-04-12 15:39:44 -070046import org.onosproject.net.device.DeviceEvent;
47import org.onosproject.net.device.DeviceListener;
48import org.onosproject.net.device.DeviceService;
Charles Chanf4586112015-11-09 16:37:23 -080049import org.onosproject.net.flow.DefaultTrafficSelector;
Charles Chanf4586112015-11-09 16:37:23 -080050import org.onosproject.net.flow.TrafficSelector;
51import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart54541d12016-04-12 15:39:44 -070052import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chanf4586112015-11-09 16:37:23 -080053import org.onosproject.net.host.HostEvent;
54import org.onosproject.net.host.HostListener;
Charles Chanc91c8782016-03-30 17:54:24 -070055import org.onosproject.net.mcast.McastEvent;
56import org.onosproject.net.mcast.McastListener;
57import org.onosproject.net.mcast.MulticastRouteService;
58import org.onosproject.net.packet.PacketPriority;
59import org.onosproject.net.topology.TopologyService;
60import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
61import org.onosproject.segmentrouting.config.DeviceConfiguration;
62import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
63import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
64import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
65import org.onosproject.segmentrouting.grouphandler.NeighborSet;
Charles Chan1eaf4802016-04-18 13:44:03 -070066import org.onosproject.segmentrouting.storekey.NeighborSetNextObjectiveStoreKey;
67import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
sangho80f11cb2015-04-01 13:05:26 -070068import org.onosproject.net.host.HostService;
sangho80f11cb2015-04-01 13:05:26 -070069import org.onosproject.net.link.LinkEvent;
70import org.onosproject.net.link.LinkListener;
71import org.onosproject.net.link.LinkService;
72import org.onosproject.net.packet.InboundPacket;
73import org.onosproject.net.packet.PacketContext;
74import org.onosproject.net.packet.PacketProcessor;
75import org.onosproject.net.packet.PacketService;
Charles Chan1eaf4802016-04-18 13:44:03 -070076import org.onosproject.segmentrouting.storekey.SubnetAssignedVidStoreKey;
77import org.onosproject.segmentrouting.storekey.SubnetNextObjectiveStoreKey;
78import org.onosproject.segmentrouting.storekey.XConnectNextObjectiveStoreKey;
Jonathan Hart54541d12016-04-12 15:39:44 -070079import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070080import org.onosproject.store.service.EventuallyConsistentMap;
81import org.onosproject.store.service.EventuallyConsistentMapBuilder;
82import org.onosproject.store.service.StorageService;
83import org.onosproject.store.service.WallClockTimestamp;
sangho80f11cb2015-04-01 13:05:26 -070084import org.slf4j.Logger;
85import org.slf4j.LoggerFactory;
86
Saurav Das7c305372015-10-28 12:39:42 -070087import java.util.Collections;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070088import java.util.HashSet;
sangho27462c62015-05-14 00:39:53 -070089import java.util.List;
sangho80f11cb2015-04-01 13:05:26 -070090import java.util.Map;
Charles Chande6655c2015-12-23 00:15:11 -080091import java.util.Optional;
Saurav Das7c305372015-10-28 12:39:42 -070092import java.util.Set;
sangho80f11cb2015-04-01 13:05:26 -070093import java.util.concurrent.ConcurrentHashMap;
94import java.util.concurrent.ConcurrentLinkedQueue;
95import java.util.concurrent.Executors;
96import java.util.concurrent.ScheduledExecutorService;
97import java.util.concurrent.ScheduledFuture;
98import java.util.concurrent.TimeUnit;
99
Charles Chand6d25332016-02-26 22:19:52 -0800100import static com.google.common.base.Preconditions.checkState;
101
Jonathan Hart54541d12016-04-12 15:39:44 -0700102
Charles Chanb7f75ac2016-01-11 18:28:54 -0800103/**
104 * Segment routing manager.
105 */
Jonathan Hart54541d12016-04-12 15:39:44 -0700106@Service
107@Component(immediate = true)
sangho27462c62015-05-14 00:39:53 -0700108public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700109
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700110 private static Logger log = LoggerFactory
111 .getLogger(SegmentRoutingManager.class);
sangho80f11cb2015-04-01 13:05:26 -0700112
113 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
114 protected CoreService coreService;
115
116 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
sangho80f11cb2015-04-01 13:05:26 -0700117 protected PacketService packetService;
118
119 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
sangho80f11cb2015-04-01 13:05:26 -0700120 protected HostService hostService;
121
122 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
123 protected DeviceService deviceService;
124
125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700126 protected FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700127
128 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
129 protected LinkService linkService;
130
131 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
sangho80f11cb2015-04-01 13:05:26 -0700132 protected MastershipService mastershipService;
sangho27462c62015-05-14 00:39:53 -0700133
Charles Chan82ab1932016-01-30 23:22:37 -0800134 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
135 protected StorageService storageService;
136
137 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
138 protected NetworkConfigRegistry cfgService;
139
Saurav Dasc3604f12016-03-23 11:22:49 -0700140 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
141 protected ComponentConfigService compCfgService;
142
Charles Chanc91c8782016-03-30 17:54:24 -0700143 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
144 protected MulticastRouteService multicastRouteService;
145
146 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
147 protected TopologyService topologyService;
148
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700149 protected ArpHandler arpHandler = null;
150 protected IcmpHandler icmpHandler = null;
151 protected IpHandler ipHandler = null;
152 protected RoutingRulePopulator routingRulePopulator = null;
sangho80f11cb2015-04-01 13:05:26 -0700153 protected ApplicationId appId;
sangho9b169e32015-04-14 16:27:13 -0700154 protected DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700155
Charles Chanc22cef32016-04-29 14:38:22 -0700156 protected DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700157 private TunnelHandler tunnelHandler = null;
158 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700159 private InternalPacketProcessor processor = null;
160 private InternalLinkListener linkListener = null;
161 private InternalDeviceListener deviceListener = null;
Charles Chan82ab1932016-01-30 23:22:37 -0800162 private NetworkConfigEventHandler netcfgHandler = null;
Charles Chan1eaf4802016-04-18 13:44:03 -0700163 private McastHandler mcastHandler = null;
164 private HostHandler hostHandler = null;
sangho80f11cb2015-04-01 13:05:26 -0700165 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan82ab1932016-01-30 23:22:37 -0800166 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700167 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
168 private final InternalMcastListener mcastListener = new InternalMcastListener();
sangho80f11cb2015-04-01 13:05:26 -0700169
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700170 private ScheduledExecutorService executorService = Executors
171 .newScheduledThreadPool(1);
sangho80f11cb2015-04-01 13:05:26 -0700172
Saurav Das2d94d312015-11-24 23:21:05 -0800173 @SuppressWarnings("unused")
sangho80f11cb2015-04-01 13:05:26 -0700174 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das2d94d312015-11-24 23:21:05 -0800175 @SuppressWarnings("rawtypes")
sangho80f11cb2015-04-01 13:05:26 -0700176 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<Event>();
Charles Chanf4586112015-11-09 16:37:23 -0800177 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chanb7f75ac2016-01-11 18:28:54 -0800178 new ConcurrentHashMap<>();
179 /**
180 * Per device next objective ID store with (device id + neighbor set) as key.
181 */
182 public EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer>
Charles Chanf4586112015-11-09 16:37:23 -0800183 nsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800184 /**
185 * Per device next objective ID store with (device id + subnet) as key.
186 */
187 public EventuallyConsistentMap<SubnetNextObjectiveStoreKey, Integer>
Charles Chanf4586112015-11-09 16:37:23 -0800188 subnetNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800189 /**
190 * Per device next objective ID store with (device id + port) as key.
191 */
192 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800193 portNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800194 /**
195 * Per cross-connect objective ID store with VLAN ID as key.
196 */
197 public EventuallyConsistentMap<XConnectNextObjectiveStoreKey, Integer>
198 xConnectNextObjStore = null;
Saurav Das7c305372015-10-28 12:39:42 -0700199 // Per device, per-subnet assigned-vlans store, with (device id + subnet
200 // IPv4 prefix) as key
201 private EventuallyConsistentMap<SubnetAssignedVidStoreKey, VlanId>
Charles Chanb7f75ac2016-01-11 18:28:54 -0800202 subnetVidStore = null;
Saurav Das2d94d312015-11-24 23:21:05 -0800203 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
204 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700205
Charles Chanc91c8782016-03-30 17:54:24 -0700206 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82ab1932016-01-30 23:22:37 -0800207 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700208 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700209 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800210 public SegmentRoutingDeviceConfig createConfig() {
211 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700212 }
213 };
Charles Chanc91c8782016-03-30 17:54:24 -0700214 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82ab1932016-01-30 23:22:37 -0800215 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700216 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800217 @Override
218 public SegmentRoutingAppConfig createConfig() {
219 return new SegmentRoutingAppConfig();
220 }
221 };
Charles Chanf4586112015-11-09 16:37:23 -0800222
Charles Chanc91c8782016-03-30 17:54:24 -0700223 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
224 new ConfigFactory<ApplicationId, McastConfig>(SubjectFactories.APP_SUBJECT_FACTORY,
225 McastConfig.class, "multicast") {
226 @Override
227 public McastConfig createConfig() {
228 return new McastConfig();
229 }
230 };
231
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700232 private Object threadSchedulerLock = new Object();
233 private static int numOfEventsQueued = 0;
234 private static int numOfEventsExecuted = 0;
sangho80f11cb2015-04-01 13:05:26 -0700235 private static int numOfHandlerExecution = 0;
236 private static int numOfHandlerScheduled = 0;
237
Charles Chan1963f4f2016-02-18 14:22:42 -0800238 /**
239 * Segment Routing App ID.
240 */
241 public static final String SR_APP_ID = "org.onosproject.segmentrouting";
Charles Chanb7f75ac2016-01-11 18:28:54 -0800242 /**
243 * The starting value of per-subnet VLAN ID assignment.
244 */
Saurav Das7c305372015-10-28 12:39:42 -0700245 private static final short ASSIGNED_VLAN_START = 4093;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800246 /**
247 * The default VLAN ID assigned to the interfaces without subnet config.
248 */
Saurav Das7c305372015-10-28 12:39:42 -0700249 public static final short ASSIGNED_VLAN_NO_SUBNET = 4094;
250
sangho80f11cb2015-04-01 13:05:26 -0700251 @Activate
252 protected void activate() {
Jonathan Hart54541d12016-04-12 15:39:44 -0700253 appId = coreService.registerApplication(SR_APP_ID);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700254
255 log.debug("Creating EC map nsnextobjectivestore");
256 EventuallyConsistentMapBuilder<NeighborSetNextObjectiveStoreKey, Integer>
257 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700258 nsNextObjStore = nsNextObjMapBuilder
259 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700260 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700261 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700262 .build();
263 log.trace("Current size {}", nsNextObjStore.size());
264
Charles Chan77277672015-10-20 16:24:19 -0700265 log.debug("Creating EC map subnetnextobjectivestore");
266 EventuallyConsistentMapBuilder<SubnetNextObjectiveStoreKey, Integer>
267 subnetNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Charles Chan77277672015-10-20 16:24:19 -0700268 subnetNextObjStore = subnetNextObjMapBuilder
269 .withName("subnetnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700270 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700271 .withTimestampProvider((k, v) -> new WallClockTimestamp())
272 .build();
273
Saurav Das2d94d312015-11-24 23:21:05 -0800274 log.debug("Creating EC map subnetnextobjectivestore");
275 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
276 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
277 portNextObjStore = portNextObjMapBuilder
278 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700279 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800280 .withTimestampProvider((k, v) -> new WallClockTimestamp())
281 .build();
282
Charles Chanb7f75ac2016-01-11 18:28:54 -0800283 log.debug("Creating EC map xconnectnextobjectivestore");
284 EventuallyConsistentMapBuilder<XConnectNextObjectiveStoreKey, Integer>
285 xConnectNextObjStoreBuilder = storageService.eventuallyConsistentMapBuilder();
286 xConnectNextObjStore = xConnectNextObjStoreBuilder
287 .withName("xconnectnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700288 .withSerializer(createSerializer())
Charles Chanb7f75ac2016-01-11 18:28:54 -0800289 .withTimestampProvider((k, v) -> new WallClockTimestamp())
290 .build();
291
sangho4a5c42a2015-05-20 22:16:38 -0700292 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
293 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700294 tunnelStore = tunnelMapBuilder
295 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700296 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700297 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700298 .build();
299
300 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
301 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700302 policyStore = policyMapBuilder
303 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700304 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700305 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700306 .build();
307
Saurav Das7c305372015-10-28 12:39:42 -0700308 EventuallyConsistentMapBuilder<SubnetAssignedVidStoreKey, VlanId>
309 subnetVidStoreMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7c305372015-10-28 12:39:42 -0700310 subnetVidStore = subnetVidStoreMapBuilder
311 .withName("subnetvidstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700312 .withSerializer(createSerializer())
Saurav Das7c305372015-10-28 12:39:42 -0700313 .withTimestampProvider((k, v) -> new WallClockTimestamp())
314 .build();
315
Saurav Dasc3604f12016-03-23 11:22:49 -0700316 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
317 "purgeOnDisconnection", "true");
318 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
319 "purgeOnDisconnection", "true");
320
Charles Chan2b078ae2015-10-14 11:24:40 -0700321 processor = new InternalPacketProcessor();
322 linkListener = new InternalLinkListener();
323 deviceListener = new InternalDeviceListener();
Charles Chan82ab1932016-01-30 23:22:37 -0800324 netcfgHandler = new NetworkConfigEventHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700325 mcastHandler = new McastHandler(this);
326 hostHandler = new HostHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700327
Charles Chand6d25332016-02-26 22:19:52 -0800328 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700329 cfgService.registerConfigFactory(deviceConfigFactory);
330 cfgService.registerConfigFactory(appConfigFactory);
331 cfgService.registerConfigFactory(mcastConfigFactory);
Charles Chan82ab1932016-01-30 23:22:37 -0800332 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700333 packetService.addProcessor(processor, PacketProcessor.director(2));
334 linkService.addListener(linkListener);
335 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700336 multicastRouteService.addListener(mcastListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700337
Charles Chande6655c2015-12-23 00:15:11 -0800338 // Request ARP packet-in
339 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
340 selector.matchEthType(Ethernet.TYPE_ARP);
341 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId, Optional.empty());
342
Charles Chan2b078ae2015-10-14 11:24:40 -0700343 cfgListener.configureNetwork();
344
sangho80f11cb2015-04-01 13:05:26 -0700345 log.info("Started");
346 }
347
Jonathan Hart54541d12016-04-12 15:39:44 -0700348 private KryoNamespace.Builder createSerializer() {
349 return new KryoNamespace.Builder()
350 .register(KryoNamespaces.API)
351 .register(NeighborSetNextObjectiveStoreKey.class,
352 SubnetNextObjectiveStoreKey.class,
353 SubnetAssignedVidStoreKey.class,
354 NeighborSet.class,
355 Tunnel.class,
356 DefaultTunnel.class,
357 Policy.class,
358 TunnelPolicy.class,
359 Policy.Type.class,
360 PortNextObjectiveStoreKey.class,
361 XConnectNextObjectiveStoreKey.class
362 );
363 }
364
sangho80f11cb2015-04-01 13:05:26 -0700365 @Deactivate
366 protected void deactivate() {
Charles Chan72f556a2015-10-05 17:50:33 -0700367 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700368 cfgService.unregisterConfigFactory(deviceConfigFactory);
369 cfgService.unregisterConfigFactory(appConfigFactory);
370 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan72f556a2015-10-05 17:50:33 -0700371
Charles Chande6655c2015-12-23 00:15:11 -0800372 // Withdraw ARP packet-in
373 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
374 selector.matchEthType(Ethernet.TYPE_ARP);
375 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId, Optional.empty());
376
sangho80f11cb2015-04-01 13:05:26 -0700377 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700378 linkService.removeListener(linkListener);
379 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700380 multicastRouteService.removeListener(mcastListener);
381
sangho80f11cb2015-04-01 13:05:26 -0700382 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700383 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700384 deviceListener = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700385 groupHandlerMap.clear();
386
Charles Chanc91c8782016-03-30 17:54:24 -0700387 nsNextObjStore.destroy();
388 subnetNextObjStore.destroy();
389 portNextObjStore.destroy();
390 xConnectNextObjStore.destroy();
391 tunnelStore.destroy();
392 policyStore.destroy();
393 subnetVidStore.destroy();
sangho80f11cb2015-04-01 13:05:26 -0700394 log.info("Stopped");
395 }
396
sangho27462c62015-05-14 00:39:53 -0700397
398 @Override
399 public List<Tunnel> getTunnels() {
400 return tunnelHandler.getTunnels();
401 }
402
403 @Override
sanghobd812f82015-06-29 14:58:47 -0700404 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
405 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700406 }
407
408 @Override
sanghobd812f82015-06-29 14:58:47 -0700409 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700410 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");
sanghobd812f82015-06-29 14:58:47 -0700415 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700416 }
417 }
418 }
sanghobd812f82015-06-29 14:58:47 -0700419 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700420 }
421
422 @Override
sanghobd812f82015-06-29 14:58:47 -0700423 public PolicyHandler.Result removePolicy(Policy policy) {
424 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700425 }
426
427 @Override
sanghobd812f82015-06-29 14:58:47 -0700428 public PolicyHandler.Result createPolicy(Policy policy) {
429 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700430 }
431
432 @Override
433 public List<Policy> getPolicies() {
434 return policyHandler.getPolicies();
435 }
436
Saurav Das07c74602016-04-27 18:35:50 -0700437 @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
sangho80f1f892015-05-19 11:57:42 -0700446 /**
447 * Returns the tunnel object with the tunnel ID.
448 *
449 * @param tunnelId Tunnel ID
450 * @return Tunnel reference
451 */
sangho27462c62015-05-14 00:39:53 -0700452 public Tunnel getTunnel(String tunnelId) {
453 return tunnelHandler.getTunnel(tunnelId);
454 }
455
sangho80f11cb2015-04-01 13:05:26 -0700456 /**
Saurav Das7c305372015-10-28 12:39:42 -0700457 * 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.
sangho80f11cb2015-04-01 13:05:26 -0700469 *
Saurav Das7c305372015-10-28 12:39:42 -0700470 * @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.
sangho80f11cb2015-04-01 13:05:26 -0700475 */
Charles Chanb7f75ac2016-01-11 18:28:54 -0800476 // TODO: We should avoid assigning VLAN IDs that are used by VLAN cross-connection.
Saurav Das7c305372015-10-28 12:39:42 -0700477 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 Chanc6ad7752015-10-29 14:58:10 -0700492 Set<Ip4Prefix> configuredSubnets = deviceConfiguration.getSubnets(deviceId);
Saurav Das7c305372015-10-28 12:39:42 -0700493 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 Chan82ab1932016-01-30 23:22:37 -0800509 // 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 }
sangho80f11cb2015-04-01 13:05:26 -0700520 }
521
Saurav Das7c305372015-10-28 12:39:42 -0700522 return subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, subnet));
sangho80f11cb2015-04-01 13:05:26 -0700523 }
524
sangho27462c62015-05-14 00:39:53 -0700525 /**
Saurav Das7c305372015-10-28 12:39:42 -0700526 * Returns the next objective ID for the given NeighborSet.
Saurav Das4c35fc42015-11-20 15:27:53 -0800527 * If the nextObjective does not exist, a new one is created and
Saurav Das2d94d312015-11-24 23:21:05 -0800528 * its id is returned.
sangho27462c62015-05-14 00:39:53 -0700529 *
sangho80f1f892015-05-19 11:57:42 -0700530 * @param deviceId Device ID
531 * @param ns NegighborSet
Saurav Das4c35fc42015-11-20 15:27:53 -0800532 * @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
sangho27462c62015-05-14 00:39:53 -0700535 */
Saurav Das4c35fc42015-11-20 15:27:53 -0800536 public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns,
537 TrafficSelector meta) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700538 if (groupHandlerMap.get(deviceId) != null) {
539 log.trace("getNextObjectiveId query in device {}", deviceId);
540 return groupHandlerMap
Saurav Das4c35fc42015-11-20 15:27:53 -0800541 .get(deviceId).getNextObjectiveId(ns, meta);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700542 } else {
Saurav Das2d94d312015-11-24 23:21:05 -0800543 log.warn("getNextObjectiveId query - groupHandler for device {} "
544 + "not found", deviceId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700545 return -1;
546 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700547 }
548
Charles Chan77277672015-10-20 16:24:19 -0700549 /**
Saurav Das2d94d312015-11-24 23:21:05 -0800550 * 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 Chan77277672015-10-20 16:24:19 -0700552 *
553 * @param deviceId Device ID
554 * @param prefix Subnet
Saurav Das2d94d312015-11-24 23:21:05 -0800555 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -0700556 */
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 Das2d94d312015-11-24 23:21:05 -0800563 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 Das07c74602016-04-27 18:35:50 -0700579 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -0800580 */
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 Chanb7f75ac2016-01-11 18:28:54 -0800588 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
589 + " not found", deviceId);
590 return -1;
591 }
592 }
593
594 /**
595 * Returns the next objective ID of type broadcast associated with the VLAN
596 * cross-connection.
597 *
598 * @param deviceId Device ID for the cross-connection
599 * @param vlanId VLAN ID for the cross-connection
600 * @return next objective ID or -1 if it was not found
601 */
602 public int getXConnectNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
603 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
604 if (ghdlr != null) {
605 return ghdlr.getXConnectNextObjectiveId(vlanId);
606 } else {
607 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
Saurav Das2d94d312015-11-24 23:21:05 -0800608 + " not found", deviceId);
Charles Chan77277672015-10-20 16:24:19 -0700609 return -1;
610 }
611 }
612
sangho80f11cb2015-04-01 13:05:26 -0700613 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -0700614 @Override
615 public void process(PacketContext context) {
616
617 if (context.isHandled()) {
618 return;
619 }
620
621 InboundPacket pkt = context.inPacket();
622 Ethernet ethernet = pkt.parsed();
Saurav Das2d94d312015-11-24 23:21:05 -0800623 log.trace("Rcvd pktin: {}", ethernet);
sangho80f11cb2015-04-01 13:05:26 -0700624 if (ethernet.getEtherType() == Ethernet.TYPE_ARP) {
625 arpHandler.processPacketIn(pkt);
626 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
627 IPv4 ipPacket = (IPv4) ethernet.getPayload();
628 ipHandler.addToPacketBuffer(ipPacket);
629 if (ipPacket.getProtocol() == IPv4.PROTOCOL_ICMP) {
630 icmpHandler.processPacketIn(pkt);
631 } else {
632 ipHandler.processPacketIn(pkt);
633 }
634 }
635 }
636 }
637
638 private class InternalLinkListener implements LinkListener {
639 @Override
640 public void event(LinkEvent event) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700641 if (event.type() == LinkEvent.Type.LINK_ADDED
642 || event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700643 log.debug("Event {} received from Link Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -0700644 scheduleEventHandlerIfNotScheduled(event);
645 }
646 }
647 }
648
649 private class InternalDeviceListener implements DeviceListener {
sangho80f11cb2015-04-01 13:05:26 -0700650 @Override
651 public void event(DeviceEvent event) {
sangho80f11cb2015-04-01 13:05:26 -0700652 switch (event.type()) {
653 case DEVICE_ADDED:
654 case PORT_REMOVED:
sanghofb7c7292015-04-13 15:15:58 -0700655 case DEVICE_UPDATED:
656 case DEVICE_AVAILABILITY_CHANGED:
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700657 log.debug("Event {} received from Device Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -0700658 scheduleEventHandlerIfNotScheduled(event);
659 break;
660 default:
661 }
662 }
663 }
664
Saurav Das2d94d312015-11-24 23:21:05 -0800665 @SuppressWarnings("rawtypes")
sangho80f11cb2015-04-01 13:05:26 -0700666 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700667 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700668 eventQueue.add(event);
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700669 numOfEventsQueued++;
670
671 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
672 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700673 eventHandlerFuture = executorService
674 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
675 numOfHandlerScheduled++;
676 }
Jonathan Hart54541d12016-04-12 15:39:44 -0700677 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700678 numOfEventsQueued,
679 numOfHandlerScheduled);
sangho80f11cb2015-04-01 13:05:26 -0700680 }
sangho80f11cb2015-04-01 13:05:26 -0700681 }
682
683 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700684 @Override
sangho80f11cb2015-04-01 13:05:26 -0700685 public void run() {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700686 try {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700687 while (true) {
Saurav Das2d94d312015-11-24 23:21:05 -0800688 @SuppressWarnings("rawtypes")
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700689 Event event = null;
690 synchronized (threadSchedulerLock) {
691 if (!eventQueue.isEmpty()) {
692 event = eventQueue.poll();
693 numOfEventsExecuted++;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700694 } else {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700695 numOfHandlerExecution++;
696 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
697 numOfHandlerExecution, numOfEventsExecuted);
698 break;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700699 }
sanghofb7c7292015-04-13 15:15:58 -0700700 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700701 if (event.type() == LinkEvent.Type.LINK_ADDED) {
702 processLinkAdded((Link) event.subject());
703 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
704 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700705 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
706 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
707 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das62af8802015-12-04 10:52:59 -0800708 DeviceId deviceId = ((Device) event.subject()).id();
709 if (deviceService.isAvailable(deviceId)) {
Saurav Dasc28b3432015-10-30 17:45:38 -0700710 log.info("Processing device event {} for available device {}",
711 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700712 processDeviceAdded((Device) event.subject());
Saurav Dasc3604f12016-03-23 11:22:49 -0700713 } else {
714 log.info("Processing device event {} for unavailable device {}",
715 event.type(), ((Device) event.subject()).id());
716 processDeviceRemoved((Device) event.subject());
717 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700718 } else if (event.type() == DeviceEvent.Type.PORT_REMOVED) {
719 processPortRemoved((Device) event.subject(),
720 ((DeviceEvent) event).port());
Saurav Dasb149be12016-06-07 10:08:06 -0700721 } else if (event.type() == DeviceEvent.Type.PORT_ADDED ||
722 event.type() == DeviceEvent.Type.PORT_UPDATED) {
723 log.info("** PORT ADDED OR UPDATED {}/{} -> {}",
724 (Device) event.subject(),
725 ((DeviceEvent) event).port(),
726 event.type());
727 /* XXX create method for single port filtering rules
728 if (defaultRoutingHandler != null) {
729 defaultRoutingHandler.populatePortAddressingRules(
730 ((Device) event.subject()).id());
731 }*/
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700732 } else {
733 log.warn("Unhandled event type: {}", event.type());
734 }
sangho80f11cb2015-04-01 13:05:26 -0700735 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700736 } catch (Exception e) {
737 log.error("SegmentRouting event handler "
738 + "thread thrown an exception: {}", e);
sangho80f11cb2015-04-01 13:05:26 -0700739 }
sangho80f11cb2015-04-01 13:05:26 -0700740 }
741 }
742
sangho80f11cb2015-04-01 13:05:26 -0700743 private void processLinkAdded(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -0700744 log.info("** LINK ADDED {}", link.toString());
Charles Chan319d1a22015-11-03 10:42:14 -0800745 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
746 log.warn("Source device of this link is not configured.");
747 return;
748 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700749 //Irrespective whether the local is a MASTER or not for this device,
750 //create group handler instance and push default TTP flow rules.
751 //Because in a multi-instance setup, instances can initiate
752 //groups for any devices. Also the default TTP rules are needed
753 //to be pushed before inserting any IP table entries for any device
754 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
755 .deviceId());
756 if (groupHandler != null) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800757 groupHandler.linkUp(link, mastershipService.isLocalMaster(
758 link.src().deviceId()));
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700759 } else {
760 Device device = deviceService.getDevice(link.src().deviceId());
761 if (device != null) {
762 log.warn("processLinkAdded: Link Added "
763 + "Notification without Device Added "
764 + "event, still handling it");
765 processDeviceAdded(device);
766 groupHandler = groupHandlerMap.get(link.src()
767 .deviceId());
Saurav Das4c35fc42015-11-20 15:27:53 -0800768 groupHandler.linkUp(link, mastershipService.isLocalMaster(device.id()));
sangho80f11cb2015-04-01 13:05:26 -0700769 }
770 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700771
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700772 log.trace("Starting optimized route population process");
773 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null);
774 //log.trace("processLinkAdded: re-starting route population process");
775 //defaultRoutingHandler.startPopulationProcess();
Charles Chan72779502016-04-23 17:36:10 -0700776
777 mcastHandler.init();
sangho80f11cb2015-04-01 13:05:26 -0700778 }
779
780 private void processLinkRemoved(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -0700781 log.info("** LINK REMOVED {}", link.toString());
sangho2165d222015-05-01 09:38:25 -0700782 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
783 if (groupHandler != null) {
Saurav Das62af8802015-12-04 10:52:59 -0800784 groupHandler.portDown(link.src().port(),
785 mastershipService.isLocalMaster(link.src().deviceId()));
sangho2165d222015-05-01 09:38:25 -0700786 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700787 log.trace("Starting optimized route population process");
788 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link);
789 //log.trace("processLinkRemoved: re-starting route population process");
790 //defaultRoutingHandler.startPopulationProcess();
Charles Chan72779502016-04-23 17:36:10 -0700791
792 mcastHandler.processLinkDown(link);
sangho80f11cb2015-04-01 13:05:26 -0700793 }
794
795 private void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -0700796 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan319d1a22015-11-03 10:42:14 -0800797 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das8ec0ec42015-11-03 14:39:27 -0800798 log.warn("Device configuration uploading. Device {} will be "
799 + "processed after config completes.", device.id());
800 return;
801 }
Charles Chan72779502016-04-23 17:36:10 -0700802 processDeviceAddedInternal(device.id());
803 }
804
805 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -0700806 // Irrespective of whether the local is a MASTER or not for this device,
807 // we need to create a SR-group-handler instance. This is because in a
808 // multi-instance setup, any instance can initiate forwarding/next-objectives
809 // for any switch (even if this instance is a SLAVE or not even connected
810 // to the switch). To handle this, a default-group-handler instance is necessary
811 // per switch.
Charles Chan72779502016-04-23 17:36:10 -0700812 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
813 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -0800814 DefaultGroupHandler groupHandler;
815 try {
816 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -0700817 createGroupHandler(deviceId,
818 appId,
819 deviceConfiguration,
820 linkService,
821 flowObjectiveService,
822 this);
Charles Chan319d1a22015-11-03 10:42:14 -0800823 } catch (DeviceConfigNotFoundException e) {
824 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
825 return;
826 }
Charles Chan72779502016-04-23 17:36:10 -0700827 log.debug("updating groupHandlerMap with new config for device: {}",
828 deviceId);
829 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -0800830 }
Saurav Dasb149be12016-06-07 10:08:06 -0700831 // Also, in some cases, drivers may need extra
832 // information to process rules (eg. Router IP/MAC); and so, we send
833 // port addressing rules to the driver as well irrespective of whether
834 // this instance is the master or not.
835 defaultRoutingHandler.populatePortAddressingRules(deviceId);
836
Charles Chan72779502016-04-23 17:36:10 -0700837 if (mastershipService.isLocalMaster(deviceId)) {
838 hostHandler.readInitialHosts(deviceId);
839 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan77277672015-10-20 16:24:19 -0700840 groupHandler.createGroupsFromSubnetConfig();
Charles Chan72779502016-04-23 17:36:10 -0700841 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
842 groupHandler.createGroupsForXConnect(deviceId);
843 routingRulePopulator.populateXConnectBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -0700844 }
Charles Chan82ab1932016-01-30 23:22:37 -0800845
Charles Chan72779502016-04-23 17:36:10 -0700846 netcfgHandler.initVRouters(deviceId);
sangho80f11cb2015-04-01 13:05:26 -0700847 }
848
Saurav Dasc3604f12016-03-23 11:22:49 -0700849 private void processDeviceRemoved(Device device) {
850 nsNextObjStore.entrySet().stream()
851 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
852 .forEach(entry -> {
853 nsNextObjStore.remove(entry.getKey());
854 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700855 subnetNextObjStore.entrySet().stream()
856 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
857 .forEach(entry -> {
858 subnetNextObjStore.remove(entry.getKey());
859 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700860 portNextObjStore.entrySet().stream()
861 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
862 .forEach(entry -> {
863 portNextObjStore.remove(entry.getKey());
864 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700865 xConnectNextObjStore.entrySet().stream()
866 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
867 .forEach(entry -> {
868 xConnectNextObjStore.remove(entry.getKey());
869 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700870 subnetVidStore.entrySet().stream()
871 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
872 .forEach(entry -> {
873 subnetVidStore.remove(entry.getKey());
874 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700875 groupHandlerMap.remove(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -0700876 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan72779502016-04-23 17:36:10 -0700877 mcastHandler.removeDevice(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -0700878 }
879
sangho80f11cb2015-04-01 13:05:26 -0700880 private void processPortRemoved(Device device, Port port) {
Saurav Dasb149be12016-06-07 10:08:06 -0700881 log.info("Port {} was removed", port.toString());
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700882 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sangho80f11cb2015-04-01 13:05:26 -0700883 if (groupHandler != null) {
Saurav Das62af8802015-12-04 10:52:59 -0800884 groupHandler.portDown(port.number(),
885 mastershipService.isLocalMaster(device.id()));
sangho80f11cb2015-04-01 13:05:26 -0700886 }
887 }
sangho27462c62015-05-14 00:39:53 -0700888
Charles Chan72f556a2015-10-05 17:50:33 -0700889 private class InternalConfigListener implements NetworkConfigListener {
Charles Chane7c61022015-10-07 14:21:45 -0700890 SegmentRoutingManager segmentRoutingManager;
891
Charles Chanb7f75ac2016-01-11 18:28:54 -0800892 /**
893 * Constructs the internal network config listener.
894 *
895 * @param srMgr segment routing manager
896 */
Charles Chane7c61022015-10-07 14:21:45 -0700897 public InternalConfigListener(SegmentRoutingManager srMgr) {
898 this.segmentRoutingManager = srMgr;
899 }
900
Charles Chanb7f75ac2016-01-11 18:28:54 -0800901 /**
902 * Reads network config and initializes related data structure accordingly.
903 */
Charles Chane7c61022015-10-07 14:21:45 -0700904 public void configureNetwork() {
Charles Chan43547ca2016-02-10 20:46:58 -0800905 deviceConfiguration = new DeviceConfiguration(appId,
906 segmentRoutingManager.cfgService);
Charles Chane7c61022015-10-07 14:21:45 -0700907
908 arpHandler = new ArpHandler(segmentRoutingManager);
909 icmpHandler = new IcmpHandler(segmentRoutingManager);
910 ipHandler = new IpHandler(segmentRoutingManager);
911 routingRulePopulator = new RoutingRulePopulator(segmentRoutingManager);
912 defaultRoutingHandler = new DefaultRoutingHandler(segmentRoutingManager);
913
914 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
915 groupHandlerMap, tunnelStore);
916 policyHandler = new PolicyHandler(appId, deviceConfiguration,
917 flowObjectiveService,
918 tunnelHandler, policyStore);
919
Charles Chane7c61022015-10-07 14:21:45 -0700920 for (Device device : deviceService.getDevices()) {
Charles Chan72779502016-04-23 17:36:10 -0700921 processDeviceAddedInternal(device.id());
Charles Chane7c61022015-10-07 14:21:45 -0700922 }
923
924 defaultRoutingHandler.startPopulationProcess();
Charles Chan72779502016-04-23 17:36:10 -0700925 mcastHandler.init();
Charles Chane7c61022015-10-07 14:21:45 -0700926 }
927
Charles Chan72f556a2015-10-05 17:50:33 -0700928 @Override
929 public void event(NetworkConfigEvent event) {
Charles Chan82ab1932016-01-30 23:22:37 -0800930 // TODO move this part to NetworkConfigEventHandler
931 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
932 switch (event.type()) {
933 case CONFIG_ADDED:
934 log.info("Segment Routing Config added.");
935 configureNetwork();
936 break;
937 case CONFIG_UPDATED:
938 log.info("Segment Routing Config updated.");
939 // TODO support dynamic configuration
940 break;
941 default:
942 break;
Charles Chan2b078ae2015-10-14 11:24:40 -0700943 }
Charles Chan82ab1932016-01-30 23:22:37 -0800944 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chand6d25332016-02-26 22:19:52 -0800945 checkState(netcfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan82ab1932016-01-30 23:22:37 -0800946 switch (event.type()) {
947 case CONFIG_ADDED:
948 netcfgHandler.processVRouterConfigAdded(event);
949 break;
950 case CONFIG_UPDATED:
951 netcfgHandler.processVRouterConfigUpdated(event);
952 break;
953 case CONFIG_REMOVED:
954 netcfgHandler.processVRouterConfigRemoved(event);
955 break;
956 default:
957 break;
Charles Chan2b078ae2015-10-14 11:24:40 -0700958 }
Charles Chan72f556a2015-10-05 17:50:33 -0700959 }
960 }
961 }
Charles Chanf4586112015-11-09 16:37:23 -0800962
963 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -0800964 @Override
965 public void event(HostEvent event) {
966 // Do not proceed without mastership
967 DeviceId deviceId = event.subject().location().deviceId();
968 if (!mastershipService.isLocalMaster(deviceId)) {
969 return;
970 }
971
972 switch (event.type()) {
973 case HOST_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -0700974 hostHandler.processHostAddedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -0800975 break;
976 case HOST_MOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -0700977 hostHandler.processHostMovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -0800978 break;
979 case HOST_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -0700980 hostHandler.processHostRemoveEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -0800981 break;
982 case HOST_UPDATED:
Charles Chan1eaf4802016-04-18 13:44:03 -0700983 hostHandler.processHostUpdatedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -0800984 break;
985 default:
986 log.warn("Unsupported host event type: {}", event.type());
987 break;
988 }
989 }
990 }
991
Charles Chanc91c8782016-03-30 17:54:24 -0700992 private class InternalMcastListener implements McastListener {
993 @Override
994 public void event(McastEvent event) {
995 switch (event.type()) {
996 case SOURCE_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -0700997 mcastHandler.processSourceAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -0700998 break;
999 case SINK_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001000 mcastHandler.processSinkAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001001 break;
1002 case SINK_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001003 mcastHandler.processSinkRemoved(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001004 break;
1005 case ROUTE_ADDED:
1006 case ROUTE_REMOVED:
1007 default:
1008 break;
1009 }
1010 }
1011 }
sangho80f11cb2015-04-01 13:05:26 -07001012}