blob: 20345aacc5d78a3d74d751d72b7ec89fb6b9f73a [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;
weibit50eb95b2014-10-25 21:47:54 -070019//import org.junit.Test;
Ray Milkey40f50b92014-11-07 13:25:53 -080020import org.junit.Test;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070021import org.onlab.onos.core.ApplicationId;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070022import org.onlab.onos.TestApplicationId;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070023import org.onlab.onos.net.flow.TrafficSelector;
24import org.onlab.onos.net.flow.TrafficTreatment;
25import org.onlab.onos.net.intent.Intent;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070026import org.onlab.onos.net.intent.IntentTestsMocks;
27import org.onlab.onos.net.intent.PathIntent;
28import org.onlab.onos.net.intent.PointToPointIntent;
29
Thomas Vachuskab97cf282014-10-20 23:31:12 -070030import java.util.List;
31
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070032import static org.hamcrest.CoreMatchers.notNullValue;
33import static org.hamcrest.MatcherAssert.assertThat;
34import static org.hamcrest.Matchers.hasSize;
35import static org.hamcrest.Matchers.is;
36import static org.onlab.onos.net.NetTestTools.connectPoint;
37import static org.onlab.onos.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
38
39/**
40 * Unit tests for the HostToHost intent compiler.
41 */
42public class TestPointToPointIntentCompiler {
43
Thomas Vachuskab97cf282014-10-20 23:31:12 -070044 private static final ApplicationId APPID = new TestApplicationId("foo");
45
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070046 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
47 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
48
49 /**
50 * Creates a PointToPoint intent based on ingress and egress device Ids.
51 *
52 * @param ingressIdString string for id of ingress device
Thomas Vachuskab97cf282014-10-20 23:31:12 -070053 * @param egressIdString string for id of egress device
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070054 * @return PointToPointIntent for the two devices
55 */
56 private PointToPointIntent makeIntent(String ingressIdString,
57 String egressIdString) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070058 return new PointToPointIntent(APPID, selector, treatment,
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070059 connectPoint(ingressIdString, 1),
60 connectPoint(egressIdString, 1));
61 }
62
63 /**
64 * Creates a compiler for HostToHost intents.
65 *
66 * @param hops string array describing the path hops to use when compiling
67 * @return HostToHost intent compiler
68 */
69 private PointToPointIntentCompiler makeCompiler(String[] hops) {
70 PointToPointIntentCompiler compiler =
71 new PointToPointIntentCompiler();
Ray Milkey40f50b92014-11-07 13:25:53 -080072 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070073 return compiler;
74 }
75
76
77 /**
78 * Tests a pair of devices in an 8 hop path, forward direction.
79 */
Ray Milkey40f50b92014-11-07 13:25:53 -080080 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070081 public void testForwardPathCompilation() {
82
83 PointToPointIntent intent = makeIntent("d1", "d8");
84 assertThat(intent, is(notNullValue()));
85
86 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
87 PointToPointIntentCompiler compiler = makeCompiler(hops);
88 assertThat(compiler, is(notNullValue()));
89
90 List<Intent> result = compiler.compile(intent);
91 assertThat(result, is(Matchers.notNullValue()));
92 assertThat(result, hasSize(1));
93 Intent forwardResultIntent = result.get(0);
94 assertThat(forwardResultIntent instanceof PathIntent, is(true));
95
96 if (forwardResultIntent instanceof PathIntent) {
97 PathIntent forwardPathIntent = (PathIntent) forwardResultIntent;
98 // 7 links for the hops, plus one default lnk on ingress and egress
99 assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1));
100 assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2"));
101 assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3"));
102 assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4"));
103 assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5"));
104 assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6"));
105 assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7"));
106 assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8"));
107 }
108 }
109
110 /**
111 * Tests a pair of devices in an 8 hop path, forward direction.
112 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800113 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700114 public void testReversePathCompilation() {
115
116 PointToPointIntent intent = makeIntent("d8", "d1");
117 assertThat(intent, is(notNullValue()));
118
119 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
120 PointToPointIntentCompiler compiler = makeCompiler(hops);
121 assertThat(compiler, is(notNullValue()));
122
123 List<Intent> result = compiler.compile(intent);
124 assertThat(result, is(Matchers.notNullValue()));
125 assertThat(result, hasSize(1));
126 Intent reverseResultIntent = result.get(0);
127 assertThat(reverseResultIntent instanceof PathIntent, is(true));
128
129 if (reverseResultIntent instanceof PathIntent) {
130 PathIntent reversePathIntent = (PathIntent) reverseResultIntent;
131 assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1));
132 assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1"));
133 assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2"));
134 assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3"));
135 assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4"));
136 assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5"));
137 assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6"));
138 assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7"));
139 }
140 }
141}