blob: bcc12ec9c13a8b0f75fc263fc6726c2ad1efba5a [file] [log] [blame]
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +05301/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +05303 *
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.pce.pceservice;
18
19import static com.google.common.base.MoreObjects.toStringHelper;
20
Priyanka Ba32f6da2016-09-02 16:10:21 +053021import java.util.Collection;
22import java.util.List;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053023import java.util.Objects;
24
Priyanka Ba32f6da2016-09-02 16:10:21 +053025import org.onlab.rest.BaseResource;
Mahesh Poojary S33536202016-05-30 07:22:36 +053026import org.onlab.util.DataRateUnit;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053027import org.onosproject.incubator.net.tunnel.Tunnel;
28import org.onosproject.incubator.net.tunnel.TunnelId;
Mahesh Poojary S33536202016-05-30 07:22:36 +053029import org.onosproject.net.intent.constraint.BandwidthConstraint;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053030import org.onosproject.net.intent.Constraint;
Mahesh Poojary S33536202016-05-30 07:22:36 +053031import org.onosproject.pce.pceservice.constraint.CostConstraint;
Priyanka Ba32f6da2016-09-02 16:10:21 +053032import org.onosproject.pce.pcestore.api.PceStore;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053033
34/**
35 * Implementation of an entity which provides functionalities of pce path.
36 */
37public final class DefaultPcePath implements PcePath {
38
39 private TunnelId id; // path id
40 private String source; // Ingress
41 private String destination; // Egress
42 private LspType lspType; // LSP type
43 private String name; // symbolic-path-name
44 private Constraint costConstraint; // cost constraint
45 private Constraint bandwidthConstraint; // bandwidth constraint
Priyanka Ba32f6da2016-09-02 16:10:21 +053046 private Collection<ExplicitPathInfo> explicitPathInfo; //list of explicit path info
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053047
48 /**
49 * Initializes PCE path attributes.
50 *
51 * @param id path id
52 * @param src ingress
53 * @param dst egress
Mahesh Poojary S33536202016-05-30 07:22:36 +053054 * @param lspType LSP type
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053055 * @param name symbolic-path-name
Mahesh Poojary S33536202016-05-30 07:22:36 +053056 * @param costConstrnt cost constraint
57 * @param bandwidthConstrnt bandwidth constraint
Priyanka Ba32f6da2016-09-02 16:10:21 +053058 * @param explicitPathInfo list of explicit path info
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053059 */
60 private DefaultPcePath(TunnelId id, String src, String dst, LspType lspType,
Priyanka Ba32f6da2016-09-02 16:10:21 +053061 String name, Constraint costConstrnt, Constraint bandwidthConstrnt,
62 Collection<ExplicitPathInfo> explicitPathInfo) {
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053063 this.id = id;
64 this.source = src;
65 this.destination = dst;
66 this.lspType = lspType;
67 this.name = name;
68 this.costConstraint = costConstrnt;
69 this.bandwidthConstraint = bandwidthConstrnt;
Priyanka Ba32f6da2016-09-02 16:10:21 +053070 this.explicitPathInfo = explicitPathInfo;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053071 }
72
73 @Override
74 public TunnelId id() {
75 return id;
76 }
77
78 @Override
79 public void id(TunnelId id) {
80 this.id = id;
81 }
82
83 @Override
84 public String source() {
85 return source;
86 }
87
88 @Override
89 public void source(String src) {
90 this.source = src;
91 }
92
93 @Override
94 public String destination() {
95 return destination;
96 }
97
98 @Override
99 public void destination(String dst) {
100 this.destination = dst;
101 }
102
103 @Override
104 public LspType lspType() {
105 return lspType;
106 }
107
108 @Override
109 public String name() {
110 return name;
111 }
112
113 @Override
114 public Constraint costConstraint() {
115 return costConstraint;
116 }
117
118 @Override
119 public Constraint bandwidthConstraint() {
120 return bandwidthConstraint;
121 }
122
123 @Override
Priyanka Ba32f6da2016-09-02 16:10:21 +0530124 public Collection<ExplicitPathInfo> explicitPathInfo() {
125 return explicitPathInfo;
126 }
127
128 @Override
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530129 public PcePath copy(PcePath path) {
130 if (null != path.source()) {
131 this.source = path.source();
132 }
133 if (null != path.destination()) {
134 this.destination = path.destination();
135 }
136
137 this.lspType = path.lspType();
138
139 if (null != path.name()) {
140 this.name = path.name();
141 }
142 if (null != path.costConstraint()) {
143 this.costConstraint = path.costConstraint();
144 }
145 if (null != path.bandwidthConstraint()) {
146 this.bandwidthConstraint = path.bandwidthConstraint();
147 }
148 return this;
149 }
150
151 @Override
152 public int hashCode() {
Priyanka Ba32f6da2016-09-02 16:10:21 +0530153 return Objects.hash(id, source, destination, lspType, name, costConstraint, bandwidthConstraint,
154 explicitPathInfo);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530155 }
156
157 @Override
158 public boolean equals(Object obj) {
159 if (this == obj) {
160 return true;
161 }
162 if (obj instanceof DefaultPcePath) {
163 DefaultPcePath that = (DefaultPcePath) obj;
164 return Objects.equals(id, that.id)
165 && Objects.equals(source, that.source)
166 && Objects.equals(destination, that.destination)
167 && Objects.equals(lspType, that.lspType)
168 && Objects.equals(name, that.name)
169 && Objects.equals(costConstraint, that.costConstraint)
Priyanka Ba32f6da2016-09-02 16:10:21 +0530170 && Objects.equals(bandwidthConstraint, that.bandwidthConstraint)
171 && Objects.equals(explicitPathInfo, that.explicitPathInfo);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530172 }
173 return false;
174 }
175
176 @Override
177 public String toString() {
178 return toStringHelper(this)
Priyanka B3fdb9dd2016-08-08 10:47:24 +0530179 .omitNullValues()
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530180 .add("id", id())
181 .add("source", source)
182 .add("destination", destination)
183 .add("lsptype", lspType)
184 .add("name", name)
Priyanka B3fdb9dd2016-08-08 10:47:24 +0530185 .add("costConstraint", costConstraint)
186 .add("bandwidthConstraint", bandwidthConstraint)
Priyanka Ba32f6da2016-09-02 16:10:21 +0530187 .add("explicitPathInfo", explicitPathInfo)
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530188 .toString();
189 }
190
191 /**
192 * Creates an instance of the pce path builder.
193 *
194 * @return instance of builder
195 */
196 public static Builder builder() {
197 return new Builder();
198 }
199
200 /**
201 * Builder class for pce path.
202 */
Priyanka Ba32f6da2016-09-02 16:10:21 +0530203 public static final class Builder extends BaseResource implements PcePath.Builder {
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530204 private TunnelId id;
205 private String source;
206 private String destination;
207 private LspType lspType;
208 private String name;
209 private Constraint costConstraint;
210 private Constraint bandwidthConstraint;
Priyanka Ba32f6da2016-09-02 16:10:21 +0530211 private Collection<ExplicitPathInfo> explicitPathInfo;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530212
213 @Override
214 public Builder id(String id) {
215 this.id = TunnelId.valueOf(id);
216 return this;
217 }
218
219 @Override
220 public Builder source(String source) {
221 this.source = source;
222 return this;
223 }
224
225 @Override
226 public Builder destination(String destination) {
227 this.destination = destination;
228 return this;
229 }
230
231 @Override
232 public Builder lspType(String type) {
233 if (null != type) {
Mahesh Poojary S33536202016-05-30 07:22:36 +0530234 this.lspType = LspType.values()[Integer.valueOf(type)];
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530235 }
236 return this;
237 }
238
239 @Override
240 public Builder name(String name) {
241 this.name = name;
242 return this;
243 }
244
245 @Override
246 public Builder costConstraint(String cost) {
Mahesh Poojary S33536202016-05-30 07:22:36 +0530247 this.costConstraint = CostConstraint.of(CostConstraint.Type.values()[Integer.valueOf(cost) - 1]);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530248 return this;
249 }
250
251 @Override
252 public Builder bandwidthConstraint(String bandwidth) {
Mahesh Poojary S33536202016-05-30 07:22:36 +0530253 this.bandwidthConstraint = BandwidthConstraint.of(Double.valueOf(bandwidth), DataRateUnit
254 .valueOf("BPS"));
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530255 return this;
256 }
257
258 @Override
Priyanka Ba32f6da2016-09-02 16:10:21 +0530259 public Builder explicitPathInfo(Collection<ExplicitPathInfo> explicitPathInfo) {
260 this.explicitPathInfo = explicitPathInfo;
261 return this;
262 }
263
264 @Override
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530265 public Builder of(Tunnel tunnel) {
266 this.id = TunnelId.valueOf(tunnel.tunnelId().id());
Priyanka B3fdb9dd2016-08-08 10:47:24 +0530267 this.source = tunnel.path().src().deviceId().toString();
268 this.destination = tunnel.path().dst().deviceId().toString();
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530269 this.name = tunnel.tunnelName().toString();
Mahesh Poojary S33536202016-05-30 07:22:36 +0530270 // LSP type
271 String lspType = tunnel.annotations().value(PcepAnnotationKeys.LSP_SIG_TYPE);
272 if (lspType != null) {
Priyanka Bb977f562016-07-22 13:02:03 +0530273 this.lspType = LspType.values()[LspType.valueOf(lspType).type()];
Mahesh Poojary S33536202016-05-30 07:22:36 +0530274 }
Priyanka Bb977f562016-07-22 13:02:03 +0530275
Mahesh Poojary S33536202016-05-30 07:22:36 +0530276 // Cost type
277 String costType = tunnel.annotations().value(PcepAnnotationKeys.COST_TYPE);
278 if (costType != null) {
Priyanka Bb977f562016-07-22 13:02:03 +0530279 this.costConstraint = CostConstraint.of(CostConstraint.Type.valueOf(costType));
Mahesh Poojary S33536202016-05-30 07:22:36 +0530280 }
Priyanka Bb977f562016-07-22 13:02:03 +0530281
Mahesh Poojary S33536202016-05-30 07:22:36 +0530282 // Bandwidth
283 String bandwidth = tunnel.annotations().value(PcepAnnotationKeys.BANDWIDTH);
284 if (bandwidth != null) {
285 this.bandwidthConstraint = BandwidthConstraint.of(Double.parseDouble(bandwidth),
286 DataRateUnit.valueOf("BPS"));
287 }
288
Priyanka Ba32f6da2016-09-02 16:10:21 +0530289 PceStore pceStore = get(PceStore.class);
290 List<ExplicitPathInfo> explicitPathInfoList = pceStore
291 .getTunnelNameExplicitPathInfoMap(tunnel.tunnelName().value());
292 if (explicitPathInfoList != null) {
293 this.explicitPathInfo = explicitPathInfoList;
294 }
295
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530296 return this;
297 }
298
299 @Override
300 public PcePath build() {
301 return new DefaultPcePath(id, source, destination, lspType, name,
Priyanka Ba32f6da2016-09-02 16:10:21 +0530302 costConstraint, bandwidthConstraint, explicitPathInfo);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530303 }
304 }
305}