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