blob: 9f0480d7cdee3dda27bf265f6ce5d278a7ba703b [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net;
tom97937552014-09-11 10:48:42 -070017
Sho SHIMIZUd01a23d2015-06-04 13:47:53 -070018import com.google.common.base.MoreObjects;
tom97937552014-09-11 10:48:42 -070019import com.google.common.collect.ImmutableList;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.net.provider.ProviderId;
tom97937552014-09-11 10:48:42 -070021
22import java.util.List;
23import java.util.Objects;
24
25import static com.google.common.base.Preconditions.checkArgument;
26import static com.google.common.base.Preconditions.checkNotNull;
27
28/**
29 * Default implementation of a network path.
30 */
31public 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
tomf5d85d42014-10-02 05:27:56 -070043 * @param annotations optional key/value annotations
tom97937552014-09-11 10:48:42 -070044 */
tomf5d85d42014-10-02 05:27:56 -070045 public DefaultPath(ProviderId providerId, List<Link> links, double cost,
46 Annotations... annotations) {
47 super(providerId, source(links), destination(links), Type.INDIRECT, annotations);
tom97937552014-09-11 10:48:42 -070048 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 SHIMIZUd01a23d2015-06-04 13:47:53 -070077 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
tom97937552014-09-11 10:48:42 -070090 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -070091 return links.hashCode();
tom97937552014-09-11 10:48:42 -070092 }
93
94 @Override
95 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -070096 if (this == obj) {
97 return true;
98 }
tom97937552014-09-11 10:48:42 -070099 if (obj instanceof DefaultPath) {
100 final DefaultPath other = (DefaultPath) obj;
101 return Objects.equals(this.links, other.links);
102 }
103 return false;
104 }
105}