blob: edf178f8e009e4d03d8546bddc090699ff0037f1 [file] [log] [blame]
Pier Ventre917127a2016-10-31 16:49:19 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Pier Ventre917127a2016-10-31 16:49:19 -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 */
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/**
Saurav Dasc88d4662017-05-15 15:34:25 -070026 * Extends its super class modifying its internal behavior.
Pier Ventre917127a2016-10-31 16:49:19 -070027 * Pick a neighbor will pick a random neighbor.
28 */
29public class RandomNeighborSet extends NeighborSet {
30
Saurav Dasc88d4662017-05-15 15:34:25 -070031 public RandomNeighborSet(Set<DeviceId> neighbors, DeviceId dstSw) {
32 super(neighbors, true, dstSw);
Pier Ventre917127a2016-10-31 16:49:19 -070033 }
34
Saurav Dasc88d4662017-05-15 15:34:25 -070035 public RandomNeighborSet(Set<DeviceId> neighbors, int edgeLabel,
36 DeviceId dstSw) {
37 super(neighbors, true, edgeLabel, dstSw);
Pier Ventre917127a2016-10-31 16:49:19 -070038 }
39
40 public RandomNeighborSet() {
41 super();
42 }
43
44 @Override
45 public DeviceId getFirstNeighbor() {
46 if (getDeviceIds().isEmpty()) {
47 return DeviceId.NONE;
48 }
49 int size = getDeviceIds().size();
50 int index = RandomUtils.nextInt(0, size);
51 return Iterables.get(getDeviceIds(), index);
52 }
53
54 @Override
55 public String toString() {
56 return " RandomNeighborset Sw: " + getDeviceIds()
Saurav Dasc88d4662017-05-15 15:34:25 -070057 + " and Label: " + getEdgeLabel()
58 + " for destination: " + getDestinationSw();
Pier Ventre917127a2016-10-31 16:49:19 -070059 }
60}