blob: 0c2d25e099104277f73341aab30d227827b9b4fc [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
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;
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -070041import java.util.stream.Collectors;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080042
43import static org.easymock.EasyMock.createMock;
44import static org.easymock.EasyMock.expect;
45import static org.easymock.EasyMock.replay;
46import static org.hamcrest.MatcherAssert.assertThat;
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -070047import static org.hamcrest.Matchers.everyItem;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080048import static org.hamcrest.Matchers.hasSize;
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -070049import static org.hamcrest.Matchers.is;
Brian O'Connor81134662015-06-25 17:23:33 -040050import static org.junit.Assert.assertEquals;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080051import static org.onosproject.net.Link.Type.DIRECT;
52import static org.onosproject.net.NetTestTools.PID;
53import static org.onosproject.net.NetTestTools.connectPoint;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070054import static org.onosproject.net.NetTestTools.createLambda;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080055
56public class OpticalPathIntentCompilerTest {
57
58 private CoreService coreService;
59 private IntentExtensionService intentExtensionService;
60 private final IdGenerator idGenerator = new MockIdGenerator();
61 private OpticalPathIntentCompiler sut;
62
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080063 private final ApplicationId appId = new TestApplicationId("test");
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080064 private final ConnectPoint d1p1 = connectPoint("s1", 0);
65 private final ConnectPoint d2p0 = connectPoint("s2", 0);
66 private final ConnectPoint d2p1 = connectPoint("s2", 1);
67 private final ConnectPoint d3p1 = connectPoint("s3", 1);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080068
69 private final List<Link> links = Arrays.asList(
Ray Milkey2693bda2016-01-22 16:08:14 -080070 DefaultLink.builder().providerId(PID).src(d1p1).dst(d2p0).type(DIRECT).build(),
71 DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build()
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080072 );
73 private final int hops = links.size() + 1;
74 private OpticalPathIntent intent;
75
76 @Before
77 public void setUp() {
78 sut = new OpticalPathIntentCompiler();
79 coreService = createMock(CoreService.class);
80 expect(coreService.registerApplication("org.onosproject.net.intent"))
81 .andReturn(appId);
82 sut.coreService = coreService;
83
84 Intent.bindIdGenerator(idGenerator);
85
86 intent = OpticalPathIntent.builder()
87 .appId(appId)
88 .src(d1p1)
89 .dst(d3p1)
90 .path(new DefaultPath(PID, links, hops))
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070091 .lambda(createLambda())
Marc De Leenheerd24420f2015-05-27 09:40:59 -070092 .signalType(OchSignalType.FIXED_GRID)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080093 .build();
94 intentExtensionService = createMock(IntentExtensionService.class);
95 intentExtensionService.registerCompiler(OpticalPathIntent.class, sut);
96 intentExtensionService.unregisterCompiler(OpticalPathIntent.class);
97 sut.intentManager = intentExtensionService;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080098
99 replay(coreService, intentExtensionService);
100 }
101
102 @After
103 public void tearDown() {
104 Intent.unbindIdGenerator(idGenerator);
105 }
106
107 @Test
108 public void testCompiler() {
109 sut.activate();
110
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800111 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800112 assertThat(compiled, hasSize(1));
113
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700114 assertThat("key is inherited",
115 compiled.stream().map(Intent::key).collect(Collectors.toList()),
116 everyItem(is(intent.key())));
117
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800118 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
119 rules.stream()
120 .filter(x -> x.deviceId().equals(d1p1.deviceId()))
121 .findFirst()
122 .get();
123
124 rules.stream()
125 .filter(x -> x.deviceId().equals(d2p1.deviceId()))
126 .findFirst()
127 .get();
128
129 rules.stream()
130 .filter(x -> x.deviceId().equals(d3p1.deviceId()))
131 .findFirst()
132 .get();
133
Brian O'Connor81134662015-06-25 17:23:33 -0400134 rules.forEach(rule -> assertEquals("FlowRule priority is incorrect",
135 intent.priority(), rule.priority()));
136
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800137 sut.deactivate();
138 }
139
Brian O'Connor81134662015-06-25 17:23:33 -0400140 //TODO test bidirectional optical paths and verify rules
141
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800142}