Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.intent.impl; |
| 2 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 3 | import org.hamcrest.Matchers; |
| 4 | import org.junit.Before; |
| 5 | import org.junit.Test; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 6 | import org.onlab.onos.ApplicationId; |
| 7 | import org.onlab.onos.TestApplicationId; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 8 | import org.onlab.onos.net.Host; |
| 9 | import org.onlab.onos.net.HostId; |
| 10 | import org.onlab.onos.net.flow.TrafficSelector; |
| 11 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 12 | import org.onlab.onos.net.host.HostService; |
| 13 | import org.onlab.onos.net.intent.HostToHostIntent; |
| 14 | import org.onlab.onos.net.intent.Intent; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 15 | import org.onlab.onos.net.intent.IntentTestsMocks; |
| 16 | import org.onlab.onos.net.intent.PathIntent; |
| 17 | import org.onlab.packet.MacAddress; |
| 18 | import org.onlab.packet.VlanId; |
| 19 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 20 | import java.util.List; |
| 21 | |
| 22 | import static org.easymock.EasyMock.*; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 23 | import static org.hamcrest.CoreMatchers.notNullValue; |
| 24 | import static org.hamcrest.MatcherAssert.assertThat; |
| 25 | import static org.hamcrest.Matchers.hasSize; |
| 26 | import static org.hamcrest.Matchers.is; |
| 27 | import static org.onlab.onos.net.NetTestTools.hid; |
| 28 | import static org.onlab.onos.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath; |
| 29 | |
| 30 | /** |
| 31 | * Unit tests for the HostToHost intent compiler. |
| 32 | */ |
| 33 | public class TestHostToHostIntentCompiler { |
| 34 | private static final String HOST_ONE_MAC = "00:00:00:00:00:01"; |
| 35 | private static final String HOST_TWO_MAC = "00:00:00:00:00:02"; |
| 36 | private static final String HOST_ONE_VLAN = "-1"; |
| 37 | private static final String HOST_TWO_VLAN = "-1"; |
| 38 | private static final String HOST_ONE = HOST_ONE_MAC + "/" + HOST_ONE_VLAN; |
| 39 | private static final String HOST_TWO = HOST_TWO_MAC + "/" + HOST_TWO_VLAN; |
| 40 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 41 | private static final ApplicationId APPID = new TestApplicationId("foo"); |
| 42 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 43 | private TrafficSelector selector = new IntentTestsMocks.MockSelector(); |
| 44 | private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); |
| 45 | |
| 46 | private HostId hostOneId = HostId.hostId(HOST_ONE); |
| 47 | private HostId hostTwoId = HostId.hostId(HOST_TWO); |
| 48 | private HostService mockHostService; |
| 49 | |
| 50 | @Before |
| 51 | public void setup() { |
| 52 | Host hostOne = createMock(Host.class); |
| 53 | expect(hostOne.mac()).andReturn(new MacAddress(HOST_ONE_MAC.getBytes())).anyTimes(); |
| 54 | expect(hostOne.vlan()).andReturn(VlanId.vlanId()).anyTimes(); |
| 55 | replay(hostOne); |
| 56 | |
| 57 | Host hostTwo = createMock(Host.class); |
| 58 | expect(hostTwo.mac()).andReturn(new MacAddress(HOST_TWO_MAC.getBytes())).anyTimes(); |
| 59 | expect(hostTwo.vlan()).andReturn(VlanId.vlanId()).anyTimes(); |
| 60 | replay(hostTwo); |
| 61 | |
| 62 | mockHostService = createMock(HostService.class); |
| 63 | expect(mockHostService.getHost(eq(hostOneId))).andReturn(hostOne).anyTimes(); |
| 64 | expect(mockHostService.getHost(eq(hostTwoId))).andReturn(hostTwo).anyTimes(); |
| 65 | replay(mockHostService); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Creates a HostToHost intent based on two host Ids. |
| 70 | * |
| 71 | * @param oneIdString string for host one id |
| 72 | * @param twoIdString string for host two id |
| 73 | * @return HostToHostIntent for the two hosts |
| 74 | */ |
| 75 | private HostToHostIntent makeIntent(String oneIdString, String twoIdString) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame^] | 76 | return new HostToHostIntent(APPID, hid(oneIdString), hid(twoIdString), |
| 77 | selector, treatment); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Creates a compiler for HostToHost intents. |
| 82 | * |
| 83 | * @param hops string array describing the path hops to use when compiling |
| 84 | * @return HostToHost intent compiler |
| 85 | */ |
| 86 | private HostToHostIntentCompiler makeCompiler(String[] hops) { |
| 87 | HostToHostIntentCompiler compiler = |
| 88 | new HostToHostIntentCompiler(); |
| 89 | compiler.pathService = new IntentTestsMocks.MockPathService(hops); |
| 90 | compiler.hostService = mockHostService; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 91 | return compiler; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /** |
| 96 | * Tests a pair of hosts with 8 hops between them. |
| 97 | */ |
| 98 | @Test |
| 99 | public void testSingleLongPathCompilation() { |
| 100 | |
| 101 | HostToHostIntent intent = makeIntent(HOST_ONE, |
| 102 | HOST_TWO); |
| 103 | assertThat(intent, is(notNullValue())); |
| 104 | |
| 105 | String[] hops = {HOST_ONE, "h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", HOST_TWO}; |
| 106 | HostToHostIntentCompiler compiler = makeCompiler(hops); |
| 107 | assertThat(compiler, is(notNullValue())); |
| 108 | |
| 109 | List<Intent> result = compiler.compile(intent); |
| 110 | assertThat(result, is(Matchers.notNullValue())); |
| 111 | assertThat(result, hasSize(2)); |
| 112 | Intent forwardResultIntent = result.get(0); |
| 113 | assertThat(forwardResultIntent instanceof PathIntent, is(true)); |
| 114 | Intent reverseResultIntent = result.get(1); |
| 115 | assertThat(reverseResultIntent instanceof PathIntent, is(true)); |
| 116 | |
| 117 | if (forwardResultIntent instanceof PathIntent) { |
| 118 | PathIntent forwardPathIntent = (PathIntent) forwardResultIntent; |
| 119 | assertThat(forwardPathIntent.path().links(), hasSize(9)); |
| 120 | assertThat(forwardPathIntent.path().links(), linksHasPath(HOST_ONE, "h1")); |
| 121 | assertThat(forwardPathIntent.path().links(), linksHasPath("h1", "h2")); |
| 122 | assertThat(forwardPathIntent.path().links(), linksHasPath("h2", "h3")); |
| 123 | assertThat(forwardPathIntent.path().links(), linksHasPath("h3", "h4")); |
| 124 | assertThat(forwardPathIntent.path().links(), linksHasPath("h4", "h5")); |
| 125 | assertThat(forwardPathIntent.path().links(), linksHasPath("h5", "h6")); |
| 126 | assertThat(forwardPathIntent.path().links(), linksHasPath("h6", "h7")); |
| 127 | assertThat(forwardPathIntent.path().links(), linksHasPath("h7", "h8")); |
| 128 | assertThat(forwardPathIntent.path().links(), linksHasPath("h8", HOST_TWO)); |
| 129 | } |
| 130 | |
| 131 | if (reverseResultIntent instanceof PathIntent) { |
| 132 | PathIntent reversePathIntent = (PathIntent) reverseResultIntent; |
| 133 | assertThat(reversePathIntent.path().links(), hasSize(9)); |
| 134 | assertThat(reversePathIntent.path().links(), linksHasPath("h1", HOST_ONE)); |
| 135 | assertThat(reversePathIntent.path().links(), linksHasPath("h2", "h1")); |
| 136 | assertThat(reversePathIntent.path().links(), linksHasPath("h3", "h2")); |
| 137 | assertThat(reversePathIntent.path().links(), linksHasPath("h4", "h3")); |
| 138 | assertThat(reversePathIntent.path().links(), linksHasPath("h5", "h4")); |
| 139 | assertThat(reversePathIntent.path().links(), linksHasPath("h6", "h5")); |
| 140 | assertThat(reversePathIntent.path().links(), linksHasPath("h7", "h6")); |
| 141 | assertThat(reversePathIntent.path().links(), linksHasPath("h8", "h7")); |
| 142 | assertThat(reversePathIntent.path().links(), linksHasPath(HOST_TWO, "h8")); |
| 143 | } |
| 144 | } |
| 145 | } |