blob: 02fa4db79ffeef0f573a32a406205a3a152baeaf [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;
28import org.onlab.onos.net.intent.HostToHostIntent;
29import org.onlab.onos.net.intent.Intent;
Ray Milkeye6684082014-10-16 16:59:47 -070030import org.onlab.onos.net.intent.IntentTestsMocks;
31import org.onlab.onos.net.intent.PathIntent;
32import org.onlab.packet.MacAddress;
33import org.onlab.packet.VlanId;
34
Thomas Vachuskab97cf282014-10-20 23:31:12 -070035import java.util.List;
36
37import static org.easymock.EasyMock.*;
Ray Milkeye6684082014-10-16 16:59:47 -070038import static org.hamcrest.CoreMatchers.notNullValue;
39import static org.hamcrest.MatcherAssert.assertThat;
40import static org.hamcrest.Matchers.hasSize;
41import static org.hamcrest.Matchers.is;
42import static org.onlab.onos.net.NetTestTools.hid;
43import static org.onlab.onos.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
44
45/**
46 * Unit tests for the HostToHost intent compiler.
47 */
48public class TestHostToHostIntentCompiler {
49 private static final String HOST_ONE_MAC = "00:00:00:00:00:01";
50 private static final String HOST_TWO_MAC = "00:00:00:00:00:02";
51 private static final String HOST_ONE_VLAN = "-1";
52 private static final String HOST_TWO_VLAN = "-1";
53 private static final String HOST_ONE = HOST_ONE_MAC + "/" + HOST_ONE_VLAN;
54 private static final String HOST_TWO = HOST_TWO_MAC + "/" + HOST_TWO_VLAN;
55
Thomas Vachuskab97cf282014-10-20 23:31:12 -070056 private static final ApplicationId APPID = new TestApplicationId("foo");
57
Ray Milkeye6684082014-10-16 16:59:47 -070058 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
59 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
60
61 private HostId hostOneId = HostId.hostId(HOST_ONE);
62 private HostId hostTwoId = HostId.hostId(HOST_TWO);
63 private HostService mockHostService;
64
65 @Before
66 public void setup() {
67 Host hostOne = createMock(Host.class);
68 expect(hostOne.mac()).andReturn(new MacAddress(HOST_ONE_MAC.getBytes())).anyTimes();
69 expect(hostOne.vlan()).andReturn(VlanId.vlanId()).anyTimes();
70 replay(hostOne);
71
72 Host hostTwo = createMock(Host.class);
73 expect(hostTwo.mac()).andReturn(new MacAddress(HOST_TWO_MAC.getBytes())).anyTimes();
74 expect(hostTwo.vlan()).andReturn(VlanId.vlanId()).anyTimes();
75 replay(hostTwo);
76
77 mockHostService = createMock(HostService.class);
78 expect(mockHostService.getHost(eq(hostOneId))).andReturn(hostOne).anyTimes();
79 expect(mockHostService.getHost(eq(hostTwoId))).andReturn(hostTwo).anyTimes();
80 replay(mockHostService);
81 }
82
83 /**
84 * Creates a HostToHost intent based on two host Ids.
85 *
86 * @param oneIdString string for host one id
87 * @param twoIdString string for host two id
88 * @return HostToHostIntent for the two hosts
89 */
90 private HostToHostIntent makeIntent(String oneIdString, String twoIdString) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070091 return new HostToHostIntent(APPID, hid(oneIdString), hid(twoIdString),
92 selector, treatment);
Ray Milkeye6684082014-10-16 16:59:47 -070093 }
94
95 /**
96 * Creates a compiler for HostToHost intents.
97 *
98 * @param hops string array describing the path hops to use when compiling
99 * @return HostToHost intent compiler
100 */
101 private HostToHostIntentCompiler makeCompiler(String[] hops) {
102 HostToHostIntentCompiler compiler =
103 new HostToHostIntentCompiler();
104 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
105 compiler.hostService = mockHostService;
Ray Milkeye6684082014-10-16 16:59:47 -0700106 return compiler;
107 }
108
109
110 /**
111 * Tests a pair of hosts with 8 hops between them.
112 */
113 @Test
114 public void testSingleLongPathCompilation() {
115
116 HostToHostIntent intent = makeIntent(HOST_ONE,
117 HOST_TWO);
118 assertThat(intent, is(notNullValue()));
119
120 String[] hops = {HOST_ONE, "h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", HOST_TWO};
121 HostToHostIntentCompiler compiler = makeCompiler(hops);
122 assertThat(compiler, is(notNullValue()));
123
124 List<Intent> result = compiler.compile(intent);
125 assertThat(result, is(Matchers.notNullValue()));
126 assertThat(result, hasSize(2));
127 Intent forwardResultIntent = result.get(0);
128 assertThat(forwardResultIntent instanceof PathIntent, is(true));
129 Intent reverseResultIntent = result.get(1);
130 assertThat(reverseResultIntent instanceof PathIntent, is(true));
131
132 if (forwardResultIntent instanceof PathIntent) {
133 PathIntent forwardPathIntent = (PathIntent) forwardResultIntent;
134 assertThat(forwardPathIntent.path().links(), hasSize(9));
135 assertThat(forwardPathIntent.path().links(), linksHasPath(HOST_ONE, "h1"));
136 assertThat(forwardPathIntent.path().links(), linksHasPath("h1", "h2"));
137 assertThat(forwardPathIntent.path().links(), linksHasPath("h2", "h3"));
138 assertThat(forwardPathIntent.path().links(), linksHasPath("h3", "h4"));
139 assertThat(forwardPathIntent.path().links(), linksHasPath("h4", "h5"));
140 assertThat(forwardPathIntent.path().links(), linksHasPath("h5", "h6"));
141 assertThat(forwardPathIntent.path().links(), linksHasPath("h6", "h7"));
142 assertThat(forwardPathIntent.path().links(), linksHasPath("h7", "h8"));
143 assertThat(forwardPathIntent.path().links(), linksHasPath("h8", HOST_TWO));
144 }
145
146 if (reverseResultIntent instanceof PathIntent) {
147 PathIntent reversePathIntent = (PathIntent) reverseResultIntent;
148 assertThat(reversePathIntent.path().links(), hasSize(9));
149 assertThat(reversePathIntent.path().links(), linksHasPath("h1", HOST_ONE));
150 assertThat(reversePathIntent.path().links(), linksHasPath("h2", "h1"));
151 assertThat(reversePathIntent.path().links(), linksHasPath("h3", "h2"));
152 assertThat(reversePathIntent.path().links(), linksHasPath("h4", "h3"));
153 assertThat(reversePathIntent.path().links(), linksHasPath("h5", "h4"));
154 assertThat(reversePathIntent.path().links(), linksHasPath("h6", "h5"));
155 assertThat(reversePathIntent.path().links(), linksHasPath("h7", "h6"));
156 assertThat(reversePathIntent.path().links(), linksHasPath("h8", "h7"));
157 assertThat(reversePathIntent.path().links(), linksHasPath(HOST_TWO, "h8"));
158 }
159 }
160}