Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.segmentrouting.grouphandler; |
| 18 | |
| 19 | import com.google.common.collect.Iterables; |
| 20 | import org.apache.commons.lang3.RandomUtils; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | |
| 23 | import java.util.Set; |
| 24 | |
| 25 | /** |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 26 | * Extends its super class modifying its internal behavior. |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 27 | * Pick a neighbor will pick a random neighbor. |
| 28 | */ |
| 29 | public class RandomNeighborSet extends NeighborSet { |
| 30 | |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 31 | public RandomNeighborSet(Set<DeviceId> neighbors, DeviceId dstSw) { |
| 32 | super(neighbors, true, dstSw); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 35 | public RandomNeighborSet(Set<DeviceId> neighbors, int edgeLabel, |
| 36 | DeviceId dstSw) { |
| 37 | super(neighbors, true, edgeLabel, dstSw); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 38 | } |
| 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 Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 57 | + " and Label: " + getEdgeLabel() |
| 58 | + " for destination: " + getDestinationSw(); |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 59 | } |
| 60 | } |