tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net; |
| 2 | |
| 3 | import com.google.common.collect.ImmutableList; |
| 4 | import org.onlab.onos.net.provider.ProviderId; |
| 5 | |
| 6 | import java.util.List; |
| 7 | import java.util.Objects; |
| 8 | |
| 9 | import static com.google.common.base.Preconditions.checkArgument; |
| 10 | import static com.google.common.base.Preconditions.checkNotNull; |
| 11 | |
| 12 | /** |
| 13 | * Default implementation of a network path. |
| 14 | */ |
| 15 | public class DefaultPath extends DefaultLink implements Path { |
| 16 | |
| 17 | private final List<Link> links; |
| 18 | private final double cost; |
| 19 | |
| 20 | /** |
| 21 | * Creates a path from the specified source and destination using the |
| 22 | * supplied list of links. |
| 23 | * |
| 24 | * @param providerId provider identity |
| 25 | * @param links contiguous links that comprise the path |
| 26 | * @param cost unit-less path cost |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame^] | 27 | * @param annotations optional key/value annotations |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 28 | */ |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame^] | 29 | public DefaultPath(ProviderId providerId, List<Link> links, double cost, |
| 30 | Annotations... annotations) { |
| 31 | super(providerId, source(links), destination(links), Type.INDIRECT, annotations); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 32 | this.links = ImmutableList.copyOf(links); |
| 33 | this.cost = cost; |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public List<Link> links() { |
| 38 | return links; |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public double cost() { |
| 43 | return cost; |
| 44 | } |
| 45 | |
| 46 | // Returns the source of the first link. |
| 47 | private static ConnectPoint source(List<Link> links) { |
| 48 | checkNotNull(links, "List of path links cannot be null"); |
| 49 | checkArgument(!links.isEmpty(), "List of path links cannot be empty"); |
| 50 | return links.get(0).src(); |
| 51 | } |
| 52 | |
| 53 | // Returns the destination of the last link. |
| 54 | private static ConnectPoint destination(List<Link> links) { |
| 55 | checkNotNull(links, "List of path links cannot be null"); |
| 56 | checkArgument(!links.isEmpty(), "List of path links cannot be empty"); |
| 57 | return links.get(links.size() - 1).dst(); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public int hashCode() { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 62 | return Objects.hash(links); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 67 | if (this == obj) { |
| 68 | return true; |
| 69 | } |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 70 | if (obj instanceof DefaultPath) { |
| 71 | final DefaultPath other = (DefaultPath) obj; |
| 72 | return Objects.equals(this.links, other.links); |
| 73 | } |
| 74 | return false; |
| 75 | } |
| 76 | } |