blob: 595afd42f24fa5aa519cb85d1b63f09de12547cd [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);
yoonseon322c9c32016-12-07 16:47:02 -0800110 setField(manager, "coreService", coreService);
Brian Stanke11f6d532016-07-05 16:17:59 -0400111 NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher());
112 manager.activate();
113
114 // Register a compiler and an installer both setup for success.
115 intentExtensionService = intentService;
116
117 testDirectory = new TestServiceDirectory()
118 .add(VirtualNetworkService.class, manager)
119 .add(VirtualNetworkStore.class, virtualNetworkManagerStore)
120 .add(IntentService.class, intentService);
yoonseonc6a69272017-01-12 18:22:20 -0800121 setField(manager, "serviceDirectory", testDirectory);
Brian Stanke11f6d532016-07-05 16:17:59 -0400122
123 compiler = new VirtualNetworkIntentCompiler();
124 compiler.manager = manager;
125 compiler.intentService = intentService;
126 compiler.store = virtualNetworkManagerStore;
127 compiler.intentManager = intentExtensionService;
128 compiler.serviceDirectory = testDirectory;
129 }
130
131 @After
132 public void tearDown() {
133 Intent.unbindIdGenerator(idGenerator);
134 manager.deactivate();
135 }
136
137 /**
138 * Method to create the virtual network for further testing.
139 *
140 * @return virtual network
141 */
142 private VirtualNetwork setupVirtualNetworkTopology() {
143 manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
144 VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
145 VirtualDevice virtualDevice1 =
146 manager.createVirtualDevice(virtualNetwork.id(), DID1);
147 VirtualDevice virtualDevice2 =
148 manager.createVirtualDevice(virtualNetwork.id(), DID2);
149 VirtualDevice virtualDevice3 =
150 manager.createVirtualDevice(virtualNetwork.id(), DID3);
151 VirtualDevice virtualDevice4 =
152 manager.createVirtualDevice(virtualNetwork.id(), DID4);
153
154 Port port1 = new DefaultPort(virtualDevice1, PortNumber.portNumber(1), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400155 cp1 = new ConnectPoint(virtualDevice1.id(), port1.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700156 manager.createVirtualPort(virtualNetwork.id(), virtualDevice1.id(), port1.number(), cp1);
Brian Stanke11f6d532016-07-05 16:17:59 -0400157
158 Port port2 = new DefaultPort(virtualDevice1, PortNumber.portNumber(2), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400159 cp2 = new ConnectPoint(virtualDevice1.id(), port2.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700160 manager.createVirtualPort(virtualNetwork.id(), virtualDevice1.id(), port2.number(), cp2);
Brian Stanke11f6d532016-07-05 16:17:59 -0400161
162 Port port3 = new DefaultPort(virtualDevice2, PortNumber.portNumber(3), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400163 cp3 = new ConnectPoint(virtualDevice2.id(), port3.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700164 manager.createVirtualPort(virtualNetwork.id(), virtualDevice2.id(), port3.number(), cp3);
Brian Stanke11f6d532016-07-05 16:17:59 -0400165
166 Port port4 = new DefaultPort(virtualDevice2, PortNumber.portNumber(4), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400167 cp4 = new ConnectPoint(virtualDevice2.id(), port4.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700168 manager.createVirtualPort(virtualNetwork.id(), virtualDevice2.id(), port4.number(), cp4);
Brian Stanke11f6d532016-07-05 16:17:59 -0400169
170 Port port5 = new DefaultPort(virtualDevice3, PortNumber.portNumber(5), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400171 cp5 = new ConnectPoint(virtualDevice3.id(), port5.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700172 manager.createVirtualPort(virtualNetwork.id(), virtualDevice3.id(), port5.number(), cp5);
Brian Stanke11f6d532016-07-05 16:17:59 -0400173
174 Port port6 = new DefaultPort(virtualDevice3, PortNumber.portNumber(6), true);
Brian Stanke11f6d532016-07-05 16:17:59 -0400175 cp6 = new ConnectPoint(virtualDevice3.id(), port6.number());
Yoonseon Han6c603892016-09-01 11:52:21 -0700176 manager.createVirtualPort(virtualNetwork.id(), virtualDevice3.id(), port6.number(), cp6);
Brian Stanke11f6d532016-07-05 16:17:59 -0400177
178 link1 = manager.createVirtualLink(virtualNetwork.id(), cp1, cp3);
179 virtualNetworkManagerStore.updateLink(link1, link1.tunnelId(), Link.State.ACTIVE);
180 link2 = manager.createVirtualLink(virtualNetwork.id(), cp3, cp1);
181 virtualNetworkManagerStore.updateLink(link2, link2.tunnelId(), Link.State.ACTIVE);
182 link3 = manager.createVirtualLink(virtualNetwork.id(), cp4, cp5);
183 virtualNetworkManagerStore.updateLink(link3, link3.tunnelId(), Link.State.ACTIVE);
184 link4 = manager.createVirtualLink(virtualNetwork.id(), cp5, cp4);
185 virtualNetworkManagerStore.updateLink(link4, link4.tunnelId(), Link.State.ACTIVE);
186
187 return virtualNetwork;
188 }
189
Brian Stankefb61df42016-07-25 11:47:51 -0400190 /**
191 * Tests the virtual network intent compiler.
192 */
Brian Stanke11f6d532016-07-05 16:17:59 -0400193 @Test
194 public void testCompiler() {
195 compiler.activate();
196 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
197
198 Key intentKey = Key.of("test", APP_ID);
199
Brian Stanke11f6d532016-07-05 16:17:59 -0400200 VirtualNetworkIntent virtualIntent = VirtualNetworkIntent.builder()
201 .networkId(virtualNetwork.id())
202 .key(intentKey)
203 .appId(APP_ID)
Brian Stankefb61df42016-07-25 11:47:51 -0400204 .ingressPoint(cp2)
205 .egressPoint(cp6)
Brian Stanke11f6d532016-07-05 16:17:59 -0400206 .build();
207
208 List<Intent> compiled = compiler.compile(virtualIntent, Collections.emptyList());
Brian Stankefb61df42016-07-25 11:47:51 -0400209 assertEquals("The virtual intents size is not as expected.", 5, compiled.size());
Brian Stanke11f6d532016-07-05 16:17:59 -0400210
211 compiler.deactivate();
212 }
213
214
215 /**
216 * Core service test class.
217 */
218 private class TestCoreService extends CoreServiceAdapter {
219
220 @Override
221 public IdGenerator getIdGenerator(String topic) {
222 return new IdGenerator() {
223 private AtomicLong counter = new AtomicLong(0);
224
225 @Override
226 public long getNewId() {
227 return counter.getAndIncrement();
228 }
229 };
230 }
231 }
232
233}