2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.net.intent.impl.compiler;
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Optional;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.onlab.packet.MplsLabel;
28 import org.onosproject.TestApplicationId;
29 import org.onosproject.core.ApplicationId;
30 import org.onosproject.core.CoreService;
31 import org.onosproject.core.IdGenerator;
32 import org.onosproject.net.ConnectPoint;
33 import org.onosproject.net.DefaultLink;
34 import org.onosproject.net.DefaultPath;
35 import org.onosproject.net.Link;
36 import org.onosproject.net.flow.DefaultTrafficSelector;
37 import org.onosproject.net.flow.DefaultTrafficTreatment;
38 import org.onosproject.net.flow.FlowRule;
39 import org.onosproject.net.flow.TrafficSelector;
40 import org.onosproject.net.flow.TrafficTreatment;
41 import org.onosproject.net.intent.FlowRuleIntent;
42 import org.onosproject.net.intent.Intent;
43 import org.onosproject.net.intent.IntentExtensionService;
44 import org.onosproject.net.intent.IntentTestsMocks;
45 import org.onosproject.net.intent.MockIdGenerator;
46 import org.onosproject.net.intent.MplsPathIntent;
47 import org.onosproject.store.trivial.SimpleLinkStore;
49 import static org.easymock.EasyMock.createMock;
50 import static org.easymock.EasyMock.expect;
51 import static org.easymock.EasyMock.replay;
52 import static org.hamcrest.MatcherAssert.assertThat;
53 import static org.hamcrest.Matchers.hasSize;
54 import static org.hamcrest.Matchers.is;
55 import static org.onosproject.net.Link.Type.DIRECT;
56 import static org.onosproject.net.NetTestTools.APP_ID;
57 import static org.onosproject.net.NetTestTools.PID;
58 import static org.onosproject.net.NetTestTools.connectPoint;
60 public class MplsPathIntentCompilerTest {
62 private final ApplicationId appId = new TestApplicationId("test");
64 private final ConnectPoint d1p1 = connectPoint("s1", 0);
65 private final ConnectPoint d2p0 = connectPoint("s2", 0);
66 private final ConnectPoint d2p1 = connectPoint("s2", 1);
67 private final ConnectPoint d3p1 = connectPoint("s3", 1);
69 private final TrafficSelector selector = DefaultTrafficSelector.builder().build();
70 private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
72 private final Optional<MplsLabel> ingressLabel =
73 Optional.of(MplsLabel.mplsLabel(10));
74 private final Optional<MplsLabel> egressLabel =
75 Optional.of(MplsLabel.mplsLabel(20));
77 private final List<Link> links = Arrays.asList(
78 new DefaultLink(PID, d1p1, d2p0, DIRECT),
79 new DefaultLink(PID, d2p1, d3p1, DIRECT)
82 private IdGenerator idGenerator = new MockIdGenerator();
84 private final int hops = links.size() - 1;
85 private MplsPathIntent intent;
86 private MplsPathIntentCompiler sut;
90 sut = new MplsPathIntentCompiler();
91 CoreService coreService = createMock(CoreService.class);
92 expect(coreService.registerApplication("org.onosproject.net.intent"))
94 sut.coreService = coreService;
95 sut.linkStore = new SimpleLinkStore();
96 sut.resourceService = new IntentTestsMocks.MockResourceService();
98 Intent.bindIdGenerator(idGenerator);
100 intent = MplsPathIntent.builder()
103 .treatment(treatment)
104 .path(new DefaultPath(PID, links, hops))
105 .ingressLabel(ingressLabel)
106 .egressLabel(egressLabel)
110 IntentExtensionService intentExtensionService = createMock(IntentExtensionService.class);
111 intentExtensionService.registerCompiler(MplsPathIntent.class, sut);
112 intentExtensionService.unregisterCompiler(MplsPathIntent.class);
113 sut.intentExtensionService = intentExtensionService;
115 replay(coreService, intentExtensionService);
119 public void tearDown() {
120 Intent.unbindIdGenerator(idGenerator);
124 public void testCompile() {
127 List<Intent> compiled = sut.compile(intent, Collections.emptyList(), Collections.emptySet());
128 assertThat(compiled, hasSize(1));
130 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
131 assertThat(rules, hasSize(1));
133 FlowRule rule = rules.stream()
134 .filter(x -> x.deviceId().equals(d2p0.deviceId()))
137 assertThat(rule.deviceId(), is(d2p0.deviceId()));