blob: 058b607c4de54dd8e2f41cab9d8ba70202ecadbb [file] [log] [blame]
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001/*
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 */
16package org.onosproject.net.intent.impl.compiler;
17
18import java.util.Arrays;
19import java.util.Collection;
20import java.util.Collections;
21import java.util.List;
22
23import org.junit.After;
24import org.junit.Before;
25import org.junit.Test;
26import org.onosproject.TestApplicationId;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
29import org.onosproject.core.IdGenerator;
30import org.onosproject.net.ConnectPoint;
31import org.onosproject.net.DefaultLink;
32import org.onosproject.net.DefaultPath;
33import org.onosproject.net.Link;
34import org.onosproject.net.flow.DefaultTrafficSelector;
35import org.onosproject.net.flow.DefaultTrafficTreatment;
36import org.onosproject.net.flow.FlowRule;
37import org.onosproject.net.flow.TrafficSelector;
38import org.onosproject.net.flow.TrafficTreatment;
39import org.onosproject.net.intent.FlowRuleIntent;
40import org.onosproject.net.intent.Intent;
41import org.onosproject.net.intent.IntentExtensionService;
42import org.onosproject.net.intent.MockIdGenerator;
43import org.onosproject.net.intent.PathIntent;
44import org.onosproject.net.provider.ProviderId;
45
46import static org.easymock.EasyMock.createMock;
47import static org.easymock.EasyMock.expect;
48import static org.easymock.EasyMock.replay;
49import static org.hamcrest.MatcherAssert.assertThat;
50import static org.hamcrest.Matchers.hasSize;
51import static org.hamcrest.Matchers.is;
52import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
53import static org.onosproject.net.Link.Type.DIRECT;
54import static org.onosproject.net.NetTestTools.APP_ID;
55import static org.onosproject.net.NetTestTools.PID;
56import static org.onosproject.net.NetTestTools.connectPoint;
57
58/**
59 * Unit tests for PathIntentCompiler.
60 */
61public class PathIntentCompilerTest {
62
63 private CoreService coreService;
64 private IntentExtensionService intentExtensionService;
65 private IdGenerator idGenerator = new MockIdGenerator();
66 private PathIntentCompiler sut;
67
68 private final TrafficSelector selector = DefaultTrafficSelector.builder().build();
69 private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
70 private final ApplicationId appId = new TestApplicationId("test");
71 private final ProviderId pid = new ProviderId("of", "test");
72 private final ConnectPoint d1p1 = connectPoint("s1", 0);
73 private final ConnectPoint d2p0 = connectPoint("s2", 0);
74 private final ConnectPoint d2p1 = connectPoint("s2", 1);
75 private final ConnectPoint d3p1 = connectPoint("s3", 1);
76 private final ConnectPoint d3p0 = connectPoint("s3", 10);
77 private final ConnectPoint d1p0 = connectPoint("s1", 10);
78
79 private final List<Link> links = Arrays.asList(
80 createEdgeLink(d1p0, true),
81 new DefaultLink(PID, d1p1, d2p0, DIRECT),
82 new DefaultLink(PID, d2p1, d3p1, DIRECT),
83 createEdgeLink(d3p0, false)
84 );
85 private final int hops = links.size() - 1;
86 private PathIntent intent;
87
88 /**
89 * Configures objects used in all the test cases.
90 */
91 @Before
92 public void setUp() {
93 sut = new PathIntentCompiler();
94 coreService = createMock(CoreService.class);
95 expect(coreService.registerApplication("org.onosproject.net.intent"))
96 .andReturn(appId);
97 sut.coreService = coreService;
98
99 Intent.bindIdGenerator(idGenerator);
100
101 intent = PathIntent.builder()
102 .appId(APP_ID)
103 .selector(selector)
104 .treatment(treatment)
105 .path(new DefaultPath(pid, links, hops))
106 .build();
107 intentExtensionService = createMock(IntentExtensionService.class);
108 intentExtensionService.registerCompiler(PathIntent.class, sut);
109 intentExtensionService.unregisterCompiler(PathIntent.class);
110 sut.intentManager = intentExtensionService;
111
112 replay(coreService, intentExtensionService);
113 }
114
115 /**
116 * Tears down objects used in all the test cases.
117 */
118 @After
119 public void tearDown() {
120 Intent.unbindIdGenerator(idGenerator);
121 }
122
123 /**
124 * Tests the compilation behavior of the path intent compiler.
125 */
126 @Test
127 public void testCompile() {
128 sut.activate();
129
130 List<Intent> compiled = sut.compile(intent, Collections.emptyList(), Collections.emptySet());
131 assertThat(compiled, hasSize(1));
132
133 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
134
135 FlowRule rule1 = rules.stream()
136 .filter(x -> x.deviceId().equals(d1p0.deviceId()))
137 .findFirst()
138 .get();
139 assertThat(rule1.deviceId(), is(d1p0.deviceId()));
140 assertThat(rule1.selector(),
141 is(DefaultTrafficSelector.builder(selector).matchInPort(d1p0.port()).build()));
142 assertThat(rule1.treatment(),
143 is(DefaultTrafficTreatment.builder().setOutput(d1p1.port()).build()));
144
145 FlowRule rule2 = rules.stream()
146 .filter(x -> x.deviceId().equals(d2p0.deviceId()))
147 .findFirst()
148 .get();
149 assertThat(rule2.deviceId(), is(d2p0.deviceId()));
150 assertThat(rule2.selector(),
151 is(DefaultTrafficSelector.builder(selector).matchInPort(d2p0.port()).build()));
152 assertThat(rule2.treatment(),
153 is(DefaultTrafficTreatment.builder().setOutput(d2p1.port()).build()));
154
155 FlowRule rule3 = rules.stream()
156 .filter(x -> x.deviceId().equals(d3p0.deviceId()))
157 .findFirst()
158 .get();
159 assertThat(rule3.deviceId(), is(d3p1.deviceId()));
160 assertThat(rule3.selector(),
161 is(DefaultTrafficSelector.builder(selector).matchInPort(d3p1.port()).build()));
162 assertThat(rule3.treatment(),
163 is(DefaultTrafficTreatment.builder(treatment).setOutput(d3p0.port()).build()));
164
165 sut.deactivate();
166 }
167}