2f40b37aa464706e3e11d436936490db4e3bad23
[onosfw.git] /
1 /*
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  */
16 package org.onosproject.net.intent.impl.compiler;
17
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.onosproject.TestApplicationId;
22 import org.onosproject.core.ApplicationId;
23 import org.onosproject.core.CoreService;
24 import org.onosproject.core.IdGenerator;
25 import org.onosproject.net.ConnectPoint;
26 import org.onosproject.net.DefaultLink;
27 import org.onosproject.net.DefaultPath;
28 import org.onosproject.net.Link;
29 import org.onosproject.net.OchSignalType;
30 import org.onosproject.net.flow.DefaultTrafficSelector;
31 import org.onosproject.net.flow.DefaultTrafficTreatment;
32 import org.onosproject.net.flow.FlowRule;
33 import org.onosproject.net.flow.TrafficSelector;
34 import org.onosproject.net.flow.TrafficTreatment;
35 import org.onosproject.net.intent.FlowRuleIntent;
36 import org.onosproject.net.intent.Intent;
37 import org.onosproject.net.intent.IntentExtensionService;
38 import org.onosproject.net.intent.IntentTestsMocks;
39 import org.onosproject.net.intent.MockIdGenerator;
40 import org.onosproject.net.intent.OpticalPathIntent;
41 import org.onosproject.net.provider.ProviderId;
42
43 import java.util.Arrays;
44 import java.util.Collection;
45 import java.util.Collections;
46 import java.util.List;
47
48 import static org.easymock.EasyMock.createMock;
49 import static org.easymock.EasyMock.expect;
50 import static org.easymock.EasyMock.replay;
51 import static org.hamcrest.MatcherAssert.assertThat;
52 import static org.hamcrest.Matchers.hasSize;
53 import static org.junit.Assert.assertEquals;
54 import static org.onosproject.net.Link.Type.DIRECT;
55 import static org.onosproject.net.NetTestTools.PID;
56 import static org.onosproject.net.NetTestTools.connectPoint;
57 import static org.onosproject.net.NetTestTools.createLambda;
58
59 public 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))
99                 .lambda(createLambda())
100                 .signalType(OchSignalType.FIXED_GRID)
101                 .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
139         rules.forEach(rule -> assertEquals("FlowRule priority is incorrect",
140                                            intent.priority(), rule.priority()));
141
142         sut.deactivate();
143     }
144
145     //TODO test bidirectional optical paths and verify rules
146
147 }