Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 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 | */ |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 16 | package org.onosproject.net.optical.intent.impl.compiler; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 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; |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 29 | import org.onosproject.net.OchSignalType; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 30 | import org.onosproject.net.flow.FlowRule; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 31 | import org.onosproject.net.intent.FlowRuleIntent; |
| 32 | import org.onosproject.net.intent.Intent; |
| 33 | import org.onosproject.net.intent.IntentExtensionService; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 34 | import org.onosproject.net.intent.MockIdGenerator; |
| 35 | import org.onosproject.net.intent.OpticalPathIntent; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 36 | |
| 37 | import java.util.Arrays; |
| 38 | import java.util.Collection; |
| 39 | import java.util.Collections; |
| 40 | import java.util.List; |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 41 | import java.util.stream.Collectors; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 42 | |
| 43 | import static org.easymock.EasyMock.createMock; |
| 44 | import static org.easymock.EasyMock.expect; |
| 45 | import static org.easymock.EasyMock.replay; |
| 46 | import static org.hamcrest.MatcherAssert.assertThat; |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 47 | import static org.hamcrest.Matchers.everyItem; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 48 | import static org.hamcrest.Matchers.hasSize; |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 49 | import static org.hamcrest.Matchers.is; |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 50 | import static org.junit.Assert.assertEquals; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 51 | import static org.onosproject.net.Link.Type.DIRECT; |
| 52 | import static org.onosproject.net.NetTestTools.PID; |
| 53 | import static org.onosproject.net.NetTestTools.connectPoint; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 54 | import static org.onosproject.net.NetTestTools.createLambda; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 55 | |
| 56 | public class OpticalPathIntentCompilerTest { |
| 57 | |
| 58 | private CoreService coreService; |
| 59 | private IntentExtensionService intentExtensionService; |
| 60 | private final IdGenerator idGenerator = new MockIdGenerator(); |
| 61 | private OpticalPathIntentCompiler sut; |
| 62 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 63 | private final ApplicationId appId = new TestApplicationId("test"); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 64 | 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 SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 68 | |
| 69 | private final List<Link> links = Arrays.asList( |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 70 | DefaultLink.builder().providerId(PID).src(d1p1).dst(d2p0).type(DIRECT).build(), |
| 71 | DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build() |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 72 | ); |
| 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 Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 91 | .lambda(createLambda()) |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 92 | .signalType(OchSignalType.FIXED_GRID) |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 93 | .build(); |
| 94 | intentExtensionService = createMock(IntentExtensionService.class); |
| 95 | intentExtensionService.registerCompiler(OpticalPathIntent.class, sut); |
| 96 | intentExtensionService.unregisterCompiler(OpticalPathIntent.class); |
| 97 | sut.intentManager = intentExtensionService; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 98 | |
| 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 SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 111 | List<Intent> compiled = sut.compile(intent, Collections.emptyList()); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 112 | assertThat(compiled, hasSize(1)); |
| 113 | |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 114 | assertThat("key is inherited", |
| 115 | compiled.stream().map(Intent::key).collect(Collectors.toList()), |
| 116 | everyItem(is(intent.key()))); |
| 117 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 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 | |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 134 | rules.forEach(rule -> assertEquals("FlowRule priority is incorrect", |
| 135 | intent.priority(), rule.priority())); |
| 136 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 137 | sut.deactivate(); |
| 138 | } |
| 139 | |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 140 | //TODO test bidirectional optical paths and verify rules |
| 141 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 142 | } |