blob: 79677aca4690f2dea43e1ae35815a50b27c8369e [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;
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
46 *
47 * @deprecated in Junco (1.9.0)
48 */
49 @Deprecated
50 public DefaultPath(ProviderId providerId, List<Link> links, double cost,
51 Annotations... annotations) {
52 super(providerId, source(links), destination(links), Type.INDIRECT,
53 State.ACTIVE, annotations);
54 this.links = ImmutableList.copyOf(links);
55 this.cost = new ScalarWeight(cost);
56 }
tom97937552014-09-11 10:48:42 -070057
58 /**
59 * Creates a path from the specified source and destination using the
60 * supplied list of links.
61 *
62 * @param providerId provider identity
63 * @param links contiguous links that comprise the path
64 * @param cost unit-less path cost
tomf5d85d42014-10-02 05:27:56 -070065 * @param annotations optional key/value annotations
tom97937552014-09-11 10:48:42 -070066 */
Andrey Komarov2398d962016-09-26 15:11:23 +030067 public DefaultPath(ProviderId providerId, List<Link> links, Weight cost,
tomf5d85d42014-10-02 05:27:56 -070068 Annotations... annotations) {
Brian Stanke612cebf2016-05-02 10:21:33 -040069 super(providerId, source(links), destination(links), Type.INDIRECT, State.ACTIVE, annotations);
tom97937552014-09-11 10:48:42 -070070 this.links = ImmutableList.copyOf(links);
71 this.cost = cost;
72 }
73
74 @Override
75 public List<Link> links() {
76 return links;
77 }
78
79 @Override
80 public double cost() {
Andrey Komarov2398d962016-09-26 15:11:23 +030081 if (cost instanceof ScalarWeight) {
82 return ((ScalarWeight) cost).value();
83 }
84 return 0;
85 }
86
87 @Override
88 public Weight weight() {
tom97937552014-09-11 10:48:42 -070089 return cost;
90 }
91
92 // Returns the source of the first link.
93 private static ConnectPoint source(List<Link> links) {
94 checkNotNull(links, "List of path links cannot be null");
95 checkArgument(!links.isEmpty(), "List of path links cannot be empty");
96 return links.get(0).src();
97 }
98
99 // Returns the destination of the last link.
100 private static ConnectPoint destination(List<Link> links) {
101 checkNotNull(links, "List of path links cannot be null");
102 checkArgument(!links.isEmpty(), "List of path links cannot be empty");
103 return links.get(links.size() - 1).dst();
104 }
105
106 @Override
Sho SHIMIZUd01a23d2015-06-04 13:47:53 -0700107 public String toString() {
108 return MoreObjects.toStringHelper(this)
109 .add("src", src())
110 .add("dst", dst())
111 .add("type", type())
112 .add("state", state())
113 .add("durable", isDurable())
114 .add("links", links)
115 .add("cost", cost)
116 .toString();
117 }
118
119 @Override
tom97937552014-09-11 10:48:42 -0700120 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700121 return links.hashCode();
tom97937552014-09-11 10:48:42 -0700122 }
123
124 @Override
125 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700126 if (this == obj) {
127 return true;
128 }
tom97937552014-09-11 10:48:42 -0700129 if (obj instanceof DefaultPath) {
130 final DefaultPath other = (DefaultPath) obj;
131 return Objects.equals(this.links, other.links);
132 }
133 return false;
134 }
135}