Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 17 | package org.onosproject.net.intent.impl.compiler; |
| 18 | |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.osgi.ServiceDirectory; |
| 23 | import org.onlab.osgi.TestServiceDirectory; |
| 24 | import org.onlab.rest.BaseResource; |
| 25 | import org.onosproject.TestApplicationId; |
| 26 | import org.onosproject.common.event.impl.TestEventDispatcher; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
| 29 | import org.onosproject.core.CoreServiceAdapter; |
| 30 | import org.onosproject.core.IdGenerator; |
| 31 | import org.onosproject.incubator.net.virtual.TenantId; |
| 32 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
| 33 | import org.onosproject.incubator.net.virtual.VirtualLink; |
| 34 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 35 | import org.onosproject.incubator.net.virtual.VirtualNetworkIntent; |
| 36 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 37 | import org.onosproject.incubator.net.virtual.VirtualNetworkStore; |
| 38 | import org.onosproject.incubator.net.virtual.impl.VirtualNetworkManager; |
| 39 | import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore; |
| 40 | import org.onosproject.net.ConnectPoint; |
| 41 | import org.onosproject.net.DefaultPort; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 42 | import org.onosproject.net.Link; |
| 43 | import org.onosproject.net.NetTestTools; |
| 44 | import org.onosproject.net.Port; |
| 45 | import org.onosproject.net.PortNumber; |
| 46 | import org.onosproject.net.TestDeviceParams; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 47 | import org.onosproject.net.intent.FakeIntentManager; |
| 48 | import org.onosproject.net.intent.Intent; |
| 49 | import org.onosproject.net.intent.IntentExtensionService; |
| 50 | import org.onosproject.net.intent.IntentService; |
| 51 | import org.onosproject.net.intent.Key; |
| 52 | import org.onosproject.net.intent.MockIdGenerator; |
| 53 | import org.onosproject.net.intent.TestableIntentService; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 54 | import org.onosproject.store.service.TestStorageService; |
| 55 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 56 | import java.util.Collections; |
| 57 | import java.util.List; |
| 58 | import java.util.concurrent.atomic.AtomicLong; |
| 59 | |
| 60 | import static org.junit.Assert.assertEquals; |
| 61 | import static org.onlab.junit.TestUtils.TestUtilsException; |
| 62 | import static org.onlab.junit.TestUtils.setField; |
| 63 | |
| 64 | /** |
| 65 | * Junit tests for virtual network intent compiler. |
| 66 | */ |
| 67 | public class VirtualNetworkIntentCompilerTest extends TestDeviceParams { |
| 68 | |
| 69 | private CoreService coreService; |
| 70 | private TestableIntentService intentService = new FakeIntentManager(); |
| 71 | private IntentExtensionService intentExtensionService; |
| 72 | private final IdGenerator idGenerator = new MockIdGenerator(); |
| 73 | private VirtualNetworkIntentCompiler compiler; |
| 74 | private VirtualNetworkManager manager; |
| 75 | private DistributedVirtualNetworkStore virtualNetworkManagerStore; |
| 76 | private ServiceDirectory testDirectory; |
| 77 | |
| 78 | private final String tenantIdValue1 = "TENANT_ID1"; |
| 79 | private static final ApplicationId APP_ID = |
| 80 | new TestApplicationId("test"); |
| 81 | |
| 82 | private ConnectPoint cp1; |
| 83 | private ConnectPoint cp2; |
| 84 | private ConnectPoint cp3; |
| 85 | private ConnectPoint cp4; |
| 86 | private ConnectPoint cp5; |
| 87 | private ConnectPoint cp6; |
| 88 | private VirtualLink link1; |
| 89 | private VirtualLink link2; |
| 90 | private VirtualLink link3; |
| 91 | private VirtualLink link4; |
| 92 | private VirtualLink link5; |
| 93 | private VirtualLink link6; |
| 94 | |
| 95 | @Before |
| 96 | public void setUp() throws TestUtilsException { |
| 97 | virtualNetworkManagerStore = new DistributedVirtualNetworkStore(); |
| 98 | |
| 99 | coreService = new TestCoreService(); |
| 100 | |
| 101 | Intent.unbindIdGenerator(idGenerator); |
| 102 | Intent.bindIdGenerator(idGenerator); |
| 103 | |
| 104 | virtualNetworkManagerStore.setCoreService(coreService); |
| 105 | setField(coreService, "coreService", new TestCoreService()); |
| 106 | setField(virtualNetworkManagerStore, "storageService", new TestStorageService()); |
| 107 | virtualNetworkManagerStore.activate(); |
| 108 | |
| 109 | manager = new VirtualNetworkManager(); |
| 110 | manager.setStore(virtualNetworkManagerStore); |
| 111 | manager.setIntentService(intentService); |
| 112 | NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher()); |
| 113 | manager.activate(); |
| 114 | |
| 115 | // Register a compiler and an installer both setup for success. |
| 116 | intentExtensionService = intentService; |
| 117 | |
| 118 | testDirectory = new TestServiceDirectory() |
| 119 | .add(VirtualNetworkService.class, manager) |
| 120 | .add(VirtualNetworkStore.class, virtualNetworkManagerStore) |
| 121 | .add(IntentService.class, intentService); |
| 122 | BaseResource.setServiceDirectory(testDirectory); |
| 123 | |
| 124 | compiler = new VirtualNetworkIntentCompiler(); |
| 125 | compiler.manager = manager; |
| 126 | compiler.intentService = intentService; |
| 127 | compiler.store = virtualNetworkManagerStore; |
| 128 | compiler.intentManager = intentExtensionService; |
| 129 | compiler.serviceDirectory = testDirectory; |
| 130 | } |
| 131 | |
| 132 | @After |
| 133 | public void tearDown() { |
| 134 | Intent.unbindIdGenerator(idGenerator); |
| 135 | manager.deactivate(); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Method to create the virtual network for further testing. |
| 140 | * |
| 141 | * @return virtual network |
| 142 | */ |
| 143 | private VirtualNetwork setupVirtualNetworkTopology() { |
| 144 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 145 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 146 | VirtualDevice virtualDevice1 = |
| 147 | manager.createVirtualDevice(virtualNetwork.id(), DID1); |
| 148 | VirtualDevice virtualDevice2 = |
| 149 | manager.createVirtualDevice(virtualNetwork.id(), DID2); |
| 150 | VirtualDevice virtualDevice3 = |
| 151 | manager.createVirtualDevice(virtualNetwork.id(), DID3); |
| 152 | VirtualDevice virtualDevice4 = |
| 153 | manager.createVirtualDevice(virtualNetwork.id(), DID4); |
| 154 | |
| 155 | Port port1 = new DefaultPort(virtualDevice1, PortNumber.portNumber(1), true); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 156 | cp1 = new ConnectPoint(virtualDevice1.id(), port1.number()); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 157 | manager.createVirtualPort(virtualNetwork.id(), virtualDevice1.id(), port1.number(), cp1); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 158 | |
| 159 | Port port2 = new DefaultPort(virtualDevice1, PortNumber.portNumber(2), true); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 160 | cp2 = new ConnectPoint(virtualDevice1.id(), port2.number()); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 161 | manager.createVirtualPort(virtualNetwork.id(), virtualDevice1.id(), port2.number(), cp2); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 162 | |
| 163 | Port port3 = new DefaultPort(virtualDevice2, PortNumber.portNumber(3), true); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 164 | cp3 = new ConnectPoint(virtualDevice2.id(), port3.number()); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 165 | manager.createVirtualPort(virtualNetwork.id(), virtualDevice2.id(), port3.number(), cp3); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 166 | |
| 167 | Port port4 = new DefaultPort(virtualDevice2, PortNumber.portNumber(4), true); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 168 | cp4 = new ConnectPoint(virtualDevice2.id(), port4.number()); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 169 | manager.createVirtualPort(virtualNetwork.id(), virtualDevice2.id(), port4.number(), cp4); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 170 | |
| 171 | Port port5 = new DefaultPort(virtualDevice3, PortNumber.portNumber(5), true); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 172 | cp5 = new ConnectPoint(virtualDevice3.id(), port5.number()); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 173 | manager.createVirtualPort(virtualNetwork.id(), virtualDevice3.id(), port5.number(), cp5); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 174 | |
| 175 | Port port6 = new DefaultPort(virtualDevice3, PortNumber.portNumber(6), true); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 176 | cp6 = new ConnectPoint(virtualDevice3.id(), port6.number()); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 177 | manager.createVirtualPort(virtualNetwork.id(), virtualDevice3.id(), port6.number(), cp6); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 178 | |
| 179 | link1 = manager.createVirtualLink(virtualNetwork.id(), cp1, cp3); |
| 180 | virtualNetworkManagerStore.updateLink(link1, link1.tunnelId(), Link.State.ACTIVE); |
| 181 | link2 = manager.createVirtualLink(virtualNetwork.id(), cp3, cp1); |
| 182 | virtualNetworkManagerStore.updateLink(link2, link2.tunnelId(), Link.State.ACTIVE); |
| 183 | link3 = manager.createVirtualLink(virtualNetwork.id(), cp4, cp5); |
| 184 | virtualNetworkManagerStore.updateLink(link3, link3.tunnelId(), Link.State.ACTIVE); |
| 185 | link4 = manager.createVirtualLink(virtualNetwork.id(), cp5, cp4); |
| 186 | virtualNetworkManagerStore.updateLink(link4, link4.tunnelId(), Link.State.ACTIVE); |
| 187 | |
| 188 | return virtualNetwork; |
| 189 | } |
| 190 | |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 191 | /** |
| 192 | * Tests the virtual network intent compiler. |
| 193 | */ |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 194 | @Test |
| 195 | public void testCompiler() { |
| 196 | compiler.activate(); |
| 197 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 198 | |
| 199 | Key intentKey = Key.of("test", APP_ID); |
| 200 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 201 | VirtualNetworkIntent virtualIntent = VirtualNetworkIntent.builder() |
| 202 | .networkId(virtualNetwork.id()) |
| 203 | .key(intentKey) |
| 204 | .appId(APP_ID) |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 205 | .ingressPoint(cp2) |
| 206 | .egressPoint(cp6) |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 207 | .build(); |
| 208 | |
| 209 | List<Intent> compiled = compiler.compile(virtualIntent, Collections.emptyList()); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 210 | assertEquals("The virtual intents size is not as expected.", 5, compiled.size()); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 211 | |
| 212 | compiler.deactivate(); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | /** |
| 217 | * Core service test class. |
| 218 | */ |
| 219 | private class TestCoreService extends CoreServiceAdapter { |
| 220 | |
| 221 | @Override |
| 222 | public IdGenerator getIdGenerator(String topic) { |
| 223 | return new IdGenerator() { |
| 224 | private AtomicLong counter = new AtomicLong(0); |
| 225 | |
| 226 | @Override |
| 227 | public long getNewId() { |
| 228 | return counter.getAndIncrement(); |
| 229 | } |
| 230 | }; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | } |