blob: 06dbdb21d542720d0bc4ffafa116d7978626b027 [file] [log] [blame]
sangho27462c62015-05-14 00:39:53 -07001/*
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
17package org.onosproject.segmentrouting;
18
sangho4a5c42a2015-05-20 22:16:38 -070019import java.util.Objects;
sangho27462c62015-05-14 00:39:53 -070020
21import static com.google.common.base.Preconditions.checkNotNull;
22
23/**
24 * Tunnel Policy.
25 */
26public final class TunnelPolicy implements Policy {
27
sangho27462c62015-05-14 00:39:53 -070028 private final Type type;
29 private final String id;
sangho27462c62015-05-14 00:39:53 -070030 private final int priority;
31 private final String tunnelId;
sangho4a5c42a2015-05-20 22:16:38 -070032 private String dstIp;
33 private String srcIp;
34 private String ipProto;
35 private short srcPort;
36 private short dstPort;
sangho27462c62015-05-14 00:39:53 -070037
sangho4a5c42a2015-05-20 22:16:38 -070038 private TunnelPolicy(String policyId, Type type, int priority, String tunnelId, String srcIp,
39 String dstIp, String ipProto, short srcPort, short dstPort) {
sangho27462c62015-05-14 00:39:53 -070040 this.id = checkNotNull(policyId);
41 this.type = type;
42 this.tunnelId = tunnelId;
43 this.priority = priority;
sangho4a5c42a2015-05-20 22:16:38 -070044 this.dstIp = dstIp;
45 this.srcIp = srcIp;
46 this.ipProto = ipProto;
47 this.srcPort = srcPort;
48 this.dstPort = dstPort;
49
sangho27462c62015-05-14 00:39:53 -070050 }
51
52 /**
53 * Creates a TunnelPolicy reference.
54 *
55 * @param p TunnelPolicy reference
56 */
57 public TunnelPolicy(TunnelPolicy p) {
sangho27462c62015-05-14 00:39:53 -070058 this.id = p.id;
59 this.type = p.type;
60 this.tunnelId = p.tunnelId;
61 this.priority = p.priority;
sangho4a5c42a2015-05-20 22:16:38 -070062 this.srcIp = p.srcIp;
63 this.dstIp = p.dstIp;
64 this.ipProto = p.ipProto;
65 this.srcPort = p.srcPort;
66 this.dstPort = p.dstPort;
sangho27462c62015-05-14 00:39:53 -070067 }
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
sangho27462c62015-05-14 00:39:53 -070084 public int priority() {
85 return priority;
86 }
87
88 @Override
89 public Type type() {
90 return type;
91 }
92
93 @Override
sangho4a5c42a2015-05-20 22:16:38 -070094 public String srcIp() {
95 return srcIp;
sangho27462c62015-05-14 00:39:53 -070096 }
97
98 @Override
sangho4a5c42a2015-05-20 22:16:38 -070099 public String dstIp() {
100 return dstIp;
101 }
sangho27462c62015-05-14 00:39:53 -0700102
sangho4a5c42a2015-05-20 22:16:38 -0700103 @Override
104 public String ipProto() {
105 return ipProto;
106 }
sangho27462c62015-05-14 00:39:53 -0700107
sangho4a5c42a2015-05-20 22:16:38 -0700108 @Override
109 public short srcPort() {
110 return srcPort;
111 }
sangho27462c62015-05-14 00:39:53 -0700112
sangho4a5c42a2015-05-20 22:16:38 -0700113 @Override
114 public short dstPort() {
115 return dstPort;
116 }
sangho27462c62015-05-14 00:39:53 -0700117
sangho4a5c42a2015-05-20 22:16:38 -0700118 @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);
sangho27462c62015-05-14 00:39:53 -0700146 }
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
sangho4a5c42a2015-05-20 22:16:38 -0700157
sangho27462c62015-05-14 00:39:53 -0700158 /**
159 * Tunnel Policy Builder.
160 */
161 public static final class Builder {
162
sangho27462c62015-05-14 00:39:53 -0700163 private String id;
164 private Type type;
sangho27462c62015-05-14 00:39:53 -0700165 private int priority;
166 private String tunnelId;
sangho4a5c42a2015-05-20 22:16:38 -0700167 private String dstIp;
168 private String srcIp;
169 private String ipProto;
170 private short srcPort;
171 private short dstPort;
sangho27462c62015-05-14 00:39:53 -0700172
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 /**
sangho4a5c42a2015-05-20 22:16:38 -0700198 * Sets the source IP address.
sangho27462c62015-05-14 00:39:53 -0700199 *
sangho4a5c42a2015-05-20 22:16:38 -0700200 * @param srcIp source IP address
sangho27462c62015-05-14 00:39:53 -0700201 * @return Builder object
202 */
sangho4a5c42a2015-05-20 22:16:38 -0700203 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;
sangho27462c62015-05-14 00:39:53 -0700253
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 /**
sangho27462c62015-05-14 00:39:53 -0700282 * Builds the policy.
283 *
284 * @return Tunnel Policy reference
285 */
286 public Policy build() {
sangho4a5c42a2015-05-20 22:16:38 -0700287 return new TunnelPolicy(id, type, priority, tunnelId, srcIp, dstIp,
288 ipProto, srcPort, dstPort);
sangho27462c62015-05-14 00:39:53 -0700289 }
290 }
291}