blob: 771a9883f89f9874c31d00c9ccd4c458112c6587 [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;
22import java.util.Optional;
23
24import org.junit.After;
25import org.junit.Before;
26import org.junit.Test;
27import org.onlab.packet.MplsLabel;
28import org.onosproject.TestApplicationId;
29import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
31import org.onosproject.core.IdGenerator;
32import org.onosproject.net.ConnectPoint;
33import org.onosproject.net.DefaultLink;
34import org.onosproject.net.DefaultPath;
35import org.onosproject.net.Link;
36import org.onosproject.net.flow.DefaultTrafficSelector;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
38import org.onosproject.net.flow.FlowRule;
39import org.onosproject.net.flow.TrafficSelector;
40import org.onosproject.net.flow.TrafficTreatment;
41import org.onosproject.net.intent.FlowRuleIntent;
42import org.onosproject.net.intent.Intent;
43import org.onosproject.net.intent.IntentExtensionService;
44import org.onosproject.net.intent.IntentTestsMocks;
45import org.onosproject.net.intent.MockIdGenerator;
46import org.onosproject.net.intent.MplsPathIntent;
Thomas Vachuskac97aa612015-06-23 16:00:18 -070047import org.onosproject.store.trivial.SimpleLinkStore;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080048
49import static org.easymock.EasyMock.createMock;
50import static org.easymock.EasyMock.expect;
51import static org.easymock.EasyMock.replay;
52import static org.hamcrest.MatcherAssert.assertThat;
53import static org.hamcrest.Matchers.hasSize;
54import static org.hamcrest.Matchers.is;
55import static org.onosproject.net.Link.Type.DIRECT;
56import static org.onosproject.net.NetTestTools.APP_ID;
57import static org.onosproject.net.NetTestTools.PID;
58import static org.onosproject.net.NetTestTools.connectPoint;
59
60public class MplsPathIntentCompilerTest {
61
62 private final ApplicationId appId = new TestApplicationId("test");
63
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);
68
69 private final TrafficSelector selector = DefaultTrafficSelector.builder().build();
70 private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
71
72 private final Optional<MplsLabel> ingressLabel =
73 Optional.of(MplsLabel.mplsLabel(10));
74 private final Optional<MplsLabel> egressLabel =
75 Optional.of(MplsLabel.mplsLabel(20));
76
77 private final List<Link> links = Arrays.asList(
78 new DefaultLink(PID, d1p1, d2p0, DIRECT),
79 new DefaultLink(PID, d2p1, d3p1, DIRECT)
80 );
81
82 private IdGenerator idGenerator = new MockIdGenerator();
83
84 private final int hops = links.size() - 1;
85 private MplsPathIntent intent;
86 private MplsPathIntentCompiler sut;
87
88 @Before
89 public void setUp() {
90 sut = new MplsPathIntentCompiler();
91 CoreService coreService = createMock(CoreService.class);
92 expect(coreService.registerApplication("org.onosproject.net.intent"))
93 .andReturn(appId);
94 sut.coreService = coreService;
95 sut.linkStore = new SimpleLinkStore();
96 sut.resourceService = new IntentTestsMocks.MockResourceService();
97
98 Intent.bindIdGenerator(idGenerator);
99
100 intent = MplsPathIntent.builder()
101 .appId(APP_ID)
102 .selector(selector)
103 .treatment(treatment)
104 .path(new DefaultPath(PID, links, hops))
105 .ingressLabel(ingressLabel)
106 .egressLabel(egressLabel)
107 .priority(55)
108 .build();
109
110 IntentExtensionService intentExtensionService = createMock(IntentExtensionService.class);
111 intentExtensionService.registerCompiler(MplsPathIntent.class, sut);
112 intentExtensionService.unregisterCompiler(MplsPathIntent.class);
113 sut.intentExtensionService = intentExtensionService;
114
115 replay(coreService, intentExtensionService);
116 }
117
118 @After
119 public void tearDown() {
120 Intent.unbindIdGenerator(idGenerator);
121 }
122
123 @Test
124 public void testCompile() {
125 sut.activate();
126
127 List<Intent> compiled = sut.compile(intent, Collections.emptyList(), Collections.emptySet());
128 assertThat(compiled, hasSize(1));
129
130 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
131 assertThat(rules, hasSize(1));
132
133 FlowRule rule = rules.stream()
134 .filter(x -> x.deviceId().equals(d2p0.deviceId()))
135 .findFirst()
136 .get();
137 assertThat(rule.deviceId(), is(d2p0.deviceId()));
138
139 sut.deactivate();
140
141 }
142}