blob: 07cf73103e43681ce6eac5ee4e9a8aca7d8d3ad1 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
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 */
Ray Milkeye6684082014-10-16 16:59:47 -070016package org.onlab.onos.net.intent.impl;
17
Ray Milkeye6684082014-10-16 16:59:47 -070018import org.hamcrest.Matchers;
19import org.junit.Before;
20import org.junit.Test;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070021import org.onlab.onos.core.ApplicationId;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070022import org.onlab.onos.TestApplicationId;
Ray Milkeye6684082014-10-16 16:59:47 -070023import org.onlab.onos.net.Host;
24import org.onlab.onos.net.HostId;
25import org.onlab.onos.net.flow.TrafficSelector;
26import org.onlab.onos.net.flow.TrafficTreatment;
27import org.onlab.onos.net.host.HostService;
Brian O'Connor520c0522014-11-23 23:50:47 -080028import org.onlab.onos.net.intent.AbstractIntentTest;
Ray Milkeye6684082014-10-16 16:59:47 -070029import org.onlab.onos.net.intent.HostToHostIntent;
30import org.onlab.onos.net.intent.Intent;
Ray Milkeye6684082014-10-16 16:59:47 -070031import org.onlab.onos.net.intent.IntentTestsMocks;
32import org.onlab.onos.net.intent.PathIntent;
33import 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;
43import static org.onlab.onos.net.NetTestTools.hid;
44import static org.onlab.onos.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
45
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";
52 private static final String HOST_ONE_VLAN = "-1";
53 private static final String HOST_TWO_VLAN = "-1";
54 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) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070093 return new HostToHostIntent(APPID, hid(oneIdString), hid(twoIdString),
94 selector, treatment);
Ray Milkeye6684082014-10-16 16:59:47 -070095 }
96
97 /**
98 * Creates a compiler for HostToHost intents.
99 *
100 * @param hops string array describing the path hops to use when compiling
101 * @return HostToHost intent compiler
102 */
103 private HostToHostIntentCompiler makeCompiler(String[] hops) {
104 HostToHostIntentCompiler compiler =
105 new HostToHostIntentCompiler();
106 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
107 compiler.hostService = mockHostService;
Ray Milkeye6684082014-10-16 16:59:47 -0700108 return compiler;
109 }
110
111
112 /**
113 * Tests a pair of hosts with 8 hops between them.
114 */
115 @Test
116 public void testSingleLongPathCompilation() {
117
118 HostToHostIntent intent = makeIntent(HOST_ONE,
119 HOST_TWO);
120 assertThat(intent, is(notNullValue()));
121
122 String[] hops = {HOST_ONE, "h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", HOST_TWO};
123 HostToHostIntentCompiler compiler = makeCompiler(hops);
124 assertThat(compiler, is(notNullValue()));
125
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700126 List<Intent> result = compiler.compile(intent, null, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700127 assertThat(result, is(Matchers.notNullValue()));
128 assertThat(result, hasSize(2));
129 Intent forwardResultIntent = result.get(0);
130 assertThat(forwardResultIntent instanceof PathIntent, is(true));
131 Intent reverseResultIntent = result.get(1);
132 assertThat(reverseResultIntent instanceof PathIntent, is(true));
133
134 if (forwardResultIntent instanceof PathIntent) {
135 PathIntent forwardPathIntent = (PathIntent) forwardResultIntent;
136 assertThat(forwardPathIntent.path().links(), hasSize(9));
137 assertThat(forwardPathIntent.path().links(), linksHasPath(HOST_ONE, "h1"));
138 assertThat(forwardPathIntent.path().links(), linksHasPath("h1", "h2"));
139 assertThat(forwardPathIntent.path().links(), linksHasPath("h2", "h3"));
140 assertThat(forwardPathIntent.path().links(), linksHasPath("h3", "h4"));
141 assertThat(forwardPathIntent.path().links(), linksHasPath("h4", "h5"));
142 assertThat(forwardPathIntent.path().links(), linksHasPath("h5", "h6"));
143 assertThat(forwardPathIntent.path().links(), linksHasPath("h6", "h7"));
144 assertThat(forwardPathIntent.path().links(), linksHasPath("h7", "h8"));
145 assertThat(forwardPathIntent.path().links(), linksHasPath("h8", HOST_TWO));
146 }
147
148 if (reverseResultIntent instanceof PathIntent) {
149 PathIntent reversePathIntent = (PathIntent) reverseResultIntent;
150 assertThat(reversePathIntent.path().links(), hasSize(9));
151 assertThat(reversePathIntent.path().links(), linksHasPath("h1", HOST_ONE));
152 assertThat(reversePathIntent.path().links(), linksHasPath("h2", "h1"));
153 assertThat(reversePathIntent.path().links(), linksHasPath("h3", "h2"));
154 assertThat(reversePathIntent.path().links(), linksHasPath("h4", "h3"));
155 assertThat(reversePathIntent.path().links(), linksHasPath("h5", "h4"));
156 assertThat(reversePathIntent.path().links(), linksHasPath("h6", "h5"));
157 assertThat(reversePathIntent.path().links(), linksHasPath("h7", "h6"));
158 assertThat(reversePathIntent.path().links(), linksHasPath("h8", "h7"));
159 assertThat(reversePathIntent.path().links(), linksHasPath(HOST_TWO, "h8"));
160 }
161 }
162}