blob: 1317edcc2e03bda00261573714d2f6ab40c66d42 [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 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;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080044import org.onosproject.net.intent.MockIdGenerator;
45import org.onosproject.net.intent.MplsPathIntent;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080046
47import static org.easymock.EasyMock.createMock;
48import static org.easymock.EasyMock.expect;
49import static org.easymock.EasyMock.replay;
50import static org.hamcrest.MatcherAssert.assertThat;
51import static org.hamcrest.Matchers.hasSize;
52import static org.hamcrest.Matchers.is;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070053import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080054import static org.onosproject.net.Link.Type.DIRECT;
55import static org.onosproject.net.NetTestTools.APP_ID;
56import static org.onosproject.net.NetTestTools.PID;
57import static org.onosproject.net.NetTestTools.connectPoint;
58
59public class MplsPathIntentCompilerTest {
60
61 private final ApplicationId appId = new TestApplicationId("test");
62
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070063 private final ConnectPoint d1pi = connectPoint("s1", 100);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080064 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);
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070068 private final ConnectPoint d3pe = connectPoint("s3", 100);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080069
70 private final TrafficSelector selector = DefaultTrafficSelector.builder().build();
71 private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
72
73 private final Optional<MplsLabel> ingressLabel =
74 Optional.of(MplsLabel.mplsLabel(10));
75 private final Optional<MplsLabel> egressLabel =
76 Optional.of(MplsLabel.mplsLabel(20));
77
78 private final List<Link> links = Arrays.asList(
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070079 createEdgeLink(d1pi, true),
Ray Milkey2693bda2016-01-22 16:08:14 -080080 DefaultLink.builder().providerId(PID).src(d1p1).dst(d2p0).type(DIRECT).build(),
81 DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build(),
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070082 createEdgeLink(d3pe, false)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080083 );
84
85 private IdGenerator idGenerator = new MockIdGenerator();
86
87 private final int hops = links.size() - 1;
88 private MplsPathIntent intent;
89 private MplsPathIntentCompiler sut;
90
91 @Before
92 public void setUp() {
93 sut = new MplsPathIntentCompiler();
94 CoreService coreService = createMock(CoreService.class);
95 expect(coreService.registerApplication("org.onosproject.net.intent"))
96 .andReturn(appId);
97 sut.coreService = coreService;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070098 sut.resourceService = new MockResourceService();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080099
100 Intent.bindIdGenerator(idGenerator);
101
102 intent = MplsPathIntent.builder()
103 .appId(APP_ID)
104 .selector(selector)
105 .treatment(treatment)
106 .path(new DefaultPath(PID, links, hops))
107 .ingressLabel(ingressLabel)
108 .egressLabel(egressLabel)
109 .priority(55)
110 .build();
111
112 IntentExtensionService intentExtensionService = createMock(IntentExtensionService.class);
113 intentExtensionService.registerCompiler(MplsPathIntent.class, sut);
114 intentExtensionService.unregisterCompiler(MplsPathIntent.class);
115 sut.intentExtensionService = intentExtensionService;
116
117 replay(coreService, intentExtensionService);
118 }
119
120 @After
121 public void tearDown() {
122 Intent.unbindIdGenerator(idGenerator);
123 }
124
125 @Test
126 public void testCompile() {
127 sut.activate();
128
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800129 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800130 assertThat(compiled, hasSize(1));
131
132 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700133 assertThat(rules, hasSize(3));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800134
135 FlowRule rule = rules.stream()
136 .filter(x -> x.deviceId().equals(d2p0.deviceId()))
137 .findFirst()
138 .get();
139 assertThat(rule.deviceId(), is(d2p0.deviceId()));
140
141 sut.deactivate();
142
143 }
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700144
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800145}