2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.net.intent.impl.compiler;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.onosproject.TestApplicationId;
22 import org.onosproject.core.ApplicationId;
23 import org.onosproject.core.CoreService;
24 import org.onosproject.core.IdGenerator;
25 import org.onosproject.net.ConnectPoint;
26 import org.onosproject.net.DefaultLink;
27 import org.onosproject.net.DefaultPath;
28 import org.onosproject.net.Link;
29 import org.onosproject.net.OchSignalType;
30 import org.onosproject.net.flow.DefaultTrafficSelector;
31 import org.onosproject.net.flow.DefaultTrafficTreatment;
32 import org.onosproject.net.flow.FlowRule;
33 import org.onosproject.net.flow.TrafficSelector;
34 import org.onosproject.net.flow.TrafficTreatment;
35 import org.onosproject.net.intent.FlowRuleIntent;
36 import org.onosproject.net.intent.Intent;
37 import org.onosproject.net.intent.IntentExtensionService;
38 import org.onosproject.net.intent.IntentTestsMocks;
39 import org.onosproject.net.intent.MockIdGenerator;
40 import org.onosproject.net.intent.OpticalPathIntent;
41 import org.onosproject.net.provider.ProviderId;
43 import java.util.Arrays;
44 import java.util.Collection;
45 import java.util.Collections;
46 import java.util.List;
48 import static org.easymock.EasyMock.createMock;
49 import static org.easymock.EasyMock.expect;
50 import static org.easymock.EasyMock.replay;
51 import static org.hamcrest.MatcherAssert.assertThat;
52 import static org.hamcrest.Matchers.hasSize;
53 import static org.junit.Assert.assertEquals;
54 import static org.onosproject.net.Link.Type.DIRECT;
55 import static org.onosproject.net.NetTestTools.PID;
56 import static org.onosproject.net.NetTestTools.connectPoint;
57 import static org.onosproject.net.NetTestTools.createLambda;
59 public class OpticalPathIntentCompilerTest {
61 private CoreService coreService;
62 private IntentExtensionService intentExtensionService;
63 private final IdGenerator idGenerator = new MockIdGenerator();
64 private OpticalPathIntentCompiler sut;
66 private final TrafficSelector selector = DefaultTrafficSelector.builder().build();
67 private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
68 private final ApplicationId appId = new TestApplicationId("test");
69 private final ProviderId pid = new ProviderId("of", "test");
70 private final ConnectPoint d1p1 = connectPoint("s1", 0);
71 private final ConnectPoint d2p0 = connectPoint("s2", 0);
72 private final ConnectPoint d2p1 = connectPoint("s2", 1);
73 private final ConnectPoint d3p1 = connectPoint("s3", 1);
74 private final ConnectPoint d3p0 = connectPoint("s3", 10);
75 private final ConnectPoint d1p0 = connectPoint("s1", 10);
77 private final List<Link> links = Arrays.asList(
78 new DefaultLink(PID, d1p1, d2p0, DIRECT),
79 new DefaultLink(PID, d2p1, d3p1, DIRECT)
81 private final int hops = links.size() + 1;
82 private OpticalPathIntent intent;
86 sut = new OpticalPathIntentCompiler();
87 coreService = createMock(CoreService.class);
88 expect(coreService.registerApplication("org.onosproject.net.intent"))
90 sut.coreService = coreService;
92 Intent.bindIdGenerator(idGenerator);
94 intent = OpticalPathIntent.builder()
98 .path(new DefaultPath(PID, links, hops))
99 .lambda(createLambda())
100 .signalType(OchSignalType.FIXED_GRID)
102 intentExtensionService = createMock(IntentExtensionService.class);
103 intentExtensionService.registerCompiler(OpticalPathIntent.class, sut);
104 intentExtensionService.unregisterCompiler(OpticalPathIntent.class);
105 sut.intentManager = intentExtensionService;
106 sut.resourceService = new IntentTestsMocks.MockResourceService();
108 replay(coreService, intentExtensionService);
112 public void tearDown() {
113 Intent.unbindIdGenerator(idGenerator);
117 public void testCompiler() {
120 List<Intent> compiled = sut.compile(intent, Collections.emptyList(), Collections.emptySet());
121 assertThat(compiled, hasSize(1));
123 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
125 .filter(x -> x.deviceId().equals(d1p1.deviceId()))
130 .filter(x -> x.deviceId().equals(d2p1.deviceId()))
135 .filter(x -> x.deviceId().equals(d3p1.deviceId()))
139 rules.forEach(rule -> assertEquals("FlowRule priority is incorrect",
140 intent.priority(), rule.priority()));
145 //TODO test bidirectional optical paths and verify rules