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