Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 17 | package org.onosproject.simplefabric.impl; |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 18 | |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.IpPrefix; |
| 21 | import org.onosproject.cluster.NodeId; |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 22 | import org.onosproject.simplefabric.api.FabricRoute; |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 23 | |
| 24 | import java.util.Objects; |
| 25 | |
| 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 27 | import static com.google.common.base.Preconditions.checkArgument; |
| 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | |
| 30 | /** |
| 31 | * Represents a route. |
| 32 | */ |
| 33 | public final class DefaultFabricRoute implements FabricRoute { |
| 34 | |
| 35 | private static final String VERSION_MISMATCH = |
| 36 | "Prefix and next hop must be in the same address family"; |
| 37 | |
| 38 | private static final NodeId UNDEFINED = new NodeId("-"); |
| 39 | |
| 40 | private final Source source; |
| 41 | private final IpPrefix prefix; |
| 42 | private final IpAddress nextHop; |
| 43 | private final NodeId sourceNode; |
| 44 | |
| 45 | /** |
| 46 | * Creates a route. |
| 47 | * |
| 48 | * @param source route source |
| 49 | * @param prefix IP prefix |
| 50 | * @param nextHop next hop IP address |
| 51 | */ |
| 52 | private DefaultFabricRoute(Source source, IpPrefix prefix, IpAddress nextHop) { |
| 53 | this(source, prefix, nextHop, UNDEFINED); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Creates a route. |
| 58 | * |
| 59 | * @param source route source |
| 60 | * @param prefix IP prefix |
| 61 | * @param nextHop next hop IP address |
| 62 | * @param sourceNode ONOS node the route was sourced from |
| 63 | */ |
| 64 | private DefaultFabricRoute(Source source, IpPrefix prefix, |
| 65 | IpAddress nextHop, NodeId sourceNode) { |
| 66 | this.source = checkNotNull(source); |
| 67 | this.prefix = prefix; |
| 68 | this.nextHop = nextHop; |
| 69 | this.sourceNode = checkNotNull(sourceNode); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public Source source() { |
| 74 | return source; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public IpPrefix prefix() { |
| 79 | return prefix; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public IpAddress nextHop() { |
| 84 | return nextHop; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public NodeId sourceNode() { |
| 89 | return sourceNode; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public int hashCode() { |
| 94 | return Objects.hash(prefix, nextHop); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public boolean equals(Object other) { |
| 99 | if (this == other) { |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | if (!(other instanceof DefaultFabricRoute)) { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | DefaultFabricRoute that = (DefaultFabricRoute) other; |
| 108 | |
| 109 | return Objects.equals(this.prefix, that.prefix) && |
| 110 | Objects.equals(this.nextHop, that.nextHop); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public String toString() { |
| 115 | return toStringHelper(this) |
| 116 | .add("prefix", prefix) |
| 117 | .add("nextHop", nextHop) |
| 118 | .toString(); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns new builder instance. |
| 123 | * |
| 124 | * @return fabric route builder |
| 125 | */ |
| 126 | public static DefaultFabricRouteBuilder builder() { |
| 127 | return new DefaultFabricRouteBuilder(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * A builder class for fabric route. |
| 132 | */ |
| 133 | public static final class DefaultFabricRouteBuilder implements Builder { |
| 134 | private Source source; |
| 135 | private IpPrefix prefix; |
| 136 | private IpAddress nextHop; |
| 137 | private NodeId sourceNode; |
| 138 | |
| 139 | private DefaultFabricRouteBuilder() { |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public Builder source(Source source) { |
| 144 | this.source = source; |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public Builder prefix(IpPrefix prefix) { |
| 150 | this.prefix = prefix; |
| 151 | return this; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public Builder nextHop(IpAddress nextHop) { |
| 156 | this.nextHop = nextHop; |
| 157 | return this; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Builder sourceNode(NodeId sourceNode) { |
| 162 | this.sourceNode = sourceNode; |
| 163 | return this; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public FabricRoute build() { |
| 168 | |
| 169 | checkNotNull(prefix); |
| 170 | checkNotNull(nextHop); |
| 171 | checkArgument(prefix.version().equals(nextHop.version()), VERSION_MISMATCH); |
| 172 | |
| 173 | if (sourceNode != null) { |
| 174 | return new DefaultFabricRoute(source, prefix, nextHop, sourceNode); |
| 175 | } else { |
| 176 | return new DefaultFabricRoute(source, prefix, nextHop); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |