blob: ab05963d0e9377ee2d6edd40d1fe084c841d8fe3 [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;
23import static com.google.common.collect.ImmutableSet.of;
24
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) {
43 super(providerId, path1.links(), path1.cost() + path2.cost());
44 this.path1 = path1;
45 this.path2 = path2;
46 }
47
48 @Override
49 public List<Link> links() {
50 if (usingPath1) {
51 return path1.links();
52 } else {
53 return path2.links();
54 }
55 }
56
57 @Override
58 public double cost() {
59 if (usingPath1) {
60 return path1.cost();
61 }
62 return path2.cost();
63 }
64
65 @Override
66 public Path primary() {
67 return path1;
68 }
69
70 @Override
71 public Path backup() {
72 return path2;
73 }
74
75 @Override
76 public int hashCode() {
77 return Objects.hash(of(path1, path2), src(), dst());
78 }
79
80 @Override
81 public boolean equals(Object obj) {
82 if (this == obj) {
83 return true;
84 }
85 if (obj instanceof DefaultDisjointPath) {
86 final DefaultDisjointPath other = (DefaultDisjointPath) obj;
87 return Objects.equals(this.path1, other.path1) && Objects.equals(this.path2, other.path2);
88 }
89 return false;
90 }
91
92 @Override
93 public boolean useBackup() {
94 if (path2 == null || path2.links() == null) {
95 return false;
96 }
97 usingPath1 = !usingPath1;
98 return true;
99 }
100}