Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-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 | */ |
| 16 | package org.onosproject.net.intent.impl.compiler; |
| 17 | |
| 18 | import java.util.Arrays; |
| 19 | import java.util.Collection; |
| 20 | import java.util.Collections; |
| 21 | import java.util.List; |
| 22 | import java.util.Optional; |
| 23 | |
| 24 | import org.junit.After; |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | import org.onlab.packet.MplsLabel; |
| 28 | import org.onosproject.TestApplicationId; |
| 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.CoreService; |
| 31 | import org.onosproject.core.IdGenerator; |
| 32 | import org.onosproject.net.ConnectPoint; |
| 33 | import org.onosproject.net.DefaultLink; |
| 34 | import org.onosproject.net.DefaultPath; |
| 35 | import org.onosproject.net.Link; |
| 36 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 37 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 38 | import org.onosproject.net.flow.FlowRule; |
| 39 | import org.onosproject.net.flow.TrafficSelector; |
| 40 | import org.onosproject.net.flow.TrafficTreatment; |
| 41 | import org.onosproject.net.intent.FlowRuleIntent; |
| 42 | import org.onosproject.net.intent.Intent; |
| 43 | import org.onosproject.net.intent.IntentExtensionService; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 44 | import org.onosproject.net.intent.MockIdGenerator; |
| 45 | import org.onosproject.net.intent.MplsPathIntent; |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 46 | import org.onosproject.net.resource.MockResourceService; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 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.hamcrest.Matchers.is; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 54 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 55 | import static org.onosproject.net.Link.Type.DIRECT; |
| 56 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 57 | import static org.onosproject.net.NetTestTools.PID; |
| 58 | import static org.onosproject.net.NetTestTools.connectPoint; |
| 59 | |
| 60 | public class MplsPathIntentCompilerTest { |
| 61 | |
| 62 | private final ApplicationId appId = new TestApplicationId("test"); |
| 63 | |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 64 | private final ConnectPoint d1pi = connectPoint("s1", 100); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 65 | private final ConnectPoint d1p1 = connectPoint("s1", 0); |
| 66 | private final ConnectPoint d2p0 = connectPoint("s2", 0); |
| 67 | private final ConnectPoint d2p1 = connectPoint("s2", 1); |
| 68 | private final ConnectPoint d3p1 = connectPoint("s3", 1); |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 69 | private final ConnectPoint d3pe = connectPoint("s3", 100); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 70 | |
| 71 | private final TrafficSelector selector = DefaultTrafficSelector.builder().build(); |
| 72 | private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 73 | |
| 74 | private final Optional<MplsLabel> ingressLabel = |
| 75 | Optional.of(MplsLabel.mplsLabel(10)); |
| 76 | private final Optional<MplsLabel> egressLabel = |
| 77 | Optional.of(MplsLabel.mplsLabel(20)); |
| 78 | |
| 79 | private final List<Link> links = Arrays.asList( |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 80 | createEdgeLink(d1pi, true), |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 81 | DefaultLink.builder().providerId(PID).src(d1p1).dst(d2p0).type(DIRECT).build(), |
| 82 | DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build(), |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 83 | createEdgeLink(d3pe, false) |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 84 | ); |
| 85 | |
| 86 | private IdGenerator idGenerator = new MockIdGenerator(); |
| 87 | |
| 88 | private final int hops = links.size() - 1; |
| 89 | private MplsPathIntent intent; |
| 90 | private MplsPathIntentCompiler sut; |
| 91 | |
| 92 | @Before |
| 93 | public void setUp() { |
| 94 | sut = new MplsPathIntentCompiler(); |
| 95 | CoreService coreService = createMock(CoreService.class); |
| 96 | expect(coreService.registerApplication("org.onosproject.net.intent")) |
| 97 | .andReturn(appId); |
| 98 | sut.coreService = coreService; |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 99 | sut.resourceService = new MockResourceService(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 100 | |
| 101 | Intent.bindIdGenerator(idGenerator); |
| 102 | |
| 103 | intent = MplsPathIntent.builder() |
| 104 | .appId(APP_ID) |
| 105 | .selector(selector) |
| 106 | .treatment(treatment) |
| 107 | .path(new DefaultPath(PID, links, hops)) |
| 108 | .ingressLabel(ingressLabel) |
| 109 | .egressLabel(egressLabel) |
| 110 | .priority(55) |
| 111 | .build(); |
| 112 | |
| 113 | IntentExtensionService intentExtensionService = createMock(IntentExtensionService.class); |
| 114 | intentExtensionService.registerCompiler(MplsPathIntent.class, sut); |
| 115 | intentExtensionService.unregisterCompiler(MplsPathIntent.class); |
| 116 | sut.intentExtensionService = intentExtensionService; |
| 117 | |
| 118 | replay(coreService, intentExtensionService); |
| 119 | } |
| 120 | |
| 121 | @After |
| 122 | public void tearDown() { |
| 123 | Intent.unbindIdGenerator(idGenerator); |
| 124 | } |
| 125 | |
| 126 | @Test |
| 127 | public void testCompile() { |
| 128 | sut.activate(); |
| 129 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 130 | List<Intent> compiled = sut.compile(intent, Collections.emptyList()); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 131 | assertThat(compiled, hasSize(1)); |
| 132 | |
| 133 | Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules(); |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 134 | assertThat(rules, hasSize(3)); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 135 | |
| 136 | FlowRule rule = rules.stream() |
| 137 | .filter(x -> x.deviceId().equals(d2p0.deviceId())) |
| 138 | .findFirst() |
| 139 | .get(); |
| 140 | assertThat(rule.deviceId(), is(d2p0.deviceId())); |
| 141 | |
| 142 | sut.deactivate(); |
| 143 | |
| 144 | } |
Sho SHIMIZU | f26e692 | 2015-10-29 16:19:20 -0700 | [diff] [blame] | 145 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 146 | } |