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