blob: acac7df790265aac8772f9461bfe25da385d3248 [file] [log] [blame]
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08003 *
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 com.google.common.collect.ImmutableSet;
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22import org.onosproject.TestApplicationId;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080023import org.onosproject.cfg.ComponentConfigAdapter;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080024import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
26import org.onosproject.core.IdGenerator;
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.DefaultLink;
29import org.onosproject.net.Link;
30import org.onosproject.net.flow.DefaultTrafficSelector;
31import org.onosproject.net.flow.DefaultTrafficTreatment;
32import org.onosproject.net.flow.FlowRule;
33import org.onosproject.net.flow.TrafficSelector;
34import org.onosproject.net.flow.TrafficTreatment;
35import org.onosproject.net.intent.FlowRuleIntent;
36import org.onosproject.net.intent.Intent;
37import org.onosproject.net.intent.IntentExtensionService;
38import org.onosproject.net.intent.LinkCollectionIntent;
39import org.onosproject.net.intent.MockIdGenerator;
40
41import java.util.Collection;
42import java.util.Collections;
43import java.util.List;
44import java.util.Set;
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.Link.Type.DIRECT;
53import static org.onosproject.net.NetTestTools.APP_ID;
54import static org.onosproject.net.NetTestTools.PID;
55import static org.onosproject.net.NetTestTools.connectPoint;
56
57public class LinkCollectionIntentCompilerTest {
58
59 private final ApplicationId appId = new TestApplicationId("test");
60
61 private final ConnectPoint d1p1 = connectPoint("s1", 0);
62 private final ConnectPoint d2p0 = connectPoint("s2", 0);
63 private final ConnectPoint d2p1 = connectPoint("s2", 1);
64 private final ConnectPoint d3p1 = connectPoint("s3", 1);
65 private final ConnectPoint d3p0 = connectPoint("s3", 10);
66 private final ConnectPoint d1p0 = connectPoint("s1", 10);
67
68 private final Set<Link> links = ImmutableSet.of(
Ray Milkey2693bda2016-01-22 16:08:14 -080069 DefaultLink.builder().providerId(PID).src(d1p1).dst(d2p0).type(DIRECT).build(),
70 DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build(),
71 DefaultLink.builder().providerId(PID).src(d1p1).dst(d3p1).type(DIRECT).build());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080072
73 private final TrafficSelector selector = DefaultTrafficSelector.builder().build();
74 private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
75
76 private CoreService coreService;
77 private IntentExtensionService intentExtensionService;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080078 private IntentConfigurableRegistrator registrator;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080079 private IdGenerator idGenerator = new MockIdGenerator();
80
81 private LinkCollectionIntent intent;
82
83 private LinkCollectionIntentCompiler sut;
84
85 @Before
Brian O'Connor81134662015-06-25 17:23:33 -040086 public void setUp() {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080087 sut = new LinkCollectionIntentCompiler();
88 coreService = createMock(CoreService.class);
89 expect(coreService.registerApplication("org.onosproject.net.intent"))
90 .andReturn(appId);
91 sut.coreService = coreService;
92
93 Intent.bindIdGenerator(idGenerator);
94
95 intent = LinkCollectionIntent.builder()
96 .appId(APP_ID)
97 .selector(selector)
98 .treatment(treatment)
99 .links(links)
100 .ingressPoints(ImmutableSet.of(d1p1))
101 .egressPoints(ImmutableSet.of(d3p1))
102 .build();
103 intentExtensionService = createMock(IntentExtensionService.class);
104 intentExtensionService.registerCompiler(LinkCollectionIntent.class, sut);
105 intentExtensionService.unregisterCompiler(LinkCollectionIntent.class);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800106
107 registrator = new IntentConfigurableRegistrator();
108 registrator.extensionService = intentExtensionService;
109 registrator.cfgService = new ComponentConfigAdapter();
110 registrator.activate();
111
112 sut.registrator = registrator;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800113
114 replay(coreService, intentExtensionService);
115 }
116
117 @After
118 public void tearDown() {
119 Intent.unbindIdGenerator(idGenerator);
120 }
121
122 @Test
123 public void testCompile() {
124 sut.activate();
125
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800126 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800127 assertThat(compiled, hasSize(1));
128
129 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
130 assertThat(rules, hasSize(links.size()));
131
132 // if not found, get() raises an exception
133 FlowRule rule1 = rules.stream()
134 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
135 .findFirst()
136 .get();
137 assertThat(rule1.selector(), is(
138 DefaultTrafficSelector.builder(intent.selector()).matchInPort(d1p1.port()).build()
139 ));
140 assertThat(rule1.treatment(), is(
141 DefaultTrafficTreatment.builder(intent.treatment()).setOutput(d1p1.port()).build()
142 ));
Brian O'Connor81134662015-06-25 17:23:33 -0400143 assertThat(rule1.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800144
145 FlowRule rule2 = rules.stream()
146 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
147 .findFirst()
148 .get();
149 assertThat(rule2.selector(), is(
150 DefaultTrafficSelector.builder(intent.selector()).matchInPort(d2p0.port()).build()
151 ));
152 assertThat(rule2.treatment(), is(
153 DefaultTrafficTreatment.builder().setOutput(d2p1.port()).build()
154 ));
Brian O'Connor81134662015-06-25 17:23:33 -0400155 assertThat(rule2.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800156
157 FlowRule rule3 = rules.stream()
158 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
159 .findFirst()
160 .get();
161 assertThat(rule3.selector(), is(
162 DefaultTrafficSelector.builder(intent.selector()).matchInPort(d3p1.port()).build()
163 ));
164 assertThat(rule3.treatment(), is(
165 DefaultTrafficTreatment.builder().setOutput(d3p1.port()).build()
166 ));
Brian O'Connor81134662015-06-25 17:23:33 -0400167 assertThat(rule3.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800168
169 sut.deactivate();
170 }
171}