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