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