Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015-present Open Networking Foundation |
| 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 static com.google.common.base.MoreObjects.toStringHelper; |
| 20 | |
| 21 | import java.util.HashSet; |
| 22 | import java.util.Map; |
| 23 | import java.util.Objects; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Represents the nexthop information associated with a route-path towards a |
| 30 | * set of destinations. |
| 31 | */ |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 32 | public class NextNeighbors { |
| 33 | private final Map<DeviceId, Set<DeviceId>> dstNextHops; |
| 34 | private final int nextId; |
| 35 | |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Constructor. |
| 38 | * |
| 39 | * @param dstNextHops map of destinations and the next-hops towards each dest |
| 40 | * @param nextId id of nextObjective that manifests the next-hop info |
| 41 | */ |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 42 | public NextNeighbors(Map<DeviceId, Set<DeviceId>> dstNextHops, int nextId) { |
| 43 | this.dstNextHops = dstNextHops; |
| 44 | this.nextId = nextId; |
| 45 | } |
| 46 | |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Returns a map of destinations and the next-hops towards them. |
| 49 | * |
| 50 | * @return map of destinations and the next-hops towards them |
| 51 | */ |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 52 | public Map<DeviceId, Set<DeviceId>> dstNextHops() { |
| 53 | return dstNextHops; |
| 54 | } |
| 55 | |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Set of next-hops towards the given destination. |
| 58 | * |
| 59 | * @param deviceId the destination |
| 60 | * @return set of nexthops towards the destination |
| 61 | */ |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 62 | public Set<DeviceId> nextHops(DeviceId deviceId) { |
| 63 | return dstNextHops.get(deviceId); |
| 64 | } |
| 65 | |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 66 | /** |
| 67 | * Return the nextId representing the nextObjective towards the next-hops. |
| 68 | * |
| 69 | * @return nextId representing the nextObjective towards the next-hops |
| 70 | */ |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 71 | public int nextId() { |
| 72 | return nextId; |
| 73 | } |
| 74 | |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 75 | /** |
| 76 | * Checks if the given nextHopId is a valid next hop to any one of the |
| 77 | * destinations. |
| 78 | * |
| 79 | * @param nextHopId the deviceId for the next hop |
| 80 | * @return true if given next |
| 81 | */ |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 82 | public boolean containsNextHop(DeviceId nextHopId) { |
| 83 | for (Set<DeviceId> nextHops : dstNextHops.values()) { |
| 84 | if (nextHops != null && nextHops.contains(nextHopId)) { |
| 85 | return true; |
| 86 | } |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 91 | /** |
| 92 | * Returns a set of destinations which have the given nextHopId as one |
| 93 | * of the next-hops to that destination. |
| 94 | * |
| 95 | * @param nextHopId the deviceId for the next hop |
| 96 | * @return set of deviceIds that have the given nextHopId as a next-hop |
| 97 | * which could be empty if no destinations were found |
| 98 | */ |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 99 | public Set<DeviceId> getDstForNextHop(DeviceId nextHopId) { |
| 100 | Set<DeviceId> dstSet = new HashSet<>(); |
| 101 | for (DeviceId dstKey : dstNextHops.keySet()) { |
| 102 | if (dstNextHops.get(dstKey).contains(nextHopId)) { |
| 103 | dstSet.add(dstKey); |
| 104 | } |
| 105 | } |
| 106 | return dstSet; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public boolean equals(Object o) { |
| 111 | if (this == o) { |
| 112 | return true; |
| 113 | } |
| 114 | if (!(o instanceof NextNeighbors)) { |
| 115 | return false; |
| 116 | } |
| 117 | NextNeighbors that = (NextNeighbors) o; |
| 118 | return (this.nextId == that.nextId) && |
| 119 | this.dstNextHops.equals(that.dstNextHops); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public int hashCode() { |
| 124 | return Objects.hash(nextId, dstNextHops); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public String toString() { |
| 129 | return toStringHelper(this) |
| 130 | .add("nextId", nextId) |
| 131 | .add("dstNextHops", dstNextHops) |
| 132 | .toString(); |
| 133 | } |
| 134 | } |