blob: 03a38a5a408720a0ecc157ba9f7a3da5b4df2f6b [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
2 * Copyright 2015 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 */
Sho SHIMIZU6c28f832015-02-20 16:12:19 -080016package org.onosproject.net.intent.impl.compiler;
Michele Santuari4b6019e2014-12-19 11:31:45 +010017
18import java.util.List;
19import java.util.Optional;
20
21import org.hamcrest.Matchers;
22import org.junit.Test;
23
24import static org.junit.Assert.assertEquals;
25
26
27import org.onlab.packet.MplsLabel;
28import org.onosproject.TestApplicationId;
29import org.onosproject.core.ApplicationId;
30import org.onosproject.net.ConnectPoint;
31import org.onosproject.net.Link;
32import org.onosproject.net.Path;
33import org.onosproject.net.flow.TrafficSelector;
34import org.onosproject.net.flow.TrafficTreatment;
35import org.onosproject.net.intent.AbstractIntentTest;
36import org.onosproject.net.intent.Intent;
37import org.onosproject.net.intent.IntentTestsMocks;
38import org.onosproject.net.intent.MplsIntent;
39import org.onosproject.net.intent.MplsPathIntent;
40
41import static org.hamcrest.CoreMatchers.instanceOf;
42import static org.hamcrest.CoreMatchers.notNullValue;
43import static org.hamcrest.MatcherAssert.assertThat;
44import static org.hamcrest.Matchers.hasSize;
45import static org.hamcrest.Matchers.is;
46import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
47import static org.onosproject.net.DeviceId.deviceId;
48import static org.onosproject.net.NetTestTools.APP_ID;
49import static org.onosproject.net.NetTestTools.connectPoint;
50import static org.onosproject.net.PortNumber.portNumber;
51import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
52
53/**
54 * Unit tests for the HostToHost intent compiler.
55 */
56public class MplsIntentCompilerTest extends AbstractIntentTest {
57
58 private static final ApplicationId APPID = new TestApplicationId("foo");
59
60 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
61 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
62
63 /**
64 * Creates a PointToPoint intent based on ingress and egress device Ids.
65 *
66 * @param ingressIdString string for id of ingress device
67 * @param egressIdString string for id of egress device
68 * @return PointToPointIntent for the two devices
69 */
70 private MplsIntent makeIntent(String ingressIdString, Optional<MplsLabel> ingressLabel,
71 String egressIdString, Optional<MplsLabel> egressLabel) {
72
Ray Milkeyebc5d222015-03-18 15:45:36 -070073 return MplsIntent.builder()
74 .appId(APPID)
75 .selector(selector)
76 .treatment(treatment)
77 .ingressPoint(connectPoint(ingressIdString, 1))
78 .ingressLabel(ingressLabel)
79 .egressPoint(connectPoint(egressIdString, 1))
80 .egressLabel(egressLabel).build();
Michele Santuari4b6019e2014-12-19 11:31:45 +010081 }
82 /**
83 * Creates a compiler for HostToHost intents.
84 *
85 * @param hops string array describing the path hops to use when compiling
86 * @return HostToHost intent compiler
87 */
88 private MplsIntentCompiler makeCompiler(String[] hops) {
89 MplsIntentCompiler compiler =
90 new MplsIntentCompiler();
91 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
92 return compiler;
93 }
94
95
96 /**
97 * Tests a pair of devices in an 8 hop path, forward direction.
98 */
99 @Test
100 public void testForwardPathCompilation() {
Ray Milkey167ae0b2015-02-26 07:33:25 -0800101 Optional<MplsLabel> ingressLabel = Optional.of(MplsLabel.mplsLabel(10));
102 Optional<MplsLabel> egressLabel = Optional.of(MplsLabel.mplsLabel(20));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100103
104 MplsIntent intent = makeIntent("d1", ingressLabel, "d8", egressLabel);
105 assertThat(intent, is(notNullValue()));
106
107 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
108 MplsIntentCompiler compiler = makeCompiler(hops);
109 assertThat(compiler, is(notNullValue()));
110
111 List<Intent> result = compiler.compile(intent, null, null);
112 assertThat(result, is(Matchers.notNullValue()));
113 assertThat(result, hasSize(1));
114 Intent forwardResultIntent = result.get(0);
115 assertThat(forwardResultIntent instanceof MplsPathIntent, is(true));
116
Ray Milkeya046a872015-02-26 12:07:50 -0800117 // if statement suppresses static analysis warnings about unchecked cast
118 if (forwardResultIntent instanceof MplsPathIntent) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100119 MplsPathIntent forwardPathIntent = (MplsPathIntent) forwardResultIntent;
120 // 7 links for the hops, plus one default lnk on ingress and egress
121 assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1));
122 assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2"));
123 assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3"));
124 assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4"));
125 assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5"));
126 assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6"));
127 assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7"));
128 assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8"));
129 assertEquals(forwardPathIntent.egressLabel(), egressLabel);
130 assertEquals(forwardPathIntent.ingressLabel(), ingressLabel);
131 }
132 }
133
134 /**
135 * Tests a pair of devices in an 8 hop path, forward direction.
136 */
137 @Test
138 public void testReversePathCompilation() {
Ray Milkey167ae0b2015-02-26 07:33:25 -0800139 Optional<MplsLabel> ingressLabel = Optional.of(MplsLabel.mplsLabel(10));
140 Optional<MplsLabel> egressLabel = Optional.of(MplsLabel.mplsLabel(20));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100141
142 MplsIntent intent = makeIntent("d8", ingressLabel, "d1", egressLabel);
143 assertThat(intent, is(notNullValue()));
144
145 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
146 MplsIntentCompiler compiler = makeCompiler(hops);
147 assertThat(compiler, is(notNullValue()));
148
149 List<Intent> result = compiler.compile(intent, null, null);
150 assertThat(result, is(Matchers.notNullValue()));
151 assertThat(result, hasSize(1));
152 Intent reverseResultIntent = result.get(0);
153 assertThat(reverseResultIntent instanceof MplsPathIntent, is(true));
154
Ray Milkeya046a872015-02-26 12:07:50 -0800155 // if statement suppresses static analysis warnings about unchecked cast
156 if (reverseResultIntent instanceof MplsPathIntent) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100157 MplsPathIntent reversePathIntent = (MplsPathIntent) reverseResultIntent;
158 assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1));
159 assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1"));
160 assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2"));
161 assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3"));
162 assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4"));
163 assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5"));
164 assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6"));
165 assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7"));
166 assertEquals(reversePathIntent.egressLabel(), egressLabel);
167 assertEquals(reversePathIntent.ingressLabel(), ingressLabel);
168 }
169 }
170
171 /**
172 * Tests compilation of the intent which designates two different ports on the same switch.
173 */
174 @Test
175 public void testSameSwitchDifferentPortsIntentCompilation() {
176 ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1));
177 ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2));
Ray Milkeyebc5d222015-03-18 15:45:36 -0700178 MplsIntent intent = MplsIntent.builder()
179 .appId(APP_ID)
180 .selector(selector)
181 .treatment(treatment)
182 .ingressPoint(src)
183 .ingressLabel(Optional.empty())
184 .egressPoint(dst)
185 .egressLabel(Optional.empty())
186 .build();
Michele Santuari4b6019e2014-12-19 11:31:45 +0100187
188 String[] hops = {"1"};
189 MplsIntentCompiler sut = makeCompiler(hops);
190
191 List<Intent> compiled = sut.compile(intent, null, null);
192
193 assertThat(compiled, hasSize(1));
194 assertThat(compiled.get(0), is(instanceOf(MplsPathIntent.class)));
195 Path path = ((MplsPathIntent) compiled.get(0)).path();
196
197 assertThat(path.links(), hasSize(2));
198 Link firstLink = path.links().get(0);
199 assertThat(firstLink, is(createEdgeLink(src, true)));
200 Link secondLink = path.links().get(1);
201 assertThat(secondLink, is(createEdgeLink(dst, false)));
202 }
203}