Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 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 | |
| 17 | package org.onlab.graph; |
| 18 | |
| 19 | import org.junit.Test; |
| 20 | import java.util.Set; |
| 21 | import java.util.HashSet; |
| 22 | import java.util.Map; |
| 23 | import java.util.HashMap; |
| 24 | |
| 25 | import static com.google.common.collect.ImmutableSet.of; |
| 26 | import static org.junit.Assert.assertTrue; |
| 27 | |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Test of the Suurballe backup path algorithm. |
| 32 | */ |
| 33 | public class SRLGGraphSearchTest extends BreadthFirstSearchTest { |
| 34 | @Override |
| 35 | protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() { |
| 36 | return new SRLGGraphSearch<TestVertex, TestEdge>(null); |
| 37 | } |
| 38 | |
| 39 | public void setWeights() { |
| 40 | weight = new EdgeWeight<TestVertex, TestEdge>() { |
| 41 | @Override |
| 42 | public double weight(TestEdge edge) { |
| 43 | return edge.weight(); |
| 44 | } |
| 45 | }; |
| 46 | } |
| 47 | public void setDefaultWeights() { |
| 48 | weight = null; |
| 49 | } |
| 50 | @Override |
| 51 | public void defaultGraphTest() { |
| 52 | |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void defaultHopCountWeight() { |
| 57 | |
| 58 | } |
| 59 | |
| 60 | @Test |
| 61 | public void onePathPair() { |
| 62 | setDefaultWeights(); |
| 63 | TestEdge aB = new TestEdge(A, B, 1); |
| 64 | TestEdge bC = new TestEdge(B, C, 1); |
| 65 | TestEdge aD = new TestEdge(A, D, 1); |
| 66 | TestEdge dC = new TestEdge(D, C, 1); |
| 67 | Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D), |
| 68 | of(aB, bC, aD, dC)); |
| 69 | Map<TestEdge, Integer> riskProfile = new HashMap<TestEdge, Integer>(); |
| 70 | riskProfile.put(aB, 0); |
| 71 | riskProfile.put(bC, 0); |
| 72 | riskProfile.put(aD, 1); |
| 73 | riskProfile.put(dC, 1); |
| 74 | SRLGGraphSearch<TestVertex, TestEdge> search = |
| 75 | new SRLGGraphSearch<TestVertex, TestEdge>(2, riskProfile); |
| 76 | Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, C, weight, GraphPathSearch.ALL_PATHS).paths(); |
| 77 | System.out.println("\n\n\n" + paths + "\n\n\n"); |
| 78 | assertTrue("one disjoint path pair found", paths.size() == 1); |
| 79 | checkIsDisjoint(paths.iterator().next(), riskProfile); |
| 80 | } |
| 81 | public void checkIsDisjoint(Path<TestVertex, TestEdge> p, Map<TestEdge, Integer> risks) { |
| 82 | assertTrue("The path is not a DisjointPathPair", (p instanceof DisjointPathPair)); |
| 83 | DisjointPathPair<TestVertex, TestEdge> q = (DisjointPathPair) p; |
| 84 | Set<Integer> p1Risks = new HashSet<Integer>(); |
| 85 | Set<Integer> p2Risks = new HashSet<Integer>(); |
| 86 | for (TestEdge e: q.edges()) { |
| 87 | p1Risks.add(risks.get(e)); |
| 88 | } |
| 89 | if (!q.hasBackup()) { |
| 90 | return; |
| 91 | } |
| 92 | Path<TestVertex, TestEdge> pq = q.path2; |
| 93 | for (TestEdge e: pq.edges()) { |
| 94 | assertTrue("The paths are not disjoint", !p1Risks.contains(risks.get(e))); |
| 95 | } |
| 96 | } |
| 97 | @Test |
| 98 | public void complexGraphTest() { |
| 99 | setDefaultWeights(); |
| 100 | TestEdge aB = new TestEdge(A, B, 1); |
| 101 | TestEdge bC = new TestEdge(B, C, 1); |
| 102 | TestEdge aD = new TestEdge(A, D, 1); |
| 103 | TestEdge dC = new TestEdge(D, C, 1); |
| 104 | TestEdge cE = new TestEdge(C, E, 1); |
| 105 | TestEdge bE = new TestEdge(B, E, 1); |
| 106 | Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D, E), |
| 107 | of(aB, bC, aD, dC, cE, bE)); |
| 108 | Map<TestEdge, Integer> riskProfile = new HashMap<TestEdge, Integer>(); |
| 109 | riskProfile.put(aB, 0); |
| 110 | riskProfile.put(bC, 0); |
| 111 | riskProfile.put(aD, 1); |
| 112 | riskProfile.put(dC, 1); |
| 113 | riskProfile.put(cE, 2); |
| 114 | riskProfile.put(bE, 3); |
| 115 | SRLGGraphSearch<TestVertex, TestEdge> search = |
| 116 | new SRLGGraphSearch<TestVertex, TestEdge>(4, riskProfile); |
| 117 | Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, E, weight, GraphPathSearch.ALL_PATHS).paths(); |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void multiplePathGraphTest() { |
| 122 | setDefaultWeights(); |
| 123 | TestEdge aB = new TestEdge(A, B, 1); |
| 124 | TestEdge bE = new TestEdge(B, E, 1); |
| 125 | TestEdge aD = new TestEdge(A, D, 1); |
| 126 | TestEdge dE = new TestEdge(D, E, 1); |
| 127 | TestEdge aC = new TestEdge(A, C, 1); |
| 128 | TestEdge cE = new TestEdge(C, E, 1); |
| 129 | Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D, E), |
| 130 | of(aB, bE, aD, dE, aC, cE)); |
| 131 | Map<TestEdge, Integer> riskProfile = new HashMap<TestEdge, Integer>(); |
| 132 | riskProfile.put(aB, 0); |
| 133 | riskProfile.put(bE, 1); |
| 134 | riskProfile.put(aD, 2); |
| 135 | riskProfile.put(dE, 3); |
| 136 | riskProfile.put(aC, 4); |
| 137 | riskProfile.put(cE, 5); |
| 138 | SRLGGraphSearch<TestVertex, TestEdge> search = |
| 139 | new SRLGGraphSearch<TestVertex, TestEdge>(6, riskProfile); |
| 140 | Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, E, weight, GraphPathSearch.ALL_PATHS).paths(); |
| 141 | assertTrue("> one disjoint path pair found", paths.size() >= 1); |
| 142 | checkIsDisjoint(paths.iterator().next(), riskProfile); |
| 143 | } |
| 144 | @Test |
| 145 | public void onePath() { |
| 146 | setDefaultWeights(); |
| 147 | TestEdge aB = new TestEdge(A, B, 1); |
| 148 | TestEdge bC = new TestEdge(B, C, 1); |
| 149 | TestEdge aD = new TestEdge(A, D, 1); |
| 150 | TestEdge dC = new TestEdge(D, C, 1); |
| 151 | Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D), |
| 152 | of(aB, bC, aD, dC)); |
| 153 | Map<TestEdge, Integer> riskProfile = new HashMap<TestEdge, Integer>(); |
| 154 | riskProfile.put(aB, 0); |
| 155 | riskProfile.put(bC, 0); |
| 156 | riskProfile.put(aD, 1); |
| 157 | riskProfile.put(dC, 0); |
| 158 | SRLGGraphSearch<TestVertex, TestEdge> search = |
| 159 | new SRLGGraphSearch<TestVertex, TestEdge>(2, riskProfile); |
| 160 | Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, C, weight, GraphPathSearch.ALL_PATHS).paths(); |
| 161 | System.out.println(paths); |
| 162 | assertTrue("no disjoint path pairs found", paths.size() == 0); |
| 163 | } |
| 164 | @Test |
| 165 | public void noPath() { |
| 166 | setDefaultWeights(); |
| 167 | TestEdge aB = new TestEdge(A, B, 1); |
| 168 | TestEdge bC = new TestEdge(B, C, 1); |
| 169 | TestEdge aD = new TestEdge(A, D, 1); |
| 170 | TestEdge dC = new TestEdge(D, C, 1); |
| 171 | Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D, E), |
| 172 | of(aB, bC, aD, dC)); |
| 173 | Map<TestEdge, Integer> riskProfile = new HashMap<>(); |
| 174 | riskProfile.put(aB, 0); |
| 175 | riskProfile.put(bC, 0); |
| 176 | riskProfile.put(aD, 1); |
| 177 | riskProfile.put(dC, 0); |
| 178 | SRLGGraphSearch<TestVertex, TestEdge> search = |
| 179 | new SRLGGraphSearch<>(2, riskProfile); |
| 180 | Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, E, weight, GraphPathSearch.ALL_PATHS).paths(); |
| 181 | assertTrue("no disjoint path pairs found", paths.size() == 0); |
| 182 | } |
| 183 | } |