blob: 3fd3eced17b5be102cc9268d14ecf6f0acffe9f5 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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;
Andrey Komarov2398d962016-09-26 15:11:23 +030020import org.onlab.graph.ScalarWeight;
21import org.onlab.graph.Weight;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.net.provider.ProviderId;
tom97937552014-09-11 10:48:42 -070023
24import java.util.List;
25import java.util.Objects;
26
27import static com.google.common.base.Preconditions.checkArgument;
28import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * Default implementation of a network path.
32 */
33public class DefaultPath extends DefaultLink implements Path {
34
35 private final List<Link> links;
Andrey Komarov2398d962016-09-26 15:11:23 +030036 private final Weight cost;
37
38 /**
39 * Creates a path from the specified source and destination using the
40 * supplied list of links.
41 *
42 * @param providerId provider identity
43 * @param links contiguous links that comprise the path
44 * @param cost unit-less path cost
45 * @param annotations optional key/value annotations
tom97937552014-09-11 10:48:42 -070046 */
Andrey Komarov2398d962016-09-26 15:11:23 +030047 public DefaultPath(ProviderId providerId, List<Link> links, Weight cost,
tomf5d85d42014-10-02 05:27:56 -070048 Annotations... annotations) {
Brian Stanke612cebf2016-05-02 10:21:33 -040049 super(providerId, source(links), destination(links), Type.INDIRECT, State.ACTIVE, annotations);
tom97937552014-09-11 10:48:42 -070050 this.links = ImmutableList.copyOf(links);
51 this.cost = cost;
52 }
53
54 @Override
55 public List<Link> links() {
56 return links;
57 }
58
59 @Override
60 public double cost() {
Andrey Komarov2398d962016-09-26 15:11:23 +030061 if (cost instanceof ScalarWeight) {
62 return ((ScalarWeight) cost).value();
63 }
64 return 0;
65 }
66
67 @Override
68 public Weight weight() {
tom97937552014-09-11 10:48:42 -070069 return cost;
70 }
71
72 // Returns the source of the first link.
73 private static ConnectPoint source(List<Link> links) {
74 checkNotNull(links, "List of path links cannot be null");
75 checkArgument(!links.isEmpty(), "List of path links cannot be empty");
76 return links.get(0).src();
77 }
78
79 // Returns the destination of the last link.
80 private static ConnectPoint destination(List<Link> links) {
81 checkNotNull(links, "List of path links cannot be null");
82 checkArgument(!links.isEmpty(), "List of path links cannot be empty");
83 return links.get(links.size() - 1).dst();
84 }
85
86 @Override
Sho SHIMIZUd01a23d2015-06-04 13:47:53 -070087 public String toString() {
88 return MoreObjects.toStringHelper(this)
89 .add("src", src())
90 .add("dst", dst())
91 .add("type", type())
92 .add("state", state())
Ray Milkey8521f812017-05-24 09:49:26 -070093 .add("expected", isExpected())
Sho SHIMIZUd01a23d2015-06-04 13:47:53 -070094 .add("links", links)
95 .add("cost", cost)
96 .toString();
97 }
98
99 @Override
tom97937552014-09-11 10:48:42 -0700100 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700101 return links.hashCode();
tom97937552014-09-11 10:48:42 -0700102 }
103
104 @Override
105 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700106 if (this == obj) {
107 return true;
108 }
tom97937552014-09-11 10:48:42 -0700109 if (obj instanceof DefaultPath) {
110 final DefaultPath other = (DefaultPath) obj;
111 return Objects.equals(this.links, other.links);
112 }
113 return false;
114 }
115}