blob: 5bdb0ee06508556b661420dc9938a25d125245d5 [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
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080018import org.junit.Before;
19import org.junit.Test;
20import org.onlab.packet.MplsLabel;
21import org.onosproject.TestApplicationId;
22import org.onosproject.core.ApplicationId;
23import org.onosproject.core.CoreService;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.DefaultLink;
26import org.onosproject.net.DefaultPath;
27import org.onosproject.net.Link;
28import org.onosproject.net.flow.DefaultTrafficSelector;
29import org.onosproject.net.flow.DefaultTrafficTreatment;
30import org.onosproject.net.flow.FlowRule;
31import org.onosproject.net.flow.TrafficSelector;
32import org.onosproject.net.flow.TrafficTreatment;
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070033import org.onosproject.net.intent.AbstractIntentTest;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080034import org.onosproject.net.intent.FlowRuleIntent;
35import org.onosproject.net.intent.Intent;
36import org.onosproject.net.intent.IntentExtensionService;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080037import org.onosproject.net.intent.MplsPathIntent;
Yuta HIGUCHId95d5902016-06-27 00:18:45 -070038import org.onosproject.net.resource.MockResourceService;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080039
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070040import java.util.Arrays;
41import java.util.Collection;
42import java.util.Collections;
43import java.util.List;
44import java.util.Optional;
45
46import static org.easymock.EasyMock.*;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080047import static org.hamcrest.MatcherAssert.assertThat;
48import static org.hamcrest.Matchers.hasSize;
49import static org.hamcrest.Matchers.is;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070050import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080051import static org.onosproject.net.Link.Type.DIRECT;
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070052import static org.onosproject.net.NetTestTools.*;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080053
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070054public class MplsPathIntentCompilerTest extends AbstractIntentTest {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080055
56 private final ApplicationId appId = new TestApplicationId("test");
57
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070058 private final ConnectPoint d1pi = connectPoint("s1", 100);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080059 private final ConnectPoint d1p1 = connectPoint("s1", 0);
60 private final ConnectPoint d2p0 = connectPoint("s2", 0);
61 private final ConnectPoint d2p1 = connectPoint("s2", 1);
62 private final ConnectPoint d3p1 = connectPoint("s3", 1);
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070063 private final ConnectPoint d3pe = connectPoint("s3", 100);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080064
65 private final TrafficSelector selector = DefaultTrafficSelector.builder().build();
66 private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
67
68 private final Optional<MplsLabel> ingressLabel =
69 Optional.of(MplsLabel.mplsLabel(10));
70 private final Optional<MplsLabel> egressLabel =
71 Optional.of(MplsLabel.mplsLabel(20));
72
73 private final List<Link> links = Arrays.asList(
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070074 createEdgeLink(d1pi, true),
Ray Milkey2693bda2016-01-22 16:08:14 -080075 DefaultLink.builder().providerId(PID).src(d1p1).dst(d2p0).type(DIRECT).build(),
76 DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build(),
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070077 createEdgeLink(d3pe, false)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080078 );
79
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080080 private final int hops = links.size() - 1;
81 private MplsPathIntent intent;
82 private MplsPathIntentCompiler sut;
83
84 @Before
85 public void setUp() {
86 sut = new MplsPathIntentCompiler();
87 CoreService coreService = createMock(CoreService.class);
88 expect(coreService.registerApplication("org.onosproject.net.intent"))
89 .andReturn(appId);
90 sut.coreService = coreService;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070091 sut.resourceService = new MockResourceService();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080092
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070093 super.setUp();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080094
95 intent = MplsPathIntent.builder()
96 .appId(APP_ID)
97 .selector(selector)
98 .treatment(treatment)
99 .path(new DefaultPath(PID, links, hops))
100 .ingressLabel(ingressLabel)
101 .egressLabel(egressLabel)
102 .priority(55)
103 .build();
104
105 IntentExtensionService intentExtensionService = createMock(IntentExtensionService.class);
106 intentExtensionService.registerCompiler(MplsPathIntent.class, sut);
107 intentExtensionService.unregisterCompiler(MplsPathIntent.class);
108 sut.intentExtensionService = intentExtensionService;
109
110 replay(coreService, intentExtensionService);
111 }
112
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800113 @Test
114 public void testCompile() {
115 sut.activate();
116
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800117 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800118 assertThat(compiled, hasSize(1));
119
120 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700121 assertThat(rules, hasSize(3));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800122
123 FlowRule rule = rules.stream()
124 .filter(x -> x.deviceId().equals(d2p0.deviceId()))
125 .findFirst()
126 .get();
127 assertThat(rule.deviceId(), is(d2p0.deviceId()));
128
129 sut.deactivate();
130
131 }
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700132
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800133}