aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/core/api/src/main/java/org/onosproject/net/DefaultDisjointPath.java
blob: 4895964f3649cb49a467e8cce8141915ed6d5a87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onosproject.net;

import org.onosproject.net.provider.ProviderId;

import java.util.List;
import java.util.Objects;
import static com.google.common.collect.ImmutableSet.of;

/**
 * Default implementation of a network disjoint path pair.
 */
public class DefaultDisjointPath extends DefaultPath implements DisjointPath {

    private final DefaultPath path1;
    private final DefaultPath path2;

    boolean usingPath1 = true;

    /**
     * Creates a disjoint path pair from two default paths.
     *
     * @param providerId provider identity
     * @param path1      primary path
     * @param path2      backup path
     */
    public DefaultDisjointPath(ProviderId providerId, DefaultPath path1, DefaultPath path2) {
        super(providerId, path1.links(), path1.cost() + path2.cost());
        this.path1 = path1;
        this.path2 = path2;
    }

    @Override
    public List<Link> links() {
        if (usingPath1) {
            return path1.links();
        } else {
            return path2.links();
        }
    }

    @Override
    public double cost() {
        if (usingPath1) {
            return path1.cost();
        }
        return path2.cost();
    }

    @Override
    public Path primary() {
        return path1;
    }

    @Override
    public Path backup() {
        return path2;
    }

    @Override
    public int hashCode() {
        return Objects.hash(of(path1, path2), src(), dst());
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof DefaultDisjointPath) {
            final DefaultDisjointPath other = (DefaultDisjointPath) obj;
            return Objects.equals(this.path1, other.path1) && Objects.equals(this.path2, other.path2);
        }
        return false;
    }

    @Override
    public boolean useBackup() {
        if (path2 == null || path2.links() == null) {
            return false;
        }
        usingPath1 = !usingPath1;
        return true;
    }
}