blob: c4e03987e9dcc2d518ce2ebd0c16f1108dd9aaa7 [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
19import org.onosproject.net.provider.ProviderId;
20
21import java.util.List;
22import java.util.Objects;
Nikhil Cheerla1cf0f9a2015-07-09 12:26:48 -070023
24/**
25 * Default implementation of a network disjoint path pair.
26 */
27public class DefaultDisjointPath extends DefaultPath implements DisjointPath {
28
29 private final DefaultPath path1;
30 private final DefaultPath path2;
31
32 boolean usingPath1 = true;
33
34 /**
35 * Creates a disjoint path pair from two default paths.
36 *
37 * @param providerId provider identity
38 * @param path1 primary path
39 * @param path2 backup path
40 */
41 public DefaultDisjointPath(ProviderId providerId, DefaultPath path1, DefaultPath path2) {
Yuta HIGUCHIdf970132016-08-09 11:18:30 -070042 // Note: cost passed to super will never be used
43 super(providerId, path1.links(), path1.cost());
Nikhil Cheerla1cf0f9a2015-07-09 12:26:48 -070044 this.path1 = path1;
45 this.path2 = path2;
46 }
47
Jayasree Ghoshd3ff5402016-08-17 18:41:19 +053048 /**
49 * Creates a disjoint path pair from single default paths.
50 *
51 * @param providerId provider identity
52 * @param path1 primary path
53 */
54 public DefaultDisjointPath(ProviderId providerId, DefaultPath path1) {
55 this(providerId, path1, null);
56 }
57
Nikhil Cheerla1cf0f9a2015-07-09 12:26:48 -070058 @Override
59 public List<Link> links() {
60 if (usingPath1) {
61 return path1.links();
62 } else {
63 return path2.links();
64 }
65 }
66
67 @Override
68 public double cost() {
69 if (usingPath1) {
70 return path1.cost();
71 }
72 return path2.cost();
73 }
74
75 @Override
76 public Path primary() {
77 return path1;
78 }
79
80 @Override
81 public Path backup() {
82 return path2;
83 }
84
85 @Override
86 public int hashCode() {
Yuta HIGUCHIdf970132016-08-09 11:18:30 -070087 // Note: DisjointPath with primary and secondary swapped
88 // must result in same hashCode
89 return Objects.hash(Objects.hashCode(path1) + Objects.hashCode(path2), src(), dst());
Nikhil Cheerla1cf0f9a2015-07-09 12:26:48 -070090 }
91
92 @Override
93 public boolean equals(Object obj) {
94 if (this == obj) {
95 return true;
96 }
97 if (obj instanceof DefaultDisjointPath) {
98 final DefaultDisjointPath other = (DefaultDisjointPath) obj;
Yuta HIGUCHIdf970132016-08-09 11:18:30 -070099 return (Objects.equals(this.path1, other.path1) && Objects.equals(this.path2, other.path2)) ||
100 (Objects.equals(this.path1, other.path2) && Objects.equals(this.path2, other.path1));
Nikhil Cheerla1cf0f9a2015-07-09 12:26:48 -0700101 }
102 return false;
103 }
104
105 @Override
106 public boolean useBackup() {
107 if (path2 == null || path2.links() == null) {
108 return false;
109 }
110 usingPath1 = !usingPath1;
111 return true;
112 }
113}