blob: 08e8e4f6ff740770ffecfb7431f82da2453a2c4a [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;
29import org.onosproject.net.flow.DefaultTrafficSelector;
30import org.onosproject.net.flow.DefaultTrafficTreatment;
31import org.onosproject.net.flow.FlowRule;
32import org.onosproject.net.flow.TrafficSelector;
33import org.onosproject.net.flow.TrafficTreatment;
34import org.onosproject.net.intent.FlowRuleIntent;
35import org.onosproject.net.intent.Intent;
36import org.onosproject.net.intent.IntentExtensionService;
37import org.onosproject.net.intent.IntentTestsMocks;
38import org.onosproject.net.intent.MockIdGenerator;
39import org.onosproject.net.intent.OpticalPathIntent;
40import org.onosproject.net.provider.ProviderId;
41
42import java.util.Arrays;
43import java.util.Collection;
44import java.util.Collections;
45import java.util.List;
46
47import static org.easymock.EasyMock.createMock;
48import static org.easymock.EasyMock.expect;
49import static org.easymock.EasyMock.replay;
50import static org.hamcrest.MatcherAssert.assertThat;
51import static org.hamcrest.Matchers.hasSize;
52import static org.onosproject.net.Link.Type.DIRECT;
53import static org.onosproject.net.NetTestTools.PID;
54import static org.onosproject.net.NetTestTools.connectPoint;
55
56public class OpticalPathIntentCompilerTest {
57
58 private CoreService coreService;
59 private IntentExtensionService intentExtensionService;
60 private final IdGenerator idGenerator = new MockIdGenerator();
61 private OpticalPathIntentCompiler sut;
62
63 private final TrafficSelector selector = DefaultTrafficSelector.builder().build();
64 private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
65 private final ApplicationId appId = new TestApplicationId("test");
66 private final ProviderId pid = new ProviderId("of", "test");
67 private final ConnectPoint d1p1 = connectPoint("s1", 0);
68 private final ConnectPoint d2p0 = connectPoint("s2", 0);
69 private final ConnectPoint d2p1 = connectPoint("s2", 1);
70 private final ConnectPoint d3p1 = connectPoint("s3", 1);
71 private final ConnectPoint d3p0 = connectPoint("s3", 10);
72 private final ConnectPoint d1p0 = connectPoint("s1", 10);
73
74 private final List<Link> links = Arrays.asList(
75 new DefaultLink(PID, d1p1, d2p0, DIRECT),
76 new DefaultLink(PID, d2p1, d3p1, DIRECT)
77 );
78 private final int hops = links.size() + 1;
79 private OpticalPathIntent intent;
80
81 @Before
82 public void setUp() {
83 sut = new OpticalPathIntentCompiler();
84 coreService = createMock(CoreService.class);
85 expect(coreService.registerApplication("org.onosproject.net.intent"))
86 .andReturn(appId);
87 sut.coreService = coreService;
88
89 Intent.bindIdGenerator(idGenerator);
90
91 intent = OpticalPathIntent.builder()
92 .appId(appId)
93 .src(d1p1)
94 .dst(d3p1)
95 .path(new DefaultPath(PID, links, hops))
96 .build();
97 intentExtensionService = createMock(IntentExtensionService.class);
98 intentExtensionService.registerCompiler(OpticalPathIntent.class, sut);
99 intentExtensionService.unregisterCompiler(OpticalPathIntent.class);
100 sut.intentManager = intentExtensionService;
101 sut.resourceService = new IntentTestsMocks.MockResourceService();
102
103 replay(coreService, intentExtensionService);
104 }
105
106 @After
107 public void tearDown() {
108 Intent.unbindIdGenerator(idGenerator);
109 }
110
111 @Test
112 public void testCompiler() {
113 sut.activate();
114
115 List<Intent> compiled = sut.compile(intent, Collections.emptyList(), Collections.emptySet());
116 assertThat(compiled, hasSize(1));
117
118 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
134 sut.deactivate();
135 }
136
137}