blob: 37f2de416962b0ae1170147303d3f6298a94f54c [file] [log] [blame]
jiangruif675cd42015-11-27 15:03:59 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
jiangruif675cd42015-11-27 15:03:59 +080018import org.onlab.packet.IpAddress;
19import org.onlab.packet.MacAddress;
20import org.onlab.util.KryoNamespace;
21import org.onosproject.core.CoreService;
Mahesh Poojary Scc11f722015-11-29 16:09:56 +053022import org.onosproject.event.AbstractListenerManager;
jiangruif675cd42015-11-27 15:03:59 +080023import org.onosproject.net.Device;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.Host;
26import org.onosproject.net.HostId;
jiangruif675cd42015-11-27 15:03:59 +080027import org.onosproject.net.device.DeviceService;
lishuaib43dbf72016-01-06 11:11:35 +080028import org.onosproject.net.host.HostService;
jiangruif675cd42015-11-27 15:03:59 +080029import org.onosproject.store.serializers.KryoNamespaces;
30import org.onosproject.store.service.EventuallyConsistentMap;
31import org.onosproject.store.service.LogicalClockService;
32import org.onosproject.store.service.StorageService;
33import org.onosproject.vtnrsc.FixedIp;
34import org.onosproject.vtnrsc.FloatingIp;
lishuaib43dbf72016-01-06 11:11:35 +080035import org.onosproject.vtnrsc.FlowClassifier;
36import org.onosproject.vtnrsc.PortChain;
37import org.onosproject.vtnrsc.PortPair;
38import org.onosproject.vtnrsc.PortPairGroup;
39import org.onosproject.vtnrsc.PortPairId;
jiangruif675cd42015-11-27 15:03:59 +080040import org.onosproject.vtnrsc.Router;
Wu wenbinacc10ea2016-05-06 16:48:56 +080041import org.onosproject.vtnrsc.RouterId;
jiangruif675cd42015-11-27 15:03:59 +080042import org.onosproject.vtnrsc.RouterInterface;
43import org.onosproject.vtnrsc.SegmentationId;
44import org.onosproject.vtnrsc.Subnet;
45import org.onosproject.vtnrsc.SubnetId;
46import org.onosproject.vtnrsc.TenantId;
lishuai8798bbe2016-05-05 16:02:03 +080047import org.onosproject.vtnrsc.TenantRouter;
jiangruif675cd42015-11-27 15:03:59 +080048import org.onosproject.vtnrsc.VirtualPort;
49import org.onosproject.vtnrsc.VirtualPortId;
50import org.onosproject.vtnrsc.event.VtnRscEvent;
51import org.onosproject.vtnrsc.event.VtnRscEventFeedback;
52import org.onosproject.vtnrsc.event.VtnRscListener;
53import org.onosproject.vtnrsc.floatingip.FloatingIpEvent;
54import org.onosproject.vtnrsc.floatingip.FloatingIpListener;
55import org.onosproject.vtnrsc.floatingip.FloatingIpService;
lishuaib43dbf72016-01-06 11:11:35 +080056import org.onosproject.vtnrsc.flowclassifier.FlowClassifierEvent;
57import org.onosproject.vtnrsc.flowclassifier.FlowClassifierListener;
58import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
59import org.onosproject.vtnrsc.portchain.PortChainEvent;
60import org.onosproject.vtnrsc.portchain.PortChainListener;
61import org.onosproject.vtnrsc.portchain.PortChainService;
62import org.onosproject.vtnrsc.portpair.PortPairEvent;
63import org.onosproject.vtnrsc.portpair.PortPairListener;
64import org.onosproject.vtnrsc.portpair.PortPairService;
65import org.onosproject.vtnrsc.portpairgroup.PortPairGroupEvent;
66import org.onosproject.vtnrsc.portpairgroup.PortPairGroupListener;
67import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
jiangruif675cd42015-11-27 15:03:59 +080068import org.onosproject.vtnrsc.router.RouterEvent;
69import org.onosproject.vtnrsc.router.RouterListener;
70import org.onosproject.vtnrsc.router.RouterService;
71import org.onosproject.vtnrsc.routerinterface.RouterInterfaceEvent;
72import org.onosproject.vtnrsc.routerinterface.RouterInterfaceListener;
73import org.onosproject.vtnrsc.routerinterface.RouterInterfaceService;
74import org.onosproject.vtnrsc.service.VtnRscService;
75import org.onosproject.vtnrsc.subnet.SubnetService;
76import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService;
Phaneendra Mandab212bc92016-07-08 16:50:11 +053077import org.onosproject.vtnrsc.virtualport.VirtualPortEvent;
78import org.onosproject.vtnrsc.virtualport.VirtualPortListener;
jiangruif675cd42015-11-27 15:03:59 +080079import org.onosproject.vtnrsc.virtualport.VirtualPortService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070080import org.osgi.service.component.annotations.Activate;
81import org.osgi.service.component.annotations.Component;
82import org.osgi.service.component.annotations.Deactivate;
83import org.osgi.service.component.annotations.Reference;
84import org.osgi.service.component.annotations.ReferenceCardinality;
jiangruif675cd42015-11-27 15:03:59 +080085import org.slf4j.Logger;
86
Ray Milkeyd84f89b2018-08-17 14:54:17 -070087import java.util.HashSet;
88import java.util.Iterator;
89import java.util.Set;
90
91import static com.google.common.base.Preconditions.checkNotNull;
92import static org.slf4j.LoggerFactory.getLogger;
93
jiangruif675cd42015-11-27 15:03:59 +080094/**
95 * Provides implementation of the VtnRsc service.
96 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070097@Component(immediate = true, service = VtnRscService.class)
Mahesh Poojary Scc11f722015-11-29 16:09:56 +053098public class VtnRscManager extends AbstractListenerManager<VtnRscEvent, VtnRscListener>
99 implements VtnRscService {
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700100 @Reference(cardinality = ReferenceCardinality.MANDATORY)
jiangruif675cd42015-11-27 15:03:59 +0800101 protected CoreService coreService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700102 @Reference(cardinality = ReferenceCardinality.MANDATORY)
jiangruif675cd42015-11-27 15:03:59 +0800103 protected StorageService storageService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700104 @Reference(cardinality = ReferenceCardinality.MANDATORY)
jiangruif675cd42015-11-27 15:03:59 +0800105 protected LogicalClockService clockService;
106
107 private final Logger log = getLogger(getClass());
jiangruif675cd42015-11-27 15:03:59 +0800108 private FloatingIpListener floatingIpListener = new InnerFloatingIpListener();
109 private RouterListener routerListener = new InnerRouterListener();
110 private RouterInterfaceListener routerInterfaceListener = new InnerRouterInterfaceListener();
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530111 private PortPairListener portPairListener = new InnerPortPairListener();
112 private PortPairGroupListener portPairGroupListener = new InnerPortPairGroupListener();
113 private FlowClassifierListener flowClassifierListener = new InnerFlowClassifierListener();
114 private PortChainListener portChainListener = new InnerPortChainListener();
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530115 private VirtualPortListener virtualPortListener = new InnerVirtualPortListener();
jiangruif675cd42015-11-27 15:03:59 +0800116
lishuai8798bbe2016-05-05 16:02:03 +0800117 private EventuallyConsistentMap<TenantId, SegmentationId> l3vniTenantMap;
118 private EventuallyConsistentMap<TenantRouter, SegmentationId> l3vniTenantRouterMap;
jiangruif675cd42015-11-27 15:03:59 +0800119 private EventuallyConsistentMap<TenantId, Set<DeviceId>> classifierOvsMap;
120 private EventuallyConsistentMap<TenantId, Set<DeviceId>> sffOvsMap;
121
122 private static final String IFACEID = "ifaceid";
123 private static final String RUNNELOPTOPOIC = "tunnel-ops-ids";
jiangruif675cd42015-11-27 15:03:59 +0800124 private static final String EVENT_NOT_NULL = "event cannot be null";
125 private static final String TENANTID_NOT_NULL = "tenantId cannot be null";
126 private static final String DEVICEID_NOT_NULL = "deviceId cannot be null";
lishuaib43dbf72016-01-06 11:11:35 +0800127 private static final String VIRTUALPORTID_NOT_NULL = "virtualPortId cannot be null";
128 private static final String HOST_NOT_NULL = "host cannot be null";
lishuai8798bbe2016-05-05 16:02:03 +0800129 private static final String L3VNITENANTMAP = "l3vniTenantMap";
130 private static final String L3VNITENANTROUTERMAP = "l3vniTenantRouterMap";
jiangruif675cd42015-11-27 15:03:59 +0800131 private static final String CLASSIFIEROVSMAP = "classifierOvsMap";
132 private static final String SFFOVSMAP = "sffOvsMap";
133
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700134 @Reference(cardinality = ReferenceCardinality.MANDATORY)
jiangruif675cd42015-11-27 15:03:59 +0800135 protected RouterService routerService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700136 @Reference(cardinality = ReferenceCardinality.MANDATORY)
jiangruif675cd42015-11-27 15:03:59 +0800137 protected FloatingIpService floatingIpService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700138 @Reference(cardinality = ReferenceCardinality.MANDATORY)
jiangruif675cd42015-11-27 15:03:59 +0800139 protected RouterInterfaceService routerInterfaceService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700140 @Reference(cardinality = ReferenceCardinality.MANDATORY)
jiangruif675cd42015-11-27 15:03:59 +0800141 protected VirtualPortService virtualPortService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700142 @Reference(cardinality = ReferenceCardinality.MANDATORY)
jiangruif675cd42015-11-27 15:03:59 +0800143 protected HostService hostService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700144 @Reference(cardinality = ReferenceCardinality.MANDATORY)
jiangruif675cd42015-11-27 15:03:59 +0800145 protected SubnetService subnetService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700146 @Reference(cardinality = ReferenceCardinality.MANDATORY)
jiangruif675cd42015-11-27 15:03:59 +0800147 protected TenantNetworkService tenantNetworkService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700148 @Reference(cardinality = ReferenceCardinality.MANDATORY)
jiangruif675cd42015-11-27 15:03:59 +0800149 protected DeviceService deviceService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700150 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530151 protected PortPairService portPairService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700152 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530153 protected PortPairGroupService portPairGroupService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700154 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530155 protected FlowClassifierService flowClassifierService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700156 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530157 protected PortChainService portChainService;
jiangruif675cd42015-11-27 15:03:59 +0800158
159 @Activate
160 public void activate() {
Phaneendra Manda5f326872015-12-07 19:25:09 +0530161 eventDispatcher.addSink(VtnRscEvent.class, listenerRegistry);
jiangruif675cd42015-11-27 15:03:59 +0800162 floatingIpService.addListener(floatingIpListener);
163 routerService.addListener(routerListener);
164 routerInterfaceService.addListener(routerInterfaceListener);
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530165 portPairService.addListener(portPairListener);
166 portPairGroupService.addListener(portPairGroupListener);
167 flowClassifierService.addListener(flowClassifierListener);
168 portChainService.addListener(portChainListener);
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530169 virtualPortService.addListener(virtualPortListener);
jiangruif675cd42015-11-27 15:03:59 +0800170
171 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
172 .register(KryoNamespaces.API)
Ray Milkey5dae9222017-06-15 11:55:51 -0700173 .register(TenantId.class, SegmentationId.class,
Wu wenbinacc10ea2016-05-06 16:48:56 +0800174 TenantRouter.class, RouterId.class);
lishuai8798bbe2016-05-05 16:02:03 +0800175 l3vniTenantMap = storageService
jiangruif675cd42015-11-27 15:03:59 +0800176 .<TenantId, SegmentationId>eventuallyConsistentMapBuilder()
lishuai8798bbe2016-05-05 16:02:03 +0800177 .withName(L3VNITENANTMAP).withSerializer(serializer)
178 .withTimestampProvider((k, v) -> clockService.getTimestamp())
179 .build();
180
181 l3vniTenantRouterMap = storageService
182 .<TenantRouter, SegmentationId>eventuallyConsistentMapBuilder()
183 .withName(L3VNITENANTROUTERMAP).withSerializer(serializer)
jiangruif675cd42015-11-27 15:03:59 +0800184 .withTimestampProvider((k, v) -> clockService.getTimestamp())
185 .build();
186
187 classifierOvsMap = storageService
188 .<TenantId, Set<DeviceId>>eventuallyConsistentMapBuilder()
189 .withName(CLASSIFIEROVSMAP).withSerializer(serializer)
190 .withTimestampProvider((k, v) -> clockService.getTimestamp())
191 .build();
192
193 sffOvsMap = storageService
194 .<TenantId, Set<DeviceId>>eventuallyConsistentMapBuilder()
195 .withName(SFFOVSMAP).withSerializer(serializer)
196 .withTimestampProvider((k, v) -> clockService.getTimestamp())
197 .build();
198 }
199
200 @Deactivate
201 public void deactivate() {
Phaneendra Manda5f326872015-12-07 19:25:09 +0530202 eventDispatcher.removeSink(VtnRscEvent.class);
jiangruif675cd42015-11-27 15:03:59 +0800203 floatingIpService.removeListener(floatingIpListener);
204 routerService.removeListener(routerListener);
205 routerInterfaceService.removeListener(routerInterfaceListener);
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530206 portPairService.removeListener(portPairListener);
207 portPairGroupService.removeListener(portPairGroupListener);
208 flowClassifierService.removeListener(flowClassifierListener);
209 portChainService.removeListener(portChainListener);
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530210 virtualPortService.removeListener(virtualPortListener);
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530211
lishuai8798bbe2016-05-05 16:02:03 +0800212 l3vniTenantMap.destroy();
213 l3vniTenantRouterMap.destroy();
jiangruif675cd42015-11-27 15:03:59 +0800214 classifierOvsMap.destroy();
215 sffOvsMap.destroy();
jiangruif675cd42015-11-27 15:03:59 +0800216 log.info("Stopped");
217 }
218
219 @Override
jiangruif675cd42015-11-27 15:03:59 +0800220 public SegmentationId getL3vni(TenantId tenantId) {
221 checkNotNull(tenantId, "tenantId cannot be null");
lishuai8798bbe2016-05-05 16:02:03 +0800222 SegmentationId l3vni = l3vniTenantMap.get(tenantId);
jiangruif675cd42015-11-27 15:03:59 +0800223 if (l3vni == null) {
224 long segmentationId = coreService.getIdGenerator(RUNNELOPTOPOIC)
225 .getNewId();
226 l3vni = SegmentationId.segmentationId(String
227 .valueOf(segmentationId));
lishuai8798bbe2016-05-05 16:02:03 +0800228 l3vniTenantMap.put(tenantId, l3vni);
229 }
230 return l3vni;
231 }
232
233 @Override
234 public SegmentationId getL3vni(TenantRouter tenantRouter) {
235 checkNotNull(tenantRouter, "tenantRouter cannot be null");
236 SegmentationId l3vni = l3vniTenantRouterMap.get(tenantRouter);
237 if (l3vni == null) {
238 long segmentationId = coreService.getIdGenerator(RUNNELOPTOPOIC)
239 .getNewId();
240 l3vni = SegmentationId.segmentationId(String
241 .valueOf(segmentationId));
242 l3vniTenantRouterMap.put(tenantRouter, l3vni);
jiangruif675cd42015-11-27 15:03:59 +0800243 }
244 return l3vni;
245 }
246
jiangruif675cd42015-11-27 15:03:59 +0800247 private class InnerFloatingIpListener implements FloatingIpListener {
248
249 @Override
250 public void event(FloatingIpEvent event) {
251 checkNotNull(event, EVENT_NOT_NULL);
252 FloatingIp floatingIp = event.subject();
253 if (FloatingIpEvent.Type.FLOATINGIP_PUT == event.type()) {
254 notifyListeners(new VtnRscEvent(
255 VtnRscEvent.Type.FLOATINGIP_PUT,
256 new VtnRscEventFeedback(
257 floatingIp)));
258 }
259 if (FloatingIpEvent.Type.FLOATINGIP_DELETE == event.type()) {
260 notifyListeners(new VtnRscEvent(
261 VtnRscEvent.Type.FLOATINGIP_DELETE,
262 new VtnRscEventFeedback(
263 floatingIp)));
264 }
lishuai762df812016-01-08 11:51:15 +0800265 if (FloatingIpEvent.Type.FLOATINGIP_BIND == event.type()) {
266 notifyListeners(new VtnRscEvent(
267 VtnRscEvent.Type.FLOATINGIP_BIND,
268 new VtnRscEventFeedback(
269 floatingIp)));
270 }
271 if (FloatingIpEvent.Type.FLOATINGIP_UNBIND == event.type()) {
272 notifyListeners(new VtnRscEvent(
273 VtnRscEvent.Type.FLOATINGIP_UNBIND,
274 new VtnRscEventFeedback(
275 floatingIp)));
276 }
jiangruif675cd42015-11-27 15:03:59 +0800277 }
278 }
279
280 private class InnerRouterListener implements RouterListener {
281
282 @Override
283 public void event(RouterEvent event) {
284 checkNotNull(event, EVENT_NOT_NULL);
285 Router router = event.subject();
286 if (RouterEvent.Type.ROUTER_PUT == event.type()) {
287 notifyListeners(new VtnRscEvent(VtnRscEvent.Type.ROUTER_PUT,
288 new VtnRscEventFeedback(router)));
289 }
290 if (RouterEvent.Type.ROUTER_DELETE == event.type()) {
291 notifyListeners(new VtnRscEvent(VtnRscEvent.Type.ROUTER_DELETE,
292 new VtnRscEventFeedback(router)));
293 }
294 }
295 }
296
297 private class InnerRouterInterfaceListener
298 implements RouterInterfaceListener {
299
300 @Override
301 public void event(RouterInterfaceEvent event) {
302 checkNotNull(event, EVENT_NOT_NULL);
303 RouterInterface routerInterface = event.subject();
304 if (RouterInterfaceEvent.Type.ROUTER_INTERFACE_PUT == event.type()) {
305 notifyListeners(new VtnRscEvent(
306 VtnRscEvent.Type.ROUTER_INTERFACE_PUT,
307 new VtnRscEventFeedback(
308 routerInterface)));
309 }
310 if (RouterInterfaceEvent.Type.ROUTER_INTERFACE_DELETE == event
311 .type()) {
312 notifyListeners(new VtnRscEvent(
313 VtnRscEvent.Type.ROUTER_INTERFACE_DELETE,
314 new VtnRscEventFeedback(
315 routerInterface)));
316 }
317 }
318 }
319
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530320 private class InnerPortPairListener implements PortPairListener {
321
322 @Override
323 public void event(PortPairEvent event) {
324 checkNotNull(event, EVENT_NOT_NULL);
325 PortPair portPair = event.subject();
326 if (PortPairEvent.Type.PORT_PAIR_PUT == event.type()) {
327 notifyListeners(new VtnRscEvent(VtnRscEvent.Type.PORT_PAIR_PUT,
328 new VtnRscEventFeedback(portPair)));
329 } else if (PortPairEvent.Type.PORT_PAIR_DELETE == event.type()) {
330 notifyListeners(new VtnRscEvent(
331 VtnRscEvent.Type.PORT_PAIR_DELETE,
332 new VtnRscEventFeedback(portPair)));
333 } else if (PortPairEvent.Type.PORT_PAIR_UPDATE == event.type()) {
334 notifyListeners(new VtnRscEvent(
335 VtnRscEvent.Type.PORT_PAIR_UPDATE,
336 new VtnRscEventFeedback(portPair)));
337 }
338 }
339 }
340
341 private class InnerPortPairGroupListener implements PortPairGroupListener {
342
343 @Override
344 public void event(PortPairGroupEvent event) {
345 checkNotNull(event, EVENT_NOT_NULL);
346 PortPairGroup portPairGroup = event.subject();
347 if (PortPairGroupEvent.Type.PORT_PAIR_GROUP_PUT == event.type()) {
348 notifyListeners(new VtnRscEvent(
349 VtnRscEvent.Type.PORT_PAIR_GROUP_PUT,
350 new VtnRscEventFeedback(portPairGroup)));
351 } else if (PortPairGroupEvent.Type.PORT_PAIR_GROUP_DELETE == event.type()) {
352 notifyListeners(new VtnRscEvent(
353 VtnRscEvent.Type.PORT_PAIR_GROUP_DELETE,
354 new VtnRscEventFeedback(portPairGroup)));
355 } else if (PortPairGroupEvent.Type.PORT_PAIR_GROUP_UPDATE == event.type()) {
356 notifyListeners(new VtnRscEvent(
357 VtnRscEvent.Type.PORT_PAIR_GROUP_UPDATE,
358 new VtnRscEventFeedback(portPairGroup)));
359 }
360 }
361 }
362
363 private class InnerFlowClassifierListener implements FlowClassifierListener {
364
365 @Override
366 public void event(FlowClassifierEvent event) {
367 checkNotNull(event, EVENT_NOT_NULL);
368 FlowClassifier flowClassifier = event.subject();
369 if (FlowClassifierEvent.Type.FLOW_CLASSIFIER_PUT == event.type()) {
370 notifyListeners(new VtnRscEvent(
371 VtnRscEvent.Type.FLOW_CLASSIFIER_PUT,
372 new VtnRscEventFeedback(flowClassifier)));
373 } else if (FlowClassifierEvent.Type.FLOW_CLASSIFIER_DELETE == event.type()) {
374 notifyListeners(new VtnRscEvent(
375 VtnRscEvent.Type.FLOW_CLASSIFIER_DELETE,
376 new VtnRscEventFeedback(flowClassifier)));
377 } else if (FlowClassifierEvent.Type.FLOW_CLASSIFIER_UPDATE == event.type()) {
378 notifyListeners(new VtnRscEvent(
379 VtnRscEvent.Type.FLOW_CLASSIFIER_UPDATE,
380 new VtnRscEventFeedback(flowClassifier)));
381 }
382 }
383 }
384
385 private class InnerPortChainListener implements PortChainListener {
386
387 @Override
388 public void event(PortChainEvent event) {
389 checkNotNull(event, EVENT_NOT_NULL);
390 PortChain portChain = event.subject();
391 if (PortChainEvent.Type.PORT_CHAIN_PUT == event.type()) {
392 notifyListeners(new VtnRscEvent(
393 VtnRscEvent.Type.PORT_CHAIN_PUT,
394 new VtnRscEventFeedback(portChain)));
395 } else if (PortChainEvent.Type.PORT_CHAIN_DELETE == event.type()) {
396 notifyListeners(new VtnRscEvent(
397 VtnRscEvent.Type.PORT_CHAIN_DELETE,
398 new VtnRscEventFeedback(portChain)));
399 } else if (PortChainEvent.Type.PORT_CHAIN_UPDATE == event.type()) {
400 notifyListeners(new VtnRscEvent(
401 VtnRscEvent.Type.PORT_CHAIN_UPDATE,
402 new VtnRscEventFeedback(portChain)));
403 }
404 }
405 }
406
Phaneendra Mandab212bc92016-07-08 16:50:11 +0530407 private class InnerVirtualPortListener implements VirtualPortListener {
408
409 @Override
410 public void event(VirtualPortEvent event) {
411 checkNotNull(event, EVENT_NOT_NULL);
412 VirtualPort virtualPort = event.subject();
413 if (VirtualPortEvent.Type.VIRTUAL_PORT_PUT == event.type()) {
414 notifyListeners(new VtnRscEvent(VtnRscEvent.Type.VIRTUAL_PORT_PUT,
415 new VtnRscEventFeedback(virtualPort)));
416 } else if (VirtualPortEvent.Type.VIRTUAL_PORT_DELETE == event.type()) {
417 notifyListeners(new VtnRscEvent(VtnRscEvent.Type.VIRTUAL_PORT_DELETE,
418 new VtnRscEventFeedback(virtualPort)));
419 }
420 }
421 }
422
jiangruif675cd42015-11-27 15:03:59 +0800423 @Override
424 public Iterator<Device> getClassifierOfTenant(TenantId tenantId) {
425 checkNotNull(tenantId, TENANTID_NOT_NULL);
426 Set<DeviceId> deviceIdSet = classifierOvsMap.get(tenantId);
427 Set<Device> deviceSet = new HashSet<>();
428 if (deviceIdSet != null) {
429 for (DeviceId deviceId : deviceIdSet) {
430 deviceSet.add(deviceService.getDevice(deviceId));
431 }
432 }
433 return deviceSet.iterator();
434 }
435
436 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -0700437 public Iterator<Device> getSffOfTenant(TenantId tenantId) {
jiangruif675cd42015-11-27 15:03:59 +0800438 checkNotNull(tenantId, TENANTID_NOT_NULL);
439 Set<DeviceId> deviceIdSet = sffOvsMap.get(tenantId);
440 Set<Device> deviceSet = new HashSet<>();
441 if (deviceIdSet != null) {
442 for (DeviceId deviceId : deviceIdSet) {
443 deviceSet.add(deviceService.getDevice(deviceId));
444 }
445 }
446 return deviceSet.iterator();
447 }
448
449 @Override
450 public MacAddress getGatewayMac(HostId hostId) {
451 checkNotNull(hostId, "hostId cannot be null");
452 Host host = hostService.getHost(hostId);
453 String ifaceId = host.annotations().value(IFACEID);
454 VirtualPortId hPortId = VirtualPortId.portId(ifaceId);
455 VirtualPort hPort = virtualPortService.getPort(hPortId);
456 SubnetId subnetId = hPort.fixedIps().iterator().next().subnetId();
457 Subnet subnet = subnetService.getSubnet(subnetId);
458 IpAddress gatewayIp = subnet.gatewayIp();
459 Iterable<VirtualPort> virtualPorts = virtualPortService.getPorts();
460 MacAddress macAddress = null;
461 for (VirtualPort port : virtualPorts) {
462 Set<FixedIp> fixedIpSet = port.fixedIps();
463 for (FixedIp fixedIp : fixedIpSet) {
464 if (fixedIp.ip().equals(gatewayIp)) {
465 macAddress = port.macAddress();
466 }
467 }
468 }
469 return macAddress;
470 }
471
472 @Override
473 public boolean isServiceFunction(VirtualPortId portId) {
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530474 return portPairService.exists(PortPairId.of(portId.portId()));
jiangruif675cd42015-11-27 15:03:59 +0800475 }
476
477 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -0700478 public DeviceId getSfToSffMaping(VirtualPortId portId) {
jiangruif675cd42015-11-27 15:03:59 +0800479 checkNotNull(portId, "portId cannot be null");
480 VirtualPort vmPort = virtualPortService.getPort(portId);
481 Set<Host> hostSet = hostService.getHostsByMac(vmPort.macAddress());
482 for (Host host : hostSet) {
Satish K01a60f22015-11-28 15:21:42 +0530483 if (host.annotations().value(IFACEID).equals(vmPort.portId().portId())) {
jiangruif675cd42015-11-27 15:03:59 +0800484 return host.location().deviceId();
485 }
486 }
487 return null;
488 }
489
lishuaib43dbf72016-01-06 11:11:35 +0800490 @Override
491 public void addDeviceIdOfOvsMap(VirtualPortId virtualPortId,
492 TenantId tenantId, DeviceId deviceId) {
493 checkNotNull(virtualPortId, VIRTUALPORTID_NOT_NULL);
494 checkNotNull(tenantId, TENANTID_NOT_NULL);
495 checkNotNull(deviceId, DEVICEID_NOT_NULL);
496 if (isServiceFunction(virtualPortId)) {
497 addDeviceIdToSpecificMap(tenantId, deviceId, sffOvsMap);
498 } else {
499 addDeviceIdToSpecificMap(tenantId, deviceId, classifierOvsMap);
500 }
501 }
502
503 @Override
504 public void removeDeviceIdOfOvsMap(Host host, TenantId tenantId, DeviceId deviceId) {
505 checkNotNull(host, HOST_NOT_NULL);
506 checkNotNull(tenantId, TENANTID_NOT_NULL);
507 checkNotNull(deviceId, DEVICEID_NOT_NULL);
508 if (isLastSFHostOfTenant(host, deviceId, tenantId)) {
509 removeDeviceIdToSpecificMap(tenantId, deviceId, sffOvsMap);
510 }
511 if (isLastClassifierHostOfTenant(host, deviceId, tenantId)) {
512 removeDeviceIdToSpecificMap(tenantId, deviceId, classifierOvsMap);
513 }
514 }
515
jiangruif675cd42015-11-27 15:03:59 +0800516 /**
517 * Checks whether the last Service Function host of a specific tenant in
518 * this device.
519 *
520 * @param host the host on device
521 * @param deviceId the device identifier
522 * @param tenantId the tenant identifier
523 * @return true or false
524 */
525 private boolean isLastSFHostOfTenant(Host host, DeviceId deviceId,
526 TenantId tenantId) {
jiangruif675cd42015-11-27 15:03:59 +0800527 Set<Host> hostSet = hostService.getConnectedHosts(deviceId);
lishuai1fbd41b2015-12-10 16:44:57 +0800528 if (hostSet != null) {
529 for (Host h : hostSet) {
530 String ifaceId = h.annotations().value(IFACEID);
531 if (ifaceId != null) {
532 VirtualPortId hPortId = VirtualPortId.portId(ifaceId);
533 if (virtualPortService.getPort(hPortId).tenantId().tenantId()
534 .equals(tenantId.tenantId())
535 && isServiceFunction(hPortId)) {
lishuaib43dbf72016-01-06 11:11:35 +0800536 if (!h.equals(host)) {
537 return false;
538 }
lishuai1fbd41b2015-12-10 16:44:57 +0800539 }
jiangruif675cd42015-11-27 15:03:59 +0800540 }
541 }
542 }
lishuaib43dbf72016-01-06 11:11:35 +0800543 return true;
jiangruif675cd42015-11-27 15:03:59 +0800544 }
545
546 /**
547 * Checks whether the last Classifier host of a specific tenant in this
548 * device.
549 *
550 * @param host the host on device
551 * @param deviceId the device identifier
552 * @param tenantId the tenant identifier
553 * @return true or false
554 */
555 private boolean isLastClassifierHostOfTenant(Host host, DeviceId deviceId,
556 TenantId tenantId) {
jiangruif675cd42015-11-27 15:03:59 +0800557 Set<Host> hostSet = hostService.getConnectedHosts(deviceId);
lishuai1fbd41b2015-12-10 16:44:57 +0800558 if (hostSet != null) {
559 for (Host h : hostSet) {
560 String ifaceId = h.annotations().value(IFACEID);
561 if (ifaceId != null) {
562 VirtualPortId hPortId = VirtualPortId.portId(ifaceId);
563 if (virtualPortService.getPort(hPortId).tenantId().tenantId()
564 .equals(tenantId.tenantId())
565 && !isServiceFunction(hPortId)) {
lishuaib43dbf72016-01-06 11:11:35 +0800566 if (!h.equals(host)) {
567 return false;
568 }
lishuai1fbd41b2015-12-10 16:44:57 +0800569 }
jiangruif675cd42015-11-27 15:03:59 +0800570 }
571 }
572 }
lishuaib43dbf72016-01-06 11:11:35 +0800573 return true;
jiangruif675cd42015-11-27 15:03:59 +0800574 }
575
576 /**
577 * Adds specify Device identifier to OvsMap.
578 *
579 * @param tenantId the tenant identifier
580 * @param deviceId the device identifier
581 * @param ovsMap the instance of map to store device identifier
582 */
lishuaib43dbf72016-01-06 11:11:35 +0800583 private void addDeviceIdToSpecificMap(TenantId tenantId,
jiangruif675cd42015-11-27 15:03:59 +0800584 DeviceId deviceId,
585 EventuallyConsistentMap<TenantId, Set<DeviceId>> ovsMap) {
jiangruif675cd42015-11-27 15:03:59 +0800586 if (ovsMap.containsKey(tenantId)) {
587 Set<DeviceId> deviceIdSet = ovsMap.get(tenantId);
588 deviceIdSet.add(deviceId);
589 ovsMap.put(tenantId, deviceIdSet);
590 } else {
591 Set<DeviceId> deviceIdSet = new HashSet<>();
592 deviceIdSet.add(deviceId);
593 ovsMap.put(tenantId, deviceIdSet);
594 }
595 }
596
597 /**
598 * Removes specify Device identifier from OvsMap.
599 *
600 * @param tenantId the tenant identifier
601 * @param deviceId the device identifier
602 * @param ovsMap the instance of map to store device identifier
603 */
lishuaib43dbf72016-01-06 11:11:35 +0800604 private void removeDeviceIdToSpecificMap(TenantId tenantId,
jiangruif675cd42015-11-27 15:03:59 +0800605 DeviceId deviceId,
606 EventuallyConsistentMap<TenantId, Set<DeviceId>> ovsMap) {
jiangruif675cd42015-11-27 15:03:59 +0800607 Set<DeviceId> deviceIdSet = ovsMap.get(tenantId);
lishuaib43dbf72016-01-06 11:11:35 +0800608 if (deviceIdSet != null && deviceIdSet.size() > 1) {
jiangruif675cd42015-11-27 15:03:59 +0800609 deviceIdSet.remove(deviceId);
610 ovsMap.put(tenantId, deviceIdSet);
611 } else {
612 ovsMap.remove(tenantId);
613 }
614 }
615
616 /**
617 * Notifies specify event to all listeners.
618 *
619 * @param event VtnRsc event
620 */
621 private void notifyListeners(VtnRscEvent event) {
622 checkNotNull(event, EVENT_NOT_NULL);
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530623 post(event);
jiangruif675cd42015-11-27 15:03:59 +0800624 }
625}