blob: 3070664194246cd7d4d0d7f7722f54c83a6f79da [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 Milkeyc0fa4db2014-10-17 08:49:54 -070016package org.onlab.onos.net.intent.impl;
17
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070018import org.hamcrest.Matchers;
Ray Milkey40f50b92014-11-07 13:25:53 -080019import org.junit.Test;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070020import org.onlab.onos.TestApplicationId;
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -080021import org.onlab.onos.core.ApplicationId;
22import org.onlab.onos.net.ConnectPoint;
23import org.onlab.onos.net.Link;
24import org.onlab.onos.net.Path;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070025import org.onlab.onos.net.flow.TrafficSelector;
26import org.onlab.onos.net.flow.TrafficTreatment;
Brian O'Connor520c0522014-11-23 23:50:47 -080027import org.onlab.onos.net.intent.AbstractIntentTest;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070028import org.onlab.onos.net.intent.Intent;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070029import org.onlab.onos.net.intent.IntentTestsMocks;
30import org.onlab.onos.net.intent.PathIntent;
31import org.onlab.onos.net.intent.PointToPointIntent;
32
Thomas Vachuskab97cf282014-10-20 23:31:12 -070033import java.util.List;
34
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -080035import static org.hamcrest.CoreMatchers.instanceOf;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070036import static org.hamcrest.CoreMatchers.notNullValue;
37import static org.hamcrest.MatcherAssert.assertThat;
38import static org.hamcrest.Matchers.hasSize;
39import static org.hamcrest.Matchers.is;
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080040import static org.onlab.onos.net.DefaultEdgeLink.createEdgeLink;
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -080041import static org.onlab.onos.net.DeviceId.deviceId;
42import static org.onlab.onos.net.NetTestTools.APP_ID;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070043import static org.onlab.onos.net.NetTestTools.connectPoint;
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -080044import static org.onlab.onos.net.PortNumber.portNumber;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070045import static org.onlab.onos.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
46
47/**
48 * Unit tests for the HostToHost intent compiler.
49 */
Brian O'Connor520c0522014-11-23 23:50:47 -080050public class TestPointToPointIntentCompiler extends AbstractIntentTest {
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070051
Thomas Vachuskab97cf282014-10-20 23:31:12 -070052 private static final ApplicationId APPID = new TestApplicationId("foo");
53
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070054 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
55 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
56
57 /**
58 * Creates a PointToPoint intent based on ingress and egress device Ids.
59 *
60 * @param ingressIdString string for id of ingress device
Thomas Vachuskab97cf282014-10-20 23:31:12 -070061 * @param egressIdString string for id of egress device
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070062 * @return PointToPointIntent for the two devices
63 */
64 private PointToPointIntent makeIntent(String ingressIdString,
65 String egressIdString) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070066 return new PointToPointIntent(APPID, selector, treatment,
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070067 connectPoint(ingressIdString, 1),
68 connectPoint(egressIdString, 1));
69 }
70
71 /**
72 * Creates a compiler for HostToHost intents.
73 *
74 * @param hops string array describing the path hops to use when compiling
75 * @return HostToHost intent compiler
76 */
77 private PointToPointIntentCompiler makeCompiler(String[] hops) {
78 PointToPointIntentCompiler compiler =
79 new PointToPointIntentCompiler();
Ray Milkey40f50b92014-11-07 13:25:53 -080080 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070081 return compiler;
82 }
83
84
85 /**
86 * Tests a pair of devices in an 8 hop path, forward direction.
87 */
Ray Milkey40f50b92014-11-07 13:25:53 -080088 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070089 public void testForwardPathCompilation() {
90
91 PointToPointIntent intent = makeIntent("d1", "d8");
92 assertThat(intent, is(notNullValue()));
93
94 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
95 PointToPointIntentCompiler compiler = makeCompiler(hops);
96 assertThat(compiler, is(notNullValue()));
97
Brian O'Connorfa81eae2014-10-30 13:20:05 -070098 List<Intent> result = compiler.compile(intent, null, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070099 assertThat(result, is(Matchers.notNullValue()));
100 assertThat(result, hasSize(1));
101 Intent forwardResultIntent = result.get(0);
102 assertThat(forwardResultIntent instanceof PathIntent, is(true));
103
104 if (forwardResultIntent instanceof PathIntent) {
105 PathIntent forwardPathIntent = (PathIntent) forwardResultIntent;
106 // 7 links for the hops, plus one default lnk on ingress and egress
107 assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1));
108 assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2"));
109 assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3"));
110 assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4"));
111 assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5"));
112 assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6"));
113 assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7"));
114 assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8"));
115 }
116 }
117
118 /**
119 * Tests a pair of devices in an 8 hop path, forward direction.
120 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800121 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700122 public void testReversePathCompilation() {
123
124 PointToPointIntent intent = makeIntent("d8", "d1");
125 assertThat(intent, is(notNullValue()));
126
127 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
128 PointToPointIntentCompiler compiler = makeCompiler(hops);
129 assertThat(compiler, is(notNullValue()));
130
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700131 List<Intent> result = compiler.compile(intent, null, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700132 assertThat(result, is(Matchers.notNullValue()));
133 assertThat(result, hasSize(1));
134 Intent reverseResultIntent = result.get(0);
135 assertThat(reverseResultIntent instanceof PathIntent, is(true));
136
137 if (reverseResultIntent instanceof PathIntent) {
138 PathIntent reversePathIntent = (PathIntent) reverseResultIntent;
139 assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1));
140 assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1"));
141 assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2"));
142 assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3"));
143 assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4"));
144 assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5"));
145 assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6"));
146 assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7"));
147 }
148 }
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800149
150 /**
151 * Tests compilation of the intent which designates two different ports on the same switch.
152 */
153 @Test
154 public void testSameSwitchDifferentPortsIntentCompilation() {
155 ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1));
156 ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2));
157 PointToPointIntent intent = new PointToPointIntent(APP_ID, selector, treatment, src, dst);
158
159 String[] hops = {"1"};
160 PointToPointIntentCompiler sut = makeCompiler(hops);
161
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700162 List<Intent> compiled = sut.compile(intent, null, null);
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800163
164 assertThat(compiled, hasSize(1));
165 assertThat(compiled.get(0), is(instanceOf(PathIntent.class)));
166 Path path = ((PathIntent) compiled.get(0)).path();
167
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800168 assertThat(path.links(), hasSize(2));
169 Link firstLink = path.links().get(0);
170 assertThat(firstLink, is(createEdgeLink(src, true)));
171 Link secondLink = path.links().get(1);
172 assertThat(secondLink, is(createEdgeLink(dst, false)));
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800173 }
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700174}