blob: 404db253a2668f35a1310b99ff5985830f5b5129 [file] [log] [blame]
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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 Bbae0eeb12016-11-30 11:59:48 +053021import java.util.Collection;
22import java.util.List;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053023import java.util.Objects;
24
Priyanka Bbae0eeb12016-11-30 11:59:48 +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;
29import org.onosproject.net.intent.Constraint;
Mahesh Poojary S33536202016-05-30 07:22:36 +053030import org.onosproject.pce.pceservice.constraint.CostConstraint;
Satish K2eb5d842017-04-04 16:28:37 +053031import org.onosproject.pce.pceservice.constraint.PceBandwidthConstraint;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053032
33/**
34 * Implementation of an entity which provides functionalities of pce path.
35 */
36public final class DefaultPcePath implements PcePath {
37
38 private TunnelId id; // path id
39 private String source; // Ingress
40 private String destination; // Egress
41 private LspType lspType; // LSP type
42 private String name; // symbolic-path-name
43 private Constraint costConstraint; // cost constraint
44 private Constraint bandwidthConstraint; // bandwidth constraint
Priyanka Bbae0eeb12016-11-30 11:59:48 +053045 private Collection<ExplicitPathInfo> explicitPathInfo; //list of explicit path info
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053046
47 /**
48 * Initializes PCE path attributes.
49 *
50 * @param id path id
51 * @param src ingress
52 * @param dst egress
Mahesh Poojary S33536202016-05-30 07:22:36 +053053 * @param lspType LSP type
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053054 * @param name symbolic-path-name
Mahesh Poojary S33536202016-05-30 07:22:36 +053055 * @param costConstrnt cost constraint
56 * @param bandwidthConstrnt bandwidth constraint
Priyanka Bbae0eeb12016-11-30 11:59:48 +053057 * @param explicitPathInfo list of explicit path info
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053058 */
59 private DefaultPcePath(TunnelId id, String src, String dst, LspType lspType,
Priyanka Bbae0eeb12016-11-30 11:59:48 +053060 String name, Constraint costConstrnt, Constraint bandwidthConstrnt,
61 Collection<ExplicitPathInfo> explicitPathInfo) {
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053062 this.id = id;
63 this.source = src;
64 this.destination = dst;
65 this.lspType = lspType;
66 this.name = name;
67 this.costConstraint = costConstrnt;
68 this.bandwidthConstraint = bandwidthConstrnt;
Priyanka Bbae0eeb12016-11-30 11:59:48 +053069 this.explicitPathInfo = explicitPathInfo;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053070 }
71
72 @Override
73 public TunnelId id() {
74 return id;
75 }
76
77 @Override
78 public void id(TunnelId id) {
79 this.id = id;
80 }
81
82 @Override
83 public String source() {
84 return source;
85 }
86
87 @Override
88 public void source(String src) {
89 this.source = src;
90 }
91
92 @Override
93 public String destination() {
94 return destination;
95 }
96
97 @Override
98 public void destination(String dst) {
99 this.destination = dst;
100 }
101
102 @Override
103 public LspType lspType() {
104 return lspType;
105 }
106
107 @Override
108 public String name() {
109 return name;
110 }
111
112 @Override
113 public Constraint costConstraint() {
114 return costConstraint;
115 }
116
117 @Override
118 public Constraint bandwidthConstraint() {
119 return bandwidthConstraint;
120 }
121
122 @Override
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530123 public Collection<ExplicitPathInfo> explicitPathInfo() {
124 return explicitPathInfo;
125 }
126
127 @Override
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530128 public PcePath copy(PcePath path) {
129 if (null != path.source()) {
130 this.source = path.source();
131 }
132 if (null != path.destination()) {
133 this.destination = path.destination();
134 }
135
136 this.lspType = path.lspType();
137
138 if (null != path.name()) {
139 this.name = path.name();
140 }
141 if (null != path.costConstraint()) {
142 this.costConstraint = path.costConstraint();
143 }
144 if (null != path.bandwidthConstraint()) {
145 this.bandwidthConstraint = path.bandwidthConstraint();
146 }
147 return this;
148 }
149
150 @Override
151 public int hashCode() {
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530152 return Objects.hash(id, source, destination, lspType, name, costConstraint, bandwidthConstraint,
153 explicitPathInfo);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530154 }
155
156 @Override
157 public boolean equals(Object obj) {
158 if (this == obj) {
159 return true;
160 }
161 if (obj instanceof DefaultPcePath) {
162 DefaultPcePath that = (DefaultPcePath) obj;
163 return Objects.equals(id, that.id)
164 && Objects.equals(source, that.source)
165 && Objects.equals(destination, that.destination)
166 && Objects.equals(lspType, that.lspType)
167 && Objects.equals(name, that.name)
168 && Objects.equals(costConstraint, that.costConstraint)
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530169 && Objects.equals(bandwidthConstraint, that.bandwidthConstraint)
170 && Objects.equals(explicitPathInfo, that.explicitPathInfo);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530171 }
172 return false;
173 }
174
175 @Override
176 public String toString() {
177 return toStringHelper(this)
Priyanka B3fdb9dd2016-08-08 10:47:24 +0530178 .omitNullValues()
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530179 .add("id", id())
180 .add("source", source)
181 .add("destination", destination)
182 .add("lsptype", lspType)
183 .add("name", name)
Priyanka B3fdb9dd2016-08-08 10:47:24 +0530184 .add("costConstraint", costConstraint)
185 .add("bandwidthConstraint", bandwidthConstraint)
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530186 .add("explicitPathInfo", explicitPathInfo)
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530187 .toString();
188 }
189
190 /**
191 * Creates an instance of the pce path builder.
192 *
193 * @return instance of builder
194 */
195 public static Builder builder() {
196 return new Builder();
197 }
198
199 /**
200 * Builder class for pce path.
201 */
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530202 public static final class Builder extends BaseResource implements PcePath.Builder {
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530203 private TunnelId id;
204 private String source;
205 private String destination;
206 private LspType lspType;
207 private String name;
208 private Constraint costConstraint;
209 private Constraint bandwidthConstraint;
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530210 private Collection<ExplicitPathInfo> explicitPathInfo;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530211
212 @Override
213 public Builder id(String id) {
214 this.id = TunnelId.valueOf(id);
215 return this;
216 }
217
218 @Override
219 public Builder source(String source) {
220 this.source = source;
221 return this;
222 }
223
224 @Override
225 public Builder destination(String destination) {
226 this.destination = destination;
227 return this;
228 }
229
230 @Override
231 public Builder lspType(String type) {
232 if (null != type) {
Mahesh Poojary S33536202016-05-30 07:22:36 +0530233 this.lspType = LspType.values()[Integer.valueOf(type)];
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530234 }
235 return this;
236 }
237
238 @Override
239 public Builder name(String name) {
240 this.name = name;
241 return this;
242 }
243
244 @Override
245 public Builder costConstraint(String cost) {
Mahesh Poojary S33536202016-05-30 07:22:36 +0530246 this.costConstraint = CostConstraint.of(CostConstraint.Type.values()[Integer.valueOf(cost) - 1]);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530247 return this;
248 }
249
250 @Override
251 public Builder bandwidthConstraint(String bandwidth) {
Satish K2eb5d842017-04-04 16:28:37 +0530252 this.bandwidthConstraint = PceBandwidthConstraint.of(Double.valueOf(bandwidth), DataRateUnit
Mahesh Poojary S33536202016-05-30 07:22:36 +0530253 .valueOf("BPS"));
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530254 return this;
255 }
256
257 @Override
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530258 public Builder explicitPathInfo(Collection<ExplicitPathInfo> explicitPathInfo) {
259 this.explicitPathInfo = explicitPathInfo;
260 return this;
261 }
262
263 @Override
264 public Builder of(Tunnel tunnel, List<ExplicitPathInfo> explicitPathInfoList) {
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530265 this.id = TunnelId.valueOf(tunnel.tunnelId().id());
Priyanka B3fdb9dd2016-08-08 10:47:24 +0530266 this.source = tunnel.path().src().deviceId().toString();
267 this.destination = tunnel.path().dst().deviceId().toString();
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530268 this.name = tunnel.tunnelName().toString();
Mahesh Poojary S33536202016-05-30 07:22:36 +0530269 // LSP type
270 String lspType = tunnel.annotations().value(PcepAnnotationKeys.LSP_SIG_TYPE);
271 if (lspType != null) {
Priyanka Bb977f562016-07-22 13:02:03 +0530272 this.lspType = LspType.values()[LspType.valueOf(lspType).type()];
Mahesh Poojary S33536202016-05-30 07:22:36 +0530273 }
Priyanka Bb977f562016-07-22 13:02:03 +0530274
Mahesh Poojary S33536202016-05-30 07:22:36 +0530275 // Cost type
276 String costType = tunnel.annotations().value(PcepAnnotationKeys.COST_TYPE);
277 if (costType != null) {
Priyanka Bb977f562016-07-22 13:02:03 +0530278 this.costConstraint = CostConstraint.of(CostConstraint.Type.valueOf(costType));
Mahesh Poojary S33536202016-05-30 07:22:36 +0530279 }
Priyanka Bb977f562016-07-22 13:02:03 +0530280
Mahesh Poojary S33536202016-05-30 07:22:36 +0530281 // Bandwidth
282 String bandwidth = tunnel.annotations().value(PcepAnnotationKeys.BANDWIDTH);
283 if (bandwidth != null) {
Satish K2eb5d842017-04-04 16:28:37 +0530284 this.bandwidthConstraint = PceBandwidthConstraint.of(Double.parseDouble(bandwidth),
Mahesh Poojary S33536202016-05-30 07:22:36 +0530285 DataRateUnit.valueOf("BPS"));
286 }
287
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530288 // Explicit Path
289 if (explicitPathInfoList != null) {
290 this.explicitPathInfo = explicitPathInfoList;
291 }
292
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530293 return this;
294 }
295
296 @Override
297 public PcePath build() {
298 return new DefaultPcePath(id, source, destination, lspType, name,
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530299 costConstraint, bandwidthConstraint, explicitPathInfo);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530300 }
301 }
302}