771a9883f89f9874c31d00c9ccd4c458112c6587
[onosfw.git] /
1 /*
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  */
16 package org.onosproject.net.intent.impl.compiler;
17
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;
23
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;
48
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;
59
60 public 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 }