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 | |
| 58 | return new MplsIntent(APPID, selector, treatment, |
| 59 | connectPoint(ingressIdString, 1), |
| 60 | ingressLabel, |
| 61 | connectPoint(egressIdString, 1), |
| 62 | egressLabel); |
| 63 | } |
| 64 | /** |
| 65 | * Creates a compiler for HostToHost intents. |
| 66 | * |
| 67 | * @param hops string array describing the path hops to use when compiling |
| 68 | * @return HostToHost intent compiler |
| 69 | */ |
| 70 | private MplsIntentCompiler makeCompiler(String[] hops) { |
| 71 | MplsIntentCompiler compiler = |
| 72 | new MplsIntentCompiler(); |
| 73 | compiler.pathService = new IntentTestsMocks.MockPathService(hops); |
| 74 | return compiler; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /** |
| 79 | * Tests a pair of devices in an 8 hop path, forward direction. |
| 80 | */ |
| 81 | @Test |
| 82 | public void testForwardPathCompilation() { |
Ray Milkey | 167ae0b | 2015-02-26 07:33:25 -0800 | [diff] [blame] | 83 | Optional<MplsLabel> ingressLabel = Optional.of(MplsLabel.mplsLabel(10)); |
| 84 | Optional<MplsLabel> egressLabel = Optional.of(MplsLabel.mplsLabel(20)); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 85 | |
| 86 | MplsIntent intent = makeIntent("d1", ingressLabel, "d8", egressLabel); |
| 87 | assertThat(intent, is(notNullValue())); |
| 88 | |
| 89 | String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"}; |
| 90 | MplsIntentCompiler compiler = makeCompiler(hops); |
| 91 | assertThat(compiler, is(notNullValue())); |
| 92 | |
| 93 | List<Intent> result = compiler.compile(intent, null, null); |
| 94 | assertThat(result, is(Matchers.notNullValue())); |
| 95 | assertThat(result, hasSize(1)); |
| 96 | Intent forwardResultIntent = result.get(0); |
| 97 | assertThat(forwardResultIntent instanceof MplsPathIntent, is(true)); |
| 98 | |
| 99 | if (forwardResultIntent instanceof MplsIntent) { |
| 100 | MplsPathIntent forwardPathIntent = (MplsPathIntent) forwardResultIntent; |
| 101 | // 7 links for the hops, plus one default lnk on ingress and egress |
| 102 | assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1)); |
| 103 | assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2")); |
| 104 | assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3")); |
| 105 | assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4")); |
| 106 | assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5")); |
| 107 | assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6")); |
| 108 | assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7")); |
| 109 | assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8")); |
| 110 | assertEquals(forwardPathIntent.egressLabel(), egressLabel); |
| 111 | assertEquals(forwardPathIntent.ingressLabel(), ingressLabel); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Tests a pair of devices in an 8 hop path, forward direction. |
| 117 | */ |
| 118 | @Test |
| 119 | public void testReversePathCompilation() { |
Ray Milkey | 167ae0b | 2015-02-26 07:33:25 -0800 | [diff] [blame] | 120 | Optional<MplsLabel> ingressLabel = Optional.of(MplsLabel.mplsLabel(10)); |
| 121 | Optional<MplsLabel> egressLabel = Optional.of(MplsLabel.mplsLabel(20)); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 122 | |
| 123 | MplsIntent intent = makeIntent("d8", ingressLabel, "d1", egressLabel); |
| 124 | assertThat(intent, is(notNullValue())); |
| 125 | |
| 126 | String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"}; |
| 127 | MplsIntentCompiler compiler = makeCompiler(hops); |
| 128 | assertThat(compiler, is(notNullValue())); |
| 129 | |
| 130 | List<Intent> result = compiler.compile(intent, null, null); |
| 131 | assertThat(result, is(Matchers.notNullValue())); |
| 132 | assertThat(result, hasSize(1)); |
| 133 | Intent reverseResultIntent = result.get(0); |
| 134 | assertThat(reverseResultIntent instanceof MplsPathIntent, is(true)); |
| 135 | |
| 136 | if (reverseResultIntent instanceof MplsIntent) { |
| 137 | MplsPathIntent reversePathIntent = (MplsPathIntent) reverseResultIntent; |
| 138 | assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1)); |
| 139 | assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1")); |
| 140 | assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2")); |
| 141 | assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3")); |
| 142 | assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4")); |
| 143 | assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5")); |
| 144 | assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6")); |
| 145 | assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7")); |
| 146 | assertEquals(reversePathIntent.egressLabel(), egressLabel); |
| 147 | assertEquals(reversePathIntent.ingressLabel(), ingressLabel); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Tests compilation of the intent which designates two different ports on the same switch. |
| 153 | */ |
| 154 | @Test |
| 155 | public void testSameSwitchDifferentPortsIntentCompilation() { |
| 156 | ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1)); |
| 157 | ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2)); |
| 158 | MplsIntent intent = new MplsIntent(APP_ID, selector, treatment, src, Optional.empty(), dst, Optional.empty()); |
| 159 | |
| 160 | String[] hops = {"1"}; |
| 161 | MplsIntentCompiler sut = makeCompiler(hops); |
| 162 | |
| 163 | List<Intent> compiled = sut.compile(intent, null, null); |
| 164 | |
| 165 | assertThat(compiled, hasSize(1)); |
| 166 | assertThat(compiled.get(0), is(instanceOf(MplsPathIntent.class))); |
| 167 | Path path = ((MplsPathIntent) compiled.get(0)).path(); |
| 168 | |
| 169 | assertThat(path.links(), hasSize(2)); |
| 170 | Link firstLink = path.links().get(0); |
| 171 | assertThat(firstLink, is(createEdgeLink(src, true))); |
| 172 | Link secondLink = path.links().get(1); |
| 173 | assertThat(secondLink, is(createEdgeLink(dst, false))); |
| 174 | } |
| 175 | } |