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