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 java.util.Arrays; |
| 19 | import java.util.Collection; |
| 20 | import java.util.Collections; |
| 21 | import java.util.List; |
| 22 | |
| 23 | import org.junit.After; |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onosproject.TestApplicationId; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
| 29 | import org.onosproject.core.IdGenerator; |
| 30 | import org.onosproject.net.ConnectPoint; |
| 31 | import org.onosproject.net.DefaultLink; |
| 32 | import org.onosproject.net.DefaultPath; |
| 33 | import org.onosproject.net.Link; |
| 34 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 35 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 36 | import org.onosproject.net.flow.FlowRule; |
| 37 | import org.onosproject.net.flow.TrafficSelector; |
| 38 | import org.onosproject.net.flow.TrafficTreatment; |
| 39 | import org.onosproject.net.intent.FlowRuleIntent; |
| 40 | import org.onosproject.net.intent.Intent; |
| 41 | import org.onosproject.net.intent.IntentExtensionService; |
| 42 | import org.onosproject.net.intent.MockIdGenerator; |
| 43 | import org.onosproject.net.intent.PathIntent; |
| 44 | import org.onosproject.net.provider.ProviderId; |
| 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.DefaultEdgeLink.createEdgeLink; |
| 53 | import static org.onosproject.net.Link.Type.DIRECT; |
| 54 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 55 | import static org.onosproject.net.NetTestTools.PID; |
| 56 | import static org.onosproject.net.NetTestTools.connectPoint; |
| 57 | |
| 58 | /** |
| 59 | * Unit tests for PathIntentCompiler. |
| 60 | */ |
| 61 | public class PathIntentCompilerTest { |
| 62 | |
| 63 | private CoreService coreService; |
| 64 | private IntentExtensionService intentExtensionService; |
| 65 | private IdGenerator idGenerator = new MockIdGenerator(); |
| 66 | private PathIntentCompiler sut; |
| 67 | |
| 68 | private final TrafficSelector selector = DefaultTrafficSelector.builder().build(); |
| 69 | private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 70 | private final ApplicationId appId = new TestApplicationId("test"); |
| 71 | private final ProviderId pid = new ProviderId("of", "test"); |
| 72 | private final ConnectPoint d1p1 = connectPoint("s1", 0); |
| 73 | private final ConnectPoint d2p0 = connectPoint("s2", 0); |
| 74 | private final ConnectPoint d2p1 = connectPoint("s2", 1); |
| 75 | private final ConnectPoint d3p1 = connectPoint("s3", 1); |
| 76 | private final ConnectPoint d3p0 = connectPoint("s3", 10); |
| 77 | private final ConnectPoint d1p0 = connectPoint("s1", 10); |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 78 | private static final int PRIORITY = 555; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 79 | |
| 80 | private final List<Link> links = Arrays.asList( |
| 81 | createEdgeLink(d1p0, true), |
| 82 | new DefaultLink(PID, d1p1, d2p0, DIRECT), |
| 83 | new DefaultLink(PID, d2p1, d3p1, DIRECT), |
| 84 | createEdgeLink(d3p0, false) |
| 85 | ); |
| 86 | private final int hops = links.size() - 1; |
| 87 | private PathIntent intent; |
| 88 | |
| 89 | /** |
| 90 | * Configures objects used in all the test cases. |
| 91 | */ |
| 92 | @Before |
| 93 | public void setUp() { |
| 94 | sut = new PathIntentCompiler(); |
| 95 | coreService = createMock(CoreService.class); |
| 96 | expect(coreService.registerApplication("org.onosproject.net.intent")) |
| 97 | .andReturn(appId); |
| 98 | sut.coreService = coreService; |
| 99 | |
| 100 | Intent.bindIdGenerator(idGenerator); |
| 101 | |
| 102 | intent = PathIntent.builder() |
| 103 | .appId(APP_ID) |
| 104 | .selector(selector) |
| 105 | .treatment(treatment) |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 106 | .priority(PRIORITY) |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 107 | .path(new DefaultPath(pid, links, hops)) |
| 108 | .build(); |
| 109 | intentExtensionService = createMock(IntentExtensionService.class); |
| 110 | intentExtensionService.registerCompiler(PathIntent.class, sut); |
| 111 | intentExtensionService.unregisterCompiler(PathIntent.class); |
| 112 | sut.intentManager = intentExtensionService; |
| 113 | |
| 114 | replay(coreService, intentExtensionService); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Tears down objects used in all the test cases. |
| 119 | */ |
| 120 | @After |
| 121 | public void tearDown() { |
| 122 | Intent.unbindIdGenerator(idGenerator); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Tests the compilation behavior of the path intent compiler. |
| 127 | */ |
| 128 | @Test |
| 129 | public void testCompile() { |
| 130 | sut.activate(); |
| 131 | |
| 132 | List<Intent> compiled = sut.compile(intent, Collections.emptyList(), Collections.emptySet()); |
| 133 | assertThat(compiled, hasSize(1)); |
| 134 | |
| 135 | Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules(); |
| 136 | |
| 137 | FlowRule rule1 = rules.stream() |
| 138 | .filter(x -> x.deviceId().equals(d1p0.deviceId())) |
| 139 | .findFirst() |
| 140 | .get(); |
| 141 | assertThat(rule1.deviceId(), is(d1p0.deviceId())); |
| 142 | assertThat(rule1.selector(), |
| 143 | is(DefaultTrafficSelector.builder(selector).matchInPort(d1p0.port()).build())); |
| 144 | assertThat(rule1.treatment(), |
| 145 | is(DefaultTrafficTreatment.builder().setOutput(d1p1.port()).build())); |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 146 | assertThat(rule1.priority(), is(intent.priority())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 147 | |
| 148 | FlowRule rule2 = rules.stream() |
| 149 | .filter(x -> x.deviceId().equals(d2p0.deviceId())) |
| 150 | .findFirst() |
| 151 | .get(); |
| 152 | assertThat(rule2.deviceId(), is(d2p0.deviceId())); |
| 153 | assertThat(rule2.selector(), |
| 154 | is(DefaultTrafficSelector.builder(selector).matchInPort(d2p0.port()).build())); |
| 155 | assertThat(rule2.treatment(), |
| 156 | is(DefaultTrafficTreatment.builder().setOutput(d2p1.port()).build())); |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 157 | assertThat(rule2.priority(), is(intent.priority())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 158 | |
| 159 | FlowRule rule3 = rules.stream() |
| 160 | .filter(x -> x.deviceId().equals(d3p0.deviceId())) |
| 161 | .findFirst() |
| 162 | .get(); |
| 163 | assertThat(rule3.deviceId(), is(d3p1.deviceId())); |
| 164 | assertThat(rule3.selector(), |
| 165 | is(DefaultTrafficSelector.builder(selector).matchInPort(d3p1.port()).build())); |
| 166 | assertThat(rule3.treatment(), |
| 167 | is(DefaultTrafficTreatment.builder(treatment).setOutput(d3p0.port()).build())); |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 168 | assertThat(rule3.priority(), is(intent.priority())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 169 | |
| 170 | sut.deactivate(); |
| 171 | } |
| 172 | } |