blob: a18f2c5cd27dec80bb4ad7f30e694666e759147b [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 org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
21import org.onosproject.TestApplicationId;
22import org.onosproject.core.ApplicationId;
23import org.onosproject.core.CoreService;
24import org.onosproject.core.IdGenerator;
25import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.DefaultLink;
27import org.onosproject.net.DefaultPath;
28import org.onosproject.net.Link;
29import org.onosproject.net.flow.DefaultTrafficSelector;
30import org.onosproject.net.flow.DefaultTrafficTreatment;
31import org.onosproject.net.flow.FlowRule;
32import org.onosproject.net.flow.TrafficSelector;
33import org.onosproject.net.flow.TrafficTreatment;
34import org.onosproject.net.intent.FlowRuleIntent;
35import org.onosproject.net.intent.Intent;
36import org.onosproject.net.intent.IntentExtensionService;
37import org.onosproject.net.intent.IntentTestsMocks;
38import org.onosproject.net.intent.MockIdGenerator;
39import org.onosproject.net.intent.OpticalPathIntent;
40import org.onosproject.net.provider.ProviderId;
41
42import java.util.Arrays;
43import java.util.Collection;
44import java.util.Collections;
45import java.util.List;
46
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.onosproject.net.Link.Type.DIRECT;
53import static org.onosproject.net.NetTestTools.PID;
54import static org.onosproject.net.NetTestTools.connectPoint;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070055import static org.onosproject.net.NetTestTools.createLambda;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080056
57public class OpticalPathIntentCompilerTest {
58
59 private CoreService coreService;
60 private IntentExtensionService intentExtensionService;
61 private final IdGenerator idGenerator = new MockIdGenerator();
62 private OpticalPathIntentCompiler sut;
63
64 private final TrafficSelector selector = DefaultTrafficSelector.builder().build();
65 private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
66 private final ApplicationId appId = new TestApplicationId("test");
67 private final ProviderId pid = new ProviderId("of", "test");
68 private final ConnectPoint d1p1 = connectPoint("s1", 0);
69 private final ConnectPoint d2p0 = connectPoint("s2", 0);
70 private final ConnectPoint d2p1 = connectPoint("s2", 1);
71 private final ConnectPoint d3p1 = connectPoint("s3", 1);
72 private final ConnectPoint d3p0 = connectPoint("s3", 10);
73 private final ConnectPoint d1p0 = connectPoint("s1", 10);
74
75 private final List<Link> links = Arrays.asList(
76 new DefaultLink(PID, d1p1, d2p0, DIRECT),
77 new DefaultLink(PID, d2p1, d3p1, DIRECT)
78 );
79 private final int hops = links.size() + 1;
80 private OpticalPathIntent intent;
81
82 @Before
83 public void setUp() {
84 sut = new OpticalPathIntentCompiler();
85 coreService = createMock(CoreService.class);
86 expect(coreService.registerApplication("org.onosproject.net.intent"))
87 .andReturn(appId);
88 sut.coreService = coreService;
89
90 Intent.bindIdGenerator(idGenerator);
91
92 intent = OpticalPathIntent.builder()
93 .appId(appId)
94 .src(d1p1)
95 .dst(d3p1)
96 .path(new DefaultPath(PID, links, hops))
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070097 .lambda(createLambda())
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080098 .build();
99 intentExtensionService = createMock(IntentExtensionService.class);
100 intentExtensionService.registerCompiler(OpticalPathIntent.class, sut);
101 intentExtensionService.unregisterCompiler(OpticalPathIntent.class);
102 sut.intentManager = intentExtensionService;
103 sut.resourceService = new IntentTestsMocks.MockResourceService();
104
105 replay(coreService, intentExtensionService);
106 }
107
108 @After
109 public void tearDown() {
110 Intent.unbindIdGenerator(idGenerator);
111 }
112
113 @Test
114 public void testCompiler() {
115 sut.activate();
116
117 List<Intent> compiled = sut.compile(intent, Collections.emptyList(), Collections.emptySet());
118 assertThat(compiled, hasSize(1));
119
120 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
121 rules.stream()
122 .filter(x -> x.deviceId().equals(d1p1.deviceId()))
123 .findFirst()
124 .get();
125
126 rules.stream()
127 .filter(x -> x.deviceId().equals(d2p1.deviceId()))
128 .findFirst()
129 .get();
130
131 rules.stream()
132 .filter(x -> x.deviceId().equals(d3p1.deviceId()))
133 .findFirst()
134 .get();
135
136 sut.deactivate();
137 }
138
139}