blob: 4dc5045e078ae0670655528bfc67c99b2211bcf8 [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;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070042
43import static org.easymock.EasyMock.*;
Ray Milkeye6684082014-10-16 16:59:47 -070044import static org.hamcrest.CoreMatchers.notNullValue;
45import static org.hamcrest.MatcherAssert.assertThat;
46import static org.hamcrest.Matchers.hasSize;
47import static org.hamcrest.Matchers.is;
Pier Ventre98308ab2016-10-12 14:35:05 -070048import static org.onosproject.net.NetTestTools.connectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import static org.onosproject.net.NetTestTools.hid;
50import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
Ray Milkeye6684082014-10-16 16:59:47 -070051
52/**
53 * Unit tests for the HostToHost intent compiler.
54 */
Ray Milkey37f6a382014-11-25 14:54:42 -080055public class HostToHostIntentCompilerTest extends AbstractIntentTest {
Ray Milkeye6684082014-10-16 16:59:47 -070056 private static final String HOST_ONE_MAC = "00:00:00:00:00:01";
57 private static final String HOST_TWO_MAC = "00:00:00:00:00:02";
Luca Prete283a9622016-03-29 16:12:20 -070058 private static final String HOST_ONE_VLAN = "None";
59 private static final String HOST_TWO_VLAN = "None";
Ray Milkeye6684082014-10-16 16:59:47 -070060 private static final String HOST_ONE = HOST_ONE_MAC + "/" + HOST_ONE_VLAN;
61 private static final String HOST_TWO = HOST_TWO_MAC + "/" + HOST_TWO_VLAN;
62
Pier Ventre98308ab2016-10-12 14:35:05 -070063 private static final int PORT_1 = 1;
64
65 private static final String HOP_1 = "h1";
66 private static final String HOP_2 = "h2";
67 private static final String HOP_3 = "h3";
68 private static final String HOP_4 = "h4";
69 private static final String HOP_5 = "h5";
70 private static final String HOP_6 = "h6";
71 private static final String HOP_7 = "h7";
72 private static final String HOP_8 = "h8";
73
Thomas Vachuskab97cf282014-10-20 23:31:12 -070074 private static final ApplicationId APPID = new TestApplicationId("foo");
75
Ray Milkeye6684082014-10-16 16:59:47 -070076 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
77 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
78
79 private HostId hostOneId = HostId.hostId(HOST_ONE);
80 private HostId hostTwoId = HostId.hostId(HOST_TWO);
81 private HostService mockHostService;
82
83 @Before
Brian O'Connor520c0522014-11-23 23:50:47 -080084 public void setUp() throws Exception {
85 super.setUp();
Ray Milkeye6684082014-10-16 16:59:47 -070086 Host hostOne = createMock(Host.class);
87 expect(hostOne.mac()).andReturn(new MacAddress(HOST_ONE_MAC.getBytes())).anyTimes();
88 expect(hostOne.vlan()).andReturn(VlanId.vlanId()).anyTimes();
89 replay(hostOne);
90
91 Host hostTwo = createMock(Host.class);
92 expect(hostTwo.mac()).andReturn(new MacAddress(HOST_TWO_MAC.getBytes())).anyTimes();
93 expect(hostTwo.vlan()).andReturn(VlanId.vlanId()).anyTimes();
94 replay(hostTwo);
95
96 mockHostService = createMock(HostService.class);
97 expect(mockHostService.getHost(eq(hostOneId))).andReturn(hostOne).anyTimes();
98 expect(mockHostService.getHost(eq(hostTwoId))).andReturn(hostTwo).anyTimes();
99 replay(mockHostService);
100 }
101
102 /**
103 * Creates a HostToHost intent based on two host Ids.
104 *
105 * @param oneIdString string for host one id
106 * @param twoIdString string for host two id
107 * @return HostToHostIntent for the two hosts
108 */
109 private HostToHostIntent makeIntent(String oneIdString, String twoIdString) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700110 return HostToHostIntent.builder()
111 .appId(APPID)
112 .one(hid(oneIdString))
113 .two(hid(twoIdString))
114 .selector(selector)
115 .treatment(treatment)
116 .build();
Ray Milkeye6684082014-10-16 16:59:47 -0700117 }
118
119 /**
120 * Creates a compiler for HostToHost intents.
121 *
122 * @param hops string array describing the path hops to use when compiling
123 * @return HostToHost intent compiler
124 */
125 private HostToHostIntentCompiler makeCompiler(String[] hops) {
126 HostToHostIntentCompiler compiler =
127 new HostToHostIntentCompiler();
128 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
129 compiler.hostService = mockHostService;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800130 compiler.resourceService = new MockResourceService();
Ray Milkeye6684082014-10-16 16:59:47 -0700131 return compiler;
132 }
133
134
135 /**
136 * Tests a pair of hosts with 8 hops between them.
137 */
138 @Test
139 public void testSingleLongPathCompilation() {
140
141 HostToHostIntent intent = makeIntent(HOST_ONE,
142 HOST_TWO);
143 assertThat(intent, is(notNullValue()));
144
Pier Ventre98308ab2016-10-12 14:35:05 -0700145 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 -0700146 HostToHostIntentCompiler compiler = makeCompiler(hops);
147 assertThat(compiler, is(notNullValue()));
148
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800149 List<Intent> result = compiler.compile(intent, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700150 assertThat(result, is(Matchers.notNullValue()));
151 assertThat(result, hasSize(2));
Pier Ventre98308ab2016-10-12 14:35:05 -0700152 Intent forwardIntent = result.get(0);
153 assertThat(forwardIntent instanceof LinkCollectionIntent, is(true));
154 Intent reverseIntent = result.get(1);
155 assertThat(reverseIntent instanceof LinkCollectionIntent, is(true));
Ray Milkeye6684082014-10-16 16:59:47 -0700156
Pier Ventre98308ab2016-10-12 14:35:05 -0700157 LinkCollectionIntent forwardLCIntent = (LinkCollectionIntent) forwardIntent;
158 Set<Link> links = forwardLCIntent.links();
159 assertThat(links, hasSize(7));
160 Set<FilteredConnectPoint> ingressPoints = ImmutableSet.of(
161 new FilteredConnectPoint(connectPoint(HOP_1, PORT_1))
162 );
163 assertThat(forwardLCIntent.filteredIngressPoints(), is(ingressPoints));
164 assertThat(links, linksHasPath(HOP_1, HOP_2));
165 assertThat(links, linksHasPath(HOP_2, HOP_3));
166 assertThat(links, linksHasPath(HOP_3, HOP_4));
167 assertThat(links, linksHasPath(HOP_4, HOP_5));
168 assertThat(links, linksHasPath(HOP_5, HOP_6));
169 assertThat(links, linksHasPath(HOP_6, HOP_7));
170 assertThat(links, linksHasPath(HOP_7, HOP_8));
171 Set<FilteredConnectPoint> egressPoints = ImmutableSet.of(
172 new FilteredConnectPoint(connectPoint(HOP_8, PORT_1))
173 );
174 assertThat(forwardLCIntent.filteredEgressPoints(), is(egressPoints));
Ray Milkeye6684082014-10-16 16:59:47 -0700175
Pier Ventre98308ab2016-10-12 14:35:05 -0700176 LinkCollectionIntent reverseLCIntent = (LinkCollectionIntent) reverseIntent;
177 links = reverseLCIntent.links();
178 assertThat(reverseLCIntent.links(), hasSize(7));
179 ingressPoints = ImmutableSet.of(new FilteredConnectPoint(connectPoint(HOP_8, PORT_1)));
180 assertThat(reverseLCIntent.filteredIngressPoints(), is(ingressPoints));
181 assertThat(links, linksHasPath(HOP_2, HOP_1));
182 assertThat(links, linksHasPath(HOP_3, HOP_2));
183 assertThat(links, linksHasPath(HOP_4, HOP_3));
184 assertThat(links, linksHasPath(HOP_5, HOP_4));
185 assertThat(links, linksHasPath(HOP_6, HOP_5));
186 assertThat(links, linksHasPath(HOP_7, HOP_6));
187 assertThat(links, linksHasPath(HOP_8, HOP_7));
188 egressPoints = ImmutableSet.of(new FilteredConnectPoint(connectPoint(HOP_1, PORT_1)));
189 assertThat(reverseLCIntent.filteredEgressPoints(), is(egressPoints));
190
Ray Milkeye6684082014-10-16 16:59:47 -0700191 }
192}