blob: a56859fedd6a42254b07739642510c04821bc467 [file] [log] [blame]
Pier Ventre229fd0b2016-10-31 16:49:19 -07001/*
2 * Copyright 2016-present 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 */
16
17package org.onosproject.segmentrouting.grouphandler;
18
19import com.google.common.collect.Iterables;
20import org.apache.commons.lang3.RandomUtils;
21import org.onosproject.net.DeviceId;
22
23import java.util.Set;
24
25/**
26 * Extends its super classe modifying its internal behavior.
27 * Pick a neighbor will pick a random neighbor.
28 */
29public class RandomNeighborSet extends NeighborSet {
30
31 public RandomNeighborSet(Set<DeviceId> neighbors) {
32 super(neighbors, true);
33 }
34
35 public RandomNeighborSet(Set<DeviceId> neighbors, int edgeLabel) {
36 super(neighbors, true, edgeLabel);
37 }
38
39 public RandomNeighborSet() {
40 super();
41 }
42
43 @Override
44 public DeviceId getFirstNeighbor() {
45 if (getDeviceIds().isEmpty()) {
46 return DeviceId.NONE;
47 }
48 int size = getDeviceIds().size();
49 int index = RandomUtils.nextInt(0, size);
50 return Iterables.get(getDeviceIds(), index);
51 }
52
53 @Override
54 public String toString() {
55 return " RandomNeighborset Sw: " + getDeviceIds()
56 + " and Label: " + getEdgeLabel();
57 }
58}