blob: bddc1915564e0c73c16598a0674b9caa65c525bd [file] [log] [blame]
Pier Ventref34966c2016-11-07 16:21:04 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Pier Ventref34966c2016-11-07 16:21:04 -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
17package org.onosproject.segmentrouting.pwaas;
18
19
20import com.google.common.base.MoreObjects;
21import org.onlab.packet.MplsLabel;
22import org.onlab.packet.VlanId;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -070023import org.onosproject.net.Link;
Pier Ventref34966c2016-11-07 16:21:04 -080024
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -070025import java.util.ArrayList;
26import java.util.List;
Pier Ventref34966c2016-11-07 16:21:04 -080027import java.util.Objects;
28
29import static com.google.common.base.Preconditions.checkArgument;
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * Implementation of the default l2 tunnel.
34 */
35public class DefaultL2Tunnel {
36
37 /**
38 * Mode of the pseudo wire.
39 */
40 private L2Mode pwMode;
41 /**
42 * Service delimiting tag.
43 */
44 private VlanId sdTag;
45 /**
46 * Tunnel id.
47 */
48 private long tunnelId;
49 /**
50 * Pseudo wire label.
51 */
52 private MplsLabel pwLabel;
53 /**
54 * Inter-CO label.
55 */
56 private MplsLabel interCoLabel;
57
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -070058 private List<Link> pathUsed;
59
Pier Ventref34966c2016-11-07 16:21:04 -080060 /**
61 * Creates a inter-co l2 tunnel using the
62 * supplied parameters.
63 *
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -070064 * @param mode the tunnel mode
65 * @param sdtag the service delimiting tag
66 * @param tunnelId the tunnel id
67 * @param pwLabel the pseudo wire label
Pier Ventref34966c2016-11-07 16:21:04 -080068 * @param interCoLabel the inter central office label
69 */
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -070070 public DefaultL2Tunnel(L2Mode mode, VlanId sdtag, long tunnelId, MplsLabel pwLabel, MplsLabel interCoLabel) {
Pier Ventref34966c2016-11-07 16:21:04 -080071 checkNotNull(mode);
72 checkArgument(tunnelId > 0);
73 checkNotNull(pwLabel);
74 checkNotNull(interCoLabel);
75
76 this.pwMode = mode;
77 this.sdTag = sdtag;
78 this.tunnelId = tunnelId;
79 this.pwLabel = pwLabel;
80 this.interCoLabel = interCoLabel;
81 }
82
83 /**
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -070084 * Creates a l2Tunnel from a given tunnel.
Pier Ventref34966c2016-11-07 16:21:04 -080085 *
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -070086 * @param l2Tunnel to replicate
Pier Ventref34966c2016-11-07 16:21:04 -080087 */
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -070088 public DefaultL2Tunnel(DefaultL2Tunnel l2Tunnel) {
89
90 this.pwMode = l2Tunnel.pwMode();
91 this.sdTag = l2Tunnel.sdTag();
92 this.tunnelId = l2Tunnel.tunnelId();
93 this.pwLabel = l2Tunnel.pwLabel();
94 this.interCoLabel = l2Tunnel.interCoLabel();
95 this.pathUsed = l2Tunnel.pathUsed();
Pier Ventref34966c2016-11-07 16:21:04 -080096 }
97
98 /**
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -070099 * Creates a intra-co l2 tunnel using the
100 * supplied parameters.
Pier Ventref34966c2016-11-07 16:21:04 -0800101 *
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700102 * @param mode the tunnel mode
103 * @param sdtag the service delimiting tag
104 * @param tunnelId the tunnel id
105 * @param pwLabel the pseudo wire label
106 */
107 public DefaultL2Tunnel(L2Mode mode, VlanId sdtag, long tunnelId, MplsLabel pwLabel) {
108 this(mode, sdtag, tunnelId, pwLabel, MplsLabel.mplsLabel(MplsLabel.MAX_MPLS));
109 }
110
111
112 /**
113 * Creates an empty l2 tunnel.
Pier Ventref34966c2016-11-07 16:21:04 -0800114 **/
115 public DefaultL2Tunnel() {
116 this.pwMode = null;
117 this.sdTag = null;
118 this.tunnelId = 0;
119 this.pwLabel = null;
120 this.interCoLabel = null;
121 }
122
123 /**
124 * Returns the mode of the pseudo wire.
125 *
126 * @return the pseudo wire mode
127 */
128 public L2Mode pwMode() {
129 return pwMode;
130 }
131
132 /**
133 * Returns the service delimitation
134 * tag.
135 *
136 * @return the service delimitation vlan id
137 */
138 public VlanId sdTag() {
139 return sdTag;
140 }
141
142 /**
143 * Returns the tunnel id of the pseudo wire.
144 *
145 * @return the pseudo wire tunnel id
146 */
147 public long tunnelId() {
148 return tunnelId;
149 }
150
151 /**
152 * Returns the pw label.
153 *
154 * @return the mpls pw label
155 */
156 public MplsLabel pwLabel() {
157 return pwLabel;
158 }
159
160 /**
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700161 * Set the path for the pseudowire.
162 *
163 * @param path The path to set
164 */
165 public void setPath(List<Link> path) {
166 pathUsed = new ArrayList<>(path);
167 }
168
169 /**
170 * Returns the used path of the pseudowire.
171 *
172 * @return pathUsed
173 */
174 public List<Link> pathUsed() {
175 return pathUsed;
176 }
177
178 /**
Pier Ventref34966c2016-11-07 16:21:04 -0800179 * Returns the inter-co label.
180 *
181 * @return the mpls inter-co label
182 */
183 public MplsLabel interCoLabel() {
184 return interCoLabel;
185 }
186
187 @Override
188 public int hashCode() {
189 return Objects.hash(this.tunnelId, this.pwLabel);
190 }
191
192 @Override
193 public boolean equals(Object o) {
194 if (this == o) {
195 return true;
196 }
197
198 if (getClass() != o.getClass()) {
199 return false;
200 }
201
202 if (o instanceof DefaultL2Tunnel) {
203 DefaultL2Tunnel that = (DefaultL2Tunnel) o;
204 return this.tunnelId == that.tunnelId &&
205 this.pwMode.equals(that.pwMode) &&
206 this.sdTag.equals(that.sdTag) &&
207 this.pwLabel.equals(that.pwLabel) &&
208 this.interCoLabel.equals(that.interCoLabel);
209 }
210
211 return false;
212 }
213
214 @Override
215 public String toString() {
216 return MoreObjects.toStringHelper(this)
217 .add("pwMode", pwMode())
218 .add("sdTag", sdTag())
219 .add("tunnelId", tunnelId())
220 .add("pwLabel", pwLabel())
221 .add("interCoLabel", interCoLabel())
222 .toString();
223 }
224
225}