Ray Milkey | d990535 | 2015-02-24 16:52:42 -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; |
| 17 | |
| 18 | import java.util.Arrays; |
| 19 | import java.util.Collection; |
| 20 | import java.util.List; |
| 21 | |
| 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.core.CoreServiceAdapter; |
| 28 | import org.onosproject.core.IdGenerator; |
| 29 | import org.onosproject.net.ConnectPoint; |
| 30 | import org.onosproject.net.DefaultLink; |
| 31 | import org.onosproject.net.DefaultPath; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.Link; |
| 34 | import org.onosproject.net.flow.FlowRuleOperation; |
| 35 | import org.onosproject.net.intent.FakeIntentManager; |
| 36 | import org.onosproject.net.intent.Intent; |
| 37 | import org.onosproject.net.intent.IntentInstaller; |
| 38 | import org.onosproject.net.intent.IntentTestsMocks; |
| 39 | import org.onosproject.net.intent.MockIdGenerator; |
| 40 | import org.onosproject.net.intent.PathIntent; |
| 41 | |
| 42 | import com.google.common.collect.ImmutableList; |
| 43 | |
| 44 | import static org.hamcrest.MatcherAssert.assertThat; |
| 45 | import static org.hamcrest.Matchers.*; |
| 46 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
| 47 | import static org.onosproject.net.Link.Type.DIRECT; |
| 48 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 49 | import static org.onosproject.net.NetTestTools.PID; |
| 50 | import static org.onosproject.net.NetTestTools.connectPoint; |
| 51 | |
| 52 | /** |
| 53 | * Unit tests for path intent installer. |
| 54 | */ |
| 55 | public class PathIntentInstallerTest { |
| 56 | |
| 57 | CoreService testCoreService; |
| 58 | IdGenerator idGenerator = new MockIdGenerator(); |
| 59 | PathIntentInstaller installer; |
| 60 | |
| 61 | private final IntentTestsMocks.MockSelector selector = new IntentTestsMocks.MockSelector(); |
| 62 | private final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment(); |
| 63 | private final ConnectPoint d1p1 = connectPoint("s1", 0); |
| 64 | private final ConnectPoint d2p0 = connectPoint("s2", 0); |
| 65 | private final ConnectPoint d2p1 = connectPoint("s2", 1); |
| 66 | private final ConnectPoint d3p1 = connectPoint("s3", 1); |
| 67 | private final ConnectPoint d3p0 = connectPoint("s3", 10); |
| 68 | private final ConnectPoint d1p0 = connectPoint("s1", 10); |
| 69 | |
| 70 | private final List<Link> links = Arrays.asList( |
| 71 | createEdgeLink(d1p0, true), |
| 72 | new DefaultLink(PID, d1p1, d2p0, DIRECT), |
| 73 | new DefaultLink(PID, d2p1, d3p1, DIRECT), |
| 74 | createEdgeLink(d3p0, false) |
| 75 | ); |
| 76 | private final int hops = links.size() - 1; |
| 77 | private PathIntent intent; |
| 78 | |
| 79 | /** |
| 80 | * Configures objects used in all the test cases. |
| 81 | */ |
| 82 | @Before |
| 83 | public void localSetup() { |
| 84 | testCoreService = new TestCoreService(); |
| 85 | Intent.bindIdGenerator(idGenerator); |
| 86 | installer = new PathIntentInstaller(); |
| 87 | installer.coreService = testCoreService; |
| 88 | installer.intentManager = new MockIntentManager(); |
| 89 | intent = new PathIntent(APP_ID, selector, treatment, |
| 90 | new DefaultPath(PID, links, hops), ImmutableList.of()); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Tears down objects used in all the test cases. |
| 95 | */ |
| 96 | @After |
| 97 | public void localTearDown() { |
| 98 | Intent.unbindIdGenerator(idGenerator); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Mock for core service. |
| 103 | */ |
| 104 | private static class TestCoreService extends CoreServiceAdapter { |
| 105 | |
| 106 | String registeredId = ""; |
| 107 | |
| 108 | @Override |
| 109 | public ApplicationId registerApplication(String identifier) { |
| 110 | registeredId = identifier; |
| 111 | return APP_ID; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Mock for intent manager service. Checks that the PathIntent |
| 117 | * installer installs and uninstalls properly. |
| 118 | */ |
| 119 | private static class MockIntentManager extends FakeIntentManager { |
| 120 | |
| 121 | boolean installerRegistered = false; |
| 122 | |
| 123 | @Override |
| 124 | public <T extends Intent> void registerInstaller( |
| 125 | Class<T> cls, |
| 126 | IntentInstaller<T> installer) { |
| 127 | assertThat(cls.getCanonicalName(), |
| 128 | equalTo("org.onosproject.net.intent.PathIntent")); |
| 129 | installerRegistered = true; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public <T extends Intent> void unregisterInstaller(Class<T> cls) { |
| 134 | assertThat(cls.getCanonicalName(), |
| 135 | equalTo("org.onosproject.net.intent.PathIntent")); |
| 136 | assertThat(installerRegistered, is(true)); |
| 137 | } |
| 138 | |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Tests activation and deactivation of the installer. |
| 143 | */ |
| 144 | @Test |
| 145 | public void activateDeactivate() { |
| 146 | installer.activate(); |
| 147 | installer.deactivate(); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Checks that a flow operation contains the correct values. |
| 152 | * |
| 153 | * @param op flow rule operation to check |
| 154 | * @param type type the flow rule operation should have |
| 155 | * @param deviceId device id the flow rule operation should have |
| 156 | */ |
| 157 | private void checkFlowOperation(FlowRuleOperation op, |
| 158 | FlowRuleOperation.Type type, |
| 159 | DeviceId deviceId) { |
| 160 | assertThat(op.type(), is(type)); |
| 161 | assertThat(op.rule().deviceId(), equalTo(deviceId)); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Tests installation operation of the path intent installer. |
| 166 | */ |
| 167 | @Test |
| 168 | public void install() { |
| 169 | installer.activate(); |
| 170 | |
| 171 | List<Collection<FlowRuleOperation>> operations = |
| 172 | installer.install(intent); |
| 173 | assertThat(operations, notNullValue()); |
| 174 | assertThat(operations, hasSize(1)); |
| 175 | |
| 176 | Collection<FlowRuleOperation> flowRuleOpsCollection = operations.get(0); |
| 177 | assertThat(flowRuleOpsCollection, hasSize(hops)); |
| 178 | FlowRuleOperation[] flowRuleOps = |
| 179 | flowRuleOpsCollection.toArray(new FlowRuleOperation[hops]); |
| 180 | |
| 181 | FlowRuleOperation op0 = flowRuleOps[0]; |
| 182 | checkFlowOperation(op0, FlowRuleOperation.Type.ADD, d1p0.deviceId()); |
| 183 | |
| 184 | FlowRuleOperation op1 = flowRuleOps[1]; |
| 185 | checkFlowOperation(op1, FlowRuleOperation.Type.ADD, d2p0.deviceId()); |
| 186 | |
| 187 | FlowRuleOperation op2 = flowRuleOps[2]; |
| 188 | checkFlowOperation(op2, FlowRuleOperation.Type.ADD, d3p0.deviceId()); |
| 189 | |
| 190 | installer.deactivate(); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Checks the uninstall operation of the path intent installer. |
| 195 | */ |
| 196 | @Test |
| 197 | public void uninstall() { |
| 198 | installer.activate(); |
| 199 | |
| 200 | List<Collection<FlowRuleOperation>> operations = |
| 201 | installer.uninstall(intent); |
| 202 | assertThat(operations, notNullValue()); |
| 203 | assertThat(operations, hasSize(1)); |
| 204 | |
| 205 | Collection<FlowRuleOperation> flowRuleOpsCollection = operations.get(0); |
| 206 | assertThat(flowRuleOpsCollection, hasSize(hops)); |
| 207 | FlowRuleOperation[] flowRuleOps = |
| 208 | flowRuleOpsCollection.toArray(new FlowRuleOperation[hops]); |
| 209 | |
| 210 | FlowRuleOperation op0 = flowRuleOps[0]; |
| 211 | checkFlowOperation(op0, FlowRuleOperation.Type.REMOVE, d1p0.deviceId()); |
| 212 | |
| 213 | FlowRuleOperation op1 = flowRuleOps[1]; |
| 214 | checkFlowOperation(op1, FlowRuleOperation.Type.REMOVE, d2p0.deviceId()); |
| 215 | |
| 216 | FlowRuleOperation op2 = flowRuleOps[2]; |
| 217 | checkFlowOperation(op2, FlowRuleOperation.Type.REMOVE, d3p0.deviceId()); |
| 218 | |
| 219 | installer.deactivate(); |
| 220 | } |
| 221 | } |