blob: b68110d233c47868ccfd468b9bb6fd6fea5f0dd3 [file] [log] [blame]
Nikhil Cheerla1cf0f9a2015-07-09 12:26:48 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Nikhil Cheerla1cf0f9a2015-07-09 12:26:48 -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 */
16
17package org.onosproject.net;
18
Andrey Komarov2398d962016-09-26 15:11:23 +030019import org.onlab.graph.Weight;
Nikhil Cheerla1cf0f9a2015-07-09 12:26:48 -070020import org.onosproject.net.provider.ProviderId;
21
22import java.util.List;
23import java.util.Objects;
Nikhil Cheerla1cf0f9a2015-07-09 12:26:48 -070024
25/**
26 * Default implementation of a network disjoint path pair.
27 */
28public class DefaultDisjointPath extends DefaultPath implements DisjointPath {
29
30 private final DefaultPath path1;
31 private final DefaultPath path2;
32
33 boolean usingPath1 = true;
34
35 /**
36 * Creates a disjoint path pair from two default paths.
37 *
38 * @param providerId provider identity
39 * @param path1 primary path
40 * @param path2 backup path
41 */
42 public DefaultDisjointPath(ProviderId providerId, DefaultPath path1, DefaultPath path2) {
Yuta HIGUCHIdf970132016-08-09 11:18:30 -070043 // Note: cost passed to super will never be used
Andrey Komarov2398d962016-09-26 15:11:23 +030044 super(providerId, path1.links(), path1.weight());
Nikhil Cheerla1cf0f9a2015-07-09 12:26:48 -070045 this.path1 = path1;
46 this.path2 = path2;
47 }
48
Jayasree Ghoshd3ff5402016-08-17 18:41:19 +053049 /**
50 * Creates a disjoint path pair from single default paths.
51 *
52 * @param providerId provider identity
53 * @param path1 primary path
54 */
55 public DefaultDisjointPath(ProviderId providerId, DefaultPath path1) {
56 this(providerId, path1, null);
57 }
58
Nikhil Cheerla1cf0f9a2015-07-09 12:26:48 -070059 @Override
60 public List<Link> links() {
61 if (usingPath1) {
62 return path1.links();
63 } else {
64 return path2.links();
65 }
66 }
67
68 @Override
69 public double cost() {
Andrey Komarov2398d962016-09-26 15:11:23 +030070 return usingPath1 ? path1.cost() : path2.cost();
71 }
72
73 @Override
74 public Weight weight() {
75 return usingPath1 ? path1.weight() : path2.weight();
Nikhil Cheerla1cf0f9a2015-07-09 12:26:48 -070076 }
77
78 @Override
79 public Path primary() {
80 return path1;
81 }
82
83 @Override
84 public Path backup() {
85 return path2;
86 }
87
88 @Override
89 public int hashCode() {
Yuta HIGUCHIdf970132016-08-09 11:18:30 -070090 // Note: DisjointPath with primary and secondary swapped
91 // must result in same hashCode
92 return Objects.hash(Objects.hashCode(path1) + Objects.hashCode(path2), src(), dst());
Nikhil Cheerla1cf0f9a2015-07-09 12:26:48 -070093 }
94
95 @Override
96 public boolean equals(Object obj) {
97 if (this == obj) {
98 return true;
99 }
100 if (obj instanceof DefaultDisjointPath) {
101 final DefaultDisjointPath other = (DefaultDisjointPath) obj;
Yuta HIGUCHIdf970132016-08-09 11:18:30 -0700102 return (Objects.equals(this.path1, other.path1) && Objects.equals(this.path2, other.path2)) ||
103 (Objects.equals(this.path1, other.path2) && Objects.equals(this.path2, other.path1));
Nikhil Cheerla1cf0f9a2015-07-09 12:26:48 -0700104 }
105 return false;
106 }
107
108 @Override
109 public boolean useBackup() {
110 if (path2 == null || path2.links() == null) {
111 return false;
112 }
113 usingPath1 = !usingPath1;
114 return true;
115 }
116}