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