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