Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 17 | |
Sho SHIMIZU | d01a23d | 2015-06-04 13:47:53 -0700 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.net.provider.ProviderId; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 21 | |
| 22 | import java.util.List; |
| 23 | import java.util.Objects; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkArgument; |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | |
| 28 | /** |
| 29 | * Default implementation of a network path. |
| 30 | */ |
| 31 | public class DefaultPath extends DefaultLink implements Path { |
| 32 | |
| 33 | private final List<Link> links; |
| 34 | private final double cost; |
| 35 | |
| 36 | /** |
| 37 | * Creates a path from the specified source and destination using the |
| 38 | * supplied list of links. |
| 39 | * |
| 40 | * @param providerId provider identity |
| 41 | * @param links contiguous links that comprise the path |
| 42 | * @param cost unit-less path cost |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 43 | * @param annotations optional key/value annotations |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 44 | */ |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 45 | public DefaultPath(ProviderId providerId, List<Link> links, double cost, |
| 46 | Annotations... annotations) { |
| 47 | super(providerId, source(links), destination(links), Type.INDIRECT, annotations); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 48 | this.links = ImmutableList.copyOf(links); |
| 49 | this.cost = cost; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public List<Link> links() { |
| 54 | return links; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public double cost() { |
| 59 | return cost; |
| 60 | } |
| 61 | |
| 62 | // Returns the source of the first link. |
| 63 | private static ConnectPoint source(List<Link> links) { |
| 64 | checkNotNull(links, "List of path links cannot be null"); |
| 65 | checkArgument(!links.isEmpty(), "List of path links cannot be empty"); |
| 66 | return links.get(0).src(); |
| 67 | } |
| 68 | |
| 69 | // Returns the destination of the last link. |
| 70 | private static ConnectPoint destination(List<Link> links) { |
| 71 | checkNotNull(links, "List of path links cannot be null"); |
| 72 | checkArgument(!links.isEmpty(), "List of path links cannot be empty"); |
| 73 | return links.get(links.size() - 1).dst(); |
| 74 | } |
| 75 | |
| 76 | @Override |
Sho SHIMIZU | d01a23d | 2015-06-04 13:47:53 -0700 | [diff] [blame] | 77 | public String toString() { |
| 78 | return MoreObjects.toStringHelper(this) |
| 79 | .add("src", src()) |
| 80 | .add("dst", dst()) |
| 81 | .add("type", type()) |
| 82 | .add("state", state()) |
| 83 | .add("durable", isDurable()) |
| 84 | .add("links", links) |
| 85 | .add("cost", cost) |
| 86 | .toString(); |
| 87 | } |
| 88 | |
| 89 | @Override |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 90 | public int hashCode() { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 91 | return links.hashCode(); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 96 | if (this == obj) { |
| 97 | return true; |
| 98 | } |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 99 | if (obj instanceof DefaultPath) { |
| 100 | final DefaultPath other = (DefaultPath) obj; |
| 101 | return Objects.equals(this.links, other.links); |
| 102 | } |
| 103 | return false; |
| 104 | } |
| 105 | } |