Sho SHIMIZU | 6c28f83 | 2015-02-20 16:12:19 -0800 | [diff] [blame] | 1 | package org.onosproject.net.intent.impl.compiler; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 2 | |
| 3 | import java.util.List; |
| 4 | import java.util.Optional; |
| 5 | |
| 6 | import org.hamcrest.Matchers; |
| 7 | import org.junit.Test; |
| 8 | |
| 9 | import static org.junit.Assert.assertEquals; |
| 10 | |
| 11 | |
| 12 | import org.onlab.packet.MplsLabel; |
| 13 | import org.onosproject.TestApplicationId; |
| 14 | import org.onosproject.core.ApplicationId; |
| 15 | import org.onosproject.net.ConnectPoint; |
| 16 | import org.onosproject.net.Link; |
| 17 | import org.onosproject.net.Path; |
| 18 | import org.onosproject.net.flow.TrafficSelector; |
| 19 | import org.onosproject.net.flow.TrafficTreatment; |
| 20 | import org.onosproject.net.intent.AbstractIntentTest; |
| 21 | import org.onosproject.net.intent.Intent; |
| 22 | import org.onosproject.net.intent.IntentTestsMocks; |
| 23 | import org.onosproject.net.intent.MplsIntent; |
| 24 | import org.onosproject.net.intent.MplsPathIntent; |
| 25 | |
| 26 | import static org.hamcrest.CoreMatchers.instanceOf; |
| 27 | import static org.hamcrest.CoreMatchers.notNullValue; |
| 28 | import static org.hamcrest.MatcherAssert.assertThat; |
| 29 | import static org.hamcrest.Matchers.hasSize; |
| 30 | import static org.hamcrest.Matchers.is; |
| 31 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
| 32 | import static org.onosproject.net.DeviceId.deviceId; |
| 33 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 34 | import static org.onosproject.net.NetTestTools.connectPoint; |
| 35 | import static org.onosproject.net.PortNumber.portNumber; |
| 36 | import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath; |
| 37 | |
| 38 | /** |
| 39 | * Unit tests for the HostToHost intent compiler. |
| 40 | */ |
| 41 | public class MplsIntentCompilerTest extends AbstractIntentTest { |
| 42 | |
| 43 | private static final ApplicationId APPID = new TestApplicationId("foo"); |
| 44 | |
| 45 | private TrafficSelector selector = new IntentTestsMocks.MockSelector(); |
| 46 | private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); |
| 47 | |
| 48 | /** |
| 49 | * Creates a PointToPoint intent based on ingress and egress device Ids. |
| 50 | * |
| 51 | * @param ingressIdString string for id of ingress device |
| 52 | * @param egressIdString string for id of egress device |
| 53 | * @return PointToPointIntent for the two devices |
| 54 | */ |
| 55 | private MplsIntent makeIntent(String ingressIdString, Optional<MplsLabel> ingressLabel, |
| 56 | String egressIdString, Optional<MplsLabel> egressLabel) { |
| 57 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 58 | return MplsIntent.builder() |
| 59 | .appId(APPID) |
| 60 | .selector(selector) |
| 61 | .treatment(treatment) |
| 62 | .ingressPoint(connectPoint(ingressIdString, 1)) |
| 63 | .ingressLabel(ingressLabel) |
| 64 | .egressPoint(connectPoint(egressIdString, 1)) |
| 65 | .egressLabel(egressLabel).build(); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 66 | } |
| 67 | /** |
| 68 | * Creates a compiler for HostToHost intents. |
| 69 | * |
| 70 | * @param hops string array describing the path hops to use when compiling |
| 71 | * @return HostToHost intent compiler |
| 72 | */ |
| 73 | private MplsIntentCompiler makeCompiler(String[] hops) { |
| 74 | MplsIntentCompiler compiler = |
| 75 | new MplsIntentCompiler(); |
| 76 | compiler.pathService = new IntentTestsMocks.MockPathService(hops); |
| 77 | return compiler; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /** |
| 82 | * Tests a pair of devices in an 8 hop path, forward direction. |
| 83 | */ |
| 84 | @Test |
| 85 | public void testForwardPathCompilation() { |
Ray Milkey | 167ae0b | 2015-02-26 07:33:25 -0800 | [diff] [blame] | 86 | Optional<MplsLabel> ingressLabel = Optional.of(MplsLabel.mplsLabel(10)); |
| 87 | Optional<MplsLabel> egressLabel = Optional.of(MplsLabel.mplsLabel(20)); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 88 | |
| 89 | MplsIntent intent = makeIntent("d1", ingressLabel, "d8", egressLabel); |
| 90 | assertThat(intent, is(notNullValue())); |
| 91 | |
| 92 | String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"}; |
| 93 | MplsIntentCompiler compiler = makeCompiler(hops); |
| 94 | assertThat(compiler, is(notNullValue())); |
| 95 | |
| 96 | List<Intent> result = compiler.compile(intent, null, null); |
| 97 | assertThat(result, is(Matchers.notNullValue())); |
| 98 | assertThat(result, hasSize(1)); |
| 99 | Intent forwardResultIntent = result.get(0); |
| 100 | assertThat(forwardResultIntent instanceof MplsPathIntent, is(true)); |
| 101 | |
Ray Milkey | a046a87 | 2015-02-26 12:07:50 -0800 | [diff] [blame] | 102 | // if statement suppresses static analysis warnings about unchecked cast |
| 103 | if (forwardResultIntent instanceof MplsPathIntent) { |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 104 | MplsPathIntent forwardPathIntent = (MplsPathIntent) forwardResultIntent; |
| 105 | // 7 links for the hops, plus one default lnk on ingress and egress |
| 106 | assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1)); |
| 107 | assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2")); |
| 108 | assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3")); |
| 109 | assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4")); |
| 110 | assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5")); |
| 111 | assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6")); |
| 112 | assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7")); |
| 113 | assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8")); |
| 114 | assertEquals(forwardPathIntent.egressLabel(), egressLabel); |
| 115 | assertEquals(forwardPathIntent.ingressLabel(), ingressLabel); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Tests a pair of devices in an 8 hop path, forward direction. |
| 121 | */ |
| 122 | @Test |
| 123 | public void testReversePathCompilation() { |
Ray Milkey | 167ae0b | 2015-02-26 07:33:25 -0800 | [diff] [blame] | 124 | Optional<MplsLabel> ingressLabel = Optional.of(MplsLabel.mplsLabel(10)); |
| 125 | Optional<MplsLabel> egressLabel = Optional.of(MplsLabel.mplsLabel(20)); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 126 | |
| 127 | MplsIntent intent = makeIntent("d8", ingressLabel, "d1", egressLabel); |
| 128 | assertThat(intent, is(notNullValue())); |
| 129 | |
| 130 | String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"}; |
| 131 | MplsIntentCompiler compiler = makeCompiler(hops); |
| 132 | assertThat(compiler, is(notNullValue())); |
| 133 | |
| 134 | List<Intent> result = compiler.compile(intent, null, null); |
| 135 | assertThat(result, is(Matchers.notNullValue())); |
| 136 | assertThat(result, hasSize(1)); |
| 137 | Intent reverseResultIntent = result.get(0); |
| 138 | assertThat(reverseResultIntent instanceof MplsPathIntent, is(true)); |
| 139 | |
Ray Milkey | a046a87 | 2015-02-26 12:07:50 -0800 | [diff] [blame] | 140 | // if statement suppresses static analysis warnings about unchecked cast |
| 141 | if (reverseResultIntent instanceof MplsPathIntent) { |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 142 | MplsPathIntent reversePathIntent = (MplsPathIntent) reverseResultIntent; |
| 143 | assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1)); |
| 144 | assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1")); |
| 145 | assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2")); |
| 146 | assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3")); |
| 147 | assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4")); |
| 148 | assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5")); |
| 149 | assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6")); |
| 150 | assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7")); |
| 151 | assertEquals(reversePathIntent.egressLabel(), egressLabel); |
| 152 | assertEquals(reversePathIntent.ingressLabel(), ingressLabel); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Tests compilation of the intent which designates two different ports on the same switch. |
| 158 | */ |
| 159 | @Test |
| 160 | public void testSameSwitchDifferentPortsIntentCompilation() { |
| 161 | ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1)); |
| 162 | ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2)); |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 163 | MplsIntent intent = MplsIntent.builder() |
| 164 | .appId(APP_ID) |
| 165 | .selector(selector) |
| 166 | .treatment(treatment) |
| 167 | .ingressPoint(src) |
| 168 | .ingressLabel(Optional.empty()) |
| 169 | .egressPoint(dst) |
| 170 | .egressLabel(Optional.empty()) |
| 171 | .build(); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 172 | |
| 173 | String[] hops = {"1"}; |
| 174 | MplsIntentCompiler sut = makeCompiler(hops); |
| 175 | |
| 176 | List<Intent> compiled = sut.compile(intent, null, null); |
| 177 | |
| 178 | assertThat(compiled, hasSize(1)); |
| 179 | assertThat(compiled.get(0), is(instanceOf(MplsPathIntent.class))); |
| 180 | Path path = ((MplsPathIntent) compiled.get(0)).path(); |
| 181 | |
| 182 | assertThat(path.links(), hasSize(2)); |
| 183 | Link firstLink = path.links().get(0); |
| 184 | assertThat(firstLink, is(createEdgeLink(src, true))); |
| 185 | Link secondLink = path.links().get(1); |
| 186 | assertThat(secondLink, is(createEdgeLink(dst, false))); |
| 187 | } |
| 188 | } |