blob: f83d0bb1755aaf7024a0e042753307ad9decbc9c [file] [log] [blame]
Michele Santuarice256492017-06-23 14:44:01 +02001/*
2 * Copyright 2017-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
17package org.onosproject.drivers.juniper;
18
19import com.google.common.annotations.Beta;
20import org.onlab.packet.Ip4Address;
21import org.onlab.packet.Ip4Prefix;
22
23import java.util.Objects;
24
25import static com.google.common.base.MoreObjects.toStringHelper;
26import static org.onosproject.net.intent.Intent.DEFAULT_INTENT_PRIORITY;
27
28
29/**
30 * Definition of a static route for the Juniper.
31 */
32@Beta
33public class StaticRoute {
34
35 /**
36 * Maximum metric value for the Juniper.
37 */
38 private static final int BEST_STATIC_METRIC = 1;
39
40 /**
41 * Max FlowRule priority that corresponds to the BEST_STATIC_METRIC of the Juniper.
42 * DEFAULT_INTENT_PRIORITY + DEFAULT_METRIC_STATIC_ROUTE - BEST_STATIC_METRIC
43 */
44 private static final int MAX_FLOWRULE_PRIORITY_ACCEPTED = 104;
45
46 /**
47 * Default static route metric.
48 */
49 public static final int DEFAULT_METRIC_STATIC_ROUTE = 5;
50
51 private final Ip4Prefix ipv4Dest;
52 private final Ip4Address nextHop;
53 private final int priority;
54 private final boolean localRoute;
55
56 public StaticRoute(Ip4Prefix ipv4Dest, Ip4Address nextHop, boolean localRoute, int priority) {
57 this.ipv4Dest = ipv4Dest;
58 this.nextHop = nextHop;
59 this.priority = priority;
60 this.localRoute = localRoute;
61 }
62
63 public StaticRoute(Ip4Prefix ipv4Dest, Ip4Address nextHop, boolean localRoute) {
64 this(ipv4Dest, nextHop, localRoute, DEFAULT_INTENT_PRIORITY);
65 }
66
67 public Ip4Prefix ipv4Dst() {
68 return ipv4Dest;
69 }
70
71 public Ip4Address nextHop() {
72 return nextHop;
73 }
74
75 /**
76 * Method to covert the priority into the Juniper metric.
77 * See https://goo.gl/ACo952.
78 * @return the metric for the Juniper
79 */
80 public int getMetric() {
81 if (priority > MAX_FLOWRULE_PRIORITY_ACCEPTED) {
82 return BEST_STATIC_METRIC;
83 }
84 return DEFAULT_INTENT_PRIORITY + DEFAULT_METRIC_STATIC_ROUTE - priority;
85 }
86
87 /**
88 * Method to convert the Juniper metric into a priority.
89 *
90 * @param metric the metric to be converted
91 * @return the priority
92 */
93 public static int toFlowRulePriority(int metric) {
94 return DEFAULT_INTENT_PRIORITY + DEFAULT_METRIC_STATIC_ROUTE - metric;
95 }
96
97 /**
98 * Directly attached routes are called local route. Such local routes are directly discovered by
99 * the router itself.
100 *
101 * @return true if it is a local route
102 */
103 public boolean isLocalRoute() {
104 return localRoute;
105 }
106
107 @Override
108 public boolean equals(Object obj) {
109 if (this == obj) {
110 return true;
111 }
112 if (obj instanceof StaticRoute) {
113 StaticRoute that = (StaticRoute) obj;
114 return Objects.equals(ipv4Dest, that.ipv4Dest) &&
115 Objects.equals(nextHop, that.nextHop) &&
116 Objects.equals(priority, that.priority) &&
117 Objects.equals(localRoute, that.localRoute);
118 }
119 return false;
120 }
121
122 @Override
123 public int hashCode() {
124 return Objects.hash(ipv4Dest, nextHop, priority, localRoute);
125 }
126
127 @Override
128 public String toString() {
129 return toStringHelper(this)
130 .omitNullValues()
131 .addValue(localRoute ? "Local route" : null)
132 .add("IP address destination", ipv4Dest)
133 .add("Next Hop IP address", nextHop)
134 .add("Priority", priority)
135 .toString();
136 }
137}