blob: 016bb69ba9f42e9386376839d1a70768b5de17b2 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
tom97937552014-09-11 10:48:42 -070016package org.onlab.onos.net;
17
18import com.google.common.collect.ImmutableList;
19import org.onlab.onos.net.provider.ProviderId;
20
21import java.util.List;
22import java.util.Objects;
23
24import static com.google.common.base.Preconditions.checkArgument;
25import static com.google.common.base.Preconditions.checkNotNull;
26
27/**
28 * Default implementation of a network path.
29 */
30public class DefaultPath extends DefaultLink implements Path {
31
32 private final List<Link> links;
33 private final double cost;
34
35 /**
36 * Creates a path from the specified source and destination using the
37 * supplied list of links.
38 *
39 * @param providerId provider identity
40 * @param links contiguous links that comprise the path
41 * @param cost unit-less path cost
tomf5d85d42014-10-02 05:27:56 -070042 * @param annotations optional key/value annotations
tom97937552014-09-11 10:48:42 -070043 */
tomf5d85d42014-10-02 05:27:56 -070044 public DefaultPath(ProviderId providerId, List<Link> links, double cost,
45 Annotations... annotations) {
46 super(providerId, source(links), destination(links), Type.INDIRECT, annotations);
tom97937552014-09-11 10:48:42 -070047 this.links = ImmutableList.copyOf(links);
48 this.cost = cost;
49 }
50
51 @Override
52 public List<Link> links() {
53 return links;
54 }
55
56 @Override
57 public double cost() {
58 return cost;
59 }
60
61 // Returns the source of the first link.
62 private static ConnectPoint source(List<Link> links) {
63 checkNotNull(links, "List of path links cannot be null");
64 checkArgument(!links.isEmpty(), "List of path links cannot be empty");
65 return links.get(0).src();
66 }
67
68 // Returns the destination of the last link.
69 private static ConnectPoint destination(List<Link> links) {
70 checkNotNull(links, "List of path links cannot be null");
71 checkArgument(!links.isEmpty(), "List of path links cannot be empty");
72 return links.get(links.size() - 1).dst();
73 }
74
75 @Override
76 public int hashCode() {
tomfc9a4ff2014-09-22 18:22:47 -070077 return Objects.hash(links);
tom97937552014-09-11 10:48:42 -070078 }
79
80 @Override
81 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -070082 if (this == obj) {
83 return true;
84 }
tom97937552014-09-11 10:48:42 -070085 if (obj instanceof DefaultPath) {
86 final DefaultPath other = (DefaultPath) obj;
87 return Objects.equals(this.links, other.links);
88 }
89 return false;
90 }
91}