blob: f7be74407a2c2753300909b9f3e68c58c9c8708a [file] [log] [blame]
jiangruif675cd42015-11-27 15:03:59 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
jiangruif675cd42015-11-27 15:03:59 +08003 *
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 */
16package org.onosproject.vtnrsc.service.impl;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19import static org.slf4j.LoggerFactory.getLogger;
20
21import java.util.HashSet;
22import java.util.Iterator;
23import java.util.Set;
24
25import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
30import org.apache.felix.scr.annotations.Service;
31import org.onlab.packet.IpAddress;
32import org.onlab.packet.MacAddress;
33import org.onlab.util.KryoNamespace;
34import org.onosproject.core.CoreService;
Mahesh Poojary Scc11f722015-11-29 16:09:56 +053035import org.onosproject.event.AbstractListenerManager;
jiangruif675cd42015-11-27 15:03:59 +080036import org.onosproject.net.Device;
37import org.onosproject.net.DeviceId;
38import org.onosproject.net.Host;
39import org.onosproject.net.HostId;
jiangruif675cd42015-11-27 15:03:59 +080040import org.onosproject.net.device.DeviceService;
lishuaib43dbf72016-01-06 11:11:35 +080041import org.onosproject.net.host.HostService;
jiangruif675cd42015-11-27 15:03:59 +080042import org.onosproject.store.serializers.KryoNamespaces;
43import org.onosproject.store.service.EventuallyConsistentMap;
44import org.onosproject.store.service.LogicalClockService;
45import org.onosproject.store.service.StorageService;
46import org.onosproject.vtnrsc.FixedIp;
47import org.onosproject.vtnrsc.FloatingIp;
lishuaib43dbf72016-01-06 11:11:35 +080048import org.onosproject.vtnrsc.FlowClassifier;
49import org.onosproject.vtnrsc.PortChain;
50import org.onosproject.vtnrsc.PortPair;
51import org.onosproject.vtnrsc.PortPairGroup;
52import org.onosproject.vtnrsc.PortPairId;
jiangruif675cd42015-11-27 15:03:59 +080053import org.onosproject.vtnrsc.Router;
Wu wenbinacc10ea2016-05-06 16:48:56 +080054import org.onosproject.vtnrsc.RouterId;
jiangruif675cd42015-11-27 15:03:59 +080055import org.onosproject.vtnrsc.RouterInterface;
56import org.onosproject.vtnrsc.SegmentationId;
57import org.onosproject.vtnrsc.Subnet;
58import org.onosproject.vtnrsc.SubnetId;
59import org.onosproject.vtnrsc.TenantId;
lishuai8798bbe2016-05-05 16:02:03 +080060import org.onosproject.vtnrsc.TenantRouter;
jiangruif675cd42015-11-27 15:03:59 +080061import org.onosproject.vtnrsc.VirtualPort;
62import org.onosproject.vtnrsc.VirtualPortId;
63import org.onosproject.vtnrsc.event.VtnRscEvent;
64import org.onosproject.vtnrsc.event.VtnRscEventFeedback;
65import org.onosproject.vtnrsc.event.VtnRscListener;
66import org.onosproject.vtnrsc.floatingip.FloatingIpEvent;
67import org.onosproject.vtnrsc.floatingip.FloatingIpListener;
68import org.onosproject.vtnrsc.floatingip.FloatingIpService;
lishuaib43dbf72016-01-06 11:11:35 +080069import org.onosproject.vtnrsc.flowclassifier.FlowClassifierEvent;
70import org.onosproject.vtnrsc.flowclassifier.FlowClassifierListener;
71import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
72import org.onosproject.vtnrsc.portchain.PortChainEvent;
73import org.onosproject.vtnrsc.portchain.PortChainListener;
74import org.onosproject.vtnrsc.portchain.PortChainService;
75import org.onosproject.vtnrsc.portpair.PortPairEvent;
76import org.onosproject.vtnrsc.portpair.PortPairListener;
77import org.onosproject.vtnrsc.portpair.PortPairService;
78import org.onosproject.vtnrsc.portpairgroup.PortPairGroupEvent;
79import org.onosproject.vtnrsc.portpairgroup.PortPairGroupListener;
80import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
jiangruif675cd42015-11-27 15:03:59 +080081import org.onosproject.vtnrsc.router.RouterEvent;
82import org.onosproject.vtnrsc.router.RouterListener;
83import org.onosproject.vtnrsc.router.RouterService;
84import org.onosproject.vtnrsc.routerinterface.RouterInterfaceEvent;
85import org.onosproject.vtnrsc.routerinterface.RouterInterfaceListener;
86import org.onosproject.vtnrsc.routerinterface.RouterInterfaceService;
87import org.onosproject.vtnrsc.service.VtnRscService;
88import org.onosproject.vtnrsc.subnet.SubnetService;
89import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService;
Phaneendra Mandab212bc92016-07-08 16:50:11 +053090import org.onosproject.vtnrsc.virtualport.VirtualPortEvent;
91import org.onosproject.vtnrsc.virtualport.VirtualPortListener;
jiangruif675cd42015-11-27 15:03:59 +080092import org.onosproject.vtnrsc.virtualport.VirtualPortService;
93import org.slf4j.Logger;
94
jiangruif675cd42015-11-27 15:03:59 +080095/**
96 * Provides implementation of the VtnRsc service.
97 */
98@Component(immediate = true)
99@Service
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530100public class VtnRscManager extends AbstractListenerManager<VtnRscEvent, VtnRscListener>
101 implements VtnRscService {
jiangruif675cd42015-11-27 15:03:59 +0800102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
103 protected CoreService coreService;
104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
105 protected StorageService storageService;
106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
107 protected LogicalClockService clockService;
108
109 private final Logger log = getLogger(getClass());
jiangruif675cd42015-11-27 15:03:59 +0800110 private FloatingIpListener floatingIpListener = new InnerFloatingIpListener();
111 private RouterListener routerListener = new InnerRouterListener();
112 private RouterInterfaceListener routerInterfaceListener = new InnerRouterInterfaceListener();
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530113 private PortPairListener portPairListener = new InnerPortPairListener();
114 private PortPairGroupListener portPairGroupListener = new InnerPortPairGroupListener();
115 private FlowClassifierListener flowClassifierListener = new InnerFlowClassifierListener();
116 private PortChainListener portChainListener = new InnerPortChainListener();
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530117 private VirtualPortListener virtualPortListener = new InnerVirtualPortListener();
jiangruif675cd42015-11-27 15:03:59 +0800118
lishuai8798bbe2016-05-05 16:02:03 +0800119 private EventuallyConsistentMap<TenantId, SegmentationId> l3vniTenantMap;
120 private EventuallyConsistentMap<TenantRouter, SegmentationId> l3vniTenantRouterMap;
jiangruif675cd42015-11-27 15:03:59 +0800121 private EventuallyConsistentMap<TenantId, Set<DeviceId>> classifierOvsMap;
122 private EventuallyConsistentMap<TenantId, Set<DeviceId>> sffOvsMap;
123
124 private static final String IFACEID = "ifaceid";
125 private static final String RUNNELOPTOPOIC = "tunnel-ops-ids";
jiangruif675cd42015-11-27 15:03:59 +0800126 private static final String EVENT_NOT_NULL = "event cannot be null";
127 private static final String TENANTID_NOT_NULL = "tenantId cannot be null";
128 private static final String DEVICEID_NOT_NULL = "deviceId cannot be null";
lishuaib43dbf72016-01-06 11:11:35 +0800129 private static final String VIRTUALPORTID_NOT_NULL = "virtualPortId cannot be null";
130 private static final String HOST_NOT_NULL = "host cannot be null";
lishuai8798bbe2016-05-05 16:02:03 +0800131 private static final String L3VNITENANTMAP = "l3vniTenantMap";
132 private static final String L3VNITENANTROUTERMAP = "l3vniTenantRouterMap";
jiangruif675cd42015-11-27 15:03:59 +0800133 private static final String CLASSIFIEROVSMAP = "classifierOvsMap";
134 private static final String SFFOVSMAP = "sffOvsMap";
135
136 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
137 protected RouterService routerService;
138 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
139 protected FloatingIpService floatingIpService;
140 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
141 protected RouterInterfaceService routerInterfaceService;
142 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
143 protected VirtualPortService virtualPortService;
144 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
145 protected HostService hostService;
146 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
147 protected SubnetService subnetService;
148 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
149 protected TenantNetworkService tenantNetworkService;
150 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
151 protected DeviceService deviceService;
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530152 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
153 protected PortPairService portPairService;
154 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
155 protected PortPairGroupService portPairGroupService;
156 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
157 protected FlowClassifierService flowClassifierService;
158 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
159 protected PortChainService portChainService;
jiangruif675cd42015-11-27 15:03:59 +0800160
161 @Activate
162 public void activate() {
Phaneendra Manda5f326872015-12-07 19:25:09 +0530163 eventDispatcher.addSink(VtnRscEvent.class, listenerRegistry);
jiangruif675cd42015-11-27 15:03:59 +0800164 floatingIpService.addListener(floatingIpListener);
165 routerService.addListener(routerListener);
166 routerInterfaceService.addListener(routerInterfaceListener);
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530167 portPairService.addListener(portPairListener);
168 portPairGroupService.addListener(portPairGroupListener);
169 flowClassifierService.addListener(flowClassifierListener);
170 portChainService.addListener(portChainListener);
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530171 virtualPortService.addListener(virtualPortListener);
jiangruif675cd42015-11-27 15:03:59 +0800172
173 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
174 .register(KryoNamespaces.API)
Ray Milkey5dae9222017-06-15 11:55:51 -0700175 .register(TenantId.class, SegmentationId.class,
Wu wenbinacc10ea2016-05-06 16:48:56 +0800176 TenantRouter.class, RouterId.class);
lishuai8798bbe2016-05-05 16:02:03 +0800177 l3vniTenantMap = storageService
jiangruif675cd42015-11-27 15:03:59 +0800178 .<TenantId, SegmentationId>eventuallyConsistentMapBuilder()
lishuai8798bbe2016-05-05 16:02:03 +0800179 .withName(L3VNITENANTMAP).withSerializer(serializer)
180 .withTimestampProvider((k, v) -> clockService.getTimestamp())
181 .build();
182
183 l3vniTenantRouterMap = storageService
184 .<TenantRouter, SegmentationId>eventuallyConsistentMapBuilder()
185 .withName(L3VNITENANTROUTERMAP).withSerializer(serializer)
jiangruif675cd42015-11-27 15:03:59 +0800186 .withTimestampProvider((k, v) -> clockService.getTimestamp())
187 .build();
188
189 classifierOvsMap = storageService
190 .<TenantId, Set<DeviceId>>eventuallyConsistentMapBuilder()
191 .withName(CLASSIFIEROVSMAP).withSerializer(serializer)
192 .withTimestampProvider((k, v) -> clockService.getTimestamp())
193 .build();
194
195 sffOvsMap = storageService
196 .<TenantId, Set<DeviceId>>eventuallyConsistentMapBuilder()
197 .withName(SFFOVSMAP).withSerializer(serializer)
198 .withTimestampProvider((k, v) -> clockService.getTimestamp())
199 .build();
200 }
201
202 @Deactivate
203 public void deactivate() {
Phaneendra Manda5f326872015-12-07 19:25:09 +0530204 eventDispatcher.removeSink(VtnRscEvent.class);
jiangruif675cd42015-11-27 15:03:59 +0800205 floatingIpService.removeListener(floatingIpListener);
206 routerService.removeListener(routerListener);
207 routerInterfaceService.removeListener(routerInterfaceListener);
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530208 portPairService.removeListener(portPairListener);
209 portPairGroupService.removeListener(portPairGroupListener);
210 flowClassifierService.removeListener(flowClassifierListener);
211 portChainService.removeListener(portChainListener);
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530212 virtualPortService.removeListener(virtualPortListener);
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530213
lishuai8798bbe2016-05-05 16:02:03 +0800214 l3vniTenantMap.destroy();
215 l3vniTenantRouterMap.destroy();
jiangruif675cd42015-11-27 15:03:59 +0800216 classifierOvsMap.destroy();
217 sffOvsMap.destroy();
jiangruif675cd42015-11-27 15:03:59 +0800218 log.info("Stopped");
219 }
220
221 @Override
jiangruif675cd42015-11-27 15:03:59 +0800222 public SegmentationId getL3vni(TenantId tenantId) {
223 checkNotNull(tenantId, "tenantId cannot be null");
lishuai8798bbe2016-05-05 16:02:03 +0800224 SegmentationId l3vni = l3vniTenantMap.get(tenantId);
jiangruif675cd42015-11-27 15:03:59 +0800225 if (l3vni == null) {
226 long segmentationId = coreService.getIdGenerator(RUNNELOPTOPOIC)
227 .getNewId();
228 l3vni = SegmentationId.segmentationId(String
229 .valueOf(segmentationId));
lishuai8798bbe2016-05-05 16:02:03 +0800230 l3vniTenantMap.put(tenantId, l3vni);
231 }
232 return l3vni;
233 }
234
235 @Override
236 public SegmentationId getL3vni(TenantRouter tenantRouter) {
237 checkNotNull(tenantRouter, "tenantRouter cannot be null");
238 SegmentationId l3vni = l3vniTenantRouterMap.get(tenantRouter);
239 if (l3vni == null) {
240 long segmentationId = coreService.getIdGenerator(RUNNELOPTOPOIC)
241 .getNewId();
242 l3vni = SegmentationId.segmentationId(String
243 .valueOf(segmentationId));
244 l3vniTenantRouterMap.put(tenantRouter, l3vni);
jiangruif675cd42015-11-27 15:03:59 +0800245 }
246 return l3vni;
247 }
248
jiangruif675cd42015-11-27 15:03:59 +0800249 private class InnerFloatingIpListener implements FloatingIpListener {
250
251 @Override
252 public void event(FloatingIpEvent event) {
253 checkNotNull(event, EVENT_NOT_NULL);
254 FloatingIp floatingIp = event.subject();
255 if (FloatingIpEvent.Type.FLOATINGIP_PUT == event.type()) {
256 notifyListeners(new VtnRscEvent(
257 VtnRscEvent.Type.FLOATINGIP_PUT,
258 new VtnRscEventFeedback(
259 floatingIp)));
260 }
261 if (FloatingIpEvent.Type.FLOATINGIP_DELETE == event.type()) {
262 notifyListeners(new VtnRscEvent(
263 VtnRscEvent.Type.FLOATINGIP_DELETE,
264 new VtnRscEventFeedback(
265 floatingIp)));
266 }
lishuai762df812016-01-08 11:51:15 +0800267 if (FloatingIpEvent.Type.FLOATINGIP_BIND == event.type()) {
268 notifyListeners(new VtnRscEvent(
269 VtnRscEvent.Type.FLOATINGIP_BIND,
270 new VtnRscEventFeedback(
271 floatingIp)));
272 }
273 if (FloatingIpEvent.Type.FLOATINGIP_UNBIND == event.type()) {
274 notifyListeners(new VtnRscEvent(
275 VtnRscEvent.Type.FLOATINGIP_UNBIND,
276 new VtnRscEventFeedback(
277 floatingIp)));
278 }
jiangruif675cd42015-11-27 15:03:59 +0800279 }
280 }
281
282 private class InnerRouterListener implements RouterListener {
283
284 @Override
285 public void event(RouterEvent event) {
286 checkNotNull(event, EVENT_NOT_NULL);
287 Router router = event.subject();
288 if (RouterEvent.Type.ROUTER_PUT == event.type()) {
289 notifyListeners(new VtnRscEvent(VtnRscEvent.Type.ROUTER_PUT,
290 new VtnRscEventFeedback(router)));
291 }
292 if (RouterEvent.Type.ROUTER_DELETE == event.type()) {
293 notifyListeners(new VtnRscEvent(VtnRscEvent.Type.ROUTER_DELETE,
294 new VtnRscEventFeedback(router)));
295 }
296 }
297 }
298
299 private class InnerRouterInterfaceListener
300 implements RouterInterfaceListener {
301
302 @Override
303 public void event(RouterInterfaceEvent event) {
304 checkNotNull(event, EVENT_NOT_NULL);
305 RouterInterface routerInterface = event.subject();
306 if (RouterInterfaceEvent.Type.ROUTER_INTERFACE_PUT == event.type()) {
307 notifyListeners(new VtnRscEvent(
308 VtnRscEvent.Type.ROUTER_INTERFACE_PUT,
309 new VtnRscEventFeedback(
310 routerInterface)));
311 }
312 if (RouterInterfaceEvent.Type.ROUTER_INTERFACE_DELETE == event
313 .type()) {
314 notifyListeners(new VtnRscEvent(
315 VtnRscEvent.Type.ROUTER_INTERFACE_DELETE,
316 new VtnRscEventFeedback(
317 routerInterface)));
318 }
319 }
320 }
321
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530322 private class InnerPortPairListener implements PortPairListener {
323
324 @Override
325 public void event(PortPairEvent event) {
326 checkNotNull(event, EVENT_NOT_NULL);
327 PortPair portPair = event.subject();
328 if (PortPairEvent.Type.PORT_PAIR_PUT == event.type()) {
329 notifyListeners(new VtnRscEvent(VtnRscEvent.Type.PORT_PAIR_PUT,
330 new VtnRscEventFeedback(portPair)));
331 } else if (PortPairEvent.Type.PORT_PAIR_DELETE == event.type()) {
332 notifyListeners(new VtnRscEvent(
333 VtnRscEvent.Type.PORT_PAIR_DELETE,
334 new VtnRscEventFeedback(portPair)));
335 } else if (PortPairEvent.Type.PORT_PAIR_UPDATE == event.type()) {
336 notifyListeners(new VtnRscEvent(
337 VtnRscEvent.Type.PORT_PAIR_UPDATE,
338 new VtnRscEventFeedback(portPair)));
339 }
340 }
341 }
342
343 private class InnerPortPairGroupListener implements PortPairGroupListener {
344
345 @Override
346 public void event(PortPairGroupEvent event) {
347 checkNotNull(event, EVENT_NOT_NULL);
348 PortPairGroup portPairGroup = event.subject();
349 if (PortPairGroupEvent.Type.PORT_PAIR_GROUP_PUT == event.type()) {
350 notifyListeners(new VtnRscEvent(
351 VtnRscEvent.Type.PORT_PAIR_GROUP_PUT,
352 new VtnRscEventFeedback(portPairGroup)));
353 } else if (PortPairGroupEvent.Type.PORT_PAIR_GROUP_DELETE == event.type()) {
354 notifyListeners(new VtnRscEvent(
355 VtnRscEvent.Type.PORT_PAIR_GROUP_DELETE,
356 new VtnRscEventFeedback(portPairGroup)));
357 } else if (PortPairGroupEvent.Type.PORT_PAIR_GROUP_UPDATE == event.type()) {
358 notifyListeners(new VtnRscEvent(
359 VtnRscEvent.Type.PORT_PAIR_GROUP_UPDATE,
360 new VtnRscEventFeedback(portPairGroup)));
361 }
362 }
363 }
364
365 private class InnerFlowClassifierListener implements FlowClassifierListener {
366
367 @Override
368 public void event(FlowClassifierEvent event) {
369 checkNotNull(event, EVENT_NOT_NULL);
370 FlowClassifier flowClassifier = event.subject();
371 if (FlowClassifierEvent.Type.FLOW_CLASSIFIER_PUT == event.type()) {
372 notifyListeners(new VtnRscEvent(
373 VtnRscEvent.Type.FLOW_CLASSIFIER_PUT,
374 new VtnRscEventFeedback(flowClassifier)));
375 } else if (FlowClassifierEvent.Type.FLOW_CLASSIFIER_DELETE == event.type()) {
376 notifyListeners(new VtnRscEvent(
377 VtnRscEvent.Type.FLOW_CLASSIFIER_DELETE,
378 new VtnRscEventFeedback(flowClassifier)));
379 } else if (FlowClassifierEvent.Type.FLOW_CLASSIFIER_UPDATE == event.type()) {
380 notifyListeners(new VtnRscEvent(
381 VtnRscEvent.Type.FLOW_CLASSIFIER_UPDATE,
382 new VtnRscEventFeedback(flowClassifier)));
383 }
384 }
385 }
386
387 private class InnerPortChainListener implements PortChainListener {
388
389 @Override
390 public void event(PortChainEvent event) {
391 checkNotNull(event, EVENT_NOT_NULL);
392 PortChain portChain = event.subject();
393 if (PortChainEvent.Type.PORT_CHAIN_PUT == event.type()) {
394 notifyListeners(new VtnRscEvent(
395 VtnRscEvent.Type.PORT_CHAIN_PUT,
396 new VtnRscEventFeedback(portChain)));
397 } else if (PortChainEvent.Type.PORT_CHAIN_DELETE == event.type()) {
398 notifyListeners(new VtnRscEvent(
399 VtnRscEvent.Type.PORT_CHAIN_DELETE,
400 new VtnRscEventFeedback(portChain)));
401 } else if (PortChainEvent.Type.PORT_CHAIN_UPDATE == event.type()) {
402 notifyListeners(new VtnRscEvent(
403 VtnRscEvent.Type.PORT_CHAIN_UPDATE,
404 new VtnRscEventFeedback(portChain)));
405 }
406 }
407 }
408
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530409 private class InnerVirtualPortListener implements VirtualPortListener {
410
411 @Override
412 public void event(VirtualPortEvent event) {
413 checkNotNull(event, EVENT_NOT_NULL);
414 VirtualPort virtualPort = event.subject();
415 if (VirtualPortEvent.Type.VIRTUAL_PORT_PUT == event.type()) {
416 notifyListeners(new VtnRscEvent(VtnRscEvent.Type.VIRTUAL_PORT_PUT,
417 new VtnRscEventFeedback(virtualPort)));
418 } else if (VirtualPortEvent.Type.VIRTUAL_PORT_DELETE == event.type()) {
419 notifyListeners(new VtnRscEvent(VtnRscEvent.Type.VIRTUAL_PORT_DELETE,
420 new VtnRscEventFeedback(virtualPort)));
421 }
422 }
423 }
424
jiangruif675cd42015-11-27 15:03:59 +0800425 @Override
426 public Iterator<Device> getClassifierOfTenant(TenantId tenantId) {
427 checkNotNull(tenantId, TENANTID_NOT_NULL);
428 Set<DeviceId> deviceIdSet = classifierOvsMap.get(tenantId);
429 Set<Device> deviceSet = new HashSet<>();
430 if (deviceIdSet != null) {
431 for (DeviceId deviceId : deviceIdSet) {
432 deviceSet.add(deviceService.getDevice(deviceId));
433 }
434 }
435 return deviceSet.iterator();
436 }
437
438 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -0700439 public Iterator<Device> getSffOfTenant(TenantId tenantId) {
jiangruif675cd42015-11-27 15:03:59 +0800440 checkNotNull(tenantId, TENANTID_NOT_NULL);
441 Set<DeviceId> deviceIdSet = sffOvsMap.get(tenantId);
442 Set<Device> deviceSet = new HashSet<>();
443 if (deviceIdSet != null) {
444 for (DeviceId deviceId : deviceIdSet) {
445 deviceSet.add(deviceService.getDevice(deviceId));
446 }
447 }
448 return deviceSet.iterator();
449 }
450
451 @Override
452 public MacAddress getGatewayMac(HostId hostId) {
453 checkNotNull(hostId, "hostId cannot be null");
454 Host host = hostService.getHost(hostId);
455 String ifaceId = host.annotations().value(IFACEID);
456 VirtualPortId hPortId = VirtualPortId.portId(ifaceId);
457 VirtualPort hPort = virtualPortService.getPort(hPortId);
458 SubnetId subnetId = hPort.fixedIps().iterator().next().subnetId();
459 Subnet subnet = subnetService.getSubnet(subnetId);
460 IpAddress gatewayIp = subnet.gatewayIp();
461 Iterable<VirtualPort> virtualPorts = virtualPortService.getPorts();
462 MacAddress macAddress = null;
463 for (VirtualPort port : virtualPorts) {
464 Set<FixedIp> fixedIpSet = port.fixedIps();
465 for (FixedIp fixedIp : fixedIpSet) {
466 if (fixedIp.ip().equals(gatewayIp)) {
467 macAddress = port.macAddress();
468 }
469 }
470 }
471 return macAddress;
472 }
473
474 @Override
475 public boolean isServiceFunction(VirtualPortId portId) {
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530476 return portPairService.exists(PortPairId.of(portId.portId()));
jiangruif675cd42015-11-27 15:03:59 +0800477 }
478
479 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -0700480 public DeviceId getSfToSffMaping(VirtualPortId portId) {
jiangruif675cd42015-11-27 15:03:59 +0800481 checkNotNull(portId, "portId cannot be null");
482 VirtualPort vmPort = virtualPortService.getPort(portId);
483 Set<Host> hostSet = hostService.getHostsByMac(vmPort.macAddress());
484 for (Host host : hostSet) {
Satish K01a60f22015-11-28 15:21:42 +0530485 if (host.annotations().value(IFACEID).equals(vmPort.portId().portId())) {
jiangruif675cd42015-11-27 15:03:59 +0800486 return host.location().deviceId();
487 }
488 }
489 return null;
490 }
491
lishuaib43dbf72016-01-06 11:11:35 +0800492 @Override
493 public void addDeviceIdOfOvsMap(VirtualPortId virtualPortId,
494 TenantId tenantId, DeviceId deviceId) {
495 checkNotNull(virtualPortId, VIRTUALPORTID_NOT_NULL);
496 checkNotNull(tenantId, TENANTID_NOT_NULL);
497 checkNotNull(deviceId, DEVICEID_NOT_NULL);
498 if (isServiceFunction(virtualPortId)) {
499 addDeviceIdToSpecificMap(tenantId, deviceId, sffOvsMap);
500 } else {
501 addDeviceIdToSpecificMap(tenantId, deviceId, classifierOvsMap);
502 }
503 }
504
505 @Override
506 public void removeDeviceIdOfOvsMap(Host host, TenantId tenantId, DeviceId deviceId) {
507 checkNotNull(host, HOST_NOT_NULL);
508 checkNotNull(tenantId, TENANTID_NOT_NULL);
509 checkNotNull(deviceId, DEVICEID_NOT_NULL);
510 if (isLastSFHostOfTenant(host, deviceId, tenantId)) {
511 removeDeviceIdToSpecificMap(tenantId, deviceId, sffOvsMap);
512 }
513 if (isLastClassifierHostOfTenant(host, deviceId, tenantId)) {
514 removeDeviceIdToSpecificMap(tenantId, deviceId, classifierOvsMap);
515 }
516 }
517
jiangruif675cd42015-11-27 15:03:59 +0800518 /**
519 * Checks whether the last Service Function host of a specific tenant in
520 * this device.
521 *
522 * @param host the host on device
523 * @param deviceId the device identifier
524 * @param tenantId the tenant identifier
525 * @return true or false
526 */
527 private boolean isLastSFHostOfTenant(Host host, DeviceId deviceId,
528 TenantId tenantId) {
jiangruif675cd42015-11-27 15:03:59 +0800529 Set<Host> hostSet = hostService.getConnectedHosts(deviceId);
lishuai1fbd41b2015-12-10 16:44:57 +0800530 if (hostSet != null) {
531 for (Host h : hostSet) {
532 String ifaceId = h.annotations().value(IFACEID);
533 if (ifaceId != null) {
534 VirtualPortId hPortId = VirtualPortId.portId(ifaceId);
535 if (virtualPortService.getPort(hPortId).tenantId().tenantId()
536 .equals(tenantId.tenantId())
537 && isServiceFunction(hPortId)) {
lishuaib43dbf72016-01-06 11:11:35 +0800538 if (!h.equals(host)) {
539 return false;
540 }
lishuai1fbd41b2015-12-10 16:44:57 +0800541 }
jiangruif675cd42015-11-27 15:03:59 +0800542 }
543 }
544 }
lishuaib43dbf72016-01-06 11:11:35 +0800545 return true;
jiangruif675cd42015-11-27 15:03:59 +0800546 }
547
548 /**
549 * Checks whether the last Classifier host of a specific tenant in this
550 * device.
551 *
552 * @param host the host on device
553 * @param deviceId the device identifier
554 * @param tenantId the tenant identifier
555 * @return true or false
556 */
557 private boolean isLastClassifierHostOfTenant(Host host, DeviceId deviceId,
558 TenantId tenantId) {
jiangruif675cd42015-11-27 15:03:59 +0800559 Set<Host> hostSet = hostService.getConnectedHosts(deviceId);
lishuai1fbd41b2015-12-10 16:44:57 +0800560 if (hostSet != null) {
561 for (Host h : hostSet) {
562 String ifaceId = h.annotations().value(IFACEID);
563 if (ifaceId != null) {
564 VirtualPortId hPortId = VirtualPortId.portId(ifaceId);
565 if (virtualPortService.getPort(hPortId).tenantId().tenantId()
566 .equals(tenantId.tenantId())
567 && !isServiceFunction(hPortId)) {
lishuaib43dbf72016-01-06 11:11:35 +0800568 if (!h.equals(host)) {
569 return false;
570 }
lishuai1fbd41b2015-12-10 16:44:57 +0800571 }
jiangruif675cd42015-11-27 15:03:59 +0800572 }
573 }
574 }
lishuaib43dbf72016-01-06 11:11:35 +0800575 return true;
jiangruif675cd42015-11-27 15:03:59 +0800576 }
577
578 /**
579 * Adds specify Device identifier to OvsMap.
580 *
581 * @param tenantId the tenant identifier
582 * @param deviceId the device identifier
583 * @param ovsMap the instance of map to store device identifier
584 */
lishuaib43dbf72016-01-06 11:11:35 +0800585 private void addDeviceIdToSpecificMap(TenantId tenantId,
jiangruif675cd42015-11-27 15:03:59 +0800586 DeviceId deviceId,
587 EventuallyConsistentMap<TenantId, Set<DeviceId>> ovsMap) {
jiangruif675cd42015-11-27 15:03:59 +0800588 if (ovsMap.containsKey(tenantId)) {
589 Set<DeviceId> deviceIdSet = ovsMap.get(tenantId);
590 deviceIdSet.add(deviceId);
591 ovsMap.put(tenantId, deviceIdSet);
592 } else {
593 Set<DeviceId> deviceIdSet = new HashSet<>();
594 deviceIdSet.add(deviceId);
595 ovsMap.put(tenantId, deviceIdSet);
596 }
597 }
598
599 /**
600 * Removes specify Device identifier from OvsMap.
601 *
602 * @param tenantId the tenant identifier
603 * @param deviceId the device identifier
604 * @param ovsMap the instance of map to store device identifier
605 */
lishuaib43dbf72016-01-06 11:11:35 +0800606 private void removeDeviceIdToSpecificMap(TenantId tenantId,
jiangruif675cd42015-11-27 15:03:59 +0800607 DeviceId deviceId,
608 EventuallyConsistentMap<TenantId, Set<DeviceId>> ovsMap) {
jiangruif675cd42015-11-27 15:03:59 +0800609 Set<DeviceId> deviceIdSet = ovsMap.get(tenantId);
lishuaib43dbf72016-01-06 11:11:35 +0800610 if (deviceIdSet != null && deviceIdSet.size() > 1) {
jiangruif675cd42015-11-27 15:03:59 +0800611 deviceIdSet.remove(deviceId);
612 ovsMap.put(tenantId, deviceIdSet);
613 } else {
614 ovsMap.remove(tenantId);
615 }
616 }
617
618 /**
619 * Notifies specify event to all listeners.
620 *
621 * @param event VtnRsc event
622 */
623 private void notifyListeners(VtnRscEvent event) {
624 checkNotNull(event, EVENT_NOT_NULL);
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530625 post(event);
jiangruif675cd42015-11-27 15:03:59 +0800626 }
627}