blob: eaca1aac5d5fccadd7d18a0aef29e215df8dec30 [file] [log] [blame]
Saurav Das7bcbe702017-06-13 15:35:54 -07001/*
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
17package org.onosproject.segmentrouting.grouphandler;
18
19import static com.google.common.base.MoreObjects.toStringHelper;
20
21import java.util.HashSet;
22import java.util.Map;
23import java.util.Objects;
24import java.util.Set;
25
26import org.onosproject.net.DeviceId;
27
Saurav Das9df5b7c2017-08-14 16:44:43 -070028/**
29 * Represents the nexthop information associated with a route-path towards a
30 * set of destinations.
31 */
Saurav Das7bcbe702017-06-13 15:35:54 -070032public class NextNeighbors {
33 private final Map<DeviceId, Set<DeviceId>> dstNextHops;
34 private final int nextId;
35
Saurav Das9df5b7c2017-08-14 16:44:43 -070036 /**
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 Das7bcbe702017-06-13 15:35:54 -070042 public NextNeighbors(Map<DeviceId, Set<DeviceId>> dstNextHops, int nextId) {
43 this.dstNextHops = dstNextHops;
44 this.nextId = nextId;
45 }
46
Saurav Das9df5b7c2017-08-14 16:44:43 -070047 /**
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 Das7bcbe702017-06-13 15:35:54 -070052 public Map<DeviceId, Set<DeviceId>> dstNextHops() {
53 return dstNextHops;
54 }
55
Saurav Das9df5b7c2017-08-14 16:44:43 -070056 /**
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 Das7bcbe702017-06-13 15:35:54 -070062 public Set<DeviceId> nextHops(DeviceId deviceId) {
63 return dstNextHops.get(deviceId);
64 }
65
Saurav Das9df5b7c2017-08-14 16:44:43 -070066 /**
67 * Return the nextId representing the nextObjective towards the next-hops.
68 *
69 * @return nextId representing the nextObjective towards the next-hops
70 */
Saurav Das7bcbe702017-06-13 15:35:54 -070071 public int nextId() {
72 return nextId;
73 }
74
Saurav Das9df5b7c2017-08-14 16:44:43 -070075 /**
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 Das7bcbe702017-06-13 15:35:54 -070082 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 Das9df5b7c2017-08-14 16:44:43 -070091 /**
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 Das7bcbe702017-06-13 15:35:54 -070099 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}