blob: a78f093037da3f348c6d58705a343a98e53d5138 [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
588 * @return next objective ID or -1 if an error was encountered during the
589 * creation of the nextObjective
sangho1e575652015-05-14 00:39:53 -0700590 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800591 public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns,
592 TrafficSelector meta) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700593 if (groupHandlerMap.get(deviceId) != null) {
594 log.trace("getNextObjectiveId query in device {}", deviceId);
595 return groupHandlerMap
Saurav Das8a0732e2015-11-20 15:27:53 -0800596 .get(deviceId).getNextObjectiveId(ns, meta);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700597 } else {
Saurav Das4ce45962015-11-24 23:21:05 -0800598 log.warn("getNextObjectiveId query - groupHandler for device {} "
599 + "not found", deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700600 return -1;
601 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700602 }
603
Charles Chanc42e84e2015-10-20 16:24:19 -0700604 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800605 * Returns the next objective ID for the given subnet prefix. It is expected
606 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700607 *
608 * @param deviceId Device ID
609 * @param prefix Subnet
Saurav Das4ce45962015-11-24 23:21:05 -0800610 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700611 */
612 public int getSubnetNextObjectiveId(DeviceId deviceId, IpPrefix prefix) {
613 if (groupHandlerMap.get(deviceId) != null) {
614 log.trace("getSubnetNextObjectiveId query in device {}", deviceId);
615 return groupHandlerMap
616 .get(deviceId).getSubnetNextObjectiveId(prefix);
617 } else {
Saurav Das4ce45962015-11-24 23:21:05 -0800618 log.warn("getSubnetNextObjectiveId query - groupHandler for "
619 + "device {} not found", deviceId);
620 return -1;
621 }
622 }
623
624 /**
625 * Returns the next objective ID for the given portNumber, given the treatment.
626 * There could be multiple different treatments to the same outport, which
627 * would result in different objectives. If the next object
628 * does not exist, a new one is created and its id is returned.
629 *
630 * @param deviceId Device ID
631 * @param portNum port number on device for which NextObjective is queried
632 * @param treatment the actions to apply on the packets (should include outport)
633 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das59232cf2016-04-27 18:35:50 -0700634 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800635 */
636 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
637 TrafficTreatment treatment,
638 TrafficSelector meta) {
639 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
640 if (ghdlr != null) {
641 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta);
642 } else {
Charles Chane849c192016-01-11 18:28:54 -0800643 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
644 + " not found", deviceId);
645 return -1;
646 }
647 }
648
sanghob35a6192015-04-01 13:05:26 -0700649 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700650 @Override
651 public void process(PacketContext context) {
652
653 if (context.isHandled()) {
654 return;
655 }
656
657 InboundPacket pkt = context.inPacket();
658 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800659
660 if (ethernet == null) {
661 return;
662 }
663
Saurav Das4ce45962015-11-24 23:21:05 -0800664 log.trace("Rcvd pktin: {}", ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800665 if (ethernet.getEtherType() == TYPE_ARP) {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800666 log.warn("{} - we are still receiving ARP packets from {}",
667 context.inPacket().receivedFrom());
668 log.debug("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800669 return;
sanghob35a6192015-04-01 13:05:26 -0700670 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800671 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
672 //ipHandler.addToPacketBuffer(ipv4Packet);
673 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
674 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700675 } else {
Charles Chan50035632017-01-13 17:20:44 -0800676 // NOTE: We don't support IP learning at this moment so this
677 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800678 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700679 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800680 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
681 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -0800682 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -0800683 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -0800684 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
685 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
686 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
687 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
688 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
689 } else {
Pier Luigi7dad71c2017-02-01 13:50:04 -0800690 // XXX Neigbour hacking, to handle the ICMPv6 packet
691 // not under our control
692 if (icmpHandler.handleUPstreamPackets(context)) {
693 log.debug("Rcvd pktin from UpStream: {}", ipv6Packet);
694 } else {
695 log.debug("Received ICMPv6 0x{} - not handled",
696 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
697 }
Pier Ventre735b8c82016-12-02 08:16:05 -0800698 }
699 } else {
700 // NOTE: We don't support IP learning at this moment so this
701 // is not necessary. Also it causes duplication of DHCPv6 packets.
702 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
703 }
sanghob35a6192015-04-01 13:05:26 -0700704 }
705 }
706 }
707
708 private class InternalLinkListener implements LinkListener {
709 @Override
710 public void event(LinkEvent event) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700711 if (event.type() == LinkEvent.Type.LINK_ADDED
712 || event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700713 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700714 scheduleEventHandlerIfNotScheduled(event);
715 }
716 }
717 }
718
719 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -0700720 @Override
721 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -0700722 switch (event.type()) {
723 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -0800724 case PORT_UPDATED:
725 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -0700726 case DEVICE_UPDATED:
727 case DEVICE_AVAILABILITY_CHANGED:
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700728 log.debug("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700729 scheduleEventHandlerIfNotScheduled(event);
730 break;
731 default:
732 }
733 }
734 }
735
Saurav Das4ce45962015-11-24 23:21:05 -0800736 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -0700737 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700738 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700739 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700740 numOfEventsQueued++;
741
742 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
743 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700744 eventHandlerFuture = executorService
745 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
746 numOfHandlerScheduled++;
747 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700748 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700749 numOfEventsQueued,
750 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -0700751 }
sanghob35a6192015-04-01 13:05:26 -0700752 }
753
754 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700755 @Override
sanghob35a6192015-04-01 13:05:26 -0700756 public void run() {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700757 try {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700758 while (true) {
Saurav Das4ce45962015-11-24 23:21:05 -0800759 @SuppressWarnings("rawtypes")
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700760 Event event = null;
761 synchronized (threadSchedulerLock) {
762 if (!eventQueue.isEmpty()) {
763 event = eventQueue.poll();
764 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700765 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700766 numOfHandlerExecution++;
767 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
768 numOfHandlerExecution, numOfEventsExecuted);
769 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700770 }
sangho20eff1d2015-04-13 15:15:58 -0700771 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700772 if (event.type() == LinkEvent.Type.LINK_ADDED) {
773 processLinkAdded((Link) event.subject());
774 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre2c515312016-09-13 21:33:40 -0700775 Link linkRemoved = (Link) event.subject();
776 if (linkRemoved.src().elementId() instanceof DeviceId &&
777 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
778 continue;
779 }
780 if (linkRemoved.dst().elementId() instanceof DeviceId &&
781 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
782 continue;
783 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700784 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700785 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
786 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
787 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800788 DeviceId deviceId = ((Device) event.subject()).id();
789 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700790 log.info("Processing device event {} for available device {}",
791 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700792 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -0700793 } else {
794 log.info("Processing device event {} for unavailable device {}",
795 event.type(), ((Device) event.subject()).id());
796 processDeviceRemoved((Device) event.subject());
797 }
Saurav Das1a129a02016-11-18 15:21:57 -0800798 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800799 // typically these calls come when device is added first time
800 // so port filtering rules are handled at the device_added event.
801 // port added calls represent all ports on the device,
802 // enabled or not.
Saurav Das1a129a02016-11-18 15:21:57 -0800803 log.debug("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800804 ((DeviceEvent) event).subject().id(),
805 ((DeviceEvent) event).port().number(),
806 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -0800807 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800808 // these calls happen for every subsequent event
809 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -0800810 log.info("** PORT UPDATED {}/{} -> {}",
811 event.subject(),
812 ((DeviceEvent) event).port(),
813 event.type());
814 processPortUpdated(((Device) event.subject()),
815 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700816 } else {
817 log.warn("Unhandled event type: {}", event.type());
818 }
sanghob35a6192015-04-01 13:05:26 -0700819 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700820 } catch (Exception e) {
821 log.error("SegmentRouting event handler "
822 + "thread thrown an exception: {}", e);
sanghob35a6192015-04-01 13:05:26 -0700823 }
sanghob35a6192015-04-01 13:05:26 -0700824 }
825 }
826
sanghob35a6192015-04-01 13:05:26 -0700827 private void processLinkAdded(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700828 log.info("** LINK ADDED {}", link.toString());
Charles Chan0b4e6182015-11-03 10:42:14 -0800829 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
830 log.warn("Source device of this link is not configured.");
831 return;
832 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700833 //Irrespective whether the local is a MASTER or not for this device,
834 //create group handler instance and push default TTP flow rules.
835 //Because in a multi-instance setup, instances can initiate
836 //groups for any devices. Also the default TTP rules are needed
837 //to be pushed before inserting any IP table entries for any device
838 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
839 .deviceId());
840 if (groupHandler != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800841 groupHandler.linkUp(link, mastershipService.isLocalMaster(
842 link.src().deviceId()));
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700843 } else {
844 Device device = deviceService.getDevice(link.src().deviceId());
845 if (device != null) {
846 log.warn("processLinkAdded: Link Added "
847 + "Notification without Device Added "
848 + "event, still handling it");
849 processDeviceAdded(device);
850 groupHandler = groupHandlerMap.get(link.src()
851 .deviceId());
Saurav Das8a0732e2015-11-20 15:27:53 -0800852 groupHandler.linkUp(link, mastershipService.isLocalMaster(device.id()));
sanghob35a6192015-04-01 13:05:26 -0700853 }
854 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700855
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700856 log.trace("Starting optimized route population process");
857 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null);
858 //log.trace("processLinkAdded: re-starting route population process");
859 //defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -0700860
861 mcastHandler.init();
sanghob35a6192015-04-01 13:05:26 -0700862 }
863
864 private void processLinkRemoved(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700865 log.info("** LINK REMOVED {}", link.toString());
sangho834e4b02015-05-01 09:38:25 -0700866 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
867 if (groupHandler != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800868 groupHandler.portDown(link.src().port(),
869 mastershipService.isLocalMaster(link.src().deviceId()));
sangho834e4b02015-05-01 09:38:25 -0700870 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700871 log.trace("Starting optimized route population process");
872 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link);
873 //log.trace("processLinkRemoved: re-starting route population process");
874 //defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -0700875
876 mcastHandler.processLinkDown(link);
sanghob35a6192015-04-01 13:05:26 -0700877 }
878
879 private void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700880 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan0b4e6182015-11-03 10:42:14 -0800881 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das2857f382015-11-03 14:39:27 -0800882 log.warn("Device configuration uploading. Device {} will be "
883 + "processed after config completes.", device.id());
884 return;
885 }
Charles Chan2199c302016-04-23 17:36:10 -0700886 processDeviceAddedInternal(device.id());
887 }
888
889 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700890 // Irrespective of whether the local is a MASTER or not for this device,
891 // we need to create a SR-group-handler instance. This is because in a
892 // multi-instance setup, any instance can initiate forwarding/next-objectives
893 // for any switch (even if this instance is a SLAVE or not even connected
894 // to the switch). To handle this, a default-group-handler instance is necessary
895 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -0700896 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
897 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800898 DefaultGroupHandler groupHandler;
899 try {
900 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -0700901 createGroupHandler(deviceId,
902 appId,
903 deviceConfiguration,
904 linkService,
905 flowObjectiveService,
906 this);
Charles Chan0b4e6182015-11-03 10:42:14 -0800907 } catch (DeviceConfigNotFoundException e) {
908 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
909 return;
910 }
Charles Chan2199c302016-04-23 17:36:10 -0700911 log.debug("updating groupHandlerMap with new config for device: {}",
912 deviceId);
913 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -0800914 }
Saurav Dasb5c236e2016-06-07 10:08:06 -0700915 // Also, in some cases, drivers may need extra
916 // information to process rules (eg. Router IP/MAC); and so, we send
917 // port addressing rules to the driver as well irrespective of whether
918 // this instance is the master or not.
919 defaultRoutingHandler.populatePortAddressingRules(deviceId);
920
Charles Chan2199c302016-04-23 17:36:10 -0700921 if (mastershipService.isLocalMaster(deviceId)) {
Charles Chan03a73e02016-10-24 14:52:01 -0700922 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -0700923 xConnectHandler.init(deviceId);
Charles Chan35fd1a72016-06-13 18:54:31 -0700924 cordConfigHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -0700925 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700926 groupHandler.createGroupsFromSubnetConfig();
Charles Chan2199c302016-04-23 17:36:10 -0700927 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700928 }
Charles Chan5270ed02016-01-30 23:22:37 -0800929
Charles Chan03a73e02016-10-24 14:52:01 -0700930 appCfgHandler.init(deviceId);
931 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -0700932 }
933
Saurav Das80980c72016-03-23 11:22:49 -0700934 private void processDeviceRemoved(Device device) {
935 nsNextObjStore.entrySet().stream()
936 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
937 .forEach(entry -> {
938 nsNextObjStore.remove(entry.getKey());
939 });
Saurav Das80980c72016-03-23 11:22:49 -0700940 subnetNextObjStore.entrySet().stream()
941 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
942 .forEach(entry -> {
943 subnetNextObjStore.remove(entry.getKey());
944 });
Saurav Das80980c72016-03-23 11:22:49 -0700945 portNextObjStore.entrySet().stream()
946 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
947 .forEach(entry -> {
948 portNextObjStore.remove(entry.getKey());
949 });
Saurav Das80980c72016-03-23 11:22:49 -0700950 subnetVidStore.entrySet().stream()
951 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
952 .forEach(entry -> {
953 subnetVidStore.remove(entry.getKey());
954 });
Saurav Das80980c72016-03-23 11:22:49 -0700955 groupHandlerMap.remove(device.id());
Saurav Das80980c72016-03-23 11:22:49 -0700956 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan2199c302016-04-23 17:36:10 -0700957 mcastHandler.removeDevice(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -0700958 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -0700959 }
960
Saurav Das1a129a02016-11-18 15:21:57 -0800961 private void processPortUpdated(Device device, Port port) {
962 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
963 log.warn("Device configuration uploading. Not handling port event for"
964 + "dev: {} port: {}", device.id(), port.number());
965 return;
966 }
967 /* XXX create method for single port filtering rules which are needed
968 for both switch-to-switch ports and edge ports
969 if (defaultRoutingHandler != null) {
970 defaultRoutingHandler.populatePortAddressingRules(
971 ((Device) event.subject()).id());
972 }*/
973
974 // portUpdated calls are for ports that have gone down or up. For switch
975 // to switch ports, link-events should take care of any re-routing or
976 // group editing necessary for port up/down. Here we only process edge ports
977 // that are already configured.
Pier Ventre10bd8d12016-11-26 21:05:22 -0800978 Ip4Prefix configuredSubnet = deviceConfiguration.getPortIPv4Subnet(device.id(),
Saurav Das1a129a02016-11-18 15:21:57 -0800979 port.number());
980 if (configuredSubnet == null) {
981 log.debug("Not handling port updated event for unconfigured port "
982 + "dev/port: {}/{}", device.id(), port.number());
983 return;
984 }
985 processEdgePort(device, port, configuredSubnet);
986 }
987
988 private void processEdgePort(Device device, Port port, Ip4Prefix subnet) {
989 boolean portUp = port.isEnabled();
990 if (portUp) {
991 log.info("Device:EdgePort {}:{} is enabled in subnet: {}", device.id(),
992 port.number(), subnet);
993 } else {
994 log.info("Device:EdgePort {}:{} is disabled in subnet: {}", device.id(),
995 port.number(), subnet);
996 }
997
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700998 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -0700999 if (groupHandler != null) {
Saurav Das1a129a02016-11-18 15:21:57 -08001000 groupHandler.processEdgePort(port.number(), subnet, portUp,
Saurav Das423fe2b2015-12-04 10:52:59 -08001001 mastershipService.isLocalMaster(device.id()));
Saurav Das1a129a02016-11-18 15:21:57 -08001002 } else {
1003 log.warn("Group handler not found for dev:{}. Not handling edge port"
1004 + " {} event for port:{}", device.id(),
1005 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001006 }
1007 }
sangho1e575652015-05-14 00:39:53 -07001008
Pier Ventre10bd8d12016-11-26 21:05:22 -08001009 /**
1010 * Registers the given connect point with the NRS, this is necessary
1011 * to receive the NDP and ARP packets from the NRS.
1012 *
1013 * @param portToRegister connect point to register
1014 */
1015 public void registerConnectPoint(ConnectPoint portToRegister) {
Pier Ventre10bd8d12016-11-26 21:05:22 -08001016 this.neighbourResolutionService.registerNeighbourHandler(
1017 portToRegister,
1018 neighbourHandler,
1019 appId
1020 );
1021 }
1022
Charles Chand6832882015-10-05 17:50:33 -07001023 private class InternalConfigListener implements NetworkConfigListener {
Charles Chan2c15aca2016-11-09 20:51:44 -08001024 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001025
Charles Chane849c192016-01-11 18:28:54 -08001026 /**
1027 * Constructs the internal network config listener.
1028 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001029 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001030 */
Charles Chan2c15aca2016-11-09 20:51:44 -08001031 public InternalConfigListener(SegmentRoutingManager srManager) {
1032 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001033 }
1034
Charles Chane849c192016-01-11 18:28:54 -08001035 /**
1036 * Reads network config and initializes related data structure accordingly.
1037 */
Charles Chan4636be02015-10-07 14:21:45 -07001038 public void configureNetwork() {
Pier Ventre10bd8d12016-11-26 21:05:22 -08001039
Charles Chan2c15aca2016-11-09 20:51:44 -08001040 deviceConfiguration = new DeviceConfiguration(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001041
Charles Chan2c15aca2016-11-09 20:51:44 -08001042 arpHandler = new ArpHandler(srManager);
1043 icmpHandler = new IcmpHandler(srManager);
1044 ipHandler = new IpHandler(srManager);
1045 routingRulePopulator = new RoutingRulePopulator(srManager);
1046 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001047
1048 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1049 groupHandlerMap, tunnelStore);
1050 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1051 flowObjectiveService,
1052 tunnelHandler, policyStore);
1053
Charles Chan4636be02015-10-07 14:21:45 -07001054 for (Device device : deviceService.getDevices()) {
Charles Chan2199c302016-04-23 17:36:10 -07001055 processDeviceAddedInternal(device.id());
Charles Chan4636be02015-10-07 14:21:45 -07001056 }
1057
1058 defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -07001059 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001060 }
1061
Charles Chand6832882015-10-05 17:50:33 -07001062 @Override
1063 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001064 // TODO move this part to NetworkConfigEventHandler
1065 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1066 switch (event.type()) {
1067 case CONFIG_ADDED:
1068 log.info("Segment Routing Config added.");
1069 configureNetwork();
1070 break;
1071 case CONFIG_UPDATED:
1072 log.info("Segment Routing Config updated.");
1073 // TODO support dynamic configuration
1074 break;
1075 default:
1076 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001077 }
Charles Chan5270ed02016-01-30 23:22:37 -08001078 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001079 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001080 switch (event.type()) {
1081 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001082 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001083 break;
1084 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001085 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001086 break;
1087 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001088 appCfgHandler.processAppConfigRemoved(event);
1089 break;
1090 default:
1091 break;
1092 }
Charles Chan03a73e02016-10-24 14:52:01 -07001093 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001094 } else if (event.configClass().equals(XConnectConfig.class)) {
1095 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1096 switch (event.type()) {
1097 case CONFIG_ADDED:
1098 xConnectHandler.processXConnectConfigAdded(event);
1099 break;
1100 case CONFIG_UPDATED:
1101 xConnectHandler.processXConnectConfigUpdated(event);
1102 break;
1103 case CONFIG_REMOVED:
1104 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001105 break;
1106 default:
1107 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001108 }
Pier Luigi7dad71c2017-02-01 13:50:04 -08001109 // XXX Neighbour hacking. This method is looking for
1110 // the Internet-Router interface. In order to retrieve
1111 // the upstream port.
1112 } else if (event.configClass().equals(InterfaceConfig.class)) {
1113 switch (event.type()) {
1114 case CONFIG_ADDED:
1115 case CONFIG_UPDATED:
Pier Luigi721b6622017-02-03 13:34:21 -08001116 updateUPstreamCP();
Pier Luigi7dad71c2017-02-01 13:50:04 -08001117 case CONFIG_REGISTERED:
1118 case CONFIG_UNREGISTERED:
1119 case CONFIG_REMOVED:
1120 break;
1121 default:
1122 break;
1123 }
1124 // XXX Neighbour hacking. This method is looking for
1125 // the vrouter port.
1126 } else if (event.configClass().equals(RouterConfig.class)) {
1127 switch (event.type()) {
1128 case CONFIG_ADDED:
1129 case CONFIG_UPDATED:
Pier Luigi721b6622017-02-03 13:34:21 -08001130 updateVRouterCP(event);
Pier Luigi7dad71c2017-02-01 13:50:04 -08001131 case CONFIG_REGISTERED:
1132 case CONFIG_UNREGISTERED:
1133 case CONFIG_REMOVED:
1134 break;
1135 default:
1136 break;
1137 }
Charles Chand6832882015-10-05 17:50:33 -07001138 }
1139 }
1140 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001141
Pier Luigi721b6622017-02-03 13:34:21 -08001142 /////////////////////////////////////////////////////////////////
1143 // XXX Neighbour hacking, temporary workaround will be //
1144 // removed as soon as possible, when the bridging will //
1145 // be implemented. For now, it's fine to leave this //
1146 /////////////////////////////////////////////////////////////////
1147
1148 // XXX Neighbour hacking. To store upstream connect
1149 // point and vRouter connect point
1150 ConnectPoint upstreamCP = null;
1151 ConnectPoint vRouterCP = null;
1152
1153 // XXX Neighbour hacking. To update the Upstream CP
1154 public void updateUPstreamCP() {
1155 Set<ConnectPoint> portSubjects = cfgService.getSubjects(ConnectPoint.class, InterfaceConfig.class);
1156 upstreamCP = null;
1157 portSubjects.stream().forEach(subject -> {
1158 InterfaceConfig config = cfgService.getConfig(subject, InterfaceConfig.class);
1159 Set<Interface> networkInterfaces;
1160 try {
1161 networkInterfaces = config.getInterfaces();
1162 } catch (ConfigException e) {
1163 log.error("Error loading port configuration");
1164 return;
1165 }
1166 networkInterfaces.forEach(networkInterface -> {
1167 if (networkInterface.name().equals("internet-router")) {
1168 upstreamCP = subject;
1169 }
1170 });
1171 });
1172
1173 }
1174
1175 // XXX Neighbour hacking. To update the Upstream CP
1176 public void updateVRouterCP(NetworkConfigEvent event) {
1177 RouterConfig config = (RouterConfig) event.config().get();
1178 if (config == null) {
1179 log.warn("Router config not available");
1180 vRouterCP = null;
1181 return;
1182 }
1183 vRouterCP = config.getControlPlaneConnectPoint();
1184 }
1185
Charles Chan68aa62d2015-11-09 16:37:23 -08001186 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001187 @Override
1188 public void event(HostEvent event) {
1189 // Do not proceed without mastership
1190 DeviceId deviceId = event.subject().location().deviceId();
1191 if (!mastershipService.isLocalMaster(deviceId)) {
1192 return;
1193 }
1194
1195 switch (event.type()) {
1196 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001197 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001198 break;
1199 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001200 hostHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001201 break;
1202 case HOST_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001203 hostHandler.processHostRemoveEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001204 break;
1205 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001206 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001207 break;
1208 default:
1209 log.warn("Unsupported host event type: {}", event.type());
1210 break;
1211 }
1212 }
1213 }
1214
Charles Chand55e84d2016-03-30 17:54:24 -07001215 private class InternalMcastListener implements McastListener {
1216 @Override
1217 public void event(McastEvent event) {
1218 switch (event.type()) {
1219 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001220 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001221 break;
1222 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001223 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001224 break;
1225 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001226 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001227 break;
1228 case ROUTE_ADDED:
1229 case ROUTE_REMOVED:
1230 default:
1231 break;
1232 }
1233 }
1234 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001235
1236 private class InternalCordConfigListener implements CordConfigListener {
1237 @Override
1238 public void event(CordConfigEvent event) {
1239 switch (event.type()) {
1240 case ACCESS_AGENT_ADDED:
1241 cordConfigHandler.processAccessAgentAddedEvent(event);
1242 break;
1243 case ACCESS_AGENT_UPDATED:
1244 cordConfigHandler.processAccessAgentUpdatedEvent(event);
1245 break;
1246 case ACCESS_AGENT_REMOVED:
1247 cordConfigHandler.processAccessAgentRemovedEvent(event);
1248 break;
1249 case ACCESS_DEVICE_ADDED:
1250 case ACCESS_DEVICE_UPDATED:
1251 case ACCESS_DEVICE_REMOVED:
1252 default:
1253 break;
1254 }
1255 }
1256 }
Charles Chan03a73e02016-10-24 14:52:01 -07001257
1258 private class InternalRouteEventListener implements RouteListener {
1259 @Override
1260 public void event(RouteEvent event) {
1261 switch (event.type()) {
1262 case ROUTE_ADDED:
1263 routeHandler.processRouteAdded(event);
1264 break;
1265 case ROUTE_UPDATED:
1266 routeHandler.processRouteUpdated(event);
1267 break;
1268 case ROUTE_REMOVED:
1269 routeHandler.processRouteRemoved(event);
1270 break;
1271 default:
1272 break;
1273 }
1274 }
1275 }
sanghob35a6192015-04-01 13:05:26 -07001276}