Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 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 com.google.common.collect.ImmutableSet; |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onosproject.TestApplicationId; |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 23 | import org.onosproject.cfg.ComponentConfigAdapter; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.core.IdGenerator; |
| 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.DefaultLink; |
| 29 | import org.onosproject.net.Link; |
| 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.LinkCollectionIntent; |
| 39 | import org.onosproject.net.intent.MockIdGenerator; |
| 40 | |
| 41 | import java.util.Collection; |
| 42 | import java.util.Collections; |
| 43 | import java.util.List; |
| 44 | import java.util.Set; |
| 45 | |
| 46 | import static org.easymock.EasyMock.createMock; |
| 47 | import static org.easymock.EasyMock.expect; |
| 48 | import static org.easymock.EasyMock.replay; |
| 49 | import static org.hamcrest.MatcherAssert.assertThat; |
| 50 | import static org.hamcrest.Matchers.hasSize; |
| 51 | import static org.hamcrest.Matchers.is; |
| 52 | import static org.onosproject.net.Link.Type.DIRECT; |
| 53 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 54 | import static org.onosproject.net.NetTestTools.PID; |
| 55 | import static org.onosproject.net.NetTestTools.connectPoint; |
| 56 | |
| 57 | public class LinkCollectionIntentCompilerTest { |
| 58 | |
| 59 | private final ApplicationId appId = new TestApplicationId("test"); |
| 60 | |
| 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); |
| 65 | private final ConnectPoint d3p0 = connectPoint("s3", 10); |
| 66 | private final ConnectPoint d1p0 = connectPoint("s1", 10); |
| 67 | |
| 68 | private final Set<Link> links = ImmutableSet.of( |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 69 | DefaultLink.builder().providerId(PID).src(d1p1).dst(d2p0).type(DIRECT).build(), |
| 70 | DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build(), |
| 71 | DefaultLink.builder().providerId(PID).src(d1p1).dst(d3p1).type(DIRECT).build()); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 72 | |
| 73 | private final TrafficSelector selector = DefaultTrafficSelector.builder().build(); |
| 74 | private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 75 | |
| 76 | private CoreService coreService; |
| 77 | private IntentExtensionService intentExtensionService; |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 78 | private IntentConfigurableRegistrator registrator; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 79 | private IdGenerator idGenerator = new MockIdGenerator(); |
| 80 | |
| 81 | private LinkCollectionIntent intent; |
| 82 | |
| 83 | private LinkCollectionIntentCompiler sut; |
| 84 | |
| 85 | @Before |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 86 | public void setUp() { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 87 | sut = new LinkCollectionIntentCompiler(); |
| 88 | coreService = createMock(CoreService.class); |
| 89 | expect(coreService.registerApplication("org.onosproject.net.intent")) |
| 90 | .andReturn(appId); |
| 91 | sut.coreService = coreService; |
| 92 | |
| 93 | Intent.bindIdGenerator(idGenerator); |
| 94 | |
| 95 | intent = LinkCollectionIntent.builder() |
| 96 | .appId(APP_ID) |
| 97 | .selector(selector) |
| 98 | .treatment(treatment) |
| 99 | .links(links) |
| 100 | .ingressPoints(ImmutableSet.of(d1p1)) |
| 101 | .egressPoints(ImmutableSet.of(d3p1)) |
| 102 | .build(); |
| 103 | intentExtensionService = createMock(IntentExtensionService.class); |
| 104 | intentExtensionService.registerCompiler(LinkCollectionIntent.class, sut); |
| 105 | intentExtensionService.unregisterCompiler(LinkCollectionIntent.class); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 106 | |
| 107 | registrator = new IntentConfigurableRegistrator(); |
| 108 | registrator.extensionService = intentExtensionService; |
| 109 | registrator.cfgService = new ComponentConfigAdapter(); |
| 110 | registrator.activate(); |
| 111 | |
| 112 | sut.registrator = registrator; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 113 | |
| 114 | replay(coreService, intentExtensionService); |
| 115 | } |
| 116 | |
| 117 | @After |
| 118 | public void tearDown() { |
| 119 | Intent.unbindIdGenerator(idGenerator); |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | public void testCompile() { |
| 124 | sut.activate(); |
| 125 | |
| 126 | List<Intent> compiled = sut.compile(intent, Collections.emptyList(), Collections.emptySet()); |
| 127 | assertThat(compiled, hasSize(1)); |
| 128 | |
| 129 | Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules(); |
| 130 | assertThat(rules, hasSize(links.size())); |
| 131 | |
| 132 | // if not found, get() raises an exception |
| 133 | FlowRule rule1 = rules.stream() |
| 134 | .filter(rule -> rule.deviceId().equals(d1p0.deviceId())) |
| 135 | .findFirst() |
| 136 | .get(); |
| 137 | assertThat(rule1.selector(), is( |
| 138 | DefaultTrafficSelector.builder(intent.selector()).matchInPort(d1p1.port()).build() |
| 139 | )); |
| 140 | assertThat(rule1.treatment(), is( |
| 141 | DefaultTrafficTreatment.builder(intent.treatment()).setOutput(d1p1.port()).build() |
| 142 | )); |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 143 | assertThat(rule1.priority(), is(intent.priority())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 144 | |
| 145 | FlowRule rule2 = rules.stream() |
| 146 | .filter(rule -> rule.deviceId().equals(d2p0.deviceId())) |
| 147 | .findFirst() |
| 148 | .get(); |
| 149 | assertThat(rule2.selector(), is( |
| 150 | DefaultTrafficSelector.builder(intent.selector()).matchInPort(d2p0.port()).build() |
| 151 | )); |
| 152 | assertThat(rule2.treatment(), is( |
| 153 | DefaultTrafficTreatment.builder().setOutput(d2p1.port()).build() |
| 154 | )); |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 155 | assertThat(rule2.priority(), is(intent.priority())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 156 | |
| 157 | FlowRule rule3 = rules.stream() |
| 158 | .filter(rule -> rule.deviceId().equals(d3p0.deviceId())) |
| 159 | .findFirst() |
| 160 | .get(); |
| 161 | assertThat(rule3.selector(), is( |
| 162 | DefaultTrafficSelector.builder(intent.selector()).matchInPort(d3p1.port()).build() |
| 163 | )); |
| 164 | assertThat(rule3.treatment(), is( |
| 165 | DefaultTrafficTreatment.builder().setOutput(d3p1.port()).build() |
| 166 | )); |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 167 | assertThat(rule3.priority(), is(intent.priority())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 168 | |
| 169 | sut.deactivate(); |
| 170 | } |
| 171 | } |