blob: fb0b281c6cf647a29680c878750be4eac56e62bb [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
tomf5d85d42014-10-02 05:27:56 -070027 * @param annotations optional key/value annotations
tom97937552014-09-11 10:48:42 -070028 */
tomf5d85d42014-10-02 05:27:56 -070029 public DefaultPath(ProviderId providerId, List<Link> links, double cost,
30 Annotations... annotations) {
31 super(providerId, source(links), destination(links), Type.INDIRECT, annotations);
tom97937552014-09-11 10:48:42 -070032 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() {
tomfc9a4ff2014-09-22 18:22:47 -070062 return Objects.hash(links);
tom97937552014-09-11 10:48:42 -070063 }
64
65 @Override
66 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -070067 if (this == obj) {
68 return true;
69 }
tom97937552014-09-11 10:48:42 -070070 if (obj instanceof DefaultPath) {
71 final DefaultPath other = (DefaultPath) obj;
72 return Objects.equals(this.links, other.links);
73 }
74 return false;
75 }
76}