blob: 43c279ded40b6374173d2a114b1afb653080d024 [file] [log] [blame]
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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 */
Yuta HIGUCHId95d5902016-06-27 00:18:45 -070016package org.onosproject.net.optical.intent.impl.compiler;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080017
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080018import org.junit.Before;
19import org.junit.Test;
Ray Milkeya7cf8c82018-02-08 15:07:06 -080020import org.onlab.graph.ScalarWeight;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080021import org.onosproject.TestApplicationId;
22import org.onosproject.core.ApplicationId;
23import org.onosproject.core.CoreService;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.DefaultLink;
26import org.onosproject.net.DefaultPath;
27import org.onosproject.net.Link;
Marc De Leenheerd24420f2015-05-27 09:40:59 -070028import org.onosproject.net.OchSignalType;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080029import org.onosproject.net.flow.FlowRule;
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070030import org.onosproject.net.intent.AbstractIntentTest;
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.OpticalPathIntent;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080035
36import java.util.Arrays;
37import java.util.Collection;
38import java.util.Collections;
39import java.util.List;
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -070040import java.util.stream.Collectors;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080041
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070042import static org.easymock.EasyMock.*;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080043import static org.hamcrest.MatcherAssert.assertThat;
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070044import static org.hamcrest.Matchers.*;
Brian O'Connor81134662015-06-25 17:23:33 -040045import static org.junit.Assert.assertEquals;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080046import static org.onosproject.net.Link.Type.DIRECT;
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070047import static org.onosproject.net.NetTestTools.*;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080048
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070049public class OpticalPathIntentCompilerTest extends AbstractIntentTest {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080050
51 private CoreService coreService;
52 private IntentExtensionService intentExtensionService;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080053 private OpticalPathIntentCompiler sut;
54
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080055 private final ApplicationId appId = new TestApplicationId("test");
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080056 private final ConnectPoint d1p1 = connectPoint("s1", 0);
57 private final ConnectPoint d2p0 = connectPoint("s2", 0);
58 private final ConnectPoint d2p1 = connectPoint("s2", 1);
59 private final ConnectPoint d3p1 = connectPoint("s3", 1);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080060
61 private final List<Link> links = Arrays.asList(
Ray Milkey2693bda2016-01-22 16:08:14 -080062 DefaultLink.builder().providerId(PID).src(d1p1).dst(d2p0).type(DIRECT).build(),
63 DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build()
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080064 );
65 private final int hops = links.size() + 1;
66 private OpticalPathIntent intent;
67
68 @Before
69 public void setUp() {
70 sut = new OpticalPathIntentCompiler();
71 coreService = createMock(CoreService.class);
72 expect(coreService.registerApplication("org.onosproject.net.intent"))
73 .andReturn(appId);
74 sut.coreService = coreService;
75
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070076 super.setUp();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080077
78 intent = OpticalPathIntent.builder()
79 .appId(appId)
80 .src(d1p1)
81 .dst(d3p1)
Ray Milkeya7cf8c82018-02-08 15:07:06 -080082 .path(new DefaultPath(PID, links, ScalarWeight.toWeight(hops)))
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070083 .lambda(createLambda())
Marc De Leenheerd24420f2015-05-27 09:40:59 -070084 .signalType(OchSignalType.FIXED_GRID)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080085 .build();
86 intentExtensionService = createMock(IntentExtensionService.class);
87 intentExtensionService.registerCompiler(OpticalPathIntent.class, sut);
88 intentExtensionService.unregisterCompiler(OpticalPathIntent.class);
89 sut.intentManager = intentExtensionService;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080090
91 replay(coreService, intentExtensionService);
92 }
93
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080094 @Test
95 public void testCompiler() {
96 sut.activate();
97
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080098 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080099 assertThat(compiled, hasSize(1));
100
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700101 assertThat("key is inherited",
102 compiled.stream().map(Intent::key).collect(Collectors.toList()),
103 everyItem(is(intent.key())));
104
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800105 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
106 rules.stream()
107 .filter(x -> x.deviceId().equals(d1p1.deviceId()))
108 .findFirst()
109 .get();
110
111 rules.stream()
112 .filter(x -> x.deviceId().equals(d2p1.deviceId()))
113 .findFirst()
114 .get();
115
116 rules.stream()
117 .filter(x -> x.deviceId().equals(d3p1.deviceId()))
118 .findFirst()
119 .get();
120
Brian O'Connor81134662015-06-25 17:23:33 -0400121 rules.forEach(rule -> assertEquals("FlowRule priority is incorrect",
122 intent.priority(), rule.priority()));
123
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800124 sut.deactivate();
125 }
126
Brian O'Connor81134662015-06-25 17:23:33 -0400127 //TODO test bidirectional optical paths and verify rules
128
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800129}