Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 17 | |
Sho SHIMIZU | d01a23d | 2015-06-04 13:47:53 -0700 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 20 | import org.onlab.graph.ScalarWeight; |
| 21 | import org.onlab.graph.Weight; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.net.provider.ProviderId; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 23 | |
| 24 | import java.util.List; |
| 25 | import java.util.Objects; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkArgument; |
| 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | |
| 30 | /** |
| 31 | * Default implementation of a network path. |
| 32 | */ |
| 33 | public class DefaultPath extends DefaultLink implements Path { |
| 34 | |
| 35 | private final List<Link> links; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 36 | private final Weight cost; |
| 37 | |
| 38 | /** |
| 39 | * Creates a path from the specified source and destination using the |
| 40 | * supplied list of links. |
| 41 | * |
| 42 | * @param providerId provider identity |
| 43 | * @param links contiguous links that comprise the path |
| 44 | * @param cost unit-less path cost |
| 45 | * @param annotations optional key/value annotations |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 46 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 47 | public DefaultPath(ProviderId providerId, List<Link> links, Weight cost, |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 48 | Annotations... annotations) { |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 49 | super(providerId, source(links), destination(links), Type.INDIRECT, State.ACTIVE, annotations); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 50 | this.links = ImmutableList.copyOf(links); |
| 51 | this.cost = cost; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public List<Link> links() { |
| 56 | return links; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public double cost() { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 61 | if (cost instanceof ScalarWeight) { |
| 62 | return ((ScalarWeight) cost).value(); |
| 63 | } |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public Weight weight() { |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 69 | return cost; |
| 70 | } |
| 71 | |
| 72 | // Returns the source of the first link. |
| 73 | private static ConnectPoint source(List<Link> links) { |
| 74 | checkNotNull(links, "List of path links cannot be null"); |
| 75 | checkArgument(!links.isEmpty(), "List of path links cannot be empty"); |
| 76 | return links.get(0).src(); |
| 77 | } |
| 78 | |
| 79 | // Returns the destination of the last link. |
| 80 | private static ConnectPoint destination(List<Link> links) { |
| 81 | checkNotNull(links, "List of path links cannot be null"); |
| 82 | checkArgument(!links.isEmpty(), "List of path links cannot be empty"); |
| 83 | return links.get(links.size() - 1).dst(); |
| 84 | } |
| 85 | |
| 86 | @Override |
Sho SHIMIZU | d01a23d | 2015-06-04 13:47:53 -0700 | [diff] [blame] | 87 | public String toString() { |
| 88 | return MoreObjects.toStringHelper(this) |
| 89 | .add("src", src()) |
| 90 | .add("dst", dst()) |
| 91 | .add("type", type()) |
| 92 | .add("state", state()) |
Ray Milkey | 8521f81 | 2017-05-24 09:49:26 -0700 | [diff] [blame] | 93 | .add("expected", isExpected()) |
Sho SHIMIZU | d01a23d | 2015-06-04 13:47:53 -0700 | [diff] [blame] | 94 | .add("links", links) |
| 95 | .add("cost", cost) |
| 96 | .toString(); |
| 97 | } |
| 98 | |
| 99 | @Override |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 100 | public int hashCode() { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 101 | return links.hashCode(); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 106 | if (this == obj) { |
| 107 | return true; |
| 108 | } |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 109 | if (obj instanceof DefaultPath) { |
| 110 | final DefaultPath other = (DefaultPath) obj; |
| 111 | return Objects.equals(this.links, other.links); |
| 112 | } |
| 113 | return false; |
| 114 | } |
| 115 | } |