blob: f57d6d9117e90a22d29400bd775bde1242af8af1 [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;
27import org.onlab.onos.net.intent.Intent;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070028import org.onlab.onos.net.intent.IntentTestsMocks;
29import org.onlab.onos.net.intent.PathIntent;
30import org.onlab.onos.net.intent.PointToPointIntent;
31
Thomas Vachuskab97cf282014-10-20 23:31:12 -070032import java.util.List;
33
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -080034import static org.hamcrest.CoreMatchers.instanceOf;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070035import static org.hamcrest.CoreMatchers.notNullValue;
36import static org.hamcrest.MatcherAssert.assertThat;
37import static org.hamcrest.Matchers.hasSize;
38import static org.hamcrest.Matchers.is;
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080039import static org.onlab.onos.net.DefaultEdgeLink.createEdgeLink;
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -080040import static org.onlab.onos.net.DeviceId.deviceId;
41import static org.onlab.onos.net.NetTestTools.APP_ID;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070042import static org.onlab.onos.net.NetTestTools.connectPoint;
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -080043import static org.onlab.onos.net.PortNumber.portNumber;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070044import static org.onlab.onos.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
45
46/**
47 * Unit tests for the HostToHost intent compiler.
48 */
49public class TestPointToPointIntentCompiler {
50
Thomas Vachuskab97cf282014-10-20 23:31:12 -070051 private static final ApplicationId APPID = new TestApplicationId("foo");
52
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070053 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
54 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
55
56 /**
57 * Creates a PointToPoint intent based on ingress and egress device Ids.
58 *
59 * @param ingressIdString string for id of ingress device
Thomas Vachuskab97cf282014-10-20 23:31:12 -070060 * @param egressIdString string for id of egress device
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070061 * @return PointToPointIntent for the two devices
62 */
63 private PointToPointIntent makeIntent(String ingressIdString,
64 String egressIdString) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070065 return new PointToPointIntent(APPID, selector, treatment,
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070066 connectPoint(ingressIdString, 1),
67 connectPoint(egressIdString, 1));
68 }
69
70 /**
71 * Creates a compiler for HostToHost intents.
72 *
73 * @param hops string array describing the path hops to use when compiling
74 * @return HostToHost intent compiler
75 */
76 private PointToPointIntentCompiler makeCompiler(String[] hops) {
77 PointToPointIntentCompiler compiler =
78 new PointToPointIntentCompiler();
Ray Milkey40f50b92014-11-07 13:25:53 -080079 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070080 return compiler;
81 }
82
83
84 /**
85 * Tests a pair of devices in an 8 hop path, forward direction.
86 */
Ray Milkey40f50b92014-11-07 13:25:53 -080087 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070088 public void testForwardPathCompilation() {
89
90 PointToPointIntent intent = makeIntent("d1", "d8");
91 assertThat(intent, is(notNullValue()));
92
93 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
94 PointToPointIntentCompiler compiler = makeCompiler(hops);
95 assertThat(compiler, is(notNullValue()));
96
Brian O'Connorfa81eae2014-10-30 13:20:05 -070097 List<Intent> result = compiler.compile(intent, null, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070098 assertThat(result, is(Matchers.notNullValue()));
99 assertThat(result, hasSize(1));
100 Intent forwardResultIntent = result.get(0);
101 assertThat(forwardResultIntent instanceof PathIntent, is(true));
102
103 if (forwardResultIntent instanceof PathIntent) {
104 PathIntent forwardPathIntent = (PathIntent) forwardResultIntent;
105 // 7 links for the hops, plus one default lnk on ingress and egress
106 assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1));
107 assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2"));
108 assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3"));
109 assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4"));
110 assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5"));
111 assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6"));
112 assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7"));
113 assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8"));
114 }
115 }
116
117 /**
118 * Tests a pair of devices in an 8 hop path, forward direction.
119 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800120 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700121 public void testReversePathCompilation() {
122
123 PointToPointIntent intent = makeIntent("d8", "d1");
124 assertThat(intent, is(notNullValue()));
125
126 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
127 PointToPointIntentCompiler compiler = makeCompiler(hops);
128 assertThat(compiler, is(notNullValue()));
129
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700130 List<Intent> result = compiler.compile(intent, null, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700131 assertThat(result, is(Matchers.notNullValue()));
132 assertThat(result, hasSize(1));
133 Intent reverseResultIntent = result.get(0);
134 assertThat(reverseResultIntent instanceof PathIntent, is(true));
135
136 if (reverseResultIntent instanceof PathIntent) {
137 PathIntent reversePathIntent = (PathIntent) reverseResultIntent;
138 assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1));
139 assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1"));
140 assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2"));
141 assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3"));
142 assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4"));
143 assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5"));
144 assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6"));
145 assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7"));
146 }
147 }
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800148
149 /**
150 * Tests compilation of the intent which designates two different ports on the same switch.
151 */
152 @Test
153 public void testSameSwitchDifferentPortsIntentCompilation() {
154 ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1));
155 ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2));
156 PointToPointIntent intent = new PointToPointIntent(APP_ID, selector, treatment, src, dst);
157
158 String[] hops = {"1"};
159 PointToPointIntentCompiler sut = makeCompiler(hops);
160
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700161 List<Intent> compiled = sut.compile(intent, null, null);
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800162
163 assertThat(compiled, hasSize(1));
164 assertThat(compiled.get(0), is(instanceOf(PathIntent.class)));
165 Path path = ((PathIntent) compiled.get(0)).path();
166
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800167 assertThat(path.links(), hasSize(2));
168 Link firstLink = path.links().get(0);
169 assertThat(firstLink, is(createEdgeLink(src, true)));
170 Link secondLink = path.links().get(1);
171 assertThat(secondLink, is(createEdgeLink(dst, false)));
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800172 }
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700173}