blob: 2f40b37aa464706e3e11d436936490db4e3bad23 [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;
Brian O'Connor81134662015-06-25 17:23:33 -040053import static org.junit.Assert.assertEquals;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080054import static org.onosproject.net.Link.Type.DIRECT;
55import static org.onosproject.net.NetTestTools.PID;
56import static org.onosproject.net.NetTestTools.connectPoint;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070057import static org.onosproject.net.NetTestTools.createLambda;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080058
59public class OpticalPathIntentCompilerTest {
60
61 private CoreService coreService;
62 private IntentExtensionService intentExtensionService;
63 private final IdGenerator idGenerator = new MockIdGenerator();
64 private OpticalPathIntentCompiler sut;
65
66 private final TrafficSelector selector = DefaultTrafficSelector.builder().build();
67 private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
68 private final ApplicationId appId = new TestApplicationId("test");
69 private final ProviderId pid = new ProviderId("of", "test");
70 private final ConnectPoint d1p1 = connectPoint("s1", 0);
71 private final ConnectPoint d2p0 = connectPoint("s2", 0);
72 private final ConnectPoint d2p1 = connectPoint("s2", 1);
73 private final ConnectPoint d3p1 = connectPoint("s3", 1);
74 private final ConnectPoint d3p0 = connectPoint("s3", 10);
75 private final ConnectPoint d1p0 = connectPoint("s1", 10);
76
77 private final List<Link> links = Arrays.asList(
78 new DefaultLink(PID, d1p1, d2p0, DIRECT),
79 new DefaultLink(PID, d2p1, d3p1, DIRECT)
80 );
81 private final int hops = links.size() + 1;
82 private OpticalPathIntent intent;
83
84 @Before
85 public void setUp() {
86 sut = new OpticalPathIntentCompiler();
87 coreService = createMock(CoreService.class);
88 expect(coreService.registerApplication("org.onosproject.net.intent"))
89 .andReturn(appId);
90 sut.coreService = coreService;
91
92 Intent.bindIdGenerator(idGenerator);
93
94 intent = OpticalPathIntent.builder()
95 .appId(appId)
96 .src(d1p1)
97 .dst(d3p1)
98 .path(new DefaultPath(PID, links, hops))
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070099 .lambda(createLambda())
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700100 .signalType(OchSignalType.FIXED_GRID)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800101 .build();
102 intentExtensionService = createMock(IntentExtensionService.class);
103 intentExtensionService.registerCompiler(OpticalPathIntent.class, sut);
104 intentExtensionService.unregisterCompiler(OpticalPathIntent.class);
105 sut.intentManager = intentExtensionService;
106 sut.resourceService = new IntentTestsMocks.MockResourceService();
107
108 replay(coreService, intentExtensionService);
109 }
110
111 @After
112 public void tearDown() {
113 Intent.unbindIdGenerator(idGenerator);
114 }
115
116 @Test
117 public void testCompiler() {
118 sut.activate();
119
120 List<Intent> compiled = sut.compile(intent, Collections.emptyList(), Collections.emptySet());
121 assertThat(compiled, hasSize(1));
122
123 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
124 rules.stream()
125 .filter(x -> x.deviceId().equals(d1p1.deviceId()))
126 .findFirst()
127 .get();
128
129 rules.stream()
130 .filter(x -> x.deviceId().equals(d2p1.deviceId()))
131 .findFirst()
132 .get();
133
134 rules.stream()
135 .filter(x -> x.deviceId().equals(d3p1.deviceId()))
136 .findFirst()
137 .get();
138
Brian O'Connor81134662015-06-25 17:23:33 -0400139 rules.forEach(rule -> assertEquals("FlowRule priority is incorrect",
140 intent.priority(), rule.priority()));
141
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800142 sut.deactivate();
143 }
144
Brian O'Connor81134662015-06-25 17:23:33 -0400145 //TODO test bidirectional optical paths and verify rules
146
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800147}