blob: fc9304a646ca56ade6396cc8bed96ff7f04fd804 [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");
Saurav Das80980c72016-03-23 11:22:49 -0700356
Charles Chanb8e10c82015-10-14 11:24:40 -0700357 processor = new InternalPacketProcessor();
358 linkListener = new InternalLinkListener();
359 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700360 appCfgHandler = new AppConfigHandler(this);
361 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700362 mcastHandler = new McastHandler(this);
363 hostHandler = new HostHandler(this);
Charles Chan35fd1a72016-06-13 18:54:31 -0700364 cordConfigHandler = new CordConfigHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700365 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800366 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700367
Charles Chan3e783d02016-02-26 22:19:52 -0800368 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700369 cfgService.registerConfigFactory(deviceConfigFactory);
370 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700371 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700372 cfgService.registerConfigFactory(mcastConfigFactory);
Charles Chan5270ed02016-01-30 23:22:37 -0800373 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700374 packetService.addProcessor(processor, PacketProcessor.director(2));
375 linkService.addListener(linkListener);
376 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700377 multicastRouteService.addListener(mcastListener);
Charles Chan35fd1a72016-06-13 18:54:31 -0700378 cordConfigService.addListener(cordConfigListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700379
380 cfgListener.configureNetwork();
381
Charles Chan03a73e02016-10-24 14:52:01 -0700382 routeService.addListener(routeListener);
383
sanghob35a6192015-04-01 13:05:26 -0700384 log.info("Started");
385 }
386
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700387 private KryoNamespace.Builder createSerializer() {
388 return new KryoNamespace.Builder()
389 .register(KryoNamespaces.API)
390 .register(NeighborSetNextObjectiveStoreKey.class,
391 SubnetNextObjectiveStoreKey.class,
392 SubnetAssignedVidStoreKey.class,
393 NeighborSet.class,
394 Tunnel.class,
395 DefaultTunnel.class,
396 Policy.class,
397 TunnelPolicy.class,
398 Policy.Type.class,
399 PortNextObjectiveStoreKey.class,
Charles Chanfc5c7802016-05-17 13:13:55 -0700400 XConnectStoreKey.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700401 );
402 }
403
sanghob35a6192015-04-01 13:05:26 -0700404 @Deactivate
405 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700406 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700407 cfgService.unregisterConfigFactory(deviceConfigFactory);
408 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700409 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700410 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chand6832882015-10-05 17:50:33 -0700411
sanghob35a6192015-04-01 13:05:26 -0700412 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700413 linkService.removeListener(linkListener);
414 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700415 multicastRouteService.removeListener(mcastListener);
Charles Chan35fd1a72016-06-13 18:54:31 -0700416 cordConfigService.removeListener(cordConfigListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700417 routeService.removeListener(routeListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700418
sanghob35a6192015-04-01 13:05:26 -0700419 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700420 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700421 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700422 groupHandlerMap.clear();
423
Charles Chand55e84d2016-03-30 17:54:24 -0700424 nsNextObjStore.destroy();
425 subnetNextObjStore.destroy();
426 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700427 tunnelStore.destroy();
428 policyStore.destroy();
429 subnetVidStore.destroy();
sanghob35a6192015-04-01 13:05:26 -0700430 log.info("Stopped");
431 }
432
sangho1e575652015-05-14 00:39:53 -0700433 @Override
434 public List<Tunnel> getTunnels() {
435 return tunnelHandler.getTunnels();
436 }
437
438 @Override
sangho71abe1b2015-06-29 14:58:47 -0700439 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
440 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700441 }
442
443 @Override
sangho71abe1b2015-06-29 14:58:47 -0700444 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700445 for (Policy policy: policyHandler.getPolicies()) {
446 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
447 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
448 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
449 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700450 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700451 }
452 }
453 }
sangho71abe1b2015-06-29 14:58:47 -0700454 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700455 }
456
457 @Override
sangho71abe1b2015-06-29 14:58:47 -0700458 public PolicyHandler.Result removePolicy(Policy policy) {
459 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700460 }
461
462 @Override
sangho71abe1b2015-06-29 14:58:47 -0700463 public PolicyHandler.Result createPolicy(Policy policy) {
464 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700465 }
466
467 @Override
468 public List<Policy> getPolicies() {
469 return policyHandler.getPolicies();
470 }
471
Saurav Das59232cf2016-04-27 18:35:50 -0700472 @Override
473 public void rerouteNetwork() {
474 cfgListener.configureNetwork();
475 for (Device device : deviceService.getDevices()) {
476 defaultRoutingHandler.populatePortAddressingRules(device.id());
477 }
478 defaultRoutingHandler.startPopulationProcess();
479 }
480
Charles Chanc81c45b2016-10-20 17:02:44 -0700481 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800482 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
483 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Charles Chanc81c45b2016-10-20 17:02:44 -0700484 deviceService.getAvailableDevices().forEach(device -> {
485 deviceSubnetMap.put(device.id(), deviceConfiguration.getSubnets(device.id()));
486 });
487 return deviceSubnetMap;
488 }
489
sanghof9d0bf12015-05-19 11:57:42 -0700490 /**
Pier Ventre98161782016-10-31 15:00:01 -0700491 * Returns the MPLS-ECMP configuration.
492 *
493 * @return MPLS-ECMP value
494 */
495 public boolean getMplsEcmp() {
496 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
497 .getConfig(this.appId, SegmentRoutingAppConfig.class);
498 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
499 }
500
501 /**
sanghof9d0bf12015-05-19 11:57:42 -0700502 * Returns the tunnel object with the tunnel ID.
503 *
504 * @param tunnelId Tunnel ID
505 * @return Tunnel reference
506 */
sangho1e575652015-05-14 00:39:53 -0700507 public Tunnel getTunnel(String tunnelId) {
508 return tunnelHandler.getTunnel(tunnelId);
509 }
510
sanghob35a6192015-04-01 13:05:26 -0700511 /**
Saurav Das0e99e2b2015-10-28 12:39:42 -0700512 * Returns the vlan-id assigned to the subnet configured for a device.
513 * If no vlan-id has been assigned, a new one is assigned out of a pool of ids,
514 * if and only if this controller instance is the master for the device.
515 * <p>
516 * USAGE: The assigned vlans are meant to be applied to untagged packets on those
517 * switches/pipelines that need this functionality. These vids are meant
518 * to be used internally within a switch, and thus need to be unique only
519 * on a switch level. Note that packets never go out on the wire with these
520 * vlans. Currently, vlan ids are assigned from value 4093 down.
521 * Vlan id 4094 expected to be used for all ports that are not assigned subnets.
522 * Vlan id 4095 is reserved and unused. Only a single vlan id is assigned
523 * per subnet.
sanghob35a6192015-04-01 13:05:26 -0700524 *
Saurav Das0e99e2b2015-10-28 12:39:42 -0700525 * @param deviceId switch dpid
Pier Ventref4b5fce2016-11-28 16:48:06 -0800526 * @param subnet IP prefix for which assigned vlan is desired
Saurav Das0e99e2b2015-10-28 12:39:42 -0700527 * @return VlanId assigned for the subnet on the device, or
528 * null if no vlan assignment was found and this instance is not
529 * the master for the device.
sanghob35a6192015-04-01 13:05:26 -0700530 */
Charles Chane849c192016-01-11 18:28:54 -0800531 // TODO: We should avoid assigning VLAN IDs that are used by VLAN cross-connection.
Pier Ventre10bd8d12016-11-26 21:05:22 -0800532 public VlanId getSubnetAssignedVlanId(DeviceId deviceId, IpPrefix subnet) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700533 VlanId assignedVid = subnetVidStore.get(new SubnetAssignedVidStoreKey(
534 deviceId, subnet));
535 if (assignedVid != null) {
536 log.debug("Query for subnet:{} on device:{} returned assigned-vlan "
537 + "{}", subnet, deviceId, assignedVid);
538 return assignedVid;
539 }
540 //check mastership for the right to assign a vlan
541 if (!mastershipService.isLocalMaster(deviceId)) {
542 log.warn("This controller instance is not the master for device {}. "
543 + "Cannot assign vlan-id for subnet {}", deviceId, subnet);
544 return null;
545 }
546 // vlan assignment is expensive but done only once
Pier Ventre10bd8d12016-11-26 21:05:22 -0800547 // FIXME for now we will do assignment considering only the ipv4 subnet.
548 Set<Ip4Prefix> configuredSubnets = deviceConfiguration.getSubnets(deviceId)
549 .stream()
550 .filter(IpPrefix::isIp4)
551 .map(IpPrefix::getIp4Prefix)
552 .collect(Collectors.toSet());
Saurav Das0e99e2b2015-10-28 12:39:42 -0700553 Set<Short> assignedVlans = new HashSet<>();
554 Set<Ip4Prefix> unassignedSubnets = new HashSet<>();
555 for (Ip4Prefix sub : configuredSubnets) {
556 VlanId v = subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId,
557 sub));
558 if (v != null) {
559 assignedVlans.add(v.toShort());
560 } else {
561 unassignedSubnets.add(sub);
562 }
563 }
564 short nextAssignedVlan = ASSIGNED_VLAN_START;
565 if (!assignedVlans.isEmpty()) {
566 nextAssignedVlan = (short) (Collections.min(assignedVlans) - 1);
567 }
568 for (Ip4Prefix unsub : unassignedSubnets) {
Pier Ventref4b5fce2016-11-28 16:48:06 -0800569 subnetVidStore.put(new SubnetAssignedVidStoreKey(deviceId, unsub),
570 VlanId.vlanId(nextAssignedVlan--));
571 log.info("Assigned vlan: {} to subnet: {} on device: {}",
572 nextAssignedVlan + 1, unsub, deviceId);
sanghob35a6192015-04-01 13:05:26 -0700573 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700574 return subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, subnet));
sanghob35a6192015-04-01 13:05:26 -0700575 }
576
sangho1e575652015-05-14 00:39:53 -0700577 /**
Saurav Das0e99e2b2015-10-28 12:39:42 -0700578 * Returns the next objective ID for the given NeighborSet.
Saurav Das8a0732e2015-11-20 15:27:53 -0800579 * If the nextObjective does not exist, a new one is created and
Saurav Das4ce45962015-11-24 23:21:05 -0800580 * its id is returned.
sangho1e575652015-05-14 00:39:53 -0700581 *
sanghof9d0bf12015-05-19 11:57:42 -0700582 * @param deviceId Device ID
583 * @param ns NegighborSet
Saurav Das8a0732e2015-11-20 15:27:53 -0800584 * @param meta metadata passed into the creation of a Next Objective
585 * @return next objective ID or -1 if an error was encountered during the
586 * creation of the nextObjective
sangho1e575652015-05-14 00:39:53 -0700587 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800588 public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns,
589 TrafficSelector meta) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700590 if (groupHandlerMap.get(deviceId) != null) {
591 log.trace("getNextObjectiveId query in device {}", deviceId);
592 return groupHandlerMap
Saurav Das8a0732e2015-11-20 15:27:53 -0800593 .get(deviceId).getNextObjectiveId(ns, meta);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700594 } else {
Saurav Das4ce45962015-11-24 23:21:05 -0800595 log.warn("getNextObjectiveId query - groupHandler for device {} "
596 + "not found", deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700597 return -1;
598 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700599 }
600
Charles Chanc42e84e2015-10-20 16:24:19 -0700601 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800602 * Returns the next objective ID for the given subnet prefix. It is expected
603 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700604 *
605 * @param deviceId Device ID
606 * @param prefix Subnet
Saurav Das4ce45962015-11-24 23:21:05 -0800607 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700608 */
609 public int getSubnetNextObjectiveId(DeviceId deviceId, IpPrefix prefix) {
610 if (groupHandlerMap.get(deviceId) != null) {
611 log.trace("getSubnetNextObjectiveId query in device {}", deviceId);
612 return groupHandlerMap
613 .get(deviceId).getSubnetNextObjectiveId(prefix);
614 } else {
Saurav Das4ce45962015-11-24 23:21:05 -0800615 log.warn("getSubnetNextObjectiveId query - groupHandler for "
616 + "device {} not found", deviceId);
617 return -1;
618 }
619 }
620
621 /**
622 * Returns the next objective ID for the given portNumber, given the treatment.
623 * There could be multiple different treatments to the same outport, which
624 * would result in different objectives. If the next object
625 * does not exist, a new one is created and its id is returned.
626 *
627 * @param deviceId Device ID
628 * @param portNum port number on device for which NextObjective is queried
629 * @param treatment the actions to apply on the packets (should include outport)
630 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das59232cf2016-04-27 18:35:50 -0700631 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800632 */
633 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
634 TrafficTreatment treatment,
635 TrafficSelector meta) {
636 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
637 if (ghdlr != null) {
638 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta);
639 } else {
Charles Chane849c192016-01-11 18:28:54 -0800640 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
641 + " not found", deviceId);
642 return -1;
643 }
644 }
645
sanghob35a6192015-04-01 13:05:26 -0700646 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700647 @Override
648 public void process(PacketContext context) {
649
650 if (context.isHandled()) {
651 return;
652 }
653
654 InboundPacket pkt = context.inPacket();
655 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800656
657 if (ethernet == null) {
658 return;
659 }
660
Saurav Das4ce45962015-11-24 23:21:05 -0800661 log.trace("Rcvd pktin: {}", ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800662 if (ethernet.getEtherType() == TYPE_ARP) {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800663 log.warn("{} - we are still receiving ARP packets from {}",
664 context.inPacket().receivedFrom());
665 log.debug("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800666 return;
sanghob35a6192015-04-01 13:05:26 -0700667 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800668 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
669 //ipHandler.addToPacketBuffer(ipv4Packet);
670 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
671 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700672 } else {
Charles Chan50035632017-01-13 17:20:44 -0800673 // NOTE: We don't support IP learning at this moment so this
674 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800675 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700676 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800677 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
678 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -0800679 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -0800680 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -0800681 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
682 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
683 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
684 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
685 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
686 } else {
Pier Luigi7dad71c2017-02-01 13:50:04 -0800687 // XXX Neigbour hacking, to handle the ICMPv6 packet
688 // not under our control
689 if (icmpHandler.handleUPstreamPackets(context)) {
690 log.debug("Rcvd pktin from UpStream: {}", ipv6Packet);
691 } else {
692 log.debug("Received ICMPv6 0x{} - not handled",
693 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
694 }
Pier Ventre735b8c82016-12-02 08:16:05 -0800695 }
696 } else {
697 // NOTE: We don't support IP learning at this moment so this
698 // is not necessary. Also it causes duplication of DHCPv6 packets.
699 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
700 }
sanghob35a6192015-04-01 13:05:26 -0700701 }
702 }
703 }
704
705 private class InternalLinkListener implements LinkListener {
706 @Override
707 public void event(LinkEvent event) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700708 if (event.type() == LinkEvent.Type.LINK_ADDED
709 || event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700710 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700711 scheduleEventHandlerIfNotScheduled(event);
712 }
713 }
714 }
715
716 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -0700717 @Override
718 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -0700719 switch (event.type()) {
720 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -0800721 case PORT_UPDATED:
722 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -0700723 case DEVICE_UPDATED:
724 case DEVICE_AVAILABILITY_CHANGED:
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700725 log.debug("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700726 scheduleEventHandlerIfNotScheduled(event);
727 break;
728 default:
729 }
730 }
731 }
732
Saurav Das4ce45962015-11-24 23:21:05 -0800733 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -0700734 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700735 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700736 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700737 numOfEventsQueued++;
738
739 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
740 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700741 eventHandlerFuture = executorService
742 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
743 numOfHandlerScheduled++;
744 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700745 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700746 numOfEventsQueued,
747 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -0700748 }
sanghob35a6192015-04-01 13:05:26 -0700749 }
750
751 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700752 @Override
sanghob35a6192015-04-01 13:05:26 -0700753 public void run() {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700754 try {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700755 while (true) {
Saurav Das4ce45962015-11-24 23:21:05 -0800756 @SuppressWarnings("rawtypes")
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700757 Event event = null;
758 synchronized (threadSchedulerLock) {
759 if (!eventQueue.isEmpty()) {
760 event = eventQueue.poll();
761 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700762 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700763 numOfHandlerExecution++;
764 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
765 numOfHandlerExecution, numOfEventsExecuted);
766 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700767 }
sangho20eff1d2015-04-13 15:15:58 -0700768 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700769 if (event.type() == LinkEvent.Type.LINK_ADDED) {
770 processLinkAdded((Link) event.subject());
771 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre2c515312016-09-13 21:33:40 -0700772 Link linkRemoved = (Link) event.subject();
773 if (linkRemoved.src().elementId() instanceof DeviceId &&
774 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
775 continue;
776 }
777 if (linkRemoved.dst().elementId() instanceof DeviceId &&
778 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
779 continue;
780 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700781 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700782 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
783 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
784 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800785 DeviceId deviceId = ((Device) event.subject()).id();
786 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700787 log.info("Processing device event {} for available device {}",
788 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700789 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -0700790 } else {
791 log.info("Processing device event {} for unavailable device {}",
792 event.type(), ((Device) event.subject()).id());
793 processDeviceRemoved((Device) event.subject());
794 }
Saurav Das1a129a02016-11-18 15:21:57 -0800795 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800796 // typically these calls come when device is added first time
797 // so port filtering rules are handled at the device_added event.
798 // port added calls represent all ports on the device,
799 // enabled or not.
Saurav Das1a129a02016-11-18 15:21:57 -0800800 log.debug("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800801 ((DeviceEvent) event).subject().id(),
802 ((DeviceEvent) event).port().number(),
803 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -0800804 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800805 // these calls happen for every subsequent event
806 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -0800807 log.info("** PORT UPDATED {}/{} -> {}",
808 event.subject(),
809 ((DeviceEvent) event).port(),
810 event.type());
811 processPortUpdated(((Device) event.subject()),
812 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700813 } else {
814 log.warn("Unhandled event type: {}", event.type());
815 }
sanghob35a6192015-04-01 13:05:26 -0700816 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700817 } catch (Exception e) {
818 log.error("SegmentRouting event handler "
819 + "thread thrown an exception: {}", e);
sanghob35a6192015-04-01 13:05:26 -0700820 }
sanghob35a6192015-04-01 13:05:26 -0700821 }
822 }
823
sanghob35a6192015-04-01 13:05:26 -0700824 private void processLinkAdded(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700825 log.info("** LINK ADDED {}", link.toString());
Charles Chan0b4e6182015-11-03 10:42:14 -0800826 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
827 log.warn("Source device of this link is not configured.");
828 return;
829 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700830 //Irrespective whether the local is a MASTER or not for this device,
831 //create group handler instance and push default TTP flow rules.
832 //Because in a multi-instance setup, instances can initiate
833 //groups for any devices. Also the default TTP rules are needed
834 //to be pushed before inserting any IP table entries for any device
835 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
836 .deviceId());
837 if (groupHandler != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800838 groupHandler.linkUp(link, mastershipService.isLocalMaster(
839 link.src().deviceId()));
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700840 } else {
841 Device device = deviceService.getDevice(link.src().deviceId());
842 if (device != null) {
843 log.warn("processLinkAdded: Link Added "
844 + "Notification without Device Added "
845 + "event, still handling it");
846 processDeviceAdded(device);
847 groupHandler = groupHandlerMap.get(link.src()
848 .deviceId());
Saurav Das8a0732e2015-11-20 15:27:53 -0800849 groupHandler.linkUp(link, mastershipService.isLocalMaster(device.id()));
sanghob35a6192015-04-01 13:05:26 -0700850 }
851 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700852
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700853 log.trace("Starting optimized route population process");
854 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null);
855 //log.trace("processLinkAdded: re-starting route population process");
856 //defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -0700857
858 mcastHandler.init();
sanghob35a6192015-04-01 13:05:26 -0700859 }
860
861 private void processLinkRemoved(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700862 log.info("** LINK REMOVED {}", link.toString());
sangho834e4b02015-05-01 09:38:25 -0700863 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
864 if (groupHandler != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800865 groupHandler.portDown(link.src().port(),
866 mastershipService.isLocalMaster(link.src().deviceId()));
sangho834e4b02015-05-01 09:38:25 -0700867 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700868 log.trace("Starting optimized route population process");
869 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link);
870 //log.trace("processLinkRemoved: re-starting route population process");
871 //defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -0700872
873 mcastHandler.processLinkDown(link);
sanghob35a6192015-04-01 13:05:26 -0700874 }
875
876 private void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700877 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan0b4e6182015-11-03 10:42:14 -0800878 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das2857f382015-11-03 14:39:27 -0800879 log.warn("Device configuration uploading. Device {} will be "
880 + "processed after config completes.", device.id());
881 return;
882 }
Charles Chan2199c302016-04-23 17:36:10 -0700883 processDeviceAddedInternal(device.id());
884 }
885
886 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700887 // Irrespective of whether the local is a MASTER or not for this device,
888 // we need to create a SR-group-handler instance. This is because in a
889 // multi-instance setup, any instance can initiate forwarding/next-objectives
890 // for any switch (even if this instance is a SLAVE or not even connected
891 // to the switch). To handle this, a default-group-handler instance is necessary
892 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -0700893 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
894 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800895 DefaultGroupHandler groupHandler;
896 try {
897 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -0700898 createGroupHandler(deviceId,
899 appId,
900 deviceConfiguration,
901 linkService,
902 flowObjectiveService,
903 this);
Charles Chan0b4e6182015-11-03 10:42:14 -0800904 } catch (DeviceConfigNotFoundException e) {
905 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
906 return;
907 }
Charles Chan2199c302016-04-23 17:36:10 -0700908 log.debug("updating groupHandlerMap with new config for device: {}",
909 deviceId);
910 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -0800911 }
Saurav Dasb5c236e2016-06-07 10:08:06 -0700912 // Also, in some cases, drivers may need extra
913 // information to process rules (eg. Router IP/MAC); and so, we send
914 // port addressing rules to the driver as well irrespective of whether
915 // this instance is the master or not.
916 defaultRoutingHandler.populatePortAddressingRules(deviceId);
917
Charles Chan2199c302016-04-23 17:36:10 -0700918 if (mastershipService.isLocalMaster(deviceId)) {
Charles Chan03a73e02016-10-24 14:52:01 -0700919 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -0700920 xConnectHandler.init(deviceId);
Charles Chan35fd1a72016-06-13 18:54:31 -0700921 cordConfigHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -0700922 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700923 groupHandler.createGroupsFromSubnetConfig();
Charles Chan2199c302016-04-23 17:36:10 -0700924 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700925 }
Charles Chan5270ed02016-01-30 23:22:37 -0800926
Charles Chan03a73e02016-10-24 14:52:01 -0700927 appCfgHandler.init(deviceId);
928 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -0700929 }
930
Saurav Das80980c72016-03-23 11:22:49 -0700931 private void processDeviceRemoved(Device device) {
932 nsNextObjStore.entrySet().stream()
933 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
934 .forEach(entry -> {
935 nsNextObjStore.remove(entry.getKey());
936 });
Saurav Das80980c72016-03-23 11:22:49 -0700937 subnetNextObjStore.entrySet().stream()
938 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
939 .forEach(entry -> {
940 subnetNextObjStore.remove(entry.getKey());
941 });
Saurav Das80980c72016-03-23 11:22:49 -0700942 portNextObjStore.entrySet().stream()
943 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
944 .forEach(entry -> {
945 portNextObjStore.remove(entry.getKey());
946 });
Saurav Das80980c72016-03-23 11:22:49 -0700947 subnetVidStore.entrySet().stream()
948 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
949 .forEach(entry -> {
950 subnetVidStore.remove(entry.getKey());
951 });
Saurav Das80980c72016-03-23 11:22:49 -0700952 groupHandlerMap.remove(device.id());
Saurav Das80980c72016-03-23 11:22:49 -0700953 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan2199c302016-04-23 17:36:10 -0700954 mcastHandler.removeDevice(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -0700955 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -0700956 }
957
Saurav Das1a129a02016-11-18 15:21:57 -0800958 private void processPortUpdated(Device device, Port port) {
959 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
960 log.warn("Device configuration uploading. Not handling port event for"
961 + "dev: {} port: {}", device.id(), port.number());
962 return;
963 }
964 /* XXX create method for single port filtering rules which are needed
965 for both switch-to-switch ports and edge ports
966 if (defaultRoutingHandler != null) {
967 defaultRoutingHandler.populatePortAddressingRules(
968 ((Device) event.subject()).id());
969 }*/
970
971 // portUpdated calls are for ports that have gone down or up. For switch
972 // to switch ports, link-events should take care of any re-routing or
973 // group editing necessary for port up/down. Here we only process edge ports
974 // that are already configured.
Pier Ventre10bd8d12016-11-26 21:05:22 -0800975 Ip4Prefix configuredSubnet = deviceConfiguration.getPortIPv4Subnet(device.id(),
Saurav Das1a129a02016-11-18 15:21:57 -0800976 port.number());
977 if (configuredSubnet == null) {
978 log.debug("Not handling port updated event for unconfigured port "
979 + "dev/port: {}/{}", device.id(), port.number());
980 return;
981 }
982 processEdgePort(device, port, configuredSubnet);
983 }
984
985 private void processEdgePort(Device device, Port port, Ip4Prefix subnet) {
986 boolean portUp = port.isEnabled();
987 if (portUp) {
988 log.info("Device:EdgePort {}:{} is enabled in subnet: {}", device.id(),
989 port.number(), subnet);
990 } else {
991 log.info("Device:EdgePort {}:{} is disabled in subnet: {}", device.id(),
992 port.number(), subnet);
993 }
994
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700995 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -0700996 if (groupHandler != null) {
Saurav Das1a129a02016-11-18 15:21:57 -0800997 groupHandler.processEdgePort(port.number(), subnet, portUp,
Saurav Das423fe2b2015-12-04 10:52:59 -0800998 mastershipService.isLocalMaster(device.id()));
Saurav Das1a129a02016-11-18 15:21:57 -0800999 } else {
1000 log.warn("Group handler not found for dev:{}. Not handling edge port"
1001 + " {} event for port:{}", device.id(),
1002 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001003 }
1004 }
sangho1e575652015-05-14 00:39:53 -07001005
Pier Ventre10bd8d12016-11-26 21:05:22 -08001006 /**
1007 * Registers the given connect point with the NRS, this is necessary
1008 * to receive the NDP and ARP packets from the NRS.
1009 *
1010 * @param portToRegister connect point to register
1011 */
1012 public void registerConnectPoint(ConnectPoint portToRegister) {
Pier Ventre10bd8d12016-11-26 21:05:22 -08001013 this.neighbourResolutionService.registerNeighbourHandler(
1014 portToRegister,
1015 neighbourHandler,
1016 appId
1017 );
1018 }
1019
Charles Chand6832882015-10-05 17:50:33 -07001020 private class InternalConfigListener implements NetworkConfigListener {
Charles Chan2c15aca2016-11-09 20:51:44 -08001021 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001022
Charles Chane849c192016-01-11 18:28:54 -08001023 /**
1024 * Constructs the internal network config listener.
1025 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001026 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001027 */
Charles Chan2c15aca2016-11-09 20:51:44 -08001028 public InternalConfigListener(SegmentRoutingManager srManager) {
1029 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001030 }
1031
Charles Chane849c192016-01-11 18:28:54 -08001032 /**
1033 * Reads network config and initializes related data structure accordingly.
1034 */
Charles Chan4636be02015-10-07 14:21:45 -07001035 public void configureNetwork() {
Pier Ventre10bd8d12016-11-26 21:05:22 -08001036
Charles Chan2c15aca2016-11-09 20:51:44 -08001037 deviceConfiguration = new DeviceConfiguration(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001038
Charles Chan2c15aca2016-11-09 20:51:44 -08001039 arpHandler = new ArpHandler(srManager);
1040 icmpHandler = new IcmpHandler(srManager);
1041 ipHandler = new IpHandler(srManager);
1042 routingRulePopulator = new RoutingRulePopulator(srManager);
1043 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001044
1045 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1046 groupHandlerMap, tunnelStore);
1047 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1048 flowObjectiveService,
1049 tunnelHandler, policyStore);
1050
Charles Chan4636be02015-10-07 14:21:45 -07001051 for (Device device : deviceService.getDevices()) {
Charles Chan2199c302016-04-23 17:36:10 -07001052 processDeviceAddedInternal(device.id());
Charles Chan4636be02015-10-07 14:21:45 -07001053 }
1054
1055 defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -07001056 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001057 }
1058
Charles Chand6832882015-10-05 17:50:33 -07001059 @Override
1060 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001061 // TODO move this part to NetworkConfigEventHandler
1062 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1063 switch (event.type()) {
1064 case CONFIG_ADDED:
1065 log.info("Segment Routing Config added.");
1066 configureNetwork();
1067 break;
1068 case CONFIG_UPDATED:
1069 log.info("Segment Routing Config updated.");
1070 // TODO support dynamic configuration
1071 break;
1072 default:
1073 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001074 }
Charles Chan5270ed02016-01-30 23:22:37 -08001075 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001076 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001077 switch (event.type()) {
1078 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001079 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001080 break;
1081 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001082 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001083 break;
1084 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001085 appCfgHandler.processAppConfigRemoved(event);
1086 break;
1087 default:
1088 break;
1089 }
Charles Chan03a73e02016-10-24 14:52:01 -07001090 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001091 } else if (event.configClass().equals(XConnectConfig.class)) {
1092 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1093 switch (event.type()) {
1094 case CONFIG_ADDED:
1095 xConnectHandler.processXConnectConfigAdded(event);
1096 break;
1097 case CONFIG_UPDATED:
1098 xConnectHandler.processXConnectConfigUpdated(event);
1099 break;
1100 case CONFIG_REMOVED:
1101 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001102 break;
1103 default:
1104 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001105 }
Pier Luigi7dad71c2017-02-01 13:50:04 -08001106 // XXX Neighbour hacking. This method is looking for
1107 // the Internet-Router interface. In order to retrieve
1108 // the upstream port.
1109 } else if (event.configClass().equals(InterfaceConfig.class)) {
1110 switch (event.type()) {
1111 case CONFIG_ADDED:
1112 case CONFIG_UPDATED:
Pier Luigi721b6622017-02-03 13:34:21 -08001113 updateUPstreamCP();
Pier Luigi7dad71c2017-02-01 13:50:04 -08001114 case CONFIG_REGISTERED:
1115 case CONFIG_UNREGISTERED:
1116 case CONFIG_REMOVED:
1117 break;
1118 default:
1119 break;
1120 }
1121 // XXX Neighbour hacking. This method is looking for
1122 // the vrouter port.
1123 } else if (event.configClass().equals(RouterConfig.class)) {
1124 switch (event.type()) {
1125 case CONFIG_ADDED:
1126 case CONFIG_UPDATED:
Pier Luigi721b6622017-02-03 13:34:21 -08001127 updateVRouterCP(event);
Pier Luigi7dad71c2017-02-01 13:50:04 -08001128 case CONFIG_REGISTERED:
1129 case CONFIG_UNREGISTERED:
1130 case CONFIG_REMOVED:
1131 break;
1132 default:
1133 break;
1134 }
Charles Chand6832882015-10-05 17:50:33 -07001135 }
1136 }
1137 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001138
Pier Luigi721b6622017-02-03 13:34:21 -08001139 /////////////////////////////////////////////////////////////////
1140 // XXX Neighbour hacking, temporary workaround will be //
1141 // removed as soon as possible, when the bridging will //
1142 // be implemented. For now, it's fine to leave this //
1143 /////////////////////////////////////////////////////////////////
1144
1145 // XXX Neighbour hacking. To store upstream connect
1146 // point and vRouter connect point
1147 ConnectPoint upstreamCP = null;
1148 ConnectPoint vRouterCP = null;
1149
1150 // XXX Neighbour hacking. To update the Upstream CP
1151 public void updateUPstreamCP() {
1152 Set<ConnectPoint> portSubjects = cfgService.getSubjects(ConnectPoint.class, InterfaceConfig.class);
1153 upstreamCP = null;
1154 portSubjects.stream().forEach(subject -> {
1155 InterfaceConfig config = cfgService.getConfig(subject, InterfaceConfig.class);
1156 Set<Interface> networkInterfaces;
1157 try {
1158 networkInterfaces = config.getInterfaces();
1159 } catch (ConfigException e) {
1160 log.error("Error loading port configuration");
1161 return;
1162 }
1163 networkInterfaces.forEach(networkInterface -> {
1164 if (networkInterface.name().equals("internet-router")) {
1165 upstreamCP = subject;
1166 }
1167 });
1168 });
1169
1170 }
1171
1172 // XXX Neighbour hacking. To update the Upstream CP
1173 public void updateVRouterCP(NetworkConfigEvent event) {
1174 RouterConfig config = (RouterConfig) event.config().get();
1175 if (config == null) {
1176 log.warn("Router config not available");
1177 vRouterCP = null;
1178 return;
1179 }
1180 vRouterCP = config.getControlPlaneConnectPoint();
1181 }
1182
Charles Chan68aa62d2015-11-09 16:37:23 -08001183 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001184 @Override
1185 public void event(HostEvent event) {
1186 // Do not proceed without mastership
1187 DeviceId deviceId = event.subject().location().deviceId();
1188 if (!mastershipService.isLocalMaster(deviceId)) {
1189 return;
1190 }
1191
1192 switch (event.type()) {
1193 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001194 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001195 break;
1196 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001197 hostHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001198 break;
1199 case HOST_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001200 hostHandler.processHostRemoveEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001201 break;
1202 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001203 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001204 break;
1205 default:
1206 log.warn("Unsupported host event type: {}", event.type());
1207 break;
1208 }
1209 }
1210 }
1211
Charles Chand55e84d2016-03-30 17:54:24 -07001212 private class InternalMcastListener implements McastListener {
1213 @Override
1214 public void event(McastEvent event) {
1215 switch (event.type()) {
1216 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001217 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001218 break;
1219 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001220 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001221 break;
1222 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001223 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001224 break;
1225 case ROUTE_ADDED:
1226 case ROUTE_REMOVED:
1227 default:
1228 break;
1229 }
1230 }
1231 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001232
1233 private class InternalCordConfigListener implements CordConfigListener {
1234 @Override
1235 public void event(CordConfigEvent event) {
1236 switch (event.type()) {
1237 case ACCESS_AGENT_ADDED:
1238 cordConfigHandler.processAccessAgentAddedEvent(event);
1239 break;
1240 case ACCESS_AGENT_UPDATED:
1241 cordConfigHandler.processAccessAgentUpdatedEvent(event);
1242 break;
1243 case ACCESS_AGENT_REMOVED:
1244 cordConfigHandler.processAccessAgentRemovedEvent(event);
1245 break;
1246 case ACCESS_DEVICE_ADDED:
1247 case ACCESS_DEVICE_UPDATED:
1248 case ACCESS_DEVICE_REMOVED:
1249 default:
1250 break;
1251 }
1252 }
1253 }
Charles Chan03a73e02016-10-24 14:52:01 -07001254
1255 private class InternalRouteEventListener implements RouteListener {
1256 @Override
1257 public void event(RouteEvent event) {
1258 switch (event.type()) {
1259 case ROUTE_ADDED:
1260 routeHandler.processRouteAdded(event);
1261 break;
1262 case ROUTE_UPDATED:
1263 routeHandler.processRouteUpdated(event);
1264 break;
1265 case ROUTE_REMOVED:
1266 routeHandler.processRouteRemoved(event);
1267 break;
1268 default:
1269 break;
1270 }
1271 }
1272 }
sanghob35a6192015-04-01 13:05:26 -07001273}