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 |
| 27 | */ |
| 28 | public DefaultPath(ProviderId providerId, List<Link> links, double cost) { |
| 29 | super(providerId, source(links), destination(links), Type.INDIRECT); |
| 30 | this.links = ImmutableList.copyOf(links); |
| 31 | this.cost = cost; |
| 32 | } |
| 33 | |
| 34 | @Override |
| 35 | public List<Link> links() { |
| 36 | return links; |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public double cost() { |
| 41 | return cost; |
| 42 | } |
| 43 | |
| 44 | // Returns the source of the first link. |
| 45 | private static ConnectPoint source(List<Link> links) { |
| 46 | checkNotNull(links, "List of path links cannot be null"); |
| 47 | checkArgument(!links.isEmpty(), "List of path links cannot be empty"); |
| 48 | return links.get(0).src(); |
| 49 | } |
| 50 | |
| 51 | // Returns the destination of the last link. |
| 52 | private static ConnectPoint destination(List<Link> links) { |
| 53 | checkNotNull(links, "List of path links cannot be null"); |
| 54 | checkArgument(!links.isEmpty(), "List of path links cannot be empty"); |
| 55 | return links.get(links.size() - 1).dst(); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public int hashCode() { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame^] | 60 | return Objects.hash(links); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame^] | 65 | if (this == obj) { |
| 66 | return true; |
| 67 | } |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 68 | if (obj instanceof DefaultPath) { |
| 69 | final DefaultPath other = (DefaultPath) obj; |
| 70 | return Objects.equals(this.links, other.links); |
| 71 | } |
| 72 | return false; |
| 73 | } |
| 74 | } |