blob: e7cf32f78f1b6d39d83aa06e6452fdf533d86e3d [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sanghob35a6192015-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
Charles Chanc81c45b2016-10-20 17:02:44 -070018import com.google.common.collect.Maps;
sanghob35a6192015-04-01 13:05:26 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho1e575652015-05-14 00:39:53 -070024import org.apache.felix.scr.annotations.Service;
sanghob35a6192015-04-01 13:05:26 -070025import org.onlab.packet.Ethernet;
Pier Ventre735b8c82016-12-02 08:16:05 -080026import org.onlab.packet.ICMP6;
Charles Chanc42e84e2015-10-20 16:24:19 -070027import org.onlab.packet.IPv4;
Pier Ventre10bd8d12016-11-26 21:05:22 -080028import org.onlab.packet.IPv6;
Charles Chanc42e84e2015-10-20 16:24:19 -070029import org.onlab.packet.Ip4Prefix;
Charles Chanc42e84e2015-10-20 16:24:19 -070030import org.onlab.packet.IpPrefix;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070031import org.onlab.packet.VlanId;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070032import org.onlab.util.KryoNamespace;
Saurav Das80980c72016-03-23 11:22:49 -070033import org.onosproject.cfg.ComponentConfigService;
sanghob35a6192015-04-01 13:05:26 -070034import org.onosproject.core.ApplicationId;
35import org.onosproject.core.CoreService;
36import org.onosproject.event.Event;
Pier Luigi721b6622017-02-03 13:34:21 -080037import org.onosproject.incubator.net.config.basics.ConfigException;
Pier Luigi7dad71c2017-02-01 13:50:04 -080038import org.onosproject.incubator.net.config.basics.InterfaceConfig;
Charles Chand55e84d2016-03-30 17:54:24 -070039import org.onosproject.incubator.net.config.basics.McastConfig;
Pier Luigi721b6622017-02-03 13:34:21 -080040import org.onosproject.incubator.net.intf.Interface;
Charles Chan2c15aca2016-11-09 20:51:44 -080041import org.onosproject.incubator.net.intf.InterfaceService;
Charles Chan03a73e02016-10-24 14:52:01 -070042import org.onosproject.incubator.net.routing.RouteEvent;
43import org.onosproject.incubator.net.routing.RouteListener;
44import org.onosproject.incubator.net.routing.RouteService;
Pier Ventre735b8c82016-12-02 08:16:05 -080045import org.onosproject.incubator.net.neighbour.NeighbourResolutionService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070046import org.onosproject.mastership.MastershipService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080047import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070048import org.onosproject.net.Device;
49import org.onosproject.net.DeviceId;
50import org.onosproject.net.Link;
51import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080052import org.onosproject.net.PortNumber;
Charles Chand6832882015-10-05 17:50:33 -070053import org.onosproject.net.config.ConfigFactory;
54import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070055import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070056import org.onosproject.net.config.NetworkConfigRegistry;
Charles Chand6832882015-10-05 17:50:33 -070057import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070058import org.onosproject.net.device.DeviceEvent;
59import org.onosproject.net.device.DeviceListener;
60import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080061import org.onosproject.net.flow.TrafficSelector;
62import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070063import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan68aa62d2015-11-09 16:37:23 -080064import org.onosproject.net.host.HostEvent;
65import org.onosproject.net.host.HostListener;
Pier Ventre10bd8d12016-11-26 21:05:22 -080066import org.onosproject.net.host.HostService;
67import org.onosproject.net.link.LinkEvent;
68import org.onosproject.net.link.LinkListener;
69import org.onosproject.net.link.LinkService;
Charles Chand55e84d2016-03-30 17:54:24 -070070import org.onosproject.net.mcast.McastEvent;
71import org.onosproject.net.mcast.McastListener;
72import org.onosproject.net.mcast.MulticastRouteService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080073import org.onosproject.net.packet.InboundPacket;
74import org.onosproject.net.packet.PacketContext;
75import org.onosproject.net.packet.PacketProcessor;
76import org.onosproject.net.packet.PacketService;
Charles Chand55e84d2016-03-30 17:54:24 -070077import org.onosproject.net.topology.TopologyService;
Pier Luigi7dad71c2017-02-01 13:50:04 -080078import org.onosproject.routing.config.RouterConfig;
Charles Chand55e84d2016-03-30 17:54:24 -070079import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
80import org.onosproject.segmentrouting.config.DeviceConfiguration;
Charles Chand55e84d2016-03-30 17:54:24 -070081import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Pier Ventre10bd8d12016-11-26 21:05:22 -080082import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -070083import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -070084import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
85import org.onosproject.segmentrouting.grouphandler.NeighborSet;
Charles Chand2990362016-04-18 13:44:03 -070086import org.onosproject.segmentrouting.storekey.NeighborSetNextObjectiveStoreKey;
87import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chand2990362016-04-18 13:44:03 -070088import org.onosproject.segmentrouting.storekey.SubnetAssignedVidStoreKey;
89import org.onosproject.segmentrouting.storekey.SubnetNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -070090import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070091import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070092import org.onosproject.store.service.EventuallyConsistentMap;
93import org.onosproject.store.service.EventuallyConsistentMapBuilder;
94import org.onosproject.store.service.StorageService;
95import org.onosproject.store.service.WallClockTimestamp;
Charles Chan35fd1a72016-06-13 18:54:31 -070096import org.opencord.cordconfig.CordConfigEvent;
97import org.opencord.cordconfig.CordConfigListener;
98import org.opencord.cordconfig.CordConfigService;
sanghob35a6192015-04-01 13:05:26 -070099import org.slf4j.Logger;
100import org.slf4j.LoggerFactory;
101
Saurav Das0e99e2b2015-10-28 12:39:42 -0700102import java.util.Collections;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700103import java.util.HashSet;
sangho1e575652015-05-14 00:39:53 -0700104import java.util.List;
sanghob35a6192015-04-01 13:05:26 -0700105import java.util.Map;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700106import java.util.Set;
sanghob35a6192015-04-01 13:05:26 -0700107import java.util.concurrent.ConcurrentHashMap;
108import java.util.concurrent.ConcurrentLinkedQueue;
109import java.util.concurrent.Executors;
110import java.util.concurrent.ScheduledExecutorService;
111import java.util.concurrent.ScheduledFuture;
112import java.util.concurrent.TimeUnit;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800113import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -0700114
Charles Chan3e783d02016-02-26 22:19:52 -0800115import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800116import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700117import static org.onlab.util.Tools.groupedThreads;
Charles Chan3e783d02016-02-26 22:19:52 -0800118
Charles Chane849c192016-01-11 18:28:54 -0800119/**
120 * Segment routing manager.
121 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700122@Service
123@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700124public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700125
Charles Chan2c15aca2016-11-09 20:51:44 -0800126 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
sanghob35a6192015-04-01 13:05:26 -0700127
128 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700129 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700130
131 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800132 private NeighbourResolutionService neighbourResolutionService;
133
134 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700135 CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700136
137 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700138 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700139
140 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700141 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700142
143 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700144 DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700145
146 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700147 FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700148
149 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700150 LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700151
Charles Chan5270ed02016-01-30 23:22:37 -0800152 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700153 MastershipService mastershipService;
154
155 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
156 StorageService storageService;
157
158 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
159 MulticastRouteService multicastRouteService;
160
161 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
162 TopologyService topologyService;
163
164 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
165 CordConfigService cordConfigService;
166
167 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
168 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800169
170 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800171 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800172
Saurav Das80980c72016-03-23 11:22:49 -0700173 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800174 public InterfaceService interfaceService;
175
Charles Chan03a73e02016-10-24 14:52:01 -0700176 ArpHandler arpHandler = null;
177 IcmpHandler icmpHandler = null;
178 IpHandler ipHandler = null;
179 RoutingRulePopulator routingRulePopulator = null;
Charles Chan2c15aca2016-11-09 20:51:44 -0800180 public ApplicationId appId;
sangho666cd6d2015-04-14 16:27:13 -0700181 protected DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700182
Charles Chan03a73e02016-10-24 14:52:01 -0700183 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700184 private TunnelHandler tunnelHandler = null;
185 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700186 private InternalPacketProcessor processor = null;
187 private InternalLinkListener linkListener = null;
188 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700189 private AppConfigHandler appCfgHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700190 XConnectHandler xConnectHandler = null;
Charles Chand2990362016-04-18 13:44:03 -0700191 private McastHandler mcastHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700192 HostHandler hostHandler = null;
Charles Chan35fd1a72016-06-13 18:54:31 -0700193 private CordConfigHandler cordConfigHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800194 private RouteHandler routeHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800195 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
sanghob35a6192015-04-01 13:05:26 -0700196 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan5270ed02016-01-30 23:22:37 -0800197 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700198 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
199 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan35fd1a72016-06-13 18:54:31 -0700200 private final InternalCordConfigListener cordConfigListener = new InternalCordConfigListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700201 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sanghob35a6192015-04-01 13:05:26 -0700202
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700203 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700204 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sanghob35a6192015-04-01 13:05:26 -0700205
Saurav Das4ce45962015-11-24 23:21:05 -0800206 @SuppressWarnings("unused")
sanghob35a6192015-04-01 13:05:26 -0700207 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800208 @SuppressWarnings("rawtypes")
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700209 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chan68aa62d2015-11-09 16:37:23 -0800210 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chane849c192016-01-11 18:28:54 -0800211 new ConcurrentHashMap<>();
212 /**
213 * Per device next objective ID store with (device id + neighbor set) as key.
214 */
215 public EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer>
Charles Chan68aa62d2015-11-09 16:37:23 -0800216 nsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800217 /**
218 * Per device next objective ID store with (device id + subnet) as key.
219 */
220 public EventuallyConsistentMap<SubnetNextObjectiveStoreKey, Integer>
Charles Chan68aa62d2015-11-09 16:37:23 -0800221 subnetNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800222 /**
223 * Per device next objective ID store with (device id + port) as key.
224 */
225 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800226 portNextObjStore = null;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700227 // Per device, per-subnet assigned-vlans store, with (device id + subnet
228 // IPv4 prefix) as key
229 private EventuallyConsistentMap<SubnetAssignedVidStoreKey, VlanId>
Charles Chane849c192016-01-11 18:28:54 -0800230 subnetVidStore = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800231 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
232 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700233
Charles Chand55e84d2016-03-30 17:54:24 -0700234 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700235 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
236 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700237 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700238 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800239 public SegmentRoutingDeviceConfig createConfig() {
240 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700241 }
242 };
Charles Chand55e84d2016-03-30 17:54:24 -0700243 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700244 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
245 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700246 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800247 @Override
248 public SegmentRoutingAppConfig createConfig() {
249 return new SegmentRoutingAppConfig();
250 }
251 };
Charles Chanfc5c7802016-05-17 13:13:55 -0700252 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
253 new ConfigFactory<ApplicationId, XConnectConfig>(
254 SubjectFactories.APP_SUBJECT_FACTORY,
255 XConnectConfig.class, "xconnect") {
256 @Override
257 public XConnectConfig createConfig() {
258 return new XConnectConfig();
259 }
260 };
Charles Chand55e84d2016-03-30 17:54:24 -0700261 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700262 new ConfigFactory<ApplicationId, McastConfig>(
263 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700264 McastConfig.class, "multicast") {
265 @Override
266 public McastConfig createConfig() {
267 return new McastConfig();
268 }
269 };
270
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700271 private Object threadSchedulerLock = new Object();
272 private static int numOfEventsQueued = 0;
273 private static int numOfEventsExecuted = 0;
sanghob35a6192015-04-01 13:05:26 -0700274 private static int numOfHandlerExecution = 0;
275 private static int numOfHandlerScheduled = 0;
276
Charles Chan116188d2016-02-18 14:22:42 -0800277 /**
278 * Segment Routing App ID.
279 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800280 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chane849c192016-01-11 18:28:54 -0800281 /**
282 * The starting value of per-subnet VLAN ID assignment.
283 */
Saurav Das0e99e2b2015-10-28 12:39:42 -0700284 private static final short ASSIGNED_VLAN_START = 4093;
Charles Chane849c192016-01-11 18:28:54 -0800285 /**
286 * The default VLAN ID assigned to the interfaces without subnet config.
287 */
Saurav Das0e99e2b2015-10-28 12:39:42 -0700288 public static final short ASSIGNED_VLAN_NO_SUBNET = 4094;
289
sanghob35a6192015-04-01 13:05:26 -0700290 @Activate
291 protected void activate() {
Charles Chan2c15aca2016-11-09 20:51:44 -0800292 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700293
294 log.debug("Creating EC map nsnextobjectivestore");
295 EventuallyConsistentMapBuilder<NeighborSetNextObjectiveStoreKey, Integer>
296 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700297 nsNextObjStore = nsNextObjMapBuilder
298 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700299 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700300 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700301 .build();
302 log.trace("Current size {}", nsNextObjStore.size());
303
Charles Chanc42e84e2015-10-20 16:24:19 -0700304 log.debug("Creating EC map subnetnextobjectivestore");
305 EventuallyConsistentMapBuilder<SubnetNextObjectiveStoreKey, Integer>
306 subnetNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Charles Chanc42e84e2015-10-20 16:24:19 -0700307 subnetNextObjStore = subnetNextObjMapBuilder
308 .withName("subnetnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700309 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700310 .withTimestampProvider((k, v) -> new WallClockTimestamp())
311 .build();
312
Saurav Das4ce45962015-11-24 23:21:05 -0800313 log.debug("Creating EC map subnetnextobjectivestore");
314 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
315 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
316 portNextObjStore = portNextObjMapBuilder
317 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700318 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800319 .withTimestampProvider((k, v) -> new WallClockTimestamp())
320 .build();
321
sangho0b2b6d12015-05-20 22:16:38 -0700322 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
323 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700324 tunnelStore = tunnelMapBuilder
325 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700326 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700327 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700328 .build();
329
330 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
331 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700332 policyStore = policyMapBuilder
333 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700334 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700335 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700336 .build();
337
Saurav Das0e99e2b2015-10-28 12:39:42 -0700338 EventuallyConsistentMapBuilder<SubnetAssignedVidStoreKey, VlanId>
339 subnetVidStoreMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das0e99e2b2015-10-28 12:39:42 -0700340 subnetVidStore = subnetVidStoreMapBuilder
341 .withName("subnetvidstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700342 .withSerializer(createSerializer())
Saurav Das0e99e2b2015-10-28 12:39:42 -0700343 .withTimestampProvider((k, v) -> new WallClockTimestamp())
344 .build();
345
Saurav Das80980c72016-03-23 11:22:49 -0700346 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800347 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700348 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800349 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800350 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
351 "requestInterceptsEnabled", "false");
Pier Luigi7e415132017-01-12 22:46:39 -0800352 compCfgService.preSetProperty("org.onosproject.incubator.net.neighbour.impl.NeighbourResolutionManager",
353 "requestInterceptsEnabled", "false");
354 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelay",
355 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800356 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
357 "greedyLearningIpv6", "true");
358
Saurav Das80980c72016-03-23 11:22:49 -0700359
Charles Chanb8e10c82015-10-14 11:24:40 -0700360 processor = new InternalPacketProcessor();
361 linkListener = new InternalLinkListener();
362 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700363 appCfgHandler = new AppConfigHandler(this);
364 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700365 mcastHandler = new McastHandler(this);
366 hostHandler = new HostHandler(this);
Charles Chan35fd1a72016-06-13 18:54:31 -0700367 cordConfigHandler = new CordConfigHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700368 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800369 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700370
Charles Chan3e783d02016-02-26 22:19:52 -0800371 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700372 cfgService.registerConfigFactory(deviceConfigFactory);
373 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700374 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700375 cfgService.registerConfigFactory(mcastConfigFactory);
Charles Chan5270ed02016-01-30 23:22:37 -0800376 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700377 packetService.addProcessor(processor, PacketProcessor.director(2));
378 linkService.addListener(linkListener);
379 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700380 multicastRouteService.addListener(mcastListener);
Charles Chan35fd1a72016-06-13 18:54:31 -0700381 cordConfigService.addListener(cordConfigListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700382
383 cfgListener.configureNetwork();
384
Charles Chan03a73e02016-10-24 14:52:01 -0700385 routeService.addListener(routeListener);
386
sanghob35a6192015-04-01 13:05:26 -0700387 log.info("Started");
388 }
389
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700390 private KryoNamespace.Builder createSerializer() {
391 return new KryoNamespace.Builder()
392 .register(KryoNamespaces.API)
393 .register(NeighborSetNextObjectiveStoreKey.class,
394 SubnetNextObjectiveStoreKey.class,
395 SubnetAssignedVidStoreKey.class,
396 NeighborSet.class,
397 Tunnel.class,
398 DefaultTunnel.class,
399 Policy.class,
400 TunnelPolicy.class,
401 Policy.Type.class,
402 PortNextObjectiveStoreKey.class,
Charles Chanfc5c7802016-05-17 13:13:55 -0700403 XConnectStoreKey.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700404 );
405 }
406
sanghob35a6192015-04-01 13:05:26 -0700407 @Deactivate
408 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700409 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700410 cfgService.unregisterConfigFactory(deviceConfigFactory);
411 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700412 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700413 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chand6832882015-10-05 17:50:33 -0700414
sanghob35a6192015-04-01 13:05:26 -0700415 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700416 linkService.removeListener(linkListener);
417 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700418 multicastRouteService.removeListener(mcastListener);
Charles Chan35fd1a72016-06-13 18:54:31 -0700419 cordConfigService.removeListener(cordConfigListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700420 routeService.removeListener(routeListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700421
sanghob35a6192015-04-01 13:05:26 -0700422 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700423 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700424 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700425 groupHandlerMap.clear();
426
Charles Chand55e84d2016-03-30 17:54:24 -0700427 nsNextObjStore.destroy();
428 subnetNextObjStore.destroy();
429 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700430 tunnelStore.destroy();
431 policyStore.destroy();
432 subnetVidStore.destroy();
sanghob35a6192015-04-01 13:05:26 -0700433 log.info("Stopped");
434 }
435
sangho1e575652015-05-14 00:39:53 -0700436 @Override
437 public List<Tunnel> getTunnels() {
438 return tunnelHandler.getTunnels();
439 }
440
441 @Override
sangho71abe1b2015-06-29 14:58:47 -0700442 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
443 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700444 }
445
446 @Override
sangho71abe1b2015-06-29 14:58:47 -0700447 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700448 for (Policy policy: policyHandler.getPolicies()) {
449 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
450 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
451 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
452 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700453 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700454 }
455 }
456 }
sangho71abe1b2015-06-29 14:58:47 -0700457 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700458 }
459
460 @Override
sangho71abe1b2015-06-29 14:58:47 -0700461 public PolicyHandler.Result removePolicy(Policy policy) {
462 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700463 }
464
465 @Override
sangho71abe1b2015-06-29 14:58:47 -0700466 public PolicyHandler.Result createPolicy(Policy policy) {
467 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700468 }
469
470 @Override
471 public List<Policy> getPolicies() {
472 return policyHandler.getPolicies();
473 }
474
Saurav Das59232cf2016-04-27 18:35:50 -0700475 @Override
476 public void rerouteNetwork() {
477 cfgListener.configureNetwork();
478 for (Device device : deviceService.getDevices()) {
479 defaultRoutingHandler.populatePortAddressingRules(device.id());
480 }
481 defaultRoutingHandler.startPopulationProcess();
482 }
483
Charles Chanc81c45b2016-10-20 17:02:44 -0700484 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800485 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
486 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Charles Chanc81c45b2016-10-20 17:02:44 -0700487 deviceService.getAvailableDevices().forEach(device -> {
488 deviceSubnetMap.put(device.id(), deviceConfiguration.getSubnets(device.id()));
489 });
490 return deviceSubnetMap;
491 }
492
sanghof9d0bf12015-05-19 11:57:42 -0700493 /**
Pier Ventre98161782016-10-31 15:00:01 -0700494 * Returns the MPLS-ECMP configuration.
495 *
496 * @return MPLS-ECMP value
497 */
498 public boolean getMplsEcmp() {
499 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
500 .getConfig(this.appId, SegmentRoutingAppConfig.class);
501 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
502 }
503
504 /**
sanghof9d0bf12015-05-19 11:57:42 -0700505 * Returns the tunnel object with the tunnel ID.
506 *
507 * @param tunnelId Tunnel ID
508 * @return Tunnel reference
509 */
sangho1e575652015-05-14 00:39:53 -0700510 public Tunnel getTunnel(String tunnelId) {
511 return tunnelHandler.getTunnel(tunnelId);
512 }
513
sanghob35a6192015-04-01 13:05:26 -0700514 /**
Saurav Das0e99e2b2015-10-28 12:39:42 -0700515 * Returns the vlan-id assigned to the subnet configured for a device.
516 * If no vlan-id has been assigned, a new one is assigned out of a pool of ids,
517 * if and only if this controller instance is the master for the device.
518 * <p>
519 * USAGE: The assigned vlans are meant to be applied to untagged packets on those
520 * switches/pipelines that need this functionality. These vids are meant
521 * to be used internally within a switch, and thus need to be unique only
522 * on a switch level. Note that packets never go out on the wire with these
523 * vlans. Currently, vlan ids are assigned from value 4093 down.
524 * Vlan id 4094 expected to be used for all ports that are not assigned subnets.
525 * Vlan id 4095 is reserved and unused. Only a single vlan id is assigned
526 * per subnet.
sanghob35a6192015-04-01 13:05:26 -0700527 *
Saurav Das0e99e2b2015-10-28 12:39:42 -0700528 * @param deviceId switch dpid
Pier Ventref4b5fce2016-11-28 16:48:06 -0800529 * @param subnet IP prefix for which assigned vlan is desired
Saurav Das0e99e2b2015-10-28 12:39:42 -0700530 * @return VlanId assigned for the subnet on the device, or
531 * null if no vlan assignment was found and this instance is not
532 * the master for the device.
sanghob35a6192015-04-01 13:05:26 -0700533 */
Charles Chane849c192016-01-11 18:28:54 -0800534 // TODO: We should avoid assigning VLAN IDs that are used by VLAN cross-connection.
Pier Ventre10bd8d12016-11-26 21:05:22 -0800535 public VlanId getSubnetAssignedVlanId(DeviceId deviceId, IpPrefix subnet) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700536 VlanId assignedVid = subnetVidStore.get(new SubnetAssignedVidStoreKey(
537 deviceId, subnet));
538 if (assignedVid != null) {
539 log.debug("Query for subnet:{} on device:{} returned assigned-vlan "
540 + "{}", subnet, deviceId, assignedVid);
541 return assignedVid;
542 }
543 //check mastership for the right to assign a vlan
544 if (!mastershipService.isLocalMaster(deviceId)) {
545 log.warn("This controller instance is not the master for device {}. "
546 + "Cannot assign vlan-id for subnet {}", deviceId, subnet);
547 return null;
548 }
549 // vlan assignment is expensive but done only once
Pier Ventre10bd8d12016-11-26 21:05:22 -0800550 // FIXME for now we will do assignment considering only the ipv4 subnet.
551 Set<Ip4Prefix> configuredSubnets = deviceConfiguration.getSubnets(deviceId)
552 .stream()
553 .filter(IpPrefix::isIp4)
554 .map(IpPrefix::getIp4Prefix)
555 .collect(Collectors.toSet());
Saurav Das0e99e2b2015-10-28 12:39:42 -0700556 Set<Short> assignedVlans = new HashSet<>();
557 Set<Ip4Prefix> unassignedSubnets = new HashSet<>();
558 for (Ip4Prefix sub : configuredSubnets) {
559 VlanId v = subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId,
560 sub));
561 if (v != null) {
562 assignedVlans.add(v.toShort());
563 } else {
564 unassignedSubnets.add(sub);
565 }
566 }
567 short nextAssignedVlan = ASSIGNED_VLAN_START;
568 if (!assignedVlans.isEmpty()) {
569 nextAssignedVlan = (short) (Collections.min(assignedVlans) - 1);
570 }
571 for (Ip4Prefix unsub : unassignedSubnets) {
Pier Ventref4b5fce2016-11-28 16:48:06 -0800572 subnetVidStore.put(new SubnetAssignedVidStoreKey(deviceId, unsub),
573 VlanId.vlanId(nextAssignedVlan--));
574 log.info("Assigned vlan: {} to subnet: {} on device: {}",
575 nextAssignedVlan + 1, unsub, deviceId);
sanghob35a6192015-04-01 13:05:26 -0700576 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700577 return subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, subnet));
sanghob35a6192015-04-01 13:05:26 -0700578 }
579
sangho1e575652015-05-14 00:39:53 -0700580 /**
Saurav Das0e99e2b2015-10-28 12:39:42 -0700581 * Returns the next objective ID for the given NeighborSet.
Saurav Das8a0732e2015-11-20 15:27:53 -0800582 * If the nextObjective does not exist, a new one is created and
Saurav Das4ce45962015-11-24 23:21:05 -0800583 * its id is returned.
sangho1e575652015-05-14 00:39:53 -0700584 *
sanghof9d0bf12015-05-19 11:57:42 -0700585 * @param deviceId Device ID
586 * @param ns NegighborSet
Saurav Das8a0732e2015-11-20 15:27:53 -0800587 * @param meta metadata passed into the creation of a Next Objective
Pier Ventre917127a2016-10-31 16:49:19 -0700588 * @param isBos indicates if it is BoS or not
Saurav Das8a0732e2015-11-20 15:27:53 -0800589 * @return next objective ID or -1 if an error was encountered during the
590 * creation of the nextObjective
sangho1e575652015-05-14 00:39:53 -0700591 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800592 public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns,
Pier Ventre917127a2016-10-31 16:49:19 -0700593 TrafficSelector meta, boolean isBos) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700594 if (groupHandlerMap.get(deviceId) != null) {
595 log.trace("getNextObjectiveId query in device {}", deviceId);
596 return groupHandlerMap
Pier Ventre917127a2016-10-31 16:49:19 -0700597 .get(deviceId).getNextObjectiveId(ns, meta, isBos);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700598 } else {
Saurav Das4ce45962015-11-24 23:21:05 -0800599 log.warn("getNextObjectiveId query - groupHandler for device {} "
600 + "not found", deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700601 return -1;
602 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700603 }
604
Charles Chanc42e84e2015-10-20 16:24:19 -0700605 /**
Pier Ventre917127a2016-10-31 16:49:19 -0700606 * Returns the next objective ID for the given NeighborSet.
607 * If the nextObjective does not exist, a new one is created and
608 * its id is returned.
609 *
610 * @param deviceId Device ID
611 * @param ns NegighborSet
612 * @param meta metadata passed into the creation of a Next Objective
613 * @return next objective ID or -1 if an error was encountered during the
614 * creation of the nextObjective
615 */
616 public int getNextObjectiveId(DeviceId deviceId,
617 NeighborSet ns,
618 TrafficSelector meta) {
619 return this.getNextObjectiveId(deviceId, ns, meta, true);
620 }
621
622 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800623 * Returns the next objective ID for the given subnet prefix. It is expected
624 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700625 *
626 * @param deviceId Device ID
627 * @param prefix Subnet
Saurav Das4ce45962015-11-24 23:21:05 -0800628 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700629 */
630 public int getSubnetNextObjectiveId(DeviceId deviceId, IpPrefix prefix) {
631 if (groupHandlerMap.get(deviceId) != null) {
632 log.trace("getSubnetNextObjectiveId query in device {}", deviceId);
633 return groupHandlerMap
634 .get(deviceId).getSubnetNextObjectiveId(prefix);
635 } else {
Saurav Das4ce45962015-11-24 23:21:05 -0800636 log.warn("getSubnetNextObjectiveId query - groupHandler for "
637 + "device {} not found", deviceId);
638 return -1;
639 }
640 }
641
642 /**
643 * Returns the next objective ID for the given portNumber, given the treatment.
644 * There could be multiple different treatments to the same outport, which
645 * would result in different objectives. If the next object
646 * does not exist, a new one is created and its id is returned.
647 *
648 * @param deviceId Device ID
649 * @param portNum port number on device for which NextObjective is queried
650 * @param treatment the actions to apply on the packets (should include outport)
651 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das59232cf2016-04-27 18:35:50 -0700652 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800653 */
654 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
655 TrafficTreatment treatment,
656 TrafficSelector meta) {
657 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
658 if (ghdlr != null) {
659 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta);
660 } else {
Charles Chane849c192016-01-11 18:28:54 -0800661 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
662 + " not found", deviceId);
663 return -1;
664 }
665 }
666
sanghob35a6192015-04-01 13:05:26 -0700667 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700668 @Override
669 public void process(PacketContext context) {
670
671 if (context.isHandled()) {
672 return;
673 }
674
675 InboundPacket pkt = context.inPacket();
676 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800677
678 if (ethernet == null) {
679 return;
680 }
681
Saurav Das4ce45962015-11-24 23:21:05 -0800682 log.trace("Rcvd pktin: {}", ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800683 if (ethernet.getEtherType() == TYPE_ARP) {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800684 log.warn("{} - we are still receiving ARP packets from {}",
685 context.inPacket().receivedFrom());
686 log.debug("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800687 return;
sanghob35a6192015-04-01 13:05:26 -0700688 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800689 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
690 //ipHandler.addToPacketBuffer(ipv4Packet);
691 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
692 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700693 } else {
Charles Chan50035632017-01-13 17:20:44 -0800694 // NOTE: We don't support IP learning at this moment so this
695 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800696 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700697 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800698 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
699 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -0800700 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -0800701 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -0800702 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
703 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
704 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
705 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
706 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
707 } else {
Pier Luigi7dad71c2017-02-01 13:50:04 -0800708 // XXX Neigbour hacking, to handle the ICMPv6 packet
709 // not under our control
710 if (icmpHandler.handleUPstreamPackets(context)) {
711 log.debug("Rcvd pktin from UpStream: {}", ipv6Packet);
712 } else {
713 log.debug("Received ICMPv6 0x{} - not handled",
714 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
715 }
Pier Ventre735b8c82016-12-02 08:16:05 -0800716 }
717 } else {
718 // NOTE: We don't support IP learning at this moment so this
719 // is not necessary. Also it causes duplication of DHCPv6 packets.
720 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
721 }
sanghob35a6192015-04-01 13:05:26 -0700722 }
723 }
724 }
725
726 private class InternalLinkListener implements LinkListener {
727 @Override
728 public void event(LinkEvent event) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700729 if (event.type() == LinkEvent.Type.LINK_ADDED
730 || event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700731 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700732 scheduleEventHandlerIfNotScheduled(event);
733 }
734 }
735 }
736
737 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -0700738 @Override
739 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -0700740 switch (event.type()) {
741 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -0800742 case PORT_UPDATED:
743 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -0700744 case DEVICE_UPDATED:
745 case DEVICE_AVAILABILITY_CHANGED:
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700746 log.debug("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700747 scheduleEventHandlerIfNotScheduled(event);
748 break;
749 default:
750 }
751 }
752 }
753
Saurav Das4ce45962015-11-24 23:21:05 -0800754 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -0700755 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700756 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700757 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700758 numOfEventsQueued++;
759
760 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
761 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700762 eventHandlerFuture = executorService
763 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
764 numOfHandlerScheduled++;
765 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700766 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700767 numOfEventsQueued,
768 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -0700769 }
sanghob35a6192015-04-01 13:05:26 -0700770 }
771
772 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700773 @Override
sanghob35a6192015-04-01 13:05:26 -0700774 public void run() {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700775 try {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700776 while (true) {
Saurav Das4ce45962015-11-24 23:21:05 -0800777 @SuppressWarnings("rawtypes")
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700778 Event event = null;
779 synchronized (threadSchedulerLock) {
780 if (!eventQueue.isEmpty()) {
781 event = eventQueue.poll();
782 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700783 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700784 numOfHandlerExecution++;
785 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
786 numOfHandlerExecution, numOfEventsExecuted);
787 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700788 }
sangho20eff1d2015-04-13 15:15:58 -0700789 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700790 if (event.type() == LinkEvent.Type.LINK_ADDED) {
791 processLinkAdded((Link) event.subject());
792 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre2c515312016-09-13 21:33:40 -0700793 Link linkRemoved = (Link) event.subject();
794 if (linkRemoved.src().elementId() instanceof DeviceId &&
795 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
796 continue;
797 }
798 if (linkRemoved.dst().elementId() instanceof DeviceId &&
799 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
800 continue;
801 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700802 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700803 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
804 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
805 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800806 DeviceId deviceId = ((Device) event.subject()).id();
807 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700808 log.info("Processing device event {} for available device {}",
809 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700810 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -0700811 } else {
812 log.info("Processing device event {} for unavailable device {}",
813 event.type(), ((Device) event.subject()).id());
814 processDeviceRemoved((Device) event.subject());
815 }
Saurav Das1a129a02016-11-18 15:21:57 -0800816 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800817 // typically these calls come when device is added first time
818 // so port filtering rules are handled at the device_added event.
819 // port added calls represent all ports on the device,
820 // enabled or not.
Saurav Das1a129a02016-11-18 15:21:57 -0800821 log.debug("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800822 ((DeviceEvent) event).subject().id(),
823 ((DeviceEvent) event).port().number(),
824 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -0800825 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800826 // these calls happen for every subsequent event
827 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -0800828 log.info("** PORT UPDATED {}/{} -> {}",
829 event.subject(),
830 ((DeviceEvent) event).port(),
831 event.type());
832 processPortUpdated(((Device) event.subject()),
833 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700834 } else {
835 log.warn("Unhandled event type: {}", event.type());
836 }
sanghob35a6192015-04-01 13:05:26 -0700837 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700838 } catch (Exception e) {
839 log.error("SegmentRouting event handler "
840 + "thread thrown an exception: {}", e);
sanghob35a6192015-04-01 13:05:26 -0700841 }
sanghob35a6192015-04-01 13:05:26 -0700842 }
843 }
844
sanghob35a6192015-04-01 13:05:26 -0700845 private void processLinkAdded(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700846 log.info("** LINK ADDED {}", link.toString());
Charles Chan0b4e6182015-11-03 10:42:14 -0800847 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
848 log.warn("Source device of this link is not configured.");
849 return;
850 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700851 //Irrespective whether the local is a MASTER or not for this device,
852 //create group handler instance and push default TTP flow rules.
853 //Because in a multi-instance setup, instances can initiate
854 //groups for any devices. Also the default TTP rules are needed
855 //to be pushed before inserting any IP table entries for any device
856 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
857 .deviceId());
858 if (groupHandler != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800859 groupHandler.linkUp(link, mastershipService.isLocalMaster(
860 link.src().deviceId()));
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700861 } else {
862 Device device = deviceService.getDevice(link.src().deviceId());
863 if (device != null) {
864 log.warn("processLinkAdded: Link Added "
865 + "Notification without Device Added "
866 + "event, still handling it");
867 processDeviceAdded(device);
868 groupHandler = groupHandlerMap.get(link.src()
869 .deviceId());
Saurav Das8a0732e2015-11-20 15:27:53 -0800870 groupHandler.linkUp(link, mastershipService.isLocalMaster(device.id()));
sanghob35a6192015-04-01 13:05:26 -0700871 }
872 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700873
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700874 log.trace("Starting optimized route population process");
875 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null);
876 //log.trace("processLinkAdded: re-starting route population process");
877 //defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -0700878
879 mcastHandler.init();
sanghob35a6192015-04-01 13:05:26 -0700880 }
881
882 private void processLinkRemoved(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700883 log.info("** LINK REMOVED {}", link.toString());
sangho834e4b02015-05-01 09:38:25 -0700884 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
885 if (groupHandler != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800886 groupHandler.portDown(link.src().port(),
887 mastershipService.isLocalMaster(link.src().deviceId()));
sangho834e4b02015-05-01 09:38:25 -0700888 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700889 log.trace("Starting optimized route population process");
890 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link);
891 //log.trace("processLinkRemoved: re-starting route population process");
892 //defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -0700893
894 mcastHandler.processLinkDown(link);
sanghob35a6192015-04-01 13:05:26 -0700895 }
896
897 private void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700898 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan0b4e6182015-11-03 10:42:14 -0800899 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das2857f382015-11-03 14:39:27 -0800900 log.warn("Device configuration uploading. Device {} will be "
901 + "processed after config completes.", device.id());
902 return;
903 }
Charles Chan2199c302016-04-23 17:36:10 -0700904 processDeviceAddedInternal(device.id());
905 }
906
907 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700908 // Irrespective of whether the local is a MASTER or not for this device,
909 // we need to create a SR-group-handler instance. This is because in a
910 // multi-instance setup, any instance can initiate forwarding/next-objectives
911 // for any switch (even if this instance is a SLAVE or not even connected
912 // to the switch). To handle this, a default-group-handler instance is necessary
913 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -0700914 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
915 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800916 DefaultGroupHandler groupHandler;
917 try {
918 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -0700919 createGroupHandler(deviceId,
920 appId,
921 deviceConfiguration,
922 linkService,
923 flowObjectiveService,
924 this);
Charles Chan0b4e6182015-11-03 10:42:14 -0800925 } catch (DeviceConfigNotFoundException e) {
926 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
927 return;
928 }
Charles Chan2199c302016-04-23 17:36:10 -0700929 log.debug("updating groupHandlerMap with new config for device: {}",
930 deviceId);
931 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -0800932 }
Saurav Dasb5c236e2016-06-07 10:08:06 -0700933 // Also, in some cases, drivers may need extra
934 // information to process rules (eg. Router IP/MAC); and so, we send
935 // port addressing rules to the driver as well irrespective of whether
936 // this instance is the master or not.
937 defaultRoutingHandler.populatePortAddressingRules(deviceId);
938
Charles Chan2199c302016-04-23 17:36:10 -0700939 if (mastershipService.isLocalMaster(deviceId)) {
Charles Chan03a73e02016-10-24 14:52:01 -0700940 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -0700941 xConnectHandler.init(deviceId);
Charles Chan35fd1a72016-06-13 18:54:31 -0700942 cordConfigHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -0700943 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700944 groupHandler.createGroupsFromSubnetConfig();
Charles Chan2199c302016-04-23 17:36:10 -0700945 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700946 }
Charles Chan5270ed02016-01-30 23:22:37 -0800947
Charles Chan03a73e02016-10-24 14:52:01 -0700948 appCfgHandler.init(deviceId);
949 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -0700950 }
951
Saurav Das80980c72016-03-23 11:22:49 -0700952 private void processDeviceRemoved(Device device) {
953 nsNextObjStore.entrySet().stream()
954 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
955 .forEach(entry -> {
956 nsNextObjStore.remove(entry.getKey());
957 });
Saurav Das80980c72016-03-23 11:22:49 -0700958 subnetNextObjStore.entrySet().stream()
959 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
960 .forEach(entry -> {
961 subnetNextObjStore.remove(entry.getKey());
962 });
Saurav Das80980c72016-03-23 11:22:49 -0700963 portNextObjStore.entrySet().stream()
964 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
965 .forEach(entry -> {
966 portNextObjStore.remove(entry.getKey());
967 });
Saurav Das80980c72016-03-23 11:22:49 -0700968 subnetVidStore.entrySet().stream()
969 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
970 .forEach(entry -> {
971 subnetVidStore.remove(entry.getKey());
972 });
Saurav Das80980c72016-03-23 11:22:49 -0700973 groupHandlerMap.remove(device.id());
Saurav Das80980c72016-03-23 11:22:49 -0700974 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan2199c302016-04-23 17:36:10 -0700975 mcastHandler.removeDevice(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -0700976 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -0700977 }
978
Saurav Das1a129a02016-11-18 15:21:57 -0800979 private void processPortUpdated(Device device, Port port) {
980 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
981 log.warn("Device configuration uploading. Not handling port event for"
982 + "dev: {} port: {}", device.id(), port.number());
983 return;
984 }
985 /* XXX create method for single port filtering rules which are needed
986 for both switch-to-switch ports and edge ports
987 if (defaultRoutingHandler != null) {
988 defaultRoutingHandler.populatePortAddressingRules(
989 ((Device) event.subject()).id());
990 }*/
991
992 // portUpdated calls are for ports that have gone down or up. For switch
993 // to switch ports, link-events should take care of any re-routing or
994 // group editing necessary for port up/down. Here we only process edge ports
995 // that are already configured.
Pier Ventre10bd8d12016-11-26 21:05:22 -0800996 Ip4Prefix configuredSubnet = deviceConfiguration.getPortIPv4Subnet(device.id(),
Saurav Das1a129a02016-11-18 15:21:57 -0800997 port.number());
998 if (configuredSubnet == null) {
999 log.debug("Not handling port updated event for unconfigured port "
1000 + "dev/port: {}/{}", device.id(), port.number());
1001 return;
1002 }
1003 processEdgePort(device, port, configuredSubnet);
1004 }
1005
1006 private void processEdgePort(Device device, Port port, Ip4Prefix subnet) {
1007 boolean portUp = port.isEnabled();
1008 if (portUp) {
1009 log.info("Device:EdgePort {}:{} is enabled in subnet: {}", device.id(),
1010 port.number(), subnet);
1011 } else {
1012 log.info("Device:EdgePort {}:{} is disabled in subnet: {}", device.id(),
1013 port.number(), subnet);
1014 }
1015
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001016 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001017 if (groupHandler != null) {
Saurav Das1a129a02016-11-18 15:21:57 -08001018 groupHandler.processEdgePort(port.number(), subnet, portUp,
Saurav Das423fe2b2015-12-04 10:52:59 -08001019 mastershipService.isLocalMaster(device.id()));
Saurav Das1a129a02016-11-18 15:21:57 -08001020 } else {
1021 log.warn("Group handler not found for dev:{}. Not handling edge port"
1022 + " {} event for port:{}", device.id(),
1023 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001024 }
1025 }
sangho1e575652015-05-14 00:39:53 -07001026
Pier Ventre10bd8d12016-11-26 21:05:22 -08001027 /**
1028 * Registers the given connect point with the NRS, this is necessary
1029 * to receive the NDP and ARP packets from the NRS.
1030 *
1031 * @param portToRegister connect point to register
1032 */
1033 public void registerConnectPoint(ConnectPoint portToRegister) {
Pier Ventre10bd8d12016-11-26 21:05:22 -08001034 this.neighbourResolutionService.registerNeighbourHandler(
1035 portToRegister,
1036 neighbourHandler,
1037 appId
1038 );
1039 }
1040
Charles Chand6832882015-10-05 17:50:33 -07001041 private class InternalConfigListener implements NetworkConfigListener {
Charles Chan2c15aca2016-11-09 20:51:44 -08001042 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001043
Charles Chane849c192016-01-11 18:28:54 -08001044 /**
1045 * Constructs the internal network config listener.
1046 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001047 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001048 */
Charles Chan2c15aca2016-11-09 20:51:44 -08001049 public InternalConfigListener(SegmentRoutingManager srManager) {
1050 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001051 }
1052
Charles Chane849c192016-01-11 18:28:54 -08001053 /**
1054 * Reads network config and initializes related data structure accordingly.
1055 */
Charles Chan4636be02015-10-07 14:21:45 -07001056 public void configureNetwork() {
Pier Ventre10bd8d12016-11-26 21:05:22 -08001057
Charles Chan2c15aca2016-11-09 20:51:44 -08001058 deviceConfiguration = new DeviceConfiguration(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001059
Charles Chan2c15aca2016-11-09 20:51:44 -08001060 arpHandler = new ArpHandler(srManager);
1061 icmpHandler = new IcmpHandler(srManager);
1062 ipHandler = new IpHandler(srManager);
1063 routingRulePopulator = new RoutingRulePopulator(srManager);
1064 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001065
1066 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1067 groupHandlerMap, tunnelStore);
1068 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1069 flowObjectiveService,
1070 tunnelHandler, policyStore);
1071
Charles Chan4636be02015-10-07 14:21:45 -07001072 for (Device device : deviceService.getDevices()) {
Charles Chan2199c302016-04-23 17:36:10 -07001073 processDeviceAddedInternal(device.id());
Charles Chan4636be02015-10-07 14:21:45 -07001074 }
1075
1076 defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -07001077 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001078 }
1079
Charles Chand6832882015-10-05 17:50:33 -07001080 @Override
1081 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001082 // TODO move this part to NetworkConfigEventHandler
1083 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1084 switch (event.type()) {
1085 case CONFIG_ADDED:
1086 log.info("Segment Routing Config added.");
1087 configureNetwork();
1088 break;
1089 case CONFIG_UPDATED:
1090 log.info("Segment Routing Config updated.");
1091 // TODO support dynamic configuration
1092 break;
1093 default:
1094 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001095 }
Charles Chan5270ed02016-01-30 23:22:37 -08001096 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001097 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001098 switch (event.type()) {
1099 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001100 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001101 break;
1102 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001103 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001104 break;
1105 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001106 appCfgHandler.processAppConfigRemoved(event);
1107 break;
1108 default:
1109 break;
1110 }
Charles Chan03a73e02016-10-24 14:52:01 -07001111 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001112 } else if (event.configClass().equals(XConnectConfig.class)) {
1113 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1114 switch (event.type()) {
1115 case CONFIG_ADDED:
1116 xConnectHandler.processXConnectConfigAdded(event);
1117 break;
1118 case CONFIG_UPDATED:
1119 xConnectHandler.processXConnectConfigUpdated(event);
1120 break;
1121 case CONFIG_REMOVED:
1122 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001123 break;
1124 default:
1125 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001126 }
Pier Luigi7dad71c2017-02-01 13:50:04 -08001127 // XXX Neighbour hacking. This method is looking for
1128 // the Internet-Router interface. In order to retrieve
1129 // the upstream port.
1130 } else if (event.configClass().equals(InterfaceConfig.class)) {
1131 switch (event.type()) {
1132 case CONFIG_ADDED:
1133 case CONFIG_UPDATED:
Pier Luigi721b6622017-02-03 13:34:21 -08001134 updateUPstreamCP();
Pier Luigi7dad71c2017-02-01 13:50:04 -08001135 case CONFIG_REGISTERED:
1136 case CONFIG_UNREGISTERED:
1137 case CONFIG_REMOVED:
1138 break;
1139 default:
1140 break;
1141 }
1142 // XXX Neighbour hacking. This method is looking for
1143 // the vrouter port.
1144 } else if (event.configClass().equals(RouterConfig.class)) {
1145 switch (event.type()) {
1146 case CONFIG_ADDED:
1147 case CONFIG_UPDATED:
Pier Luigi721b6622017-02-03 13:34:21 -08001148 updateVRouterCP(event);
Pier Luigi7dad71c2017-02-01 13:50:04 -08001149 case CONFIG_REGISTERED:
1150 case CONFIG_UNREGISTERED:
1151 case CONFIG_REMOVED:
1152 break;
1153 default:
1154 break;
1155 }
Charles Chand6832882015-10-05 17:50:33 -07001156 }
1157 }
1158 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001159
Pier Luigi721b6622017-02-03 13:34:21 -08001160 /////////////////////////////////////////////////////////////////
1161 // XXX Neighbour hacking, temporary workaround will be //
1162 // removed as soon as possible, when the bridging will //
1163 // be implemented. For now, it's fine to leave this //
1164 /////////////////////////////////////////////////////////////////
1165
1166 // XXX Neighbour hacking. To store upstream connect
1167 // point and vRouter connect point
1168 ConnectPoint upstreamCP = null;
1169 ConnectPoint vRouterCP = null;
1170
1171 // XXX Neighbour hacking. To update the Upstream CP
1172 public void updateUPstreamCP() {
1173 Set<ConnectPoint> portSubjects = cfgService.getSubjects(ConnectPoint.class, InterfaceConfig.class);
1174 upstreamCP = null;
1175 portSubjects.stream().forEach(subject -> {
1176 InterfaceConfig config = cfgService.getConfig(subject, InterfaceConfig.class);
1177 Set<Interface> networkInterfaces;
1178 try {
1179 networkInterfaces = config.getInterfaces();
1180 } catch (ConfigException e) {
1181 log.error("Error loading port configuration");
1182 return;
1183 }
1184 networkInterfaces.forEach(networkInterface -> {
1185 if (networkInterface.name().equals("internet-router")) {
1186 upstreamCP = subject;
1187 }
1188 });
1189 });
1190
1191 }
1192
1193 // XXX Neighbour hacking. To update the Upstream CP
1194 public void updateVRouterCP(NetworkConfigEvent event) {
1195 RouterConfig config = (RouterConfig) event.config().get();
1196 if (config == null) {
1197 log.warn("Router config not available");
1198 vRouterCP = null;
1199 return;
1200 }
1201 vRouterCP = config.getControlPlaneConnectPoint();
1202 }
1203
Charles Chan68aa62d2015-11-09 16:37:23 -08001204 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001205 @Override
1206 public void event(HostEvent event) {
1207 // Do not proceed without mastership
1208 DeviceId deviceId = event.subject().location().deviceId();
1209 if (!mastershipService.isLocalMaster(deviceId)) {
1210 return;
1211 }
1212
1213 switch (event.type()) {
1214 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001215 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001216 break;
1217 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001218 hostHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001219 break;
1220 case HOST_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001221 hostHandler.processHostRemoveEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001222 break;
1223 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001224 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001225 break;
1226 default:
1227 log.warn("Unsupported host event type: {}", event.type());
1228 break;
1229 }
1230 }
1231 }
1232
Charles Chand55e84d2016-03-30 17:54:24 -07001233 private class InternalMcastListener implements McastListener {
1234 @Override
1235 public void event(McastEvent event) {
1236 switch (event.type()) {
1237 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001238 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001239 break;
1240 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001241 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001242 break;
1243 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001244 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001245 break;
1246 case ROUTE_ADDED:
1247 case ROUTE_REMOVED:
1248 default:
1249 break;
1250 }
1251 }
1252 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001253
1254 private class InternalCordConfigListener implements CordConfigListener {
1255 @Override
1256 public void event(CordConfigEvent event) {
1257 switch (event.type()) {
1258 case ACCESS_AGENT_ADDED:
1259 cordConfigHandler.processAccessAgentAddedEvent(event);
1260 break;
1261 case ACCESS_AGENT_UPDATED:
1262 cordConfigHandler.processAccessAgentUpdatedEvent(event);
1263 break;
1264 case ACCESS_AGENT_REMOVED:
1265 cordConfigHandler.processAccessAgentRemovedEvent(event);
1266 break;
1267 case ACCESS_DEVICE_ADDED:
1268 case ACCESS_DEVICE_UPDATED:
1269 case ACCESS_DEVICE_REMOVED:
1270 default:
1271 break;
1272 }
1273 }
1274 }
Charles Chan03a73e02016-10-24 14:52:01 -07001275
1276 private class InternalRouteEventListener implements RouteListener {
1277 @Override
1278 public void event(RouteEvent event) {
1279 switch (event.type()) {
1280 case ROUTE_ADDED:
1281 routeHandler.processRouteAdded(event);
1282 break;
1283 case ROUTE_UPDATED:
1284 routeHandler.processRouteUpdated(event);
1285 break;
1286 case ROUTE_REMOVED:
1287 routeHandler.processRouteRemoved(event);
1288 break;
1289 default:
1290 break;
1291 }
1292 }
1293 }
sanghob35a6192015-04-01 13:05:26 -07001294}