blob: 4bba2326b82b8de874b11d9a4ef040fe7b752298 [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
Ray Milkeye6684082014-10-16 16:59:47 -070018import org.hamcrest.Matchers;
19import org.junit.Before;
20import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.core.ApplicationId;
22import org.onosproject.TestApplicationId;
23import org.onosproject.net.Host;
24import org.onosproject.net.HostId;
25import org.onosproject.net.flow.TrafficSelector;
26import org.onosproject.net.flow.TrafficTreatment;
27import org.onosproject.net.host.HostService;
28import org.onosproject.net.intent.AbstractIntentTest;
29import org.onosproject.net.intent.HostToHostIntent;
30import org.onosproject.net.intent.Intent;
31import org.onosproject.net.intent.IntentTestsMocks;
32import org.onosproject.net.intent.PathIntent;
Ray Milkeye6684082014-10-16 16:59:47 -070033import org.onlab.packet.MacAddress;
34import org.onlab.packet.VlanId;
35
Thomas Vachuskab97cf282014-10-20 23:31:12 -070036import java.util.List;
37
38import static org.easymock.EasyMock.*;
Ray Milkeye6684082014-10-16 16:59:47 -070039import static org.hamcrest.CoreMatchers.notNullValue;
40import static org.hamcrest.MatcherAssert.assertThat;
41import static org.hamcrest.Matchers.hasSize;
42import static org.hamcrest.Matchers.is;
Brian O'Connorabafb502014-12-02 22:26:20 -080043import static org.onosproject.net.NetTestTools.hid;
44import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
Ray Milkeye6684082014-10-16 16:59:47 -070045
46/**
47 * Unit tests for the HostToHost intent compiler.
48 */
Ray Milkey37f6a382014-11-25 14:54:42 -080049public class HostToHostIntentCompilerTest extends AbstractIntentTest {
Ray Milkeye6684082014-10-16 16:59:47 -070050 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 Prete283a9622016-03-29 16:12:20 -070052 private static final String HOST_ONE_VLAN = "None";
53 private static final String HOST_TWO_VLAN = "None";
Ray Milkeye6684082014-10-16 16:59:47 -070054 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 Vachuskab97cf282014-10-20 23:31:12 -070057 private static final ApplicationId APPID = new TestApplicationId("foo");
58
Ray Milkeye6684082014-10-16 16:59:47 -070059 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'Connor520c0522014-11-23 23:50:47 -080067 public void setUp() throws Exception {
68 super.setUp();
Ray Milkeye6684082014-10-16 16:59:47 -070069 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 Milkeyebc5d222015-03-18 15:45:36 -070093 return HostToHostIntent.builder()
94 .appId(APPID)
95 .one(hid(oneIdString))
96 .two(hid(twoIdString))
97 .selector(selector)
98 .treatment(treatment)
99 .build();
Ray Milkeye6684082014-10-16 16:59:47 -0700100 }
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 SHIMIZUb1681bd2016-02-22 12:47:50 -0800113 compiler.resourceService = new MockResourceService();
Ray Milkeye6684082014-10-16 16:59:47 -0700114 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 SHIMIZUec07ffd2016-02-22 20:45:21 -0800132 List<Intent> result = compiler.compile(intent, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700133 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}