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