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