Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.intent.impl; |
| 2 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 3 | import org.hamcrest.Matchers; |
| 4 | import org.junit.Test; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 5 | import org.onlab.onos.ApplicationId; |
| 6 | import org.onlab.onos.TestApplicationId; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 7 | import org.onlab.onos.net.ConnectPoint; |
| 8 | import org.onlab.onos.net.ElementId; |
| 9 | import org.onlab.onos.net.Path; |
| 10 | import org.onlab.onos.net.flow.TrafficSelector; |
| 11 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 12 | import org.onlab.onos.net.intent.Intent; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 13 | import org.onlab.onos.net.intent.IntentTestsMocks; |
| 14 | import org.onlab.onos.net.intent.LinkCollectionIntent; |
| 15 | import org.onlab.onos.net.intent.MultiPointToSinglePointIntent; |
| 16 | import org.onlab.onos.net.topology.LinkWeight; |
| 17 | import org.onlab.onos.net.topology.PathService; |
| 18 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 19 | import java.util.HashSet; |
| 20 | import java.util.List; |
| 21 | import java.util.Set; |
| 22 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 23 | import static org.hamcrest.CoreMatchers.notNullValue; |
| 24 | import static org.hamcrest.MatcherAssert.assertThat; |
| 25 | import static org.hamcrest.Matchers.hasSize; |
| 26 | import static org.hamcrest.Matchers.is; |
| 27 | import static org.onlab.onos.net.NetTestTools.connectPoint; |
| 28 | import static org.onlab.onos.net.NetTestTools.createPath; |
| 29 | import static org.onlab.onos.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath; |
| 30 | |
| 31 | /** |
| 32 | * Unit tests for the MultiPointToSinglePoint intent compiler. |
| 33 | */ |
| 34 | public class TestMultiPointToSinglePointIntentCompiler { |
| 35 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 36 | private static final ApplicationId APPID = new TestApplicationId("foo"); |
| 37 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 38 | private TrafficSelector selector = new IntentTestsMocks.MockSelector(); |
| 39 | private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); |
| 40 | |
| 41 | /** |
| 42 | * Mock path service for creating paths within the test. |
| 43 | */ |
| 44 | private static class MockPathService implements PathService { |
| 45 | |
| 46 | final String[] pathHops; |
| 47 | |
| 48 | /** |
| 49 | * Constructor that provides a set of hops to mock. |
| 50 | * |
| 51 | * @param pathHops path hops to mock |
| 52 | */ |
| 53 | MockPathService(String[] pathHops) { |
| 54 | this.pathHops = pathHops; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public Set<Path> getPaths(ElementId src, ElementId dst) { |
| 59 | Set<Path> result = new HashSet<>(); |
| 60 | |
| 61 | String[] allHops = new String[pathHops.length + 1]; |
| 62 | allHops[0] = src.toString(); |
| 63 | System.arraycopy(pathHops, 0, allHops, 1, pathHops.length); |
| 64 | |
| 65 | result.add(createPath(allHops)); |
| 66 | return result; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) { |
| 71 | return null; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Creates a MultiPointToSinglePoint intent for a group of ingress points |
| 77 | * and an egress point. |
| 78 | * |
| 79 | * @param ingressIds array of ingress device ids |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 80 | * @param egressId device id of the egress point |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 81 | * @return MultiPointToSinglePoint intent |
| 82 | */ |
| 83 | private MultiPointToSinglePointIntent makeIntent(String[] ingressIds, String egressId) { |
| 84 | Set<ConnectPoint> ingressPoints = new HashSet<>(); |
| 85 | ConnectPoint egressPoint = connectPoint(egressId, 1); |
| 86 | |
| 87 | for (String ingressId : ingressIds) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 88 | ingressPoints.add(connectPoint(ingressId, 1)); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 91 | return new MultiPointToSinglePointIntent(APPID, selector, treatment, |
| 92 | ingressPoints, egressPoint); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Creates a compiler for MultiPointToSinglePoint intents. |
| 97 | * |
| 98 | * @param hops hops to use while computing paths for this intent |
| 99 | * @return MultiPointToSinglePoint intent |
| 100 | */ |
| 101 | private MultiPointToSinglePointIntentCompiler makeCompiler(String[] hops) { |
| 102 | MultiPointToSinglePointIntentCompiler compiler = |
| 103 | new MultiPointToSinglePointIntentCompiler(); |
| 104 | compiler.pathService = new MockPathService(hops); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 105 | return compiler; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Tests a single ingress point with 8 hops to its egress point. |
| 110 | */ |
| 111 | @Test |
| 112 | public void testSingleLongPathCompilation() { |
| 113 | |
| 114 | String[] ingress = {"ingress"}; |
| 115 | String egress = "egress"; |
| 116 | |
| 117 | MultiPointToSinglePointIntent intent = makeIntent(ingress, egress); |
| 118 | assertThat(intent, is(notNullValue())); |
| 119 | |
| 120 | String[] hops = {"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 121 | egress}; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 122 | MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops); |
| 123 | assertThat(compiler, is(notNullValue())); |
| 124 | |
| 125 | List<Intent> result = compiler.compile(intent); |
| 126 | assertThat(result, is(Matchers.notNullValue())); |
| 127 | assertThat(result, hasSize(1)); |
| 128 | Intent resultIntent = result.get(0); |
| 129 | assertThat(resultIntent instanceof LinkCollectionIntent, is(true)); |
| 130 | |
| 131 | if (resultIntent instanceof LinkCollectionIntent) { |
| 132 | LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent; |
| 133 | assertThat(linkIntent.links(), hasSize(9)); |
| 134 | assertThat(linkIntent.links(), linksHasPath("ingress", "h1")); |
| 135 | assertThat(linkIntent.links(), linksHasPath("h1", "h2")); |
| 136 | assertThat(linkIntent.links(), linksHasPath("h2", "h3")); |
| 137 | assertThat(linkIntent.links(), linksHasPath("h4", "h5")); |
| 138 | assertThat(linkIntent.links(), linksHasPath("h5", "h6")); |
| 139 | assertThat(linkIntent.links(), linksHasPath("h7", "h8")); |
| 140 | assertThat(linkIntent.links(), linksHasPath("h8", "egress")); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Tests a simple topology where two ingress points share some path segments |
| 146 | * and some path segments are not shared. |
| 147 | */ |
| 148 | @Test |
| 149 | public void testTwoIngressCompilation() { |
| 150 | String[] ingress = {"ingress1", "ingress2"}; |
| 151 | String egress = "egress"; |
| 152 | |
| 153 | MultiPointToSinglePointIntent intent = makeIntent(ingress, egress); |
| 154 | assertThat(intent, is(notNullValue())); |
| 155 | |
| 156 | final String[] hops = {"inner1", "inner2", egress}; |
| 157 | MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops); |
| 158 | assertThat(compiler, is(notNullValue())); |
| 159 | |
| 160 | List<Intent> result = compiler.compile(intent); |
| 161 | assertThat(result, is(notNullValue())); |
| 162 | assertThat(result, hasSize(1)); |
| 163 | Intent resultIntent = result.get(0); |
| 164 | assertThat(resultIntent instanceof LinkCollectionIntent, is(true)); |
| 165 | |
| 166 | if (resultIntent instanceof LinkCollectionIntent) { |
| 167 | LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent; |
| 168 | assertThat(linkIntent.links(), hasSize(4)); |
| 169 | assertThat(linkIntent.links(), linksHasPath("ingress1", "inner1")); |
| 170 | assertThat(linkIntent.links(), linksHasPath("ingress2", "inner1")); |
| 171 | assertThat(linkIntent.links(), linksHasPath("inner1", "inner2")); |
| 172 | assertThat(linkIntent.links(), linksHasPath("inner2", "egress")); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Tests a large number of ingress points that share a common path to the |
| 178 | * egress point. |
| 179 | */ |
| 180 | @Test |
| 181 | public void testMultiIngressCompilation() { |
| 182 | String[] ingress = {"i1", "i2", "i3", "i4", "i5", |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 183 | "i6", "i7", "i8", "i9", "i10"}; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 184 | String egress = "e"; |
| 185 | |
| 186 | MultiPointToSinglePointIntent intent = makeIntent(ingress, egress); |
| 187 | assertThat(intent, is(notNullValue())); |
| 188 | |
| 189 | final String[] hops = {"n1", egress}; |
| 190 | MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops); |
| 191 | assertThat(compiler, is(notNullValue())); |
| 192 | |
| 193 | List<Intent> result = compiler.compile(intent); |
| 194 | assertThat(result, is(notNullValue())); |
| 195 | assertThat(result, hasSize(1)); |
| 196 | Intent resultIntent = result.get(0); |
| 197 | assertThat(resultIntent instanceof LinkCollectionIntent, is(true)); |
| 198 | |
| 199 | if (resultIntent instanceof LinkCollectionIntent) { |
| 200 | LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent; |
| 201 | assertThat(linkIntent.links(), hasSize(ingress.length + 1)); |
| 202 | for (String ingressToCheck : ingress) { |
| 203 | assertThat(linkIntent.links(), |
| 204 | linksHasPath(ingressToCheck, |
| 205 | "n1")); |
| 206 | } |
| 207 | assertThat(linkIntent.links(), linksHasPath("n1", egress)); |
| 208 | } |
| 209 | } |
| 210 | } |