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