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; |
| 41 | |
| 42 | import static org.easymock.EasyMock.createMock; |
| 43 | import static org.easymock.EasyMock.expect; |
| 44 | import static org.easymock.EasyMock.replay; |
| 45 | import static org.hamcrest.MatcherAssert.assertThat; |
| 46 | import static org.hamcrest.Matchers.hasSize; |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 47 | import static org.junit.Assert.assertEquals; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 48 | import static org.onosproject.net.Link.Type.DIRECT; |
| 49 | import static org.onosproject.net.NetTestTools.PID; |
| 50 | import static org.onosproject.net.NetTestTools.connectPoint; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 51 | import static org.onosproject.net.NetTestTools.createLambda; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 52 | |
| 53 | public class OpticalPathIntentCompilerTest { |
| 54 | |
| 55 | private CoreService coreService; |
| 56 | private IntentExtensionService intentExtensionService; |
| 57 | private final IdGenerator idGenerator = new MockIdGenerator(); |
| 58 | private OpticalPathIntentCompiler sut; |
| 59 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 60 | private final ApplicationId appId = new TestApplicationId("test"); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 61 | private final ConnectPoint d1p1 = connectPoint("s1", 0); |
| 62 | private final ConnectPoint d2p0 = connectPoint("s2", 0); |
| 63 | private final ConnectPoint d2p1 = connectPoint("s2", 1); |
| 64 | private final ConnectPoint d3p1 = connectPoint("s3", 1); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 65 | |
| 66 | private final List<Link> links = Arrays.asList( |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 67 | DefaultLink.builder().providerId(PID).src(d1p1).dst(d2p0).type(DIRECT).build(), |
| 68 | DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build() |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 69 | ); |
| 70 | private final int hops = links.size() + 1; |
| 71 | private OpticalPathIntent intent; |
| 72 | |
| 73 | @Before |
| 74 | public void setUp() { |
| 75 | sut = new OpticalPathIntentCompiler(); |
| 76 | coreService = createMock(CoreService.class); |
| 77 | expect(coreService.registerApplication("org.onosproject.net.intent")) |
| 78 | .andReturn(appId); |
| 79 | sut.coreService = coreService; |
| 80 | |
| 81 | Intent.bindIdGenerator(idGenerator); |
| 82 | |
| 83 | intent = OpticalPathIntent.builder() |
| 84 | .appId(appId) |
| 85 | .src(d1p1) |
| 86 | .dst(d3p1) |
| 87 | .path(new DefaultPath(PID, links, hops)) |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 88 | .lambda(createLambda()) |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 89 | .signalType(OchSignalType.FIXED_GRID) |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 90 | .build(); |
| 91 | intentExtensionService = createMock(IntentExtensionService.class); |
| 92 | intentExtensionService.registerCompiler(OpticalPathIntent.class, sut); |
| 93 | intentExtensionService.unregisterCompiler(OpticalPathIntent.class); |
| 94 | sut.intentManager = intentExtensionService; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 95 | |
| 96 | replay(coreService, intentExtensionService); |
| 97 | } |
| 98 | |
| 99 | @After |
| 100 | public void tearDown() { |
| 101 | Intent.unbindIdGenerator(idGenerator); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void testCompiler() { |
| 106 | sut.activate(); |
| 107 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 108 | List<Intent> compiled = sut.compile(intent, Collections.emptyList()); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 109 | assertThat(compiled, hasSize(1)); |
| 110 | |
| 111 | Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules(); |
| 112 | rules.stream() |
| 113 | .filter(x -> x.deviceId().equals(d1p1.deviceId())) |
| 114 | .findFirst() |
| 115 | .get(); |
| 116 | |
| 117 | rules.stream() |
| 118 | .filter(x -> x.deviceId().equals(d2p1.deviceId())) |
| 119 | .findFirst() |
| 120 | .get(); |
| 121 | |
| 122 | rules.stream() |
| 123 | .filter(x -> x.deviceId().equals(d3p1.deviceId())) |
| 124 | .findFirst() |
| 125 | .get(); |
| 126 | |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 127 | rules.forEach(rule -> assertEquals("FlowRule priority is incorrect", |
| 128 | intent.priority(), rule.priority())); |
| 129 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 130 | sut.deactivate(); |
| 131 | } |
| 132 | |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 133 | //TODO test bidirectional optical paths and verify rules |
| 134 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 135 | } |