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