blob: 7f7e6d38eee1a7d5a0331fcd87dfa56c0c8a887d [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 SHIMIZUde8e6b52014-11-13 11:23:17 -080039import static org.onlab.onos.net.DeviceId.deviceId;
40import static org.onlab.onos.net.NetTestTools.APP_ID;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070041import static org.onlab.onos.net.NetTestTools.connectPoint;
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -080042import static org.onlab.onos.net.PortNumber.portNumber;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070043import static org.onlab.onos.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
44
45/**
46 * Unit tests for the HostToHost intent compiler.
47 */
48public class TestPointToPointIntentCompiler {
49
Thomas Vachuskab97cf282014-10-20 23:31:12 -070050 private static final ApplicationId APPID = new TestApplicationId("foo");
51
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070052 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
53 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
54
55 /**
56 * Creates a PointToPoint intent based on ingress and egress device Ids.
57 *
58 * @param ingressIdString string for id of ingress device
Thomas Vachuskab97cf282014-10-20 23:31:12 -070059 * @param egressIdString string for id of egress device
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070060 * @return PointToPointIntent for the two devices
61 */
62 private PointToPointIntent makeIntent(String ingressIdString,
63 String egressIdString) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070064 return new PointToPointIntent(APPID, selector, treatment,
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070065 connectPoint(ingressIdString, 1),
66 connectPoint(egressIdString, 1));
67 }
68
69 /**
70 * Creates a compiler for HostToHost intents.
71 *
72 * @param hops string array describing the path hops to use when compiling
73 * @return HostToHost intent compiler
74 */
75 private PointToPointIntentCompiler makeCompiler(String[] hops) {
76 PointToPointIntentCompiler compiler =
77 new PointToPointIntentCompiler();
Ray Milkey40f50b92014-11-07 13:25:53 -080078 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070079 return compiler;
80 }
81
82
83 /**
84 * Tests a pair of devices in an 8 hop path, forward direction.
85 */
Ray Milkey40f50b92014-11-07 13:25:53 -080086 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070087 public void testForwardPathCompilation() {
88
89 PointToPointIntent intent = makeIntent("d1", "d8");
90 assertThat(intent, is(notNullValue()));
91
92 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
93 PointToPointIntentCompiler compiler = makeCompiler(hops);
94 assertThat(compiler, is(notNullValue()));
95
96 List<Intent> result = compiler.compile(intent);
97 assertThat(result, is(Matchers.notNullValue()));
98 assertThat(result, hasSize(1));
99 Intent forwardResultIntent = result.get(0);
100 assertThat(forwardResultIntent instanceof PathIntent, is(true));
101
102 if (forwardResultIntent instanceof PathIntent) {
103 PathIntent forwardPathIntent = (PathIntent) forwardResultIntent;
104 // 7 links for the hops, plus one default lnk on ingress and egress
105 assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1));
106 assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2"));
107 assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3"));
108 assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4"));
109 assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5"));
110 assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6"));
111 assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7"));
112 assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8"));
113 }
114 }
115
116 /**
117 * Tests a pair of devices in an 8 hop path, forward direction.
118 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800119 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700120 public void testReversePathCompilation() {
121
122 PointToPointIntent intent = makeIntent("d8", "d1");
123 assertThat(intent, is(notNullValue()));
124
125 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
126 PointToPointIntentCompiler compiler = makeCompiler(hops);
127 assertThat(compiler, is(notNullValue()));
128
129 List<Intent> result = compiler.compile(intent);
130 assertThat(result, is(Matchers.notNullValue()));
131 assertThat(result, hasSize(1));
132 Intent reverseResultIntent = result.get(0);
133 assertThat(reverseResultIntent instanceof PathIntent, is(true));
134
135 if (reverseResultIntent instanceof PathIntent) {
136 PathIntent reversePathIntent = (PathIntent) reverseResultIntent;
137 assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1));
138 assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1"));
139 assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2"));
140 assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3"));
141 assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4"));
142 assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5"));
143 assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6"));
144 assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7"));
145 }
146 }
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800147
148 /**
149 * Tests compilation of the intent which designates two different ports on the same switch.
150 */
151 @Test
152 public void testSameSwitchDifferentPortsIntentCompilation() {
153 ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1));
154 ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2));
155 PointToPointIntent intent = new PointToPointIntent(APP_ID, selector, treatment, src, dst);
156
157 String[] hops = {"1"};
158 PointToPointIntentCompiler sut = makeCompiler(hops);
159
160 List<Intent> compiled = sut.compile(intent);
161
162 assertThat(compiled, hasSize(1));
163 assertThat(compiled.get(0), is(instanceOf(PathIntent.class)));
164 Path path = ((PathIntent) compiled.get(0)).path();
165
166 assertThat(path.links(), hasSize(1));
167 Link link = path.links().get(0);
168 assertThat(link.src(), is(src));
169 assertThat(link.dst(), is(dst));
170 }
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700171}