blob: d2a388fdfd79b85c799a31e94159e7b25d909f98 [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 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.FlowRule;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080031import org.onosproject.net.intent.FlowRuleIntent;
32import org.onosproject.net.intent.Intent;
33import org.onosproject.net.intent.IntentExtensionService;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080034import org.onosproject.net.intent.MockIdGenerator;
35import org.onosproject.net.intent.OpticalPathIntent;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080036
37import java.util.Arrays;
38import java.util.Collection;
39import java.util.Collections;
40import java.util.List;
41
42import static org.easymock.EasyMock.createMock;
43import static org.easymock.EasyMock.expect;
44import static org.easymock.EasyMock.replay;
45import static org.hamcrest.MatcherAssert.assertThat;
46import static org.hamcrest.Matchers.hasSize;
Brian O'Connor81134662015-06-25 17:23:33 -040047import static org.junit.Assert.assertEquals;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080048import static org.onosproject.net.Link.Type.DIRECT;
49import static org.onosproject.net.NetTestTools.PID;
50import static org.onosproject.net.NetTestTools.connectPoint;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070051import static org.onosproject.net.NetTestTools.createLambda;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080052
53public class OpticalPathIntentCompilerTest {
54
55 private CoreService coreService;
56 private IntentExtensionService intentExtensionService;
57 private final IdGenerator idGenerator = new MockIdGenerator();
58 private OpticalPathIntentCompiler sut;
59
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080060 private final ApplicationId appId = new TestApplicationId("test");
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080061 private final ConnectPoint d1p1 = connectPoint("s1", 0);
62 private final ConnectPoint d2p0 = connectPoint("s2", 0);
63 private final ConnectPoint d2p1 = connectPoint("s2", 1);
64 private final ConnectPoint d3p1 = connectPoint("s3", 1);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080065
66 private final List<Link> links = Arrays.asList(
Ray Milkey2693bda2016-01-22 16:08:14 -080067 DefaultLink.builder().providerId(PID).src(d1p1).dst(d2p0).type(DIRECT).build(),
68 DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build()
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080069 );
70 private final int hops = links.size() + 1;
71 private OpticalPathIntent intent;
72
73 @Before
74 public void setUp() {
75 sut = new OpticalPathIntentCompiler();
76 coreService = createMock(CoreService.class);
77 expect(coreService.registerApplication("org.onosproject.net.intent"))
78 .andReturn(appId);
79 sut.coreService = coreService;
80
81 Intent.bindIdGenerator(idGenerator);
82
83 intent = OpticalPathIntent.builder()
84 .appId(appId)
85 .src(d1p1)
86 .dst(d3p1)
87 .path(new DefaultPath(PID, links, hops))
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070088 .lambda(createLambda())
Marc De Leenheerd24420f2015-05-27 09:40:59 -070089 .signalType(OchSignalType.FIXED_GRID)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080090 .build();
91 intentExtensionService = createMock(IntentExtensionService.class);
92 intentExtensionService.registerCompiler(OpticalPathIntent.class, sut);
93 intentExtensionService.unregisterCompiler(OpticalPathIntent.class);
94 sut.intentManager = intentExtensionService;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080095
96 replay(coreService, intentExtensionService);
97 }
98
99 @After
100 public void tearDown() {
101 Intent.unbindIdGenerator(idGenerator);
102 }
103
104 @Test
105 public void testCompiler() {
106 sut.activate();
107
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800108 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800109 assertThat(compiled, hasSize(1));
110
111 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
112 rules.stream()
113 .filter(x -> x.deviceId().equals(d1p1.deviceId()))
114 .findFirst()
115 .get();
116
117 rules.stream()
118 .filter(x -> x.deviceId().equals(d2p1.deviceId()))
119 .findFirst()
120 .get();
121
122 rules.stream()
123 .filter(x -> x.deviceId().equals(d3p1.deviceId()))
124 .findFirst()
125 .get();
126
Brian O'Connor81134662015-06-25 17:23:33 -0400127 rules.forEach(rule -> assertEquals("FlowRule priority is incorrect",
128 intent.priority(), rule.priority()));
129
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800130 sut.deactivate();
131 }
132
Brian O'Connor81134662015-06-25 17:23:33 -0400133 //TODO test bidirectional optical paths and verify rules
134
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800135}