blob: 1af72a813894d73ea15ae6bb6e5a1413fd693816 [file] [log] [blame]
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070016package org.onosproject.segmentrouting.grouphandler;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080017
18import static com.google.common.base.Preconditions.checkNotNull;
19import static org.slf4j.LoggerFactory.getLogger;
20
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070021import java.net.URI;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080022import java.util.ArrayList;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070023import java.util.Collections;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080024import java.util.HashMap;
25import java.util.HashSet;
26import java.util.List;
sangho1e575652015-05-14 00:39:53 -070027import java.util.Map;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080028import java.util.Set;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070029import java.util.stream.Collectors;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080030
Charles Chanc42e84e2015-10-20 16:24:19 -070031import org.onlab.packet.Ip4Prefix;
32import org.onlab.packet.IpPrefix;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080033import org.onlab.packet.MacAddress;
sangho32a59322015-02-17 12:07:41 -080034import org.onlab.packet.MplsLabel;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070035import org.onlab.util.KryoNamespace;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080036import org.onosproject.core.ApplicationId;
37import org.onosproject.net.DeviceId;
38import org.onosproject.net.Link;
39import org.onosproject.net.PortNumber;
40import org.onosproject.net.flow.DefaultTrafficTreatment;
41import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070042import org.onosproject.net.flowobjective.DefaultNextObjective;
43import org.onosproject.net.flowobjective.FlowObjectiveService;
44import org.onosproject.net.flowobjective.NextObjective;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -070045import org.onosproject.net.flowobjective.Objective;
46import org.onosproject.net.flowobjective.ObjectiveContext;
47import org.onosproject.net.flowobjective.ObjectiveError;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070048import org.onosproject.net.group.DefaultGroupKey;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080049import org.onosproject.net.group.GroupKey;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080050import org.onosproject.net.link.LinkService;
Charles Chan0b4e6182015-11-03 10:42:14 -080051import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
52import org.onosproject.segmentrouting.config.DeviceProperties;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070053import org.onosproject.store.service.EventuallyConsistentMap;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080054import org.slf4j.Logger;
55
56/**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070057 * Default ECMP group handler creation module. This component creates a set of
58 * ECMP groups for every neighbor that this device is connected to based on
59 * whether the current device is an edge device or a transit device.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080060 */
61public class DefaultGroupHandler {
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -070062 protected static final Logger log = getLogger(DefaultGroupHandler.class);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080063
64 protected final DeviceId deviceId;
65 protected final ApplicationId appId;
66 protected final DeviceProperties deviceConfig;
67 protected final List<Integer> allSegmentIds;
Charles Chan0b4e6182015-11-03 10:42:14 -080068 protected int nodeSegmentId = -1;
69 protected boolean isEdgeRouter = false;
70 protected MacAddress nodeMacAddr = null;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080071 protected LinkService linkService;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070072 protected FlowObjectiveService flowObjectiveService;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080073
Thomas Vachuska6cdbdd82015-05-15 09:10:58 -070074 protected HashMap<DeviceId, Set<PortNumber>> devicePortMap = new HashMap<>();
75 protected HashMap<PortNumber, DeviceId> portDeviceMap = new HashMap<>();
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070076 //protected HashMap<NeighborSet, Integer> deviceNextObjectiveIds =
77 // new HashMap<NeighborSet, Integer>();
78 protected EventuallyConsistentMap<
79 NeighborSetNextObjectiveStoreKey, Integer> nsNextObjStore = null;
Charles Chanc42e84e2015-10-20 16:24:19 -070080 protected EventuallyConsistentMap<
81 SubnetNextObjectiveStoreKey, Integer> subnetNextObjStore = null;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080082
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070083 protected KryoNamespace.Builder kryo = new KryoNamespace.Builder()
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070084 .register(URI.class).register(HashSet.class)
85 .register(DeviceId.class).register(PortNumber.class)
86 .register(NeighborSet.class).register(PolicyGroupIdentifier.class)
87 .register(PolicyGroupParams.class)
88 .register(GroupBucketIdentifier.class)
89 .register(GroupBucketIdentifier.BucketOutputType.class);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080090
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070091 protected DefaultGroupHandler(DeviceId deviceId, ApplicationId appId,
92 DeviceProperties config,
93 LinkService linkService,
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070094 FlowObjectiveService flowObjService,
95 EventuallyConsistentMap<
Charles Chanc42e84e2015-10-20 16:24:19 -070096 NeighborSetNextObjectiveStoreKey,
97 Integer> nsNextObjStore,
98 EventuallyConsistentMap<SubnetNextObjectiveStoreKey,
99 Integer> subnetNextObjStore) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800100 this.deviceId = checkNotNull(deviceId);
101 this.appId = checkNotNull(appId);
102 this.deviceConfig = checkNotNull(config);
103 this.linkService = checkNotNull(linkService);
Charles Chan0b4e6182015-11-03 10:42:14 -0800104 this.allSegmentIds = checkNotNull(config.getAllDeviceSegmentIds());
105 try {
106 this.nodeSegmentId = config.getSegmentId(deviceId);
107 this.isEdgeRouter = config.isEdgeDevice(deviceId);
108 this.nodeMacAddr = checkNotNull(config.getDeviceMac(deviceId));
109 } catch (DeviceConfigNotFoundException e) {
110 log.warn(e.getMessage()
111 + " Skipping value assignment in DefaultGroupHandler");
112 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700113 this.flowObjectiveService = flowObjService;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700114 this.nsNextObjStore = nsNextObjStore;
Charles Chanc42e84e2015-10-20 16:24:19 -0700115 this.subnetNextObjStore = subnetNextObjStore;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800116
117 populateNeighborMaps();
118 }
119
120 /**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700121 * Creates a group handler object based on the type of device. If device is
122 * of edge type it returns edge group handler, else it returns transit group
123 * handler.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800124 *
125 * @param deviceId device identifier
126 * @param appId application identifier
127 * @param config interface to retrieve the device properties
128 * @param linkService link service object
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700129 * @param flowObjService flow objective service object
Charles Chanc42e84e2015-10-20 16:24:19 -0700130 * @param nsNextObjStore NeighborSet next objective store map
131 * @param subnetNextObjStore subnet next objective store map
Charles Chan0b4e6182015-11-03 10:42:14 -0800132 * @throws DeviceConfigNotFoundException if the device configuration is not found
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800133 * @return default group handler type
134 */
135 public static DefaultGroupHandler createGroupHandler(DeviceId deviceId,
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700136 ApplicationId appId,
137 DeviceProperties config,
138 LinkService linkService,
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700139 FlowObjectiveService flowObjService,
Charles Chanc42e84e2015-10-20 16:24:19 -0700140 EventuallyConsistentMap<
141 NeighborSetNextObjectiveStoreKey,
142 Integer> nsNextObjStore,
143 EventuallyConsistentMap<SubnetNextObjectiveStoreKey,
Charles Chan0b4e6182015-11-03 10:42:14 -0800144 Integer> subnetNextObjStore)
145 throws DeviceConfigNotFoundException {
146 // handle possible exception in the caller
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800147 if (config.isEdgeDevice(deviceId)) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700148 return new DefaultEdgeGroupHandler(deviceId, appId, config,
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700149 linkService,
150 flowObjService,
Charles Chanc42e84e2015-10-20 16:24:19 -0700151 nsNextObjStore,
152 subnetNextObjStore);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800153 } else {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700154 return new DefaultTransitGroupHandler(deviceId, appId, config,
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700155 linkService,
156 flowObjService,
Charles Chanc42e84e2015-10-20 16:24:19 -0700157 nsNextObjStore,
158 subnetNextObjStore);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800159 }
160 }
161
162 /**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700163 * Creates the auto created groups for this device based on the current
164 * snapshot of the topology.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800165 */
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700166 // Empty implementations to be overridden by derived classes
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800167 public void createGroups() {
168 }
169
170 /**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700171 * Performs group creation or update procedures when a new link is
172 * discovered on this device.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800173 *
174 * @param newLink new neighbor link
175 */
176 public void linkUp(Link newLink) {
sanghob35a6192015-04-01 13:05:26 -0700177
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800178 if (newLink.type() != Link.Type.DIRECT) {
179 log.warn("linkUp: unknown link type");
180 return;
181 }
182
183 if (!newLink.src().deviceId().equals(deviceId)) {
184 log.warn("linkUp: deviceId{} doesn't match with link src{}",
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700185 deviceId, newLink.src().deviceId());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800186 return;
187 }
188
Charles Chan0b4e6182015-11-03 10:42:14 -0800189 MacAddress dstMac;
190 try {
191 dstMac = deviceConfig.getDeviceMac(newLink.dst().deviceId());
192 } catch (DeviceConfigNotFoundException e) {
193 log.warn(e.getMessage() + " Aborting linkUp.");
194 return;
195 }
196
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700197 log.debug("Device {} linkUp at local port {} to neighbor {}", deviceId,
198 newLink.src().port(), newLink.dst().deviceId());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700199 addNeighborAtPort(newLink.dst().deviceId(),
200 newLink.src().port());
201 /*if (devicePortMap.get(newLink.dst().deviceId()) == null) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800202 // New Neighbor
203 newNeighbor(newLink);
204 } else {
205 // Old Neighbor
206 newPortToExistingNeighbor(newLink);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700207 }*/
208 Set<NeighborSet> nsSet = nsNextObjStore.keySet()
209 .stream()
210 .filter((nsStoreEntry) -> (nsStoreEntry.deviceId().equals(deviceId)))
211 .map((nsStoreEntry) -> (nsStoreEntry.neighborSet()))
212 .filter((ns) -> (ns.getDeviceIds()
213 .contains(newLink.dst().deviceId())))
214 .collect(Collectors.toSet());
215 log.trace("linkUp: nsNextObjStore contents for device {}:",
sangho0b2b6d12015-05-20 22:16:38 -0700216 deviceId,
217 nsSet);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700218 for (NeighborSet ns : nsSet) {
219 // Create the new bucket to be updated
220 TrafficTreatment.Builder tBuilder =
221 DefaultTrafficTreatment.builder();
222 tBuilder.setOutput(newLink.src().port())
Charles Chan0b4e6182015-11-03 10:42:14 -0800223 .setEthDst(dstMac)
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700224 .setEthSrc(nodeMacAddr);
225 if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) {
226 tBuilder.pushMpls()
227 .setMpls(MplsLabel.
228 mplsLabel(ns.getEdgeLabel()));
229 }
230
231 Integer nextId = nsNextObjStore.
232 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
233 if (nextId != null) {
234 NextObjective.Builder nextObjBuilder = DefaultNextObjective
235 .builder().withId(nextId)
236 .withType(NextObjective.Type.HASHED).fromApp(appId);
237
238 nextObjBuilder.addTreatment(tBuilder.build());
239
240 log.debug("linkUp in device {}: Adding Bucket "
241 + "with Port {} to next object id {}",
242 deviceId,
243 newLink.src().port(),
244 nextId);
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700245 NextObjective nextObjective = nextObjBuilder.
246 add(new SRNextObjectiveContext(deviceId));
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700247 flowObjectiveService.next(deviceId, nextObjective);
248 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800249 }
250 }
251
252 /**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700253 * Performs group recovery procedures when a port goes down on this device.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800254 *
255 * @param port port number that has gone down
256 */
257 public void portDown(PortNumber port) {
258 if (portDeviceMap.get(port) == null) {
259 log.warn("portDown: unknown port");
260 return;
261 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800262
263 MacAddress dstMac;
264 try {
265 dstMac = deviceConfig.getDeviceMac(portDeviceMap.get(port));
266 } catch (DeviceConfigNotFoundException e) {
267 log.warn(e.getMessage() + " Aborting portDown.");
268 return;
269 }
270
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700271 log.debug("Device {} portDown {} to neighbor {}", deviceId, port,
272 portDeviceMap.get(port));
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700273 /*Set<NeighborSet> nsSet = computeImpactedNeighborsetForPortEvent(portDeviceMap
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700274 .get(port),
275 devicePortMap
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700276 .keySet());*/
277 Set<NeighborSet> nsSet = nsNextObjStore.keySet()
278 .stream()
279 .filter((nsStoreEntry) -> (nsStoreEntry.deviceId().equals(deviceId)))
280 .map((nsStoreEntry) -> (nsStoreEntry.neighborSet()))
281 .filter((ns) -> (ns.getDeviceIds()
282 .contains(portDeviceMap.get(port))))
283 .collect(Collectors.toSet());
284 log.trace("portDown: nsNextObjStore contents for device {}:",
285 deviceId,
286 nsSet);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800287 for (NeighborSet ns : nsSet) {
288 // Create the bucket to be removed
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700289 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment
290 .builder();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800291 tBuilder.setOutput(port)
Charles Chan0b4e6182015-11-03 10:42:14 -0800292 .setEthDst(dstMac)
293 .setEthSrc(nodeMacAddr);
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700294 if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700295 tBuilder.pushMpls().setMpls(MplsLabel.mplsLabel(ns
296 .getEdgeLabel()));
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700297 }
sangho834e4b02015-05-01 09:38:25 -0700298
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700299 Integer nextId = nsNextObjStore.
300 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
sangho834e4b02015-05-01 09:38:25 -0700301 if (nextId != null) {
302 NextObjective.Builder nextObjBuilder = DefaultNextObjective
303 .builder().withType(NextObjective.Type.SIMPLE).withId(nextId).fromApp(appId);
304
305 nextObjBuilder.addTreatment(tBuilder.build());
306
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700307 log.debug("portDown in device {}: Removing Bucket "
308 + "with Port {} to next object id {}",
309 deviceId,
310 port,
311 nextId);
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700312 NextObjective nextObjective = nextObjBuilder.
313 remove(new SRNextObjectiveContext(deviceId));
sangho834e4b02015-05-01 09:38:25 -0700314
315 flowObjectiveService.next(deviceId, nextObjective);
316 }
317
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800318 }
319
320 devicePortMap.get(portDeviceMap.get(port)).remove(port);
321 portDeviceMap.remove(port);
322 }
323
324 /**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700325 * Returns the next objective associated with the neighborset.
326 * If there is no next objective for this neighborset, this API
327 * would create a next objective and return.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800328 *
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700329 * @param ns neighborset
330 * @return int if found or -1
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800331 */
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700332 public int getNextObjectiveId(NeighborSet ns) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700333 Integer nextId = nsNextObjStore.
334 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700335 if (nextId == null) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700336 log.trace("getNextObjectiveId in device{}: Next objective id "
337 + "not found for {} and creating", deviceId, ns);
338 log.trace("getNextObjectiveId: nsNextObjStore contents for device {}: {}",
339 deviceId,
340 nsNextObjStore.entrySet()
341 .stream()
342 .filter((nsStoreEntry) ->
343 (nsStoreEntry.getKey().deviceId().equals(deviceId)))
344 .collect(Collectors.toList()));
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700345 createGroupsFromNeighborsets(Collections.singleton(ns));
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700346 nextId = nsNextObjStore.
347 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700348 if (nextId == null) {
349 log.warn("getNextObjectiveId: unable to create next objective");
350 return -1;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700351 } else {
352 log.debug("getNextObjectiveId in device{}: Next objective id {} "
Sho SHIMIZUaf973432015-09-11 14:24:50 -0700353 + "created for {}", deviceId, nextId, ns);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700354 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700355 } else {
356 log.trace("getNextObjectiveId in device{}: Next objective id {} "
Sho SHIMIZUaf973432015-09-11 14:24:50 -0700357 + "found for {}", deviceId, nextId, ns);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700358 }
Sho SHIMIZUaf973432015-09-11 14:24:50 -0700359 return nextId;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800360 }
361
sangho0b2b6d12015-05-20 22:16:38 -0700362 /**
Charles Chan9f676b62015-10-29 14:58:10 -0700363 * Returns the next objective associated with the subnet.
364 * If there is no next objective for this subnet, this API
Charles Chanc42e84e2015-10-20 16:24:19 -0700365 * would create a next objective and return.
366 *
367 * @param prefix subnet information
368 * @return int if found or -1
369 */
370 public int getSubnetNextObjectiveId(IpPrefix prefix) {
371 Integer nextId = subnetNextObjStore.
372 get(new SubnetNextObjectiveStoreKey(deviceId, prefix));
Charles Chan9f676b62015-10-29 14:58:10 -0700373
374 return (nextId != null) ? nextId : -1;
Charles Chanc42e84e2015-10-20 16:24:19 -0700375 }
376
377 /**
sangho0b2b6d12015-05-20 22:16:38 -0700378 * Checks if the next objective ID (group) for the neighbor set exists or not.
379 *
380 * @param ns neighbor set to check
381 * @return true if it exists, false otherwise
382 */
383 public boolean hasNextObjectiveId(NeighborSet ns) {
384 Integer nextId = nsNextObjStore.
385 get(new NeighborSetNextObjectiveStoreKey(deviceId, ns));
386 if (nextId == null) {
387 return false;
388 }
389
390 return true;
391 }
392
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700393 // Empty implementation
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800394 protected void newNeighbor(Link newLink) {
395 }
396
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700397 // Empty implementation
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800398 protected void newPortToExistingNeighbor(Link newLink) {
399 }
400
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700401 // Empty implementation
402 protected Set<NeighborSet>
403 computeImpactedNeighborsetForPortEvent(DeviceId impactedNeighbor,
404 Set<DeviceId> updatedNeighbors) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800405 return null;
406 }
407
408 private void populateNeighborMaps() {
409 Set<Link> outgoingLinks = linkService.getDeviceEgressLinks(deviceId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700410 for (Link link : outgoingLinks) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800411 if (link.type() != Link.Type.DIRECT) {
412 continue;
413 }
414 addNeighborAtPort(link.dst().deviceId(), link.src().port());
415 }
416 }
417
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700418 protected void addNeighborAtPort(DeviceId neighborId,
419 PortNumber portToNeighbor) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800420 // Update DeviceToPort database
421 log.debug("Device {} addNeighborAtPort: neighbor {} at port {}",
422 deviceId, neighborId, portToNeighbor);
423 if (devicePortMap.get(neighborId) != null) {
424 devicePortMap.get(neighborId).add(portToNeighbor);
425 } else {
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700426 Set<PortNumber> ports = new HashSet<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800427 ports.add(portToNeighbor);
428 devicePortMap.put(neighborId, ports);
429 }
430
431 // Update portToDevice database
432 if (portDeviceMap.get(portToNeighbor) == null) {
433 portDeviceMap.put(portToNeighbor, neighborId);
434 }
435 }
436
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700437 protected Set<Set<DeviceId>> getPowerSetOfNeighbors(Set<DeviceId> neighbors) {
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700438 List<DeviceId> list = new ArrayList<>(neighbors);
439 Set<Set<DeviceId>> sets = new HashSet<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800440 // get the number of elements in the neighbors
441 int elements = list.size();
442 // the number of members of a power set is 2^n
443 // including the empty set
444 int powerElements = (1 << elements);
445
446 // run a binary counter for the number of power elements
447 // NOTE: Exclude empty set
448 for (long i = 1; i < powerElements; i++) {
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700449 Set<DeviceId> neighborSubSet = new HashSet<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800450 for (int j = 0; j < elements; j++) {
451 if ((i >> j) % 2 == 1) {
452 neighborSubSet.add(list.get(j));
453 }
454 }
455 sets.add(neighborSubSet);
456 }
457 return sets;
458 }
459
460 private boolean isSegmentIdSameAsNodeSegmentId(DeviceId deviceId, int sId) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800461 int segmentId;
462 try {
463 segmentId = deviceConfig.getSegmentId(deviceId);
464 } catch (DeviceConfigNotFoundException e) {
465 log.warn(e.getMessage() + " Aborting isSegmentIdSameAsNodeSegmentId.");
466 return false;
467 }
468
469 return segmentId == sId;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800470 }
471
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700472 protected List<Integer> getSegmentIdsTobePairedWithNeighborSet(Set<DeviceId> neighbors) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800473
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -0700474 List<Integer> nsSegmentIds = new ArrayList<>();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800475
sanghob35a6192015-04-01 13:05:26 -0700476 // Always pair up with no edge label
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700477 // If (neighbors.size() == 1) {
sanghob35a6192015-04-01 13:05:26 -0700478 nsSegmentIds.add(-1);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700479 // }
sanghob35a6192015-04-01 13:05:26 -0700480
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800481 // Filter out SegmentIds matching with the
482 // nodes in the combo
483 for (Integer sId : allSegmentIds) {
484 if (sId.equals(nodeSegmentId)) {
485 continue;
486 }
487 boolean filterOut = false;
488 // Check if the edge label being set is of
489 // any node in the Neighbor set
490 for (DeviceId deviceId : neighbors) {
491 if (isSegmentIdSameAsNodeSegmentId(deviceId, sId)) {
492 filterOut = true;
493 break;
494 }
495 }
496 if (!filterOut) {
497 nsSegmentIds.add(sId);
498 }
499 }
500 return nsSegmentIds;
501 }
502
sangho1e575652015-05-14 00:39:53 -0700503 /**
504 * Creates Groups from a set of NeighborSet given.
505 *
506 * @param nsSet a set of NeighborSet
507 */
508 public void createGroupsFromNeighborsets(Set<NeighborSet> nsSet) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800509 for (NeighborSet ns : nsSet) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700510 int nextId = flowObjectiveService.allocateNextId();
511 NextObjective.Builder nextObjBuilder = DefaultNextObjective
512 .builder().withId(nextId)
513 .withType(NextObjective.Type.HASHED).fromApp(appId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800514 for (DeviceId d : ns.getDeviceIds()) {
sangho834e4b02015-05-01 09:38:25 -0700515 if (devicePortMap.get(d) == null) {
516 log.warn("Device {} is not in the port map yet", d);
517 return;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700518 } else if (devicePortMap.get(d).size() == 0) {
519 log.warn("There are no ports for "
520 + "the Device {} in the port map yet", d);
521 return;
sangho834e4b02015-05-01 09:38:25 -0700522 }
523
Charles Chan0b4e6182015-11-03 10:42:14 -0800524 MacAddress deviceMac;
525 try {
526 deviceMac = deviceConfig.getDeviceMac(d);
527 } catch (DeviceConfigNotFoundException e) {
528 log.warn(e.getMessage() + " Aborting createGroupsFromNeighborsets.");
529 return;
530 }
531
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800532 for (PortNumber sp : devicePortMap.get(d)) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700533 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment
534 .builder();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800535 tBuilder.setOutput(sp)
Charles Chan0b4e6182015-11-03 10:42:14 -0800536 .setEthDst(deviceMac)
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700537 .setEthSrc(nodeMacAddr);
538 if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700539 tBuilder.pushMpls().setMpls(MplsLabel.mplsLabel(ns
sangho834e4b02015-05-01 09:38:25 -0700540 .getEdgeLabel()));
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700541 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700542 nextObjBuilder.addTreatment(tBuilder.build());
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800543 }
544 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800545
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700546 NextObjective nextObj = nextObjBuilder.
547 add(new SRNextObjectiveContext(deviceId));
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700548 flowObjectiveService.next(deviceId, nextObj);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700549 log.debug("createGroupsFromNeighborsets: Submited "
sangho1e575652015-05-14 00:39:53 -0700550 + "next objective {} in device {}",
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700551 nextId, deviceId);
552 nsNextObjStore.put(new NeighborSetNextObjectiveStoreKey(deviceId, ns),
553 nextId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800554 }
555 }
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700556
Charles Chanc42e84e2015-10-20 16:24:19 -0700557 public void createGroupsFromSubnetConfig() {
558 Map<Ip4Prefix, List<PortNumber>> subnetPortMap =
559 this.deviceConfig.getSubnetPortsMap(this.deviceId);
560
561 // Construct a broadcast group for each subnet
562 subnetPortMap.forEach((subnet, ports) -> {
Charles Chan9f676b62015-10-29 14:58:10 -0700563 SubnetNextObjectiveStoreKey key =
564 new SubnetNextObjectiveStoreKey(deviceId, subnet);
565
566 if (subnetNextObjStore.containsKey(key)) {
567 log.debug("Broadcast group for device {} and subnet {} exists",
568 deviceId, subnet);
569 return;
570 }
571
Charles Chanc42e84e2015-10-20 16:24:19 -0700572 int nextId = flowObjectiveService.allocateNextId();
573
574 NextObjective.Builder nextObjBuilder = DefaultNextObjective
575 .builder().withId(nextId)
576 .withType(NextObjective.Type.BROADCAST).fromApp(appId);
577
578 ports.forEach(port -> {
579 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
580 tBuilder.setOutput(port);
581 nextObjBuilder.addTreatment(tBuilder.build());
582 });
583
584 NextObjective nextObj = nextObjBuilder.add();
585 flowObjectiveService.next(deviceId, nextObj);
586 log.debug("createGroupFromSubnetConfig: Submited "
587 + "next objective {} in device {}",
588 nextId, deviceId);
Charles Chan9f676b62015-10-29 14:58:10 -0700589
Charles Chanc42e84e2015-10-20 16:24:19 -0700590 subnetNextObjStore.put(key, nextId);
591 });
592 }
593
sanghob35a6192015-04-01 13:05:26 -0700594 public GroupKey getGroupKey(Object obj) {
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700595 return new DefaultGroupKey(kryo.build().serialize(obj));
596 }
sanghob35a6192015-04-01 13:05:26 -0700597
sangho1e575652015-05-14 00:39:53 -0700598 /**
599 * Removes groups for the next objective ID given.
600 *
601 * @param objectiveId next objective ID to remove
602 * @return true if succeeds, false otherwise
603 */
604 public boolean removeGroup(int objectiveId) {
605
606 if (nsNextObjStore.containsValue(objectiveId)) {
607 NextObjective.Builder nextObjBuilder = DefaultNextObjective
608 .builder().withId(objectiveId)
609 .withType(NextObjective.Type.HASHED).fromApp(appId);
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700610 NextObjective nextObjective = nextObjBuilder.
611 remove(new SRNextObjectiveContext(deviceId));
sangho1e575652015-05-14 00:39:53 -0700612 flowObjectiveService.next(deviceId, nextObjective);
613
614 for (Map.Entry<NeighborSetNextObjectiveStoreKey, Integer> entry: nsNextObjStore.entrySet()) {
615 if (entry.getValue().equals(objectiveId)) {
616 nsNextObjStore.remove(entry.getKey());
617 break;
618 }
619 }
sangho0b2b6d12015-05-20 22:16:38 -0700620 return true;
sangho1e575652015-05-14 00:39:53 -0700621 }
622
623 return false;
624 }
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -0700625
626 protected static class SRNextObjectiveContext implements ObjectiveContext {
627 final DeviceId deviceId;
628
629 SRNextObjectiveContext(DeviceId deviceId) {
630 this.deviceId = deviceId;
631 }
632 @Override
633 public void onSuccess(Objective objective) {
634 log.debug("Next objective operation successful in device {}",
635 deviceId);
636 }
637
638 @Override
639 public void onError(Objective objective, ObjectiveError error) {
640 log.warn("Next objective {} operation failed with error: {} in device {}",
641 objective, error, deviceId);
642 }
643 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800644}