blob: c479fca83406e82abffd298aefc2fa371514d5cb [file] [log] [blame]
Jonathan Hart96c146b2017-02-24 16:32:00 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jonathan Hart96c146b2017-02-24 16:32:00 -08003 *
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 Milkey69ec8712017-08-08 13:00:43 -070017package org.onosproject.routeservice;
Jonathan Hart96c146b2017-02-24 16:32:00 -080018
19import com.google.common.annotations.Beta;
Charles Chan50f3f132018-06-21 17:41:50 -070020import com.google.common.base.MoreObjects;
Jonathan Hart96c146b2017-02-24 16:32:00 -080021import org.onlab.packet.IpPrefix;
22
23import java.util.Objects;
24import java.util.Optional;
25import java.util.Set;
26
27import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
30 * Routing information for a given prefix.
31 */
32@Beta
33public 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 Chan50f3f132018-06-21 17:41:50 -0700101
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 Hart96c146b2017-02-24 16:32:00 -0800110}