blob: f314fe9b7ce25b5b293f037bfe427f73b7666526 [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 com.google.common.collect.ImmutableSet;
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22import org.onosproject.TestApplicationId;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.core.CoreService;
25import org.onosproject.core.IdGenerator;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DefaultLink;
28import org.onosproject.net.Link;
29import org.onosproject.net.flow.DefaultTrafficSelector;
30import org.onosproject.net.flow.DefaultTrafficTreatment;
31import org.onosproject.net.flow.FlowRule;
32import org.onosproject.net.flow.TrafficSelector;
33import org.onosproject.net.flow.TrafficTreatment;
34import org.onosproject.net.intent.FlowRuleIntent;
35import org.onosproject.net.intent.Intent;
36import org.onosproject.net.intent.IntentExtensionService;
37import org.onosproject.net.intent.LinkCollectionIntent;
38import org.onosproject.net.intent.MockIdGenerator;
39
40import java.util.Collection;
41import java.util.Collections;
42import java.util.List;
43import java.util.Set;
44
45import static org.easymock.EasyMock.createMock;
46import static org.easymock.EasyMock.expect;
47import static org.easymock.EasyMock.replay;
48import static org.hamcrest.MatcherAssert.assertThat;
49import static org.hamcrest.Matchers.hasSize;
50import static org.hamcrest.Matchers.is;
51import static org.onosproject.net.Link.Type.DIRECT;
52import static org.onosproject.net.NetTestTools.APP_ID;
53import static org.onosproject.net.NetTestTools.PID;
54import static org.onosproject.net.NetTestTools.connectPoint;
55
56public class LinkCollectionIntentCompilerTest {
57
58 private final ApplicationId appId = new TestApplicationId("test");
59
60 private final ConnectPoint d1p1 = connectPoint("s1", 0);
61 private final ConnectPoint d2p0 = connectPoint("s2", 0);
62 private final ConnectPoint d2p1 = connectPoint("s2", 1);
63 private final ConnectPoint d3p1 = connectPoint("s3", 1);
64 private final ConnectPoint d3p0 = connectPoint("s3", 10);
65 private final ConnectPoint d1p0 = connectPoint("s1", 10);
66
67 private final Set<Link> links = ImmutableSet.of(
68 new DefaultLink(PID, d1p1, d2p0, DIRECT),
69 new DefaultLink(PID, d2p1, d3p1, DIRECT),
70 new DefaultLink(PID, d1p1, d3p1, DIRECT));
71
72 private final TrafficSelector selector = DefaultTrafficSelector.builder().build();
73 private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
74
75 private CoreService coreService;
76 private IntentExtensionService intentExtensionService;
77 private IdGenerator idGenerator = new MockIdGenerator();
78
79 private LinkCollectionIntent intent;
80
81 private LinkCollectionIntentCompiler sut;
82
83 @Before
84 public void setUP() {
85 sut = new LinkCollectionIntentCompiler();
86 coreService = createMock(CoreService.class);
87 expect(coreService.registerApplication("org.onosproject.net.intent"))
88 .andReturn(appId);
89 sut.coreService = coreService;
90
91 Intent.bindIdGenerator(idGenerator);
92
93 intent = LinkCollectionIntent.builder()
94 .appId(APP_ID)
95 .selector(selector)
96 .treatment(treatment)
97 .links(links)
98 .ingressPoints(ImmutableSet.of(d1p1))
99 .egressPoints(ImmutableSet.of(d3p1))
100 .build();
101 intentExtensionService = createMock(IntentExtensionService.class);
102 intentExtensionService.registerCompiler(LinkCollectionIntent.class, sut);
103 intentExtensionService.unregisterCompiler(LinkCollectionIntent.class);
104 sut.intentManager = intentExtensionService;
105
106 replay(coreService, intentExtensionService);
107 }
108
109 @After
110 public void tearDown() {
111 Intent.unbindIdGenerator(idGenerator);
112 }
113
114 @Test
115 public void testCompile() {
116 sut.activate();
117
118 List<Intent> compiled = sut.compile(intent, Collections.emptyList(), Collections.emptySet());
119 assertThat(compiled, hasSize(1));
120
121 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
122 assertThat(rules, hasSize(links.size()));
123
124 // if not found, get() raises an exception
125 FlowRule rule1 = rules.stream()
126 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
127 .findFirst()
128 .get();
129 assertThat(rule1.selector(), is(
130 DefaultTrafficSelector.builder(intent.selector()).matchInPort(d1p1.port()).build()
131 ));
132 assertThat(rule1.treatment(), is(
133 DefaultTrafficTreatment.builder(intent.treatment()).setOutput(d1p1.port()).build()
134 ));
135
136 FlowRule rule2 = rules.stream()
137 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
138 .findFirst()
139 .get();
140 assertThat(rule2.selector(), is(
141 DefaultTrafficSelector.builder(intent.selector()).matchInPort(d2p0.port()).build()
142 ));
143 assertThat(rule2.treatment(), is(
144 DefaultTrafficTreatment.builder().setOutput(d2p1.port()).build()
145 ));
146
147 FlowRule rule3 = rules.stream()
148 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
149 .findFirst()
150 .get();
151 assertThat(rule3.selector(), is(
152 DefaultTrafficSelector.builder(intent.selector()).matchInPort(d3p1.port()).build()
153 ));
154 assertThat(rule3.treatment(), is(
155 DefaultTrafficTreatment.builder().setOutput(d3p1.port()).build()
156 ));
157
158 sut.deactivate();
159 }
160}