Brian Stanke

ONOS-2184 - Removing obsolete PtToPtIntentVirtualNetworkProvider.

Change-Id: Ib1266e0088fea60f403cb6c90c3dca4c084a4afe
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.incubator.net.virtual.impl;
18 -
19 -
20 -import org.apache.felix.scr.annotations.Activate;
21 -import org.apache.felix.scr.annotations.Component;
22 -import org.apache.felix.scr.annotations.Deactivate;
23 -import org.apache.felix.scr.annotations.Reference;
24 -import org.apache.felix.scr.annotations.ReferenceCardinality;
25 -import org.apache.felix.scr.annotations.Service;
26 -import org.onosproject.core.ApplicationId;
27 -import org.onosproject.core.CoreService;
28 -import org.onosproject.incubator.net.tunnel.TunnelId;
29 -import org.onosproject.incubator.net.virtual.DefaultVirtualLink;
30 -import org.onosproject.incubator.net.virtual.NetworkId;
31 -import org.onosproject.incubator.net.virtual.VirtualNetworkProvider;
32 -import org.onosproject.incubator.net.virtual.VirtualNetworkProviderRegistry;
33 -import org.onosproject.incubator.net.virtual.VirtualNetworkProviderService;
34 -import org.onosproject.net.ConnectPoint;
35 -import org.onosproject.net.EncapsulationType;
36 -import org.onosproject.net.intent.Constraint;
37 -import org.onosproject.net.intent.Intent;
38 -import org.onosproject.net.intent.IntentEvent;
39 -import org.onosproject.net.intent.IntentListener;
40 -import org.onosproject.net.intent.IntentService;
41 -import org.onosproject.net.intent.Key;
42 -import org.onosproject.net.intent.PointToPointIntent;
43 -import org.onosproject.net.intent.constraint.EncapsulationConstraint;
44 -import org.onosproject.net.provider.AbstractProvider;
45 -import org.slf4j.Logger;
46 -
47 -import java.util.ArrayList;
48 -import java.util.List;
49 -import java.util.StringTokenizer;
50 -
51 -import static com.google.common.base.Preconditions.checkNotNull;
52 -import static org.slf4j.LoggerFactory.getLogger;
53 -
54 -/**
55 - * Point to point intent VirtualNetworkProvider implementation.
56 - */
57 -@Component(immediate = true)
58 -@Service
59 -public class PtToPtIntentVirtualNetworkProvider extends AbstractProvider
60 - implements VirtualNetworkProvider {
61 -
62 - private final Logger log = getLogger(PtToPtIntentVirtualNetworkProvider.class);
63 - private static final String NETWORK_ID_NULL = "Network ID cannot be null";
64 - private static final String CONNECT_POINT_NULL = "Connect Point cannot be null";
65 - private static final String INTENT_NULL = "Intent cannot be null";
66 - private static final String NETWORK_ID = "networkId=";
67 - protected static final String KEY_FORMAT = NETWORK_ID + "%s, src=%s, dst=%s";
68 - private static final int MAX_WAIT_COUNT = 30;
69 -
70 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 - protected VirtualNetworkProviderRegistry providerRegistry;
72 -
73 - private VirtualNetworkProviderService providerService;
74 -
75 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
76 - protected IntentService intentService;
77 -
78 - private final InternalPtPtIntentListener intentListener = new InternalPtPtIntentListener();
79 -
80 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 - protected CoreService coreService;
82 -
83 - protected static final String PTPT_INTENT_APPID = "org.onosproject.vnet.intent";
84 - private ApplicationId appId;
85 -
86 - /**
87 - * Default constructor.
88 - */
89 - public PtToPtIntentVirtualNetworkProvider() {
90 - super(DefaultVirtualLink.PID);
91 - }
92 -
93 - @Activate
94 - public void activate() {
95 - providerService = providerRegistry.register(this);
96 - appId = coreService.registerApplication(PTPT_INTENT_APPID);
97 -
98 - intentService.addListener(intentListener);
99 - log.info("Started");
100 - }
101 -
102 - @Deactivate
103 - public void deactivate() {
104 - intentService.removeListener(intentListener);
105 - providerRegistry.unregister(this);
106 - providerService = null;
107 - log.info("Stopped");
108 - }
109 -
110 - @Override
111 - public boolean isTraversable(ConnectPoint src, ConnectPoint dst) {
112 - return false;
113 - }
114 -
115 - @Override
116 - public TunnelId createTunnel(NetworkId networkId, ConnectPoint src, ConnectPoint dst) {
117 - checkNotNull(networkId, NETWORK_ID_NULL);
118 - checkNotNull(src, CONNECT_POINT_NULL);
119 - checkNotNull(dst, CONNECT_POINT_NULL);
120 - Key intentKey = encodeKey(networkId, src, dst);
121 -
122 - List<Constraint> constraints = new ArrayList<>();
123 - constraints.add(new EncapsulationConstraint(EncapsulationType.VLAN));
124 -
125 - // TODO Currently there can only be one tunnel/intent between the src and dst across
126 - // all virtual networks. We may want to support multiple intents between the same src/dst pairs.
127 - PointToPointIntent intent = PointToPointIntent.builder()
128 - .key(intentKey)
129 - .appId(appId)
130 - .ingressPoint(src)
131 - .egressPoint(dst)
132 - .constraints(constraints)
133 - .build();
134 - intentService.submit(intent);
135 -
136 - // construct tunnelId from the key
137 - return TunnelId.valueOf(intentKey.toString());
138 - }
139 -
140 - @Override
141 - public void destroyTunnel(NetworkId networkId, TunnelId tunnelId) {
142 - String key = tunnelId.id();
143 - Key intentKey = Key.of(key, appId);
144 - Intent intent = intentService.getIntent(intentKey);
145 - checkNotNull(intent, INTENT_NULL);
146 - intentService.withdraw(intent);
147 - }
148 -
149 - private NetworkId decodeNetworkIdFromKey(Key intentKey) {
150 - // Extract the network identifier from the intent key
151 - StringTokenizer tokenizer = new StringTokenizer(intentKey.toString(), ",");
152 - String networkIdString = tokenizer.nextToken().substring(NETWORK_ID.length());
153 - return NetworkId.networkId(Integer.valueOf(networkIdString));
154 - }
155 -
156 - private Key encodeKey(NetworkId networkId, ConnectPoint src, ConnectPoint dst) {
157 - String key = String.format(KEY_FORMAT, networkId, src, dst);
158 - return Key.of(key, appId);
159 - }
160 -
161 - private class InternalPtPtIntentListener implements IntentListener {
162 - @Override
163 - public void event(IntentEvent event) {
164 - PointToPointIntent intent = (PointToPointIntent) event.subject();
165 - Key intentKey = intent.key();
166 -
167 - // Ignore intent events that are not relevant.
168 - if (!isRelevant(event)) {
169 - return;
170 - }
171 -
172 - NetworkId networkId = decodeNetworkIdFromKey(intentKey);
173 - ConnectPoint src = intent.ingressPoint();
174 - ConnectPoint dst = intent.egressPoint();
175 -
176 - switch (event.type()) {
177 - case INSTALLED:
178 - providerService.tunnelUp(networkId, src, dst, TunnelId.valueOf(intentKey.toString()));
179 - break;
180 - case WITHDRAWN:
181 - intentService.purge(intent);
182 - // Fall through and notify the provider service that the tunnel is down
183 - // for both WITHDRAWN and FAILED intent event types.
184 - case FAILED:
185 - providerService.tunnelDown(networkId, src, dst, TunnelId.valueOf(intentKey.toString()));
186 - break;
187 - case INSTALL_REQ:
188 - case CORRUPT:
189 - case PURGED:
190 - break; // Not sure what do with these events, ignore for now.
191 - default:
192 - break;
193 - }
194 - }
195 -
196 - @Override
197 - public boolean isRelevant(IntentEvent event) {
198 - if (event.subject() instanceof PointToPointIntent) {
199 - PointToPointIntent intent = (PointToPointIntent) event.subject();
200 -
201 - // Only events that are for this appId are relevent.
202 - return intent.appId().equals(appId);
203 - }
204 - return false;
205 - }
206 - }
207 -}
208 -
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.incubator.net.virtual.impl;
18 -
19 -import com.google.common.collect.Lists;
20 -import org.junit.After;
21 -import org.junit.Before;
22 -import org.junit.Test;
23 -import org.onosproject.TestApplicationId;
24 -import org.onosproject.core.ApplicationId;
25 -import org.onosproject.core.CoreService;
26 -import org.onosproject.core.CoreServiceAdapter;
27 -import org.onosproject.core.IdGenerator;
28 -import org.onosproject.incubator.net.tunnel.TunnelId;
29 -import org.onosproject.incubator.net.virtual.NetworkId;
30 -import org.onosproject.incubator.net.virtual.VirtualNetworkProvider;
31 -import org.onosproject.incubator.net.virtual.VirtualNetworkProviderRegistry;
32 -import org.onosproject.incubator.net.virtual.VirtualNetworkProviderService;
33 -import org.onosproject.net.ConnectPoint;
34 -import org.onosproject.net.DeviceId;
35 -import org.onosproject.net.PortNumber;
36 -import org.onosproject.net.intent.FakeIntentManager;
37 -import org.onosproject.net.intent.FlowRuleIntent;
38 -import org.onosproject.net.intent.Intent;
39 -import org.onosproject.net.intent.IntentCompiler;
40 -import org.onosproject.net.intent.IntentEvent;
41 -import org.onosproject.net.intent.IntentExtensionService;
42 -import org.onosproject.net.intent.IntentListener;
43 -import org.onosproject.net.intent.IntentTestsMocks;
44 -import org.onosproject.net.intent.MockIdGenerator;
45 -import org.onosproject.net.intent.PointToPointIntent;
46 -import org.onosproject.net.intent.TestableIntentService;
47 -import org.onosproject.net.provider.AbstractProviderService;
48 -import org.onosproject.net.provider.ProviderId;
49 -
50 -import java.util.Collections;
51 -import java.util.List;
52 -import java.util.Set;
53 -import java.util.concurrent.Semaphore;
54 -import java.util.concurrent.TimeUnit;
55 -import java.util.concurrent.atomic.AtomicLong;
56 -
57 -import static org.easymock.EasyMock.*;
58 -import static org.junit.Assert.*;
59 -
60 -/**
61 - * Junit tests for PtToPtIntentVirtualNetworkProvider.
62 - */
63 -public class PtToPtIntentVirtualNetworkProviderTest {
64 -
65 - private PtToPtIntentVirtualNetworkProvider provider;
66 - private VirtualNetworkProviderRegistry providerRegistry;
67 -
68 - private final VirtualNetworkRegistryAdapter virtualNetworkRegistry = new VirtualNetworkRegistryAdapter();
69 - private TestableIntentService intentService = new FakeIntentManager();
70 - private TestListener listener = new TestListener();
71 - protected TestIntentCompiler compiler = new TestIntentCompiler();
72 - private IntentExtensionService intentExtensionService;
73 -
74 - private static final ApplicationId APP_ID =
75 - TestApplicationId.create(PtToPtIntentVirtualNetworkProvider.PTPT_INTENT_APPID);
76 -
77 - private IdGenerator idGenerator = new MockIdGenerator();
78 - private static final int MAX_WAIT_TIME = 5;
79 - private static final int MAX_PERMITS = 2;
80 - private static Semaphore created;
81 - private static Semaphore removed;
82 -
83 - @Before
84 - public void setUp() {
85 - provider = new PtToPtIntentVirtualNetworkProvider();
86 - provider.providerRegistry = virtualNetworkRegistry;
87 - final CoreService mockCoreService = createMock(CoreService.class);
88 - provider.coreService = mockCoreService;
89 - expect(mockCoreService.registerApplication(PtToPtIntentVirtualNetworkProvider.PTPT_INTENT_APPID))
90 - .andReturn(APP_ID).anyTimes();
91 - replay(mockCoreService);
92 - Intent.unbindIdGenerator(idGenerator);
93 - Intent.bindIdGenerator(idGenerator);
94 -
95 - intentService.addListener(listener);
96 - provider.intentService = intentService;
97 -
98 - // Register a compiler and an installer both setup for success.
99 - intentExtensionService = intentService;
100 - intentExtensionService.registerCompiler(PointToPointIntent.class, compiler);
101 -
102 - provider.activate();
103 - created = new Semaphore(0, true);
104 - removed = new Semaphore(0, true);
105 - }
106 -
107 - @After
108 - public void tearDown() {
109 - Intent.unbindIdGenerator(idGenerator);
110 - intentService.removeListener(listener);
111 - provider.deactivate();
112 - provider.providerRegistry = null;
113 - provider.coreService = null;
114 - provider.intentService = null;
115 - created = null;
116 - removed = null;
117 - }
118 -
119 - @Test
120 - public void basics() {
121 - assertNotNull("registration expected", provider);
122 - }
123 -
124 - /**
125 - * Test a null network identifier.
126 - */
127 - @Test(expected = NullPointerException.class)
128 - public void testCreateTunnelNullNetworkId() {
129 - provider.createTunnel(null, null, null);
130 - }
131 -
132 - /**
133 - * Test a null source connect point.
134 - */
135 - @Test(expected = NullPointerException.class)
136 - public void testCreateTunnelNullSrc() {
137 - ConnectPoint dst = new ConnectPoint(DeviceId.deviceId("device2"), PortNumber.portNumber(2));
138 -
139 - provider.createTunnel(NetworkId.networkId(0), null, dst);
140 - }
141 -
142 - /**
143 - * Test a null destination connect point.
144 - */
145 - @Test(expected = NullPointerException.class)
146 - public void testCreateTunnelNullDst() {
147 - ConnectPoint src = new ConnectPoint(DeviceId.deviceId("device1"), PortNumber.portNumber(1));
148 -
149 - provider.createTunnel(NetworkId.networkId(0), src, null);
150 - }
151 -
152 - /**
153 - * Test creating/destroying a valid tunnel.
154 - */
155 - @Test
156 - public void testCreateRemoveTunnel() {
157 - NetworkId networkId = NetworkId.networkId(0);
158 - ConnectPoint src = new ConnectPoint(DeviceId.deviceId("device1"), PortNumber.portNumber(1));
159 - ConnectPoint dst = new ConnectPoint(DeviceId.deviceId("device2"), PortNumber.portNumber(2));
160 -
161 - TunnelId tunnelId = provider.createTunnel(networkId, src, dst);
162 -
163 - // Wait for the tunnel to go into an INSTALLED state, and that the tunnelUp method was called.
164 - try {
165 - if (!created.tryAcquire(MAX_PERMITS, MAX_WAIT_TIME, TimeUnit.SECONDS)) {
166 - fail("Failed to wait for tunnel to get installed.");
167 - }
168 - } catch (InterruptedException e) {
169 - fail("Semaphore exception during tunnel installation." + e.getMessage());
170 - }
171 -
172 - String key = String.format(PtToPtIntentVirtualNetworkProvider.KEY_FORMAT,
173 - networkId.toString(), src.toString(), dst.toString());
174 -
175 - assertEquals("TunnelId does not match as expected.", key, tunnelId.toString());
176 - provider.destroyTunnel(networkId, tunnelId);
177 -
178 - // Wait for the tunnel to go into a WITHDRAWN state, and that the tunnelDown method was called.
179 - try {
180 - if (!removed.tryAcquire(MAX_PERMITS, MAX_WAIT_TIME, TimeUnit.SECONDS)) {
181 - fail("Failed to wait for tunnel to get removed.");
182 - }
183 - } catch (InterruptedException e) {
184 - fail("Semaphore exception during tunnel removal." + e.getMessage());
185 - }
186 - }
187 -
188 - /**
189 - * Virtual network registry implementation for this test class.
190 - */
191 - private class VirtualNetworkRegistryAdapter implements VirtualNetworkProviderRegistry {
192 - private VirtualNetworkProvider provider;
193 -
194 - @Override
195 - public VirtualNetworkProviderService register(VirtualNetworkProvider theProvider) {
196 - this.provider = theProvider;
197 - return new TestVirtualNetworkProviderService(theProvider);
198 - }
199 -
200 - @Override
201 - public void unregister(VirtualNetworkProvider theProvider) {
202 - this.provider = null;
203 - }
204 -
205 - @Override
206 - public Set<ProviderId> getProviders() {
207 - return null;
208 - }
209 - }
210 -
211 - /**
212 - * Virtual network provider service implementation for this test class.
213 - */
214 - private class TestVirtualNetworkProviderService
215 - extends AbstractProviderService<VirtualNetworkProvider>
216 - implements VirtualNetworkProviderService {
217 -
218 - protected TestVirtualNetworkProviderService(VirtualNetworkProvider provider) {
219 - super(provider);
220 - }
221 -
222 - @Override
223 - public void topologyChanged(Set<Set<ConnectPoint>> clusters) {
224 -
225 - }
226 -
227 - @Override
228 - public void tunnelUp(NetworkId networkId, ConnectPoint src, ConnectPoint dst, TunnelId tunnelId) {
229 - // Release one permit on the created semaphore since the tunnelUp method was called.
230 - created.release();
231 - }
232 -
233 - @Override
234 - public void tunnelDown(NetworkId networkId, ConnectPoint src, ConnectPoint dst, TunnelId tunnelId) {
235 - // Release one permit on the removed semaphore since the tunnelDown method was called.
236 - removed.release();
237 - }
238 - }
239 -
240 - private static class TestListener implements IntentListener {
241 -
242 - @Override
243 - public void event(IntentEvent event) {
244 - switch (event.type()) {
245 - case INSTALLED:
246 - // Release one permit on the created semaphore since the Intent event was received.
247 - created.release();
248 - break;
249 - case WITHDRAWN:
250 - // Release one permit on the removed semaphore since the Intent event was received.
251 - removed.release();
252 - break;
253 - default:
254 - break;
255 - }
256 - }
257 - }
258 -
259 - /**
260 - * Core service test class.
261 - */
262 - private class TestCoreService extends CoreServiceAdapter {
263 -
264 - @Override
265 - public IdGenerator getIdGenerator(String topic) {
266 - return new IdGenerator() {
267 - private AtomicLong counter = new AtomicLong(0);
268 -
269 - @Override
270 - public long getNewId() {
271 - return counter.getAndIncrement();
272 - }
273 - };
274 - }
275 - }
276 -
277 - private static class TestIntentCompiler implements IntentCompiler<PointToPointIntent> {
278 - @Override
279 - public List<Intent> compile(PointToPointIntent intent, List<Intent> installable) {
280 - return Lists.newArrayList(new MockInstallableIntent());
281 - }
282 - }
283 -
284 - private static class MockInstallableIntent extends FlowRuleIntent {
285 -
286 - public MockInstallableIntent() {
287 - super(APP_ID, Collections.singletonList(new IntentTestsMocks.MockFlowRule(100)), Collections.emptyList());
288 - }
289 - }
290 -}
...@@ -81,7 +81,7 @@ public class VirtualNetworkIntentServiceTest extends TestDeviceParams { ...@@ -81,7 +81,7 @@ public class VirtualNetworkIntentServiceTest extends TestDeviceParams {
81 81
82 private final String tenantIdValue1 = "TENANT_ID1"; 82 private final String tenantIdValue1 = "TENANT_ID1";
83 private static final ApplicationId APP_ID = 83 private static final ApplicationId APP_ID =
84 - new TestApplicationId(PtToPtIntentVirtualNetworkProvider.PTPT_INTENT_APPID); 84 + new TestApplicationId("MyAppId");
85 85
86 private ConnectPoint cp1; 86 private ConnectPoint cp1;
87 private ConnectPoint cp2; 87 private ConnectPoint cp2;
......