sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
| 17 | package org.onosproject.segmentrouting; |
| 18 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 19 | import java.util.Objects; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | /** |
| 24 | * Tunnel Policy. |
| 25 | */ |
| 26 | public final class TunnelPolicy implements Policy { |
| 27 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 28 | private final Type type; |
| 29 | private final String id; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 30 | private final int priority; |
| 31 | private final String tunnelId; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 32 | private String dstIp; |
| 33 | private String srcIp; |
| 34 | private String ipProto; |
| 35 | private short srcPort; |
| 36 | private short dstPort; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 37 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 38 | private TunnelPolicy(String policyId, Type type, int priority, String tunnelId, String srcIp, |
| 39 | String dstIp, String ipProto, short srcPort, short dstPort) { |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 40 | this.id = checkNotNull(policyId); |
| 41 | this.type = type; |
| 42 | this.tunnelId = tunnelId; |
| 43 | this.priority = priority; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 44 | this.dstIp = dstIp; |
| 45 | this.srcIp = srcIp; |
| 46 | this.ipProto = ipProto; |
| 47 | this.srcPort = srcPort; |
| 48 | this.dstPort = dstPort; |
| 49 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Creates a TunnelPolicy reference. |
| 54 | * |
| 55 | * @param p TunnelPolicy reference |
| 56 | */ |
| 57 | public TunnelPolicy(TunnelPolicy p) { |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 58 | this.id = p.id; |
| 59 | this.type = p.type; |
| 60 | this.tunnelId = p.tunnelId; |
| 61 | this.priority = p.priority; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 62 | this.srcIp = p.srcIp; |
| 63 | this.dstIp = p.dstIp; |
| 64 | this.ipProto = p.ipProto; |
| 65 | this.srcPort = p.srcPort; |
| 66 | this.dstPort = p.dstPort; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns the TunnelPolicy builder reference. |
| 71 | * |
| 72 | * @return TunnelPolicy builder |
| 73 | */ |
| 74 | public static TunnelPolicy.Builder builder() { |
| 75 | return new Builder(); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public String id() { |
| 80 | return this.id; |
| 81 | } |
| 82 | |
| 83 | @Override |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 84 | public int priority() { |
| 85 | return priority; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public Type type() { |
| 90 | return type; |
| 91 | } |
| 92 | |
| 93 | @Override |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 94 | public String srcIp() { |
| 95 | return srcIp; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 99 | public String dstIp() { |
| 100 | return dstIp; |
| 101 | } |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 102 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 103 | @Override |
| 104 | public String ipProto() { |
| 105 | return ipProto; |
| 106 | } |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 107 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 108 | @Override |
| 109 | public short srcPort() { |
| 110 | return srcPort; |
| 111 | } |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 112 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 113 | @Override |
| 114 | public short dstPort() { |
| 115 | return dstPort; |
| 116 | } |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 117 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 118 | @Override |
| 119 | public boolean equals(Object o) { |
| 120 | if (this == o) { |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | if (o instanceof TunnelPolicy) { |
| 125 | TunnelPolicy that = (TunnelPolicy) o; |
| 126 | // We do not compare the policy ID |
| 127 | if (this.type.equals(that.type) && |
| 128 | this.tunnelId.equals(that.tunnelId) && |
| 129 | this.priority == that.priority && |
| 130 | this.srcIp.equals(that.srcIp) && |
| 131 | this.dstIp.equals(that.dstIp) && |
| 132 | this.srcPort == that.srcPort && |
| 133 | this.dstPort == that.dstPort && |
| 134 | this.ipProto.equals(that.ipProto)) { |
| 135 | return true; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public int hashCode() { |
| 144 | return Objects.hash(type, tunnelId, srcIp, dstIp, ipProto, |
| 145 | srcPort, dstPort, priority); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Returns the tunnel ID of the policy. |
| 150 | * |
| 151 | * @return Tunnel ID |
| 152 | */ |
| 153 | public String tunnelId() { |
| 154 | return this.tunnelId; |
| 155 | } |
| 156 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 157 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 158 | /** |
| 159 | * Tunnel Policy Builder. |
| 160 | */ |
| 161 | public static final class Builder { |
| 162 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 163 | private String id; |
| 164 | private Type type; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 165 | private int priority; |
| 166 | private String tunnelId; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 167 | private String dstIp; |
| 168 | private String srcIp; |
| 169 | private String ipProto; |
| 170 | private short srcPort; |
| 171 | private short dstPort; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * Sets the policy Id. |
| 175 | * |
| 176 | * @param id policy Id |
| 177 | * @return Builder object |
| 178 | */ |
| 179 | public Builder setPolicyId(String id) { |
| 180 | this.id = id; |
| 181 | |
| 182 | return this; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Sets the policy type. |
| 187 | * |
| 188 | * @param type policy type |
| 189 | * @return Builder object |
| 190 | */ |
| 191 | public Builder setType(Type type) { |
| 192 | this.type = type; |
| 193 | |
| 194 | return this; |
| 195 | } |
| 196 | |
| 197 | /** |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 198 | * Sets the source IP address. |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 199 | * |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 200 | * @param srcIp source IP address |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 201 | * @return Builder object |
| 202 | */ |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 203 | public Builder setSrcIp(String srcIp) { |
| 204 | this.srcIp = srcIp; |
| 205 | |
| 206 | return this; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Sets the destination IP address. |
| 211 | * |
| 212 | * @param dstIp destination IP address |
| 213 | * @return Builder object |
| 214 | */ |
| 215 | public Builder setDstIp(String dstIp) { |
| 216 | this.dstIp = dstIp; |
| 217 | |
| 218 | return this; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Sets the IP protocol. |
| 223 | * |
| 224 | * @param proto IP protocol |
| 225 | * @return Builder object |
| 226 | */ |
| 227 | public Builder setIpProto(String proto) { |
| 228 | this.ipProto = proto; |
| 229 | |
| 230 | return this; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Sets the source port. |
| 235 | * |
| 236 | * @param srcPort source port |
| 237 | * @return Builder object |
| 238 | */ |
| 239 | public Builder setSrcPort(short srcPort) { |
| 240 | this.srcPort = srcPort; |
| 241 | |
| 242 | return this; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Sets the destination port. |
| 247 | * |
| 248 | * @param dstPort destination port |
| 249 | * @return Builder object |
| 250 | */ |
| 251 | public Builder setDstPort(short dstPort) { |
| 252 | this.dstPort = dstPort; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 253 | |
| 254 | return this; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Sets the priority of the policy. |
| 259 | * |
| 260 | * @param p priority |
| 261 | * @return Builder object |
| 262 | */ |
| 263 | public Builder setPriority(int p) { |
| 264 | this.priority = p; |
| 265 | |
| 266 | return this; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Sets the tunnel Id. |
| 271 | * |
| 272 | * @param tunnelId tunnel Id |
| 273 | * @return Builder object |
| 274 | */ |
| 275 | public Builder setTunnelId(String tunnelId) { |
| 276 | this.tunnelId = tunnelId; |
| 277 | |
| 278 | return this; |
| 279 | } |
| 280 | |
| 281 | /** |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 282 | * Builds the policy. |
| 283 | * |
| 284 | * @return Tunnel Policy reference |
| 285 | */ |
| 286 | public Policy build() { |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 287 | return new TunnelPolicy(id, type, priority, tunnelId, srcIp, dstIp, |
| 288 | ipProto, srcPort, dstPort); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | } |