blob: 63c9e884639aabe0f652ae54210b8137cc2f5ee3 [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() {
60 return 31 * super.hashCode() + Objects.hash(links);
61 }
62
63 @Override
64 public boolean equals(Object obj) {
65 if (obj instanceof DefaultPath) {
66 final DefaultPath other = (DefaultPath) obj;
67 return Objects.equals(this.links, other.links);
68 }
69 return false;
70 }
71}