blob: cab41b89cc51f4f54064f61a3a6403d4d0241473 [file] [log] [blame]
tom97937552014-09-11 10:48:42 -07001package org.onlab.onos.net;
2
3import com.google.common.collect.ImmutableList;
4import org.onlab.onos.net.provider.ProviderId;
5
6import java.util.List;
7import java.util.Objects;
8
9import static com.google.common.base.Preconditions.checkArgument;
10import static com.google.common.base.Preconditions.checkNotNull;
11
12/**
13 * Default implementation of a network path.
14 */
15public 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() {
tomfc9a4ff2014-09-22 18:22:47 -070060 return Objects.hash(links);
tom97937552014-09-11 10:48:42 -070061 }
62
63 @Override
64 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -070065 if (this == obj) {
66 return true;
67 }
tom97937552014-09-11 10:48:42 -070068 if (obj instanceof DefaultPath) {
69 final DefaultPath other = (DefaultPath) obj;
70 return Objects.equals(this.links, other.links);
71 }
72 return false;
73 }
74}