blob: f07bf42c9848b2391542cc561a7556691026f3f5 [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);
Brian O'Connor81134662015-06-25 17:23:33 -040078 private static final int PRIORITY = 555;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080079
80 private final List<Link> links = Arrays.asList(
81 createEdgeLink(d1p0, true),
82 new DefaultLink(PID, d1p1, d2p0, DIRECT),
83 new DefaultLink(PID, d2p1, d3p1, DIRECT),
84 createEdgeLink(d3p0, false)
85 );
86 private final int hops = links.size() - 1;
87 private PathIntent intent;
88
89 /**
90 * Configures objects used in all the test cases.
91 */
92 @Before
93 public void setUp() {
94 sut = new PathIntentCompiler();
95 coreService = createMock(CoreService.class);
96 expect(coreService.registerApplication("org.onosproject.net.intent"))
97 .andReturn(appId);
98 sut.coreService = coreService;
99
100 Intent.bindIdGenerator(idGenerator);
101
102 intent = PathIntent.builder()
103 .appId(APP_ID)
104 .selector(selector)
105 .treatment(treatment)
Brian O'Connor81134662015-06-25 17:23:33 -0400106 .priority(PRIORITY)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800107 .path(new DefaultPath(pid, links, hops))
108 .build();
109 intentExtensionService = createMock(IntentExtensionService.class);
110 intentExtensionService.registerCompiler(PathIntent.class, sut);
111 intentExtensionService.unregisterCompiler(PathIntent.class);
112 sut.intentManager = intentExtensionService;
113
114 replay(coreService, intentExtensionService);
115 }
116
117 /**
118 * Tears down objects used in all the test cases.
119 */
120 @After
121 public void tearDown() {
122 Intent.unbindIdGenerator(idGenerator);
123 }
124
125 /**
126 * Tests the compilation behavior of the path intent compiler.
127 */
128 @Test
129 public void testCompile() {
130 sut.activate();
131
132 List<Intent> compiled = sut.compile(intent, Collections.emptyList(), Collections.emptySet());
133 assertThat(compiled, hasSize(1));
134
135 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
136
137 FlowRule rule1 = rules.stream()
138 .filter(x -> x.deviceId().equals(d1p0.deviceId()))
139 .findFirst()
140 .get();
141 assertThat(rule1.deviceId(), is(d1p0.deviceId()));
142 assertThat(rule1.selector(),
143 is(DefaultTrafficSelector.builder(selector).matchInPort(d1p0.port()).build()));
144 assertThat(rule1.treatment(),
145 is(DefaultTrafficTreatment.builder().setOutput(d1p1.port()).build()));
Brian O'Connor81134662015-06-25 17:23:33 -0400146 assertThat(rule1.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800147
148 FlowRule rule2 = rules.stream()
149 .filter(x -> x.deviceId().equals(d2p0.deviceId()))
150 .findFirst()
151 .get();
152 assertThat(rule2.deviceId(), is(d2p0.deviceId()));
153 assertThat(rule2.selector(),
154 is(DefaultTrafficSelector.builder(selector).matchInPort(d2p0.port()).build()));
155 assertThat(rule2.treatment(),
156 is(DefaultTrafficTreatment.builder().setOutput(d2p1.port()).build()));
Brian O'Connor81134662015-06-25 17:23:33 -0400157 assertThat(rule2.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800158
159 FlowRule rule3 = rules.stream()
160 .filter(x -> x.deviceId().equals(d3p0.deviceId()))
161 .findFirst()
162 .get();
163 assertThat(rule3.deviceId(), is(d3p1.deviceId()));
164 assertThat(rule3.selector(),
165 is(DefaultTrafficSelector.builder(selector).matchInPort(d3p1.port()).build()));
166 assertThat(rule3.treatment(),
167 is(DefaultTrafficTreatment.builder(treatment).setOutput(d3p0.port()).build()));
Brian O'Connor81134662015-06-25 17:23:33 -0400168 assertThat(rule3.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800169
170 sut.deactivate();
171 }
172}