blob: 2ddca7dbce20f620720f06a1cc9410ba62fd9fc2 [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;
Yuta HIGUCHId95d5902016-06-27 00:18:45 -070046import org.onosproject.net.resource.MockResourceService;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080047
48import static org.easymock.EasyMock.createMock;
49import static org.easymock.EasyMock.expect;
50import static org.easymock.EasyMock.replay;
51import static org.hamcrest.MatcherAssert.assertThat;
52import static org.hamcrest.Matchers.hasSize;
53import static org.hamcrest.Matchers.is;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070054import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080055import 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
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070064 private final ConnectPoint d1pi = connectPoint("s1", 100);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080065 private final ConnectPoint d1p1 = connectPoint("s1", 0);
66 private final ConnectPoint d2p0 = connectPoint("s2", 0);
67 private final ConnectPoint d2p1 = connectPoint("s2", 1);
68 private final ConnectPoint d3p1 = connectPoint("s3", 1);
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070069 private final ConnectPoint d3pe = connectPoint("s3", 100);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080070
71 private final TrafficSelector selector = DefaultTrafficSelector.builder().build();
72 private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
73
74 private final Optional<MplsLabel> ingressLabel =
75 Optional.of(MplsLabel.mplsLabel(10));
76 private final Optional<MplsLabel> egressLabel =
77 Optional.of(MplsLabel.mplsLabel(20));
78
79 private final List<Link> links = Arrays.asList(
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070080 createEdgeLink(d1pi, true),
Ray Milkey2693bda2016-01-22 16:08:14 -080081 DefaultLink.builder().providerId(PID).src(d1p1).dst(d2p0).type(DIRECT).build(),
82 DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build(),
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070083 createEdgeLink(d3pe, false)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080084 );
85
86 private IdGenerator idGenerator = new MockIdGenerator();
87
88 private final int hops = links.size() - 1;
89 private MplsPathIntent intent;
90 private MplsPathIntentCompiler sut;
91
92 @Before
93 public void setUp() {
94 sut = new MplsPathIntentCompiler();
95 CoreService coreService = createMock(CoreService.class);
96 expect(coreService.registerApplication("org.onosproject.net.intent"))
97 .andReturn(appId);
98 sut.coreService = coreService;
Sho SHIMIZUf26e6922015-10-29 16:19:20 -070099 sut.resourceService = new MockResourceService();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800100
Thomas Vachuska23235962017-02-03 11:44:15 -0800101 Intent.unbindIdGenerator(idGenerator);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800102 Intent.bindIdGenerator(idGenerator);
103
104 intent = MplsPathIntent.builder()
105 .appId(APP_ID)
106 .selector(selector)
107 .treatment(treatment)
108 .path(new DefaultPath(PID, links, hops))
109 .ingressLabel(ingressLabel)
110 .egressLabel(egressLabel)
111 .priority(55)
112 .build();
113
114 IntentExtensionService intentExtensionService = createMock(IntentExtensionService.class);
115 intentExtensionService.registerCompiler(MplsPathIntent.class, sut);
116 intentExtensionService.unregisterCompiler(MplsPathIntent.class);
117 sut.intentExtensionService = intentExtensionService;
118
119 replay(coreService, intentExtensionService);
120 }
121
122 @After
123 public void tearDown() {
124 Intent.unbindIdGenerator(idGenerator);
125 }
126
127 @Test
128 public void testCompile() {
129 sut.activate();
130
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800131 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800132 assertThat(compiled, hasSize(1));
133
134 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700135 assertThat(rules, hasSize(3));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800136
137 FlowRule rule = rules.stream()
138 .filter(x -> x.deviceId().equals(d2p0.deviceId()))
139 .findFirst()
140 .get();
141 assertThat(rule.deviceId(), is(d2p0.deviceId()));
142
143 sut.deactivate();
144
145 }
Sho SHIMIZUf26e6922015-10-29 16:19:20 -0700146
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800147}