blob: 939cab041582a761829b58d8161cc010c5ea1f17 [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;
Yuta HIGUCHId95d5902016-06-27 00:18:45 -070033import org.onosproject.net.resource.MockResourceService;
Ray Milkeye6684082014-10-16 16:59:47 -070034import org.onlab.packet.MacAddress;
35import org.onlab.packet.VlanId;
36
Thomas Vachuskab97cf282014-10-20 23:31:12 -070037import java.util.List;
38
39import static org.easymock.EasyMock.*;
Ray Milkeye6684082014-10-16 16:59:47 -070040import static org.hamcrest.CoreMatchers.notNullValue;
41import static org.hamcrest.MatcherAssert.assertThat;
42import static org.hamcrest.Matchers.hasSize;
43import static org.hamcrest.Matchers.is;
Brian O'Connorabafb502014-12-02 22:26:20 -080044import static org.onosproject.net.NetTestTools.hid;
45import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
Ray Milkeye6684082014-10-16 16:59:47 -070046
47/**
48 * Unit tests for the HostToHost intent compiler.
49 */
Ray Milkey37f6a382014-11-25 14:54:42 -080050public class HostToHostIntentCompilerTest extends AbstractIntentTest {
Ray Milkeye6684082014-10-16 16:59:47 -070051 private static final String HOST_ONE_MAC = "00:00:00:00:00:01";
52 private static final String HOST_TWO_MAC = "00:00:00:00:00:02";
Luca Prete283a9622016-03-29 16:12:20 -070053 private static final String HOST_ONE_VLAN = "None";
54 private static final String HOST_TWO_VLAN = "None";
Ray Milkeye6684082014-10-16 16:59:47 -070055 private static final String HOST_ONE = HOST_ONE_MAC + "/" + HOST_ONE_VLAN;
56 private static final String HOST_TWO = HOST_TWO_MAC + "/" + HOST_TWO_VLAN;
57
Thomas Vachuskab97cf282014-10-20 23:31:12 -070058 private static final ApplicationId APPID = new TestApplicationId("foo");
59
Ray Milkeye6684082014-10-16 16:59:47 -070060 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
61 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
62
63 private HostId hostOneId = HostId.hostId(HOST_ONE);
64 private HostId hostTwoId = HostId.hostId(HOST_TWO);
65 private HostService mockHostService;
66
67 @Before
Brian O'Connor520c0522014-11-23 23:50:47 -080068 public void setUp() throws Exception {
69 super.setUp();
Ray Milkeye6684082014-10-16 16:59:47 -070070 Host hostOne = createMock(Host.class);
71 expect(hostOne.mac()).andReturn(new MacAddress(HOST_ONE_MAC.getBytes())).anyTimes();
72 expect(hostOne.vlan()).andReturn(VlanId.vlanId()).anyTimes();
73 replay(hostOne);
74
75 Host hostTwo = createMock(Host.class);
76 expect(hostTwo.mac()).andReturn(new MacAddress(HOST_TWO_MAC.getBytes())).anyTimes();
77 expect(hostTwo.vlan()).andReturn(VlanId.vlanId()).anyTimes();
78 replay(hostTwo);
79
80 mockHostService = createMock(HostService.class);
81 expect(mockHostService.getHost(eq(hostOneId))).andReturn(hostOne).anyTimes();
82 expect(mockHostService.getHost(eq(hostTwoId))).andReturn(hostTwo).anyTimes();
83 replay(mockHostService);
84 }
85
86 /**
87 * Creates a HostToHost intent based on two host Ids.
88 *
89 * @param oneIdString string for host one id
90 * @param twoIdString string for host two id
91 * @return HostToHostIntent for the two hosts
92 */
93 private HostToHostIntent makeIntent(String oneIdString, String twoIdString) {
Ray Milkeyebc5d222015-03-18 15:45:36 -070094 return HostToHostIntent.builder()
95 .appId(APPID)
96 .one(hid(oneIdString))
97 .two(hid(twoIdString))
98 .selector(selector)
99 .treatment(treatment)
100 .build();
Ray Milkeye6684082014-10-16 16:59:47 -0700101 }
102
103 /**
104 * Creates a compiler for HostToHost intents.
105 *
106 * @param hops string array describing the path hops to use when compiling
107 * @return HostToHost intent compiler
108 */
109 private HostToHostIntentCompiler makeCompiler(String[] hops) {
110 HostToHostIntentCompiler compiler =
111 new HostToHostIntentCompiler();
112 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
113 compiler.hostService = mockHostService;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800114 compiler.resourceService = new MockResourceService();
Ray Milkeye6684082014-10-16 16:59:47 -0700115 return compiler;
116 }
117
118
119 /**
120 * Tests a pair of hosts with 8 hops between them.
121 */
122 @Test
123 public void testSingleLongPathCompilation() {
124
125 HostToHostIntent intent = makeIntent(HOST_ONE,
126 HOST_TWO);
127 assertThat(intent, is(notNullValue()));
128
129 String[] hops = {HOST_ONE, "h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", HOST_TWO};
130 HostToHostIntentCompiler compiler = makeCompiler(hops);
131 assertThat(compiler, is(notNullValue()));
132
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800133 List<Intent> result = compiler.compile(intent, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700134 assertThat(result, is(Matchers.notNullValue()));
135 assertThat(result, hasSize(2));
136 Intent forwardResultIntent = result.get(0);
137 assertThat(forwardResultIntent instanceof PathIntent, is(true));
138 Intent reverseResultIntent = result.get(1);
139 assertThat(reverseResultIntent instanceof PathIntent, is(true));
140
141 if (forwardResultIntent instanceof PathIntent) {
142 PathIntent forwardPathIntent = (PathIntent) forwardResultIntent;
143 assertThat(forwardPathIntent.path().links(), hasSize(9));
144 assertThat(forwardPathIntent.path().links(), linksHasPath(HOST_ONE, "h1"));
145 assertThat(forwardPathIntent.path().links(), linksHasPath("h1", "h2"));
146 assertThat(forwardPathIntent.path().links(), linksHasPath("h2", "h3"));
147 assertThat(forwardPathIntent.path().links(), linksHasPath("h3", "h4"));
148 assertThat(forwardPathIntent.path().links(), linksHasPath("h4", "h5"));
149 assertThat(forwardPathIntent.path().links(), linksHasPath("h5", "h6"));
150 assertThat(forwardPathIntent.path().links(), linksHasPath("h6", "h7"));
151 assertThat(forwardPathIntent.path().links(), linksHasPath("h7", "h8"));
152 assertThat(forwardPathIntent.path().links(), linksHasPath("h8", HOST_TWO));
153 }
154
155 if (reverseResultIntent instanceof PathIntent) {
156 PathIntent reversePathIntent = (PathIntent) reverseResultIntent;
157 assertThat(reversePathIntent.path().links(), hasSize(9));
158 assertThat(reversePathIntent.path().links(), linksHasPath("h1", HOST_ONE));
159 assertThat(reversePathIntent.path().links(), linksHasPath("h2", "h1"));
160 assertThat(reversePathIntent.path().links(), linksHasPath("h3", "h2"));
161 assertThat(reversePathIntent.path().links(), linksHasPath("h4", "h3"));
162 assertThat(reversePathIntent.path().links(), linksHasPath("h5", "h4"));
163 assertThat(reversePathIntent.path().links(), linksHasPath("h6", "h5"));
164 assertThat(reversePathIntent.path().links(), linksHasPath("h7", "h6"));
165 assertThat(reversePathIntent.path().links(), linksHasPath("h8", "h7"));
166 assertThat(reversePathIntent.path().links(), linksHasPath(HOST_TWO, "h8"));
167 }
168 }
169}