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; |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 19 | //import org.junit.Test; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 20 | import org.onlab.onos.core.ApplicationId; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 21 | import org.onlab.onos.TestApplicationId; |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.flow.TrafficSelector; |
| 23 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 24 | import org.onlab.onos.net.intent.Intent; |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.intent.IntentTestsMocks; |
| 26 | import org.onlab.onos.net.intent.PathIntent; |
| 27 | import org.onlab.onos.net.intent.PointToPointIntent; |
| 28 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 29 | import java.util.List; |
| 30 | |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 31 | import static org.hamcrest.CoreMatchers.notNullValue; |
| 32 | import static org.hamcrest.MatcherAssert.assertThat; |
| 33 | import static org.hamcrest.Matchers.hasSize; |
| 34 | import static org.hamcrest.Matchers.is; |
| 35 | import static org.onlab.onos.net.NetTestTools.connectPoint; |
| 36 | import static org.onlab.onos.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath; |
| 37 | |
| 38 | /** |
| 39 | * Unit tests for the HostToHost intent compiler. |
| 40 | */ |
| 41 | public class TestPointToPointIntentCompiler { |
| 42 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 43 | private static final ApplicationId APPID = new TestApplicationId("foo"); |
| 44 | |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 52 | * @param egressIdString string for id of egress device |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 53 | * @return PointToPointIntent for the two devices |
| 54 | */ |
| 55 | private PointToPointIntent makeIntent(String ingressIdString, |
| 56 | String egressIdString) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 57 | return new PointToPointIntent(APPID, selector, treatment, |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 58 | connectPoint(ingressIdString, 1), |
| 59 | connectPoint(egressIdString, 1)); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Creates a compiler for HostToHost intents. |
| 64 | * |
| 65 | * @param hops string array describing the path hops to use when compiling |
| 66 | * @return HostToHost intent compiler |
| 67 | */ |
| 68 | private PointToPointIntentCompiler makeCompiler(String[] hops) { |
| 69 | PointToPointIntentCompiler compiler = |
| 70 | new PointToPointIntentCompiler(); |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame^] | 71 | // compiler.topologyService = new IntentTestsMocks.MockPathService(hops); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 72 | return compiler; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | /** |
| 77 | * Tests a pair of devices in an 8 hop path, forward direction. |
| 78 | */ |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 79 | // @Test |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 80 | public void testForwardPathCompilation() { |
| 81 | |
| 82 | PointToPointIntent intent = makeIntent("d1", "d8"); |
| 83 | assertThat(intent, is(notNullValue())); |
| 84 | |
| 85 | String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"}; |
| 86 | PointToPointIntentCompiler compiler = makeCompiler(hops); |
| 87 | assertThat(compiler, is(notNullValue())); |
| 88 | |
| 89 | List<Intent> result = compiler.compile(intent); |
| 90 | assertThat(result, is(Matchers.notNullValue())); |
| 91 | assertThat(result, hasSize(1)); |
| 92 | Intent forwardResultIntent = result.get(0); |
| 93 | assertThat(forwardResultIntent instanceof PathIntent, is(true)); |
| 94 | |
| 95 | if (forwardResultIntent instanceof PathIntent) { |
| 96 | PathIntent forwardPathIntent = (PathIntent) forwardResultIntent; |
| 97 | // 7 links for the hops, plus one default lnk on ingress and egress |
| 98 | assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1)); |
| 99 | assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2")); |
| 100 | assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3")); |
| 101 | assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4")); |
| 102 | assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5")); |
| 103 | assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6")); |
| 104 | assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7")); |
| 105 | assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8")); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Tests a pair of devices in an 8 hop path, forward direction. |
| 111 | */ |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame] | 112 | // @Test |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 113 | public void testReversePathCompilation() { |
| 114 | |
| 115 | PointToPointIntent intent = makeIntent("d8", "d1"); |
| 116 | assertThat(intent, is(notNullValue())); |
| 117 | |
| 118 | String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"}; |
| 119 | PointToPointIntentCompiler compiler = makeCompiler(hops); |
| 120 | assertThat(compiler, is(notNullValue())); |
| 121 | |
| 122 | List<Intent> result = compiler.compile(intent); |
| 123 | assertThat(result, is(Matchers.notNullValue())); |
| 124 | assertThat(result, hasSize(1)); |
| 125 | Intent reverseResultIntent = result.get(0); |
| 126 | assertThat(reverseResultIntent instanceof PathIntent, is(true)); |
| 127 | |
| 128 | if (reverseResultIntent instanceof PathIntent) { |
| 129 | PathIntent reversePathIntent = (PathIntent) reverseResultIntent; |
| 130 | assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1)); |
| 131 | assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1")); |
| 132 | assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2")); |
| 133 | assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3")); |
| 134 | assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4")); |
| 135 | assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5")); |
| 136 | assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6")); |
| 137 | assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7")); |
| 138 | } |
| 139 | } |
| 140 | } |