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