blob: f0472aa6c1e8c735b88f0de4fd1b9667da2da604 [file] [log] [blame]
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +05301/*
2 * Copyright 2016 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.pce.pceservice;
18
19import static com.google.common.base.MoreObjects.toStringHelper;
20
21import java.util.Objects;
22
Mahesh Poojary S33536202016-05-30 07:22:36 +053023import org.onlab.util.DataRateUnit;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053024import org.onosproject.incubator.net.tunnel.Tunnel;
25import org.onosproject.incubator.net.tunnel.TunnelId;
Mahesh Poojary S33536202016-05-30 07:22:36 +053026import org.onosproject.net.intent.constraint.BandwidthConstraint;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053027import org.onosproject.net.intent.Constraint;
Mahesh Poojary S33536202016-05-30 07:22:36 +053028import org.onosproject.pce.pceservice.constraint.CostConstraint;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053029
30/**
31 * Implementation of an entity which provides functionalities of pce path.
32 */
33public final class DefaultPcePath implements PcePath {
34
35 private TunnelId id; // path id
36 private String source; // Ingress
37 private String destination; // Egress
38 private LspType lspType; // LSP type
39 private String name; // symbolic-path-name
40 private Constraint costConstraint; // cost constraint
41 private Constraint bandwidthConstraint; // bandwidth constraint
42
43 /**
44 * Initializes PCE path attributes.
45 *
46 * @param id path id
47 * @param src ingress
48 * @param dst egress
Mahesh Poojary S33536202016-05-30 07:22:36 +053049 * @param lspType LSP type
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053050 * @param name symbolic-path-name
Mahesh Poojary S33536202016-05-30 07:22:36 +053051 * @param costConstrnt cost constraint
52 * @param bandwidthConstrnt bandwidth constraint
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053053 */
54 private DefaultPcePath(TunnelId id, String src, String dst, LspType lspType,
55 String name, Constraint costConstrnt, Constraint bandwidthConstrnt) {
56
57 this.id = id;
58 this.source = src;
59 this.destination = dst;
60 this.lspType = lspType;
61 this.name = name;
62 this.costConstraint = costConstrnt;
63 this.bandwidthConstraint = bandwidthConstrnt;
64 }
65
66 @Override
67 public TunnelId id() {
68 return id;
69 }
70
71 @Override
72 public void id(TunnelId id) {
73 this.id = id;
74 }
75
76 @Override
77 public String source() {
78 return source;
79 }
80
81 @Override
82 public void source(String src) {
83 this.source = src;
84 }
85
86 @Override
87 public String destination() {
88 return destination;
89 }
90
91 @Override
92 public void destination(String dst) {
93 this.destination = dst;
94 }
95
96 @Override
97 public LspType lspType() {
98 return lspType;
99 }
100
101 @Override
102 public String name() {
103 return name;
104 }
105
106 @Override
107 public Constraint costConstraint() {
108 return costConstraint;
109 }
110
111 @Override
112 public Constraint bandwidthConstraint() {
113 return bandwidthConstraint;
114 }
115
116 @Override
117 public PcePath copy(PcePath path) {
118 if (null != path.source()) {
119 this.source = path.source();
120 }
121 if (null != path.destination()) {
122 this.destination = path.destination();
123 }
124
125 this.lspType = path.lspType();
126
127 if (null != path.name()) {
128 this.name = path.name();
129 }
130 if (null != path.costConstraint()) {
131 this.costConstraint = path.costConstraint();
132 }
133 if (null != path.bandwidthConstraint()) {
134 this.bandwidthConstraint = path.bandwidthConstraint();
135 }
136 return this;
137 }
138
139 @Override
140 public int hashCode() {
141 return Objects.hash(id, source, destination, lspType, name, costConstraint, bandwidthConstraint);
142 }
143
144 @Override
145 public boolean equals(Object obj) {
146 if (this == obj) {
147 return true;
148 }
149 if (obj instanceof DefaultPcePath) {
150 DefaultPcePath that = (DefaultPcePath) obj;
151 return Objects.equals(id, that.id)
152 && Objects.equals(source, that.source)
153 && Objects.equals(destination, that.destination)
154 && Objects.equals(lspType, that.lspType)
155 && Objects.equals(name, that.name)
156 && Objects.equals(costConstraint, that.costConstraint)
157 && Objects.equals(bandwidthConstraint, that.bandwidthConstraint);
158 }
159 return false;
160 }
161
162 @Override
163 public String toString() {
164 return toStringHelper(this)
Priyanka B3fdb9dd2016-08-08 10:47:24 +0530165 .omitNullValues()
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530166 .add("id", id())
167 .add("source", source)
168 .add("destination", destination)
169 .add("lsptype", lspType)
170 .add("name", name)
Priyanka B3fdb9dd2016-08-08 10:47:24 +0530171 .add("costConstraint", costConstraint)
172 .add("bandwidthConstraint", bandwidthConstraint)
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530173 .toString();
174 }
175
176 /**
177 * Creates an instance of the pce path builder.
178 *
179 * @return instance of builder
180 */
181 public static Builder builder() {
182 return new Builder();
183 }
184
185 /**
186 * Builder class for pce path.
187 */
188 public static final class Builder implements PcePath.Builder {
189 private TunnelId id;
190 private String source;
191 private String destination;
192 private LspType lspType;
193 private String name;
194 private Constraint costConstraint;
195 private Constraint bandwidthConstraint;
196
197 @Override
198 public Builder id(String id) {
199 this.id = TunnelId.valueOf(id);
200 return this;
201 }
202
203 @Override
204 public Builder source(String source) {
205 this.source = source;
206 return this;
207 }
208
209 @Override
210 public Builder destination(String destination) {
211 this.destination = destination;
212 return this;
213 }
214
215 @Override
216 public Builder lspType(String type) {
217 if (null != type) {
Mahesh Poojary S33536202016-05-30 07:22:36 +0530218 this.lspType = LspType.values()[Integer.valueOf(type)];
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530219 }
220 return this;
221 }
222
223 @Override
224 public Builder name(String name) {
225 this.name = name;
226 return this;
227 }
228
229 @Override
230 public Builder costConstraint(String cost) {
Mahesh Poojary S33536202016-05-30 07:22:36 +0530231 this.costConstraint = CostConstraint.of(CostConstraint.Type.values()[Integer.valueOf(cost) - 1]);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530232 return this;
233 }
234
235 @Override
236 public Builder bandwidthConstraint(String bandwidth) {
Mahesh Poojary S33536202016-05-30 07:22:36 +0530237 this.bandwidthConstraint = BandwidthConstraint.of(Double.valueOf(bandwidth), DataRateUnit
238 .valueOf("BPS"));
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530239 return this;
240 }
241
242 @Override
243 public Builder of(Tunnel tunnel) {
244 this.id = TunnelId.valueOf(tunnel.tunnelId().id());
Priyanka B3fdb9dd2016-08-08 10:47:24 +0530245 this.source = tunnel.path().src().deviceId().toString();
246 this.destination = tunnel.path().dst().deviceId().toString();
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530247 this.name = tunnel.tunnelName().toString();
Mahesh Poojary S33536202016-05-30 07:22:36 +0530248 // LSP type
249 String lspType = tunnel.annotations().value(PcepAnnotationKeys.LSP_SIG_TYPE);
250 if (lspType != null) {
Priyanka Bb977f562016-07-22 13:02:03 +0530251 this.lspType = LspType.values()[LspType.valueOf(lspType).type()];
Mahesh Poojary S33536202016-05-30 07:22:36 +0530252 }
Priyanka Bb977f562016-07-22 13:02:03 +0530253
Mahesh Poojary S33536202016-05-30 07:22:36 +0530254 // Cost type
255 String costType = tunnel.annotations().value(PcepAnnotationKeys.COST_TYPE);
256 if (costType != null) {
Priyanka Bb977f562016-07-22 13:02:03 +0530257 this.costConstraint = CostConstraint.of(CostConstraint.Type.valueOf(costType));
Mahesh Poojary S33536202016-05-30 07:22:36 +0530258 }
Priyanka Bb977f562016-07-22 13:02:03 +0530259
Mahesh Poojary S33536202016-05-30 07:22:36 +0530260 // Bandwidth
261 String bandwidth = tunnel.annotations().value(PcepAnnotationKeys.BANDWIDTH);
262 if (bandwidth != null) {
263 this.bandwidthConstraint = BandwidthConstraint.of(Double.parseDouble(bandwidth),
264 DataRateUnit.valueOf("BPS"));
265 }
266
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530267 return this;
268 }
269
270 @Override
271 public PcePath build() {
272 return new DefaultPcePath(id, source, destination, lspType, name,
273 costConstraint, bandwidthConstraint);
274 }
275 }
276}