Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.pce.pceservice; |
| 18 | |
| 19 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 20 | |
| 21 | import java.util.Objects; |
| 22 | |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 23 | import org.onlab.util.DataRateUnit; |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 24 | import org.onosproject.incubator.net.tunnel.Tunnel; |
| 25 | import org.onosproject.incubator.net.tunnel.TunnelId; |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 26 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 27 | import org.onosproject.net.intent.Constraint; |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 28 | import org.onosproject.pce.pceservice.constraint.CostConstraint; |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Implementation of an entity which provides functionalities of pce path. |
| 32 | */ |
| 33 | public 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 S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 49 | * @param lspType LSP type |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 50 | * @param name symbolic-path-name |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 51 | * @param costConstrnt cost constraint |
| 52 | * @param bandwidthConstrnt bandwidth constraint |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 53 | */ |
| 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) |
| 165 | .add("id", id()) |
| 166 | .add("source", source) |
| 167 | .add("destination", destination) |
| 168 | .add("lsptype", lspType) |
| 169 | .add("name", name) |
| 170 | .add("costConstraint", costConstraint.toString()) |
| 171 | .add("bandwidthConstraint", bandwidthConstraint.toString()) |
| 172 | .toString(); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Creates an instance of the pce path builder. |
| 177 | * |
| 178 | * @return instance of builder |
| 179 | */ |
| 180 | public static Builder builder() { |
| 181 | return new Builder(); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Builder class for pce path. |
| 186 | */ |
| 187 | public static final class Builder implements PcePath.Builder { |
| 188 | private TunnelId id; |
| 189 | private String source; |
| 190 | private String destination; |
| 191 | private LspType lspType; |
| 192 | private String name; |
| 193 | private Constraint costConstraint; |
| 194 | private Constraint bandwidthConstraint; |
| 195 | |
| 196 | @Override |
| 197 | public Builder id(String id) { |
| 198 | this.id = TunnelId.valueOf(id); |
| 199 | return this; |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public Builder source(String source) { |
| 204 | this.source = source; |
| 205 | return this; |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public Builder destination(String destination) { |
| 210 | this.destination = destination; |
| 211 | return this; |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public Builder lspType(String type) { |
| 216 | if (null != type) { |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 217 | this.lspType = LspType.values()[Integer.valueOf(type)]; |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 218 | } |
| 219 | return this; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public Builder name(String name) { |
| 224 | this.name = name; |
| 225 | return this; |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public Builder costConstraint(String cost) { |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 230 | this.costConstraint = CostConstraint.of(CostConstraint.Type.values()[Integer.valueOf(cost) - 1]); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 231 | return this; |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public Builder bandwidthConstraint(String bandwidth) { |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 236 | this.bandwidthConstraint = BandwidthConstraint.of(Double.valueOf(bandwidth), DataRateUnit |
| 237 | .valueOf("BPS")); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 238 | return this; |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public Builder of(Tunnel tunnel) { |
| 243 | this.id = TunnelId.valueOf(tunnel.tunnelId().id()); |
| 244 | this.source = tunnel.src().toString(); |
| 245 | this.destination = tunnel.dst().toString(); |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 246 | this.name = tunnel.tunnelName().toString(); |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 247 | // LSP type |
| 248 | String lspType = tunnel.annotations().value(PcepAnnotationKeys.LSP_SIG_TYPE); |
| 249 | if (lspType != null) { |
Priyanka B | b977f56 | 2016-07-22 13:02:03 +0530 | [diff] [blame^] | 250 | this.lspType = LspType.values()[LspType.valueOf(lspType).type()]; |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 251 | } |
Priyanka B | b977f56 | 2016-07-22 13:02:03 +0530 | [diff] [blame^] | 252 | |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 253 | // Cost type |
| 254 | String costType = tunnel.annotations().value(PcepAnnotationKeys.COST_TYPE); |
| 255 | if (costType != null) { |
Priyanka B | b977f56 | 2016-07-22 13:02:03 +0530 | [diff] [blame^] | 256 | this.costConstraint = CostConstraint.of(CostConstraint.Type.valueOf(costType)); |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 257 | } |
Priyanka B | b977f56 | 2016-07-22 13:02:03 +0530 | [diff] [blame^] | 258 | |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 259 | // Bandwidth |
| 260 | String bandwidth = tunnel.annotations().value(PcepAnnotationKeys.BANDWIDTH); |
| 261 | if (bandwidth != null) { |
| 262 | this.bandwidthConstraint = BandwidthConstraint.of(Double.parseDouble(bandwidth), |
| 263 | DataRateUnit.valueOf("BPS")); |
| 264 | } |
| 265 | |
Mahesh Poojary S | d7a3692 | 2016-04-01 16:11:14 +0530 | [diff] [blame] | 266 | return this; |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public PcePath build() { |
| 271 | return new DefaultPcePath(id, source, destination, lspType, name, |
| 272 | costConstraint, bandwidthConstraint); |
| 273 | } |
| 274 | } |
| 275 | } |