Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 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 | |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 17 | package org.onosproject.routeservice; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 18 | |
| 19 | import com.google.common.annotations.Beta; |
Charles Chan | 93888fb | 2018-06-21 17:41:50 -0700 | [diff] [blame] | 20 | import com.google.common.base.MoreObjects; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 21 | import org.onlab.packet.IpPrefix; |
| 22 | |
| 23 | import java.util.Objects; |
| 24 | import java.util.Optional; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | /** |
| 30 | * Routing information for a given prefix. |
| 31 | */ |
| 32 | @Beta |
| 33 | public class RouteInfo { |
| 34 | |
| 35 | private final IpPrefix prefix; |
| 36 | private final ResolvedRoute bestRoute; |
| 37 | private final Set<ResolvedRoute> allRoutes; |
| 38 | |
| 39 | /** |
| 40 | * Creates a new route info object. |
| 41 | * |
| 42 | * @param prefix IP prefix |
| 43 | * @param bestRoute best route for this prefix if one exists |
| 44 | * @param allRoutes all known routes for this prefix |
| 45 | */ |
| 46 | @Beta |
| 47 | public RouteInfo(IpPrefix prefix, ResolvedRoute bestRoute, Set<ResolvedRoute> allRoutes) { |
| 48 | this.prefix = checkNotNull(prefix); |
| 49 | this.bestRoute = bestRoute; |
| 50 | this.allRoutes = checkNotNull(allRoutes); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns the IP prefix. |
| 55 | * |
| 56 | * @return IP prefix |
| 57 | */ |
| 58 | public IpPrefix prefix() { |
| 59 | return prefix; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Returns the best route for this prefix if one exists. |
| 64 | * |
| 65 | * @return optional best route |
| 66 | */ |
| 67 | public Optional<ResolvedRoute> bestRoute() { |
| 68 | return Optional.ofNullable(bestRoute); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns all routes for this prefix. |
| 73 | * |
| 74 | * @return all routes |
| 75 | */ |
| 76 | public Set<ResolvedRoute> allRoutes() { |
| 77 | return allRoutes; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public int hashCode() { |
| 82 | return Objects.hash(prefix, bestRoute, allRoutes); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public boolean equals(Object other) { |
| 87 | if (this == other) { |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | if (!(other instanceof RouteInfo)) { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | RouteInfo that = (RouteInfo) other; |
| 96 | |
| 97 | return Objects.equals(this.prefix, that.prefix) && |
| 98 | Objects.equals(this.bestRoute, that.bestRoute) && |
| 99 | Objects.equals(this.allRoutes, that.allRoutes); |
| 100 | } |
Charles Chan | 93888fb | 2018-06-21 17:41:50 -0700 | [diff] [blame] | 101 | |
| 102 | @Override |
| 103 | public String toString() { |
| 104 | return MoreObjects.toStringHelper(RouteSet.class) |
| 105 | .add("prefix", prefix) |
| 106 | .add("bestRoute", bestRoute) |
| 107 | .add("allRoutes", allRoutes) |
| 108 | .toString(); |
| 109 | } |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 110 | } |