blob: 072592d16e2a19046b37750a65a337de7e6e1b48 [file] [log] [blame]
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-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 */
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;
20import org.onosproject.TestApplicationId;
21import org.onosproject.core.ApplicationId;
22import org.onosproject.core.CoreService;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080023import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.DefaultLink;
25import org.onosproject.net.DefaultPath;
26import org.onosproject.net.Link;
Marc De Leenheerd24420f2015-05-27 09:40:59 -070027import org.onosproject.net.OchSignalType;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080028import org.onosproject.net.flow.FlowRule;
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070029import org.onosproject.net.intent.AbstractIntentTest;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080030import org.onosproject.net.intent.FlowRuleIntent;
31import org.onosproject.net.intent.Intent;
32import org.onosproject.net.intent.IntentExtensionService;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080033import org.onosproject.net.intent.OpticalPathIntent;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080034
35import java.util.Arrays;
36import java.util.Collection;
37import java.util.Collections;
38import java.util.List;
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -070039import java.util.stream.Collectors;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080040
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070041import static org.easymock.EasyMock.*;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080042import static org.hamcrest.MatcherAssert.assertThat;
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070043import static org.hamcrest.Matchers.*;
Brian O'Connor81134662015-06-25 17:23:33 -040044import static org.junit.Assert.assertEquals;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080045import static org.onosproject.net.Link.Type.DIRECT;
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070046import static org.onosproject.net.NetTestTools.*;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080047
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070048public class OpticalPathIntentCompilerTest extends AbstractIntentTest {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080049
50 private CoreService coreService;
51 private IntentExtensionService intentExtensionService;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080052 private OpticalPathIntentCompiler sut;
53
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080054 private final ApplicationId appId = new TestApplicationId("test");
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080055 private final ConnectPoint d1p1 = connectPoint("s1", 0);
56 private final ConnectPoint d2p0 = connectPoint("s2", 0);
57 private final ConnectPoint d2p1 = connectPoint("s2", 1);
58 private final ConnectPoint d3p1 = connectPoint("s3", 1);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080059
60 private final List<Link> links = Arrays.asList(
Ray Milkey2693bda2016-01-22 16:08:14 -080061 DefaultLink.builder().providerId(PID).src(d1p1).dst(d2p0).type(DIRECT).build(),
62 DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build()
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080063 );
64 private final int hops = links.size() + 1;
65 private OpticalPathIntent intent;
66
67 @Before
68 public void setUp() {
69 sut = new OpticalPathIntentCompiler();
70 coreService = createMock(CoreService.class);
71 expect(coreService.registerApplication("org.onosproject.net.intent"))
72 .andReturn(appId);
73 sut.coreService = coreService;
74
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070075 super.setUp();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080076
77 intent = OpticalPathIntent.builder()
78 .appId(appId)
79 .src(d1p1)
80 .dst(d3p1)
81 .path(new DefaultPath(PID, links, hops))
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070082 .lambda(createLambda())
Marc De Leenheerd24420f2015-05-27 09:40:59 -070083 .signalType(OchSignalType.FIXED_GRID)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080084 .build();
85 intentExtensionService = createMock(IntentExtensionService.class);
86 intentExtensionService.registerCompiler(OpticalPathIntent.class, sut);
87 intentExtensionService.unregisterCompiler(OpticalPathIntent.class);
88 sut.intentManager = intentExtensionService;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080089
90 replay(coreService, intentExtensionService);
91 }
92
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080093 @Test
94 public void testCompiler() {
95 sut.activate();
96
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080097 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080098 assertThat(compiled, hasSize(1));
99
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700100 assertThat("key is inherited",
101 compiled.stream().map(Intent::key).collect(Collectors.toList()),
102 everyItem(is(intent.key())));
103
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800104 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
105 rules.stream()
106 .filter(x -> x.deviceId().equals(d1p1.deviceId()))
107 .findFirst()
108 .get();
109
110 rules.stream()
111 .filter(x -> x.deviceId().equals(d2p1.deviceId()))
112 .findFirst()
113 .get();
114
115 rules.stream()
116 .filter(x -> x.deviceId().equals(d3p1.deviceId()))
117 .findFirst()
118 .get();
119
Brian O'Connor81134662015-06-25 17:23:33 -0400120 rules.forEach(rule -> assertEquals("FlowRule priority is incorrect",
121 intent.priority(), rule.priority()));
122
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800123 sut.deactivate();
124 }
125
Brian O'Connor81134662015-06-25 17:23:33 -0400126 //TODO test bidirectional optical paths and verify rules
127
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800128}