blob: 65a4597c1a7825a9a2414578aa2de94304066f29 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.impl;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070017
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070018import org.hamcrest.Matchers;
Ray Milkey40f50b92014-11-07 13:25:53 -080019import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.TestApplicationId;
21import org.onosproject.core.ApplicationId;
22import org.onosproject.net.ConnectPoint;
23import org.onosproject.net.Link;
24import org.onosproject.net.Path;
25import org.onosproject.net.flow.TrafficSelector;
26import org.onosproject.net.flow.TrafficTreatment;
27import org.onosproject.net.intent.AbstractIntentTest;
28import org.onosproject.net.intent.Intent;
29import org.onosproject.net.intent.IntentTestsMocks;
30import org.onosproject.net.intent.PathIntent;
31import org.onosproject.net.intent.PointToPointIntent;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070032
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.MatcherAssert.assertThat;
37import static org.hamcrest.Matchers.hasSize;
38import static org.hamcrest.Matchers.is;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
40import static org.onosproject.net.DeviceId.deviceId;
41import static org.onosproject.net.NetTestTools.APP_ID;
42import static org.onosproject.net.NetTestTools.connectPoint;
43import static org.onosproject.net.PortNumber.portNumber;
44import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070045
46/**
47 * Unit tests for the HostToHost intent compiler.
48 */
Ray Milkey37f6a382014-11-25 14:54:42 -080049public class PointToPointIntentCompilerTest extends AbstractIntentTest {
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070050
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");
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070091
92 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
93 PointToPointIntentCompiler compiler = makeCompiler(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070094
Brian O'Connorfa81eae2014-10-30 13:20:05 -070095 List<Intent> result = compiler.compile(intent, null, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070096 assertThat(result, is(Matchers.notNullValue()));
97 assertThat(result, hasSize(1));
98 Intent forwardResultIntent = result.get(0);
99 assertThat(forwardResultIntent instanceof PathIntent, is(true));
100
101 if (forwardResultIntent instanceof PathIntent) {
102 PathIntent forwardPathIntent = (PathIntent) forwardResultIntent;
103 // 7 links for the hops, plus one default lnk on ingress and egress
104 assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1));
105 assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2"));
106 assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3"));
107 assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4"));
108 assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5"));
109 assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6"));
110 assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7"));
111 assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8"));
112 }
113 }
114
115 /**
116 * Tests a pair of devices in an 8 hop path, forward direction.
117 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800118 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700119 public void testReversePathCompilation() {
120
121 PointToPointIntent intent = makeIntent("d8", "d1");
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700122
123 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
124 PointToPointIntentCompiler compiler = makeCompiler(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700125
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700126 List<Intent> result = compiler.compile(intent, null, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700127 assertThat(result, is(Matchers.notNullValue()));
128 assertThat(result, hasSize(1));
129 Intent reverseResultIntent = result.get(0);
130 assertThat(reverseResultIntent instanceof PathIntent, is(true));
131
132 if (reverseResultIntent instanceof PathIntent) {
133 PathIntent reversePathIntent = (PathIntent) reverseResultIntent;
134 assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1));
135 assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1"));
136 assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2"));
137 assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3"));
138 assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4"));
139 assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5"));
140 assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6"));
141 assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7"));
142 }
143 }
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800144
145 /**
146 * Tests compilation of the intent which designates two different ports on the same switch.
147 */
148 @Test
149 public void testSameSwitchDifferentPortsIntentCompilation() {
150 ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1));
151 ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2));
152 PointToPointIntent intent = new PointToPointIntent(APP_ID, selector, treatment, src, dst);
153
154 String[] hops = {"1"};
155 PointToPointIntentCompiler sut = makeCompiler(hops);
156
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700157 List<Intent> compiled = sut.compile(intent, null, null);
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800158
159 assertThat(compiled, hasSize(1));
160 assertThat(compiled.get(0), is(instanceOf(PathIntent.class)));
161 Path path = ((PathIntent) compiled.get(0)).path();
162
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800163 assertThat(path.links(), hasSize(2));
164 Link firstLink = path.links().get(0);
165 assertThat(firstLink, is(createEdgeLink(src, true)));
166 Link secondLink = path.links().get(1);
167 assertThat(secondLink, is(createEdgeLink(dst, false)));
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800168 }
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700169}