blob: d6333ffb2a73fbe04d23d0a5e57c868deef4d953 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 SHIMIZU6c28f832015-02-20 16:12:19 -080016package org.onosproject.net.intent.impl.compiler;
Ray Milkeye6684082014-10-16 16:59:47 -070017
Pier Ventre98308ab2016-10-12 14:35:05 -070018import com.google.common.collect.ImmutableSet;
Ray Milkeye6684082014-10-16 16:59:47 -070019import org.hamcrest.Matchers;
20import org.junit.Before;
21import org.junit.Test;
Pier Ventre98308ab2016-10-12 14:35:05 -070022import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.TestApplicationId;
Pier Ventre98308ab2016-10-12 14:35:05 -070025import org.onosproject.core.ApplicationId;
26import org.onosproject.net.FilteredConnectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.Host;
28import org.onosproject.net.HostId;
Pier Ventre98308ab2016-10-12 14:35:05 -070029import org.onosproject.net.Link;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.flow.TrafficSelector;
31import org.onosproject.net.flow.TrafficTreatment;
32import org.onosproject.net.host.HostService;
33import org.onosproject.net.intent.AbstractIntentTest;
34import org.onosproject.net.intent.HostToHostIntent;
35import org.onosproject.net.intent.Intent;
36import org.onosproject.net.intent.IntentTestsMocks;
Pier Ventre98308ab2016-10-12 14:35:05 -070037import org.onosproject.net.intent.LinkCollectionIntent;
Yuta HIGUCHId95d5902016-06-27 00:18:45 -070038import org.onosproject.net.resource.MockResourceService;
Ray Milkeye6684082014-10-16 16:59:47 -070039
Thomas Vachuskab97cf282014-10-20 23:31:12 -070040import java.util.List;
Pier Ventre98308ab2016-10-12 14:35:05 -070041import java.util.Set;
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -070042import java.util.stream.Collectors;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070043
44import static org.easymock.EasyMock.*;
Ray Milkeye6684082014-10-16 16:59:47 -070045import static org.hamcrest.CoreMatchers.notNullValue;
46import static org.hamcrest.MatcherAssert.assertThat;
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -070047import static org.hamcrest.Matchers.everyItem;
Ray Milkeye6684082014-10-16 16:59:47 -070048import static org.hamcrest.Matchers.hasSize;
49import static org.hamcrest.Matchers.is;
Pier Ventre98308ab2016-10-12 14:35:05 -070050import static org.onosproject.net.NetTestTools.connectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080051import static org.onosproject.net.NetTestTools.hid;
52import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
Ray Milkeye6684082014-10-16 16:59:47 -070053
54/**
55 * Unit tests for the HostToHost intent compiler.
56 */
Ray Milkey37f6a382014-11-25 14:54:42 -080057public class HostToHostIntentCompilerTest extends AbstractIntentTest {
Ray Milkeye6684082014-10-16 16:59:47 -070058 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 Prete283a9622016-03-29 16:12:20 -070060 private static final String HOST_ONE_VLAN = "None";
61 private static final String HOST_TWO_VLAN = "None";
Ray Milkeye6684082014-10-16 16:59:47 -070062 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 Ventre98308ab2016-10-12 14:35:05 -070065 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 Vachuskab97cf282014-10-20 23:31:12 -070076 private static final ApplicationId APPID = new TestApplicationId("foo");
77
Ray Milkeye6684082014-10-16 16:59:47 -070078 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 HIGUCHI652f27f2016-10-31 16:54:30 -070085 @Override
Ray Milkeye6684082014-10-16 16:59:47 -070086 @Before
Brian O'Connor520c0522014-11-23 23:50:47 -080087 public void setUp() throws Exception {
88 super.setUp();
Ray Milkeye6684082014-10-16 16:59:47 -070089 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 Milkeyebc5d222015-03-18 15:45:36 -0700113 return HostToHostIntent.builder()
114 .appId(APPID)
115 .one(hid(oneIdString))
116 .two(hid(twoIdString))
117 .selector(selector)
118 .treatment(treatment)
119 .build();
Ray Milkeye6684082014-10-16 16:59:47 -0700120 }
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 SHIMIZUb1681bd2016-02-22 12:47:50 -0800133 compiler.resourceService = new MockResourceService();
Ray Milkeye6684082014-10-16 16:59:47 -0700134 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 Ventre98308ab2016-10-12 14:35:05 -0700148 String[] hops = {HOST_ONE, HOP_1, HOP_2, HOP_3, HOP_4, HOP_5, HOP_6, HOP_7, HOP_8, HOST_TWO};
Ray Milkeye6684082014-10-16 16:59:47 -0700149 HostToHostIntentCompiler compiler = makeCompiler(hops);
150 assertThat(compiler, is(notNullValue()));
151
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800152 List<Intent> result = compiler.compile(intent, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700153 assertThat(result, is(Matchers.notNullValue()));
154 assertThat(result, hasSize(2));
Pier Ventre98308ab2016-10-12 14:35:05 -0700155 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 Milkeye6684082014-10-16 16:59:47 -0700159
Pier Ventre98308ab2016-10-12 14:35:05 -0700160 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 Milkeye6684082014-10-16 16:59:47 -0700178
Pier Ventre98308ab2016-10-12 14:35:05 -0700179 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 HIGUCHI652f27f2016-10-31 16:54:30 -0700194
195 assertThat("key is inherited",
196 result.stream().map(Intent::key).collect(Collectors.toList()),
197 everyItem(is(intent.key())));
Ray Milkeye6684082014-10-16 16:59:47 -0700198 }
199}