Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Sho SHIMIZU | 6c28f83 | 2015-02-20 16:12:19 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl.compiler; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 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; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.TestApplicationId; |
| 23 | import org.onosproject.net.Host; |
| 24 | import org.onosproject.net.HostId; |
| 25 | import org.onosproject.net.flow.TrafficSelector; |
| 26 | import org.onosproject.net.flow.TrafficTreatment; |
| 27 | import org.onosproject.net.host.HostService; |
| 28 | import org.onosproject.net.intent.AbstractIntentTest; |
| 29 | import org.onosproject.net.intent.HostToHostIntent; |
| 30 | import org.onosproject.net.intent.Intent; |
| 31 | import org.onosproject.net.intent.IntentTestsMocks; |
| 32 | import org.onosproject.net.intent.PathIntent; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 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; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 43 | import static org.onosproject.net.NetTestTools.hid; |
| 44 | import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 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"; |
Luca Prete | 283a962 | 2016-03-29 16:12:20 -0700 | [diff] [blame] | 52 | private static final String HOST_ONE_VLAN = "None"; |
| 53 | private static final String HOST_TWO_VLAN = "None"; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 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) { |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 93 | return HostToHostIntent.builder() |
| 94 | .appId(APPID) |
| 95 | .one(hid(oneIdString)) |
| 96 | .two(hid(twoIdString)) |
| 97 | .selector(selector) |
| 98 | .treatment(treatment) |
| 99 | .build(); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Creates a compiler for HostToHost intents. |
| 104 | * |
| 105 | * @param hops string array describing the path hops to use when compiling |
| 106 | * @return HostToHost intent compiler |
| 107 | */ |
| 108 | private HostToHostIntentCompiler makeCompiler(String[] hops) { |
| 109 | HostToHostIntentCompiler compiler = |
| 110 | new HostToHostIntentCompiler(); |
| 111 | compiler.pathService = new IntentTestsMocks.MockPathService(hops); |
| 112 | compiler.hostService = mockHostService; |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 113 | compiler.resourceService = new MockResourceService(); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 114 | return compiler; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | /** |
| 119 | * Tests a pair of hosts with 8 hops between them. |
| 120 | */ |
| 121 | @Test |
| 122 | public void testSingleLongPathCompilation() { |
| 123 | |
| 124 | HostToHostIntent intent = makeIntent(HOST_ONE, |
| 125 | HOST_TWO); |
| 126 | assertThat(intent, is(notNullValue())); |
| 127 | |
| 128 | String[] hops = {HOST_ONE, "h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", HOST_TWO}; |
| 129 | HostToHostIntentCompiler compiler = makeCompiler(hops); |
| 130 | assertThat(compiler, is(notNullValue())); |
| 131 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 132 | List<Intent> result = compiler.compile(intent, null); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 133 | assertThat(result, is(Matchers.notNullValue())); |
| 134 | assertThat(result, hasSize(2)); |
| 135 | Intent forwardResultIntent = result.get(0); |
| 136 | assertThat(forwardResultIntent instanceof PathIntent, is(true)); |
| 137 | Intent reverseResultIntent = result.get(1); |
| 138 | assertThat(reverseResultIntent instanceof PathIntent, is(true)); |
| 139 | |
| 140 | if (forwardResultIntent instanceof PathIntent) { |
| 141 | PathIntent forwardPathIntent = (PathIntent) forwardResultIntent; |
| 142 | assertThat(forwardPathIntent.path().links(), hasSize(9)); |
| 143 | assertThat(forwardPathIntent.path().links(), linksHasPath(HOST_ONE, "h1")); |
| 144 | assertThat(forwardPathIntent.path().links(), linksHasPath("h1", "h2")); |
| 145 | assertThat(forwardPathIntent.path().links(), linksHasPath("h2", "h3")); |
| 146 | assertThat(forwardPathIntent.path().links(), linksHasPath("h3", "h4")); |
| 147 | assertThat(forwardPathIntent.path().links(), linksHasPath("h4", "h5")); |
| 148 | assertThat(forwardPathIntent.path().links(), linksHasPath("h5", "h6")); |
| 149 | assertThat(forwardPathIntent.path().links(), linksHasPath("h6", "h7")); |
| 150 | assertThat(forwardPathIntent.path().links(), linksHasPath("h7", "h8")); |
| 151 | assertThat(forwardPathIntent.path().links(), linksHasPath("h8", HOST_TWO)); |
| 152 | } |
| 153 | |
| 154 | if (reverseResultIntent instanceof PathIntent) { |
| 155 | PathIntent reversePathIntent = (PathIntent) reverseResultIntent; |
| 156 | assertThat(reversePathIntent.path().links(), hasSize(9)); |
| 157 | assertThat(reversePathIntent.path().links(), linksHasPath("h1", HOST_ONE)); |
| 158 | assertThat(reversePathIntent.path().links(), linksHasPath("h2", "h1")); |
| 159 | assertThat(reversePathIntent.path().links(), linksHasPath("h3", "h2")); |
| 160 | assertThat(reversePathIntent.path().links(), linksHasPath("h4", "h3")); |
| 161 | assertThat(reversePathIntent.path().links(), linksHasPath("h5", "h4")); |
| 162 | assertThat(reversePathIntent.path().links(), linksHasPath("h6", "h5")); |
| 163 | assertThat(reversePathIntent.path().links(), linksHasPath("h7", "h6")); |
| 164 | assertThat(reversePathIntent.path().links(), linksHasPath("h8", "h7")); |
| 165 | assertThat(reversePathIntent.path().links(), linksHasPath(HOST_TWO, "h8")); |
| 166 | } |
| 167 | } |
| 168 | } |