blob: b39e82ca64238920322a0b8b6819b158b46e6cc5 [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;
90import org.onosproject.vtnrsc.virtualport.VirtualPortService;
91import org.slf4j.Logger;
92
jiangruif675cd42015-11-27 15:03:59 +080093/**
94 * Provides implementation of the VtnRsc service.
95 */
96@Component(immediate = true)
97@Service
Mahesh Poojary Scc11f722015-11-29 16:09:56 +053098public class VtnRscManager extends AbstractListenerManager<VtnRscEvent, VtnRscListener>
99 implements VtnRscService {
jiangruif675cd42015-11-27 15:03:59 +0800100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
101 protected CoreService coreService;
102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
103 protected StorageService storageService;
104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
105 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();
jiangruif675cd42015-11-27 15:03:59 +0800115
lishuai8798bbe2016-05-05 16:02:03 +0800116 private EventuallyConsistentMap<TenantId, SegmentationId> l3vniTenantMap;
117 private EventuallyConsistentMap<TenantRouter, SegmentationId> l3vniTenantRouterMap;
jiangruif675cd42015-11-27 15:03:59 +0800118 private EventuallyConsistentMap<TenantId, Set<DeviceId>> classifierOvsMap;
119 private EventuallyConsistentMap<TenantId, Set<DeviceId>> sffOvsMap;
120
121 private static final String IFACEID = "ifaceid";
122 private static final String RUNNELOPTOPOIC = "tunnel-ops-ids";
jiangruif675cd42015-11-27 15:03:59 +0800123 private static final String EVENT_NOT_NULL = "event cannot be null";
124 private static final String TENANTID_NOT_NULL = "tenantId cannot be null";
125 private static final String DEVICEID_NOT_NULL = "deviceId cannot be null";
lishuaib43dbf72016-01-06 11:11:35 +0800126 private static final String VIRTUALPORTID_NOT_NULL = "virtualPortId cannot be null";
127 private static final String HOST_NOT_NULL = "host cannot be null";
lishuai8798bbe2016-05-05 16:02:03 +0800128 private static final String L3VNITENANTMAP = "l3vniTenantMap";
129 private static final String L3VNITENANTROUTERMAP = "l3vniTenantRouterMap";
jiangruif675cd42015-11-27 15:03:59 +0800130 private static final String CLASSIFIEROVSMAP = "classifierOvsMap";
131 private static final String SFFOVSMAP = "sffOvsMap";
132
133 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
134 protected RouterService routerService;
135 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
136 protected FloatingIpService floatingIpService;
137 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
138 protected RouterInterfaceService routerInterfaceService;
139 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
140 protected VirtualPortService virtualPortService;
141 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
142 protected HostService hostService;
143 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
144 protected SubnetService subnetService;
145 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
146 protected TenantNetworkService tenantNetworkService;
147 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
148 protected DeviceService deviceService;
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530149 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
150 protected PortPairService portPairService;
151 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
152 protected PortPairGroupService portPairGroupService;
153 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
154 protected FlowClassifierService flowClassifierService;
155 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
156 protected PortChainService portChainService;
jiangruif675cd42015-11-27 15:03:59 +0800157
158 @Activate
159 public void activate() {
Phaneendra Manda5f326872015-12-07 19:25:09 +0530160 eventDispatcher.addSink(VtnRscEvent.class, listenerRegistry);
jiangruif675cd42015-11-27 15:03:59 +0800161 floatingIpService.addListener(floatingIpListener);
162 routerService.addListener(routerListener);
163 routerInterfaceService.addListener(routerInterfaceListener);
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530164 portPairService.addListener(portPairListener);
165 portPairGroupService.addListener(portPairGroupListener);
166 flowClassifierService.addListener(flowClassifierListener);
167 portChainService.addListener(portChainListener);
jiangruif675cd42015-11-27 15:03:59 +0800168
169 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
170 .register(KryoNamespaces.API)
Wu wenbinacc10ea2016-05-06 16:48:56 +0800171 .register(TenantId.class, DeviceId.class, SegmentationId.class,
172 TenantRouter.class, RouterId.class);
lishuai8798bbe2016-05-05 16:02:03 +0800173 l3vniTenantMap = storageService
jiangruif675cd42015-11-27 15:03:59 +0800174 .<TenantId, SegmentationId>eventuallyConsistentMapBuilder()
lishuai8798bbe2016-05-05 16:02:03 +0800175 .withName(L3VNITENANTMAP).withSerializer(serializer)
176 .withTimestampProvider((k, v) -> clockService.getTimestamp())
177 .build();
178
179 l3vniTenantRouterMap = storageService
180 .<TenantRouter, SegmentationId>eventuallyConsistentMapBuilder()
181 .withName(L3VNITENANTROUTERMAP).withSerializer(serializer)
jiangruif675cd42015-11-27 15:03:59 +0800182 .withTimestampProvider((k, v) -> clockService.getTimestamp())
183 .build();
184
185 classifierOvsMap = storageService
186 .<TenantId, Set<DeviceId>>eventuallyConsistentMapBuilder()
187 .withName(CLASSIFIEROVSMAP).withSerializer(serializer)
188 .withTimestampProvider((k, v) -> clockService.getTimestamp())
189 .build();
190
191 sffOvsMap = storageService
192 .<TenantId, Set<DeviceId>>eventuallyConsistentMapBuilder()
193 .withName(SFFOVSMAP).withSerializer(serializer)
194 .withTimestampProvider((k, v) -> clockService.getTimestamp())
195 .build();
196 }
197
198 @Deactivate
199 public void deactivate() {
Phaneendra Manda5f326872015-12-07 19:25:09 +0530200 eventDispatcher.removeSink(VtnRscEvent.class);
jiangruif675cd42015-11-27 15:03:59 +0800201 floatingIpService.removeListener(floatingIpListener);
202 routerService.removeListener(routerListener);
203 routerInterfaceService.removeListener(routerInterfaceListener);
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530204 portPairService.removeListener(portPairListener);
205 portPairGroupService.removeListener(portPairGroupListener);
206 flowClassifierService.removeListener(flowClassifierListener);
207 portChainService.removeListener(portChainListener);
208
lishuai8798bbe2016-05-05 16:02:03 +0800209 l3vniTenantMap.destroy();
210 l3vniTenantRouterMap.destroy();
jiangruif675cd42015-11-27 15:03:59 +0800211 classifierOvsMap.destroy();
212 sffOvsMap.destroy();
jiangruif675cd42015-11-27 15:03:59 +0800213 log.info("Stopped");
214 }
215
216 @Override
jiangruif675cd42015-11-27 15:03:59 +0800217 public SegmentationId getL3vni(TenantId tenantId) {
218 checkNotNull(tenantId, "tenantId cannot be null");
lishuai8798bbe2016-05-05 16:02:03 +0800219 SegmentationId l3vni = l3vniTenantMap.get(tenantId);
jiangruif675cd42015-11-27 15:03:59 +0800220 if (l3vni == null) {
221 long segmentationId = coreService.getIdGenerator(RUNNELOPTOPOIC)
222 .getNewId();
223 l3vni = SegmentationId.segmentationId(String
224 .valueOf(segmentationId));
lishuai8798bbe2016-05-05 16:02:03 +0800225 l3vniTenantMap.put(tenantId, l3vni);
226 }
227 return l3vni;
228 }
229
230 @Override
231 public SegmentationId getL3vni(TenantRouter tenantRouter) {
232 checkNotNull(tenantRouter, "tenantRouter cannot be null");
233 SegmentationId l3vni = l3vniTenantRouterMap.get(tenantRouter);
234 if (l3vni == null) {
235 long segmentationId = coreService.getIdGenerator(RUNNELOPTOPOIC)
236 .getNewId();
237 l3vni = SegmentationId.segmentationId(String
238 .valueOf(segmentationId));
239 l3vniTenantRouterMap.put(tenantRouter, l3vni);
jiangruif675cd42015-11-27 15:03:59 +0800240 }
241 return l3vni;
242 }
243
jiangruif675cd42015-11-27 15:03:59 +0800244 private class InnerFloatingIpListener implements FloatingIpListener {
245
246 @Override
247 public void event(FloatingIpEvent event) {
248 checkNotNull(event, EVENT_NOT_NULL);
249 FloatingIp floatingIp = event.subject();
250 if (FloatingIpEvent.Type.FLOATINGIP_PUT == event.type()) {
251 notifyListeners(new VtnRscEvent(
252 VtnRscEvent.Type.FLOATINGIP_PUT,
253 new VtnRscEventFeedback(
254 floatingIp)));
255 }
256 if (FloatingIpEvent.Type.FLOATINGIP_DELETE == event.type()) {
257 notifyListeners(new VtnRscEvent(
258 VtnRscEvent.Type.FLOATINGIP_DELETE,
259 new VtnRscEventFeedback(
260 floatingIp)));
261 }
lishuai762df812016-01-08 11:51:15 +0800262 if (FloatingIpEvent.Type.FLOATINGIP_BIND == event.type()) {
263 notifyListeners(new VtnRscEvent(
264 VtnRscEvent.Type.FLOATINGIP_BIND,
265 new VtnRscEventFeedback(
266 floatingIp)));
267 }
268 if (FloatingIpEvent.Type.FLOATINGIP_UNBIND == event.type()) {
269 notifyListeners(new VtnRscEvent(
270 VtnRscEvent.Type.FLOATINGIP_UNBIND,
271 new VtnRscEventFeedback(
272 floatingIp)));
273 }
jiangruif675cd42015-11-27 15:03:59 +0800274 }
275 }
276
277 private class InnerRouterListener implements RouterListener {
278
279 @Override
280 public void event(RouterEvent event) {
281 checkNotNull(event, EVENT_NOT_NULL);
282 Router router = event.subject();
283 if (RouterEvent.Type.ROUTER_PUT == event.type()) {
284 notifyListeners(new VtnRscEvent(VtnRscEvent.Type.ROUTER_PUT,
285 new VtnRscEventFeedback(router)));
286 }
287 if (RouterEvent.Type.ROUTER_DELETE == event.type()) {
288 notifyListeners(new VtnRscEvent(VtnRscEvent.Type.ROUTER_DELETE,
289 new VtnRscEventFeedback(router)));
290 }
291 }
292 }
293
294 private class InnerRouterInterfaceListener
295 implements RouterInterfaceListener {
296
297 @Override
298 public void event(RouterInterfaceEvent event) {
299 checkNotNull(event, EVENT_NOT_NULL);
300 RouterInterface routerInterface = event.subject();
301 if (RouterInterfaceEvent.Type.ROUTER_INTERFACE_PUT == event.type()) {
302 notifyListeners(new VtnRscEvent(
303 VtnRscEvent.Type.ROUTER_INTERFACE_PUT,
304 new VtnRscEventFeedback(
305 routerInterface)));
306 }
307 if (RouterInterfaceEvent.Type.ROUTER_INTERFACE_DELETE == event
308 .type()) {
309 notifyListeners(new VtnRscEvent(
310 VtnRscEvent.Type.ROUTER_INTERFACE_DELETE,
311 new VtnRscEventFeedback(
312 routerInterface)));
313 }
314 }
315 }
316
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530317 private class InnerPortPairListener implements PortPairListener {
318
319 @Override
320 public void event(PortPairEvent event) {
321 checkNotNull(event, EVENT_NOT_NULL);
322 PortPair portPair = event.subject();
323 if (PortPairEvent.Type.PORT_PAIR_PUT == event.type()) {
324 notifyListeners(new VtnRscEvent(VtnRscEvent.Type.PORT_PAIR_PUT,
325 new VtnRscEventFeedback(portPair)));
326 } else if (PortPairEvent.Type.PORT_PAIR_DELETE == event.type()) {
327 notifyListeners(new VtnRscEvent(
328 VtnRscEvent.Type.PORT_PAIR_DELETE,
329 new VtnRscEventFeedback(portPair)));
330 } else if (PortPairEvent.Type.PORT_PAIR_UPDATE == event.type()) {
331 notifyListeners(new VtnRscEvent(
332 VtnRscEvent.Type.PORT_PAIR_UPDATE,
333 new VtnRscEventFeedback(portPair)));
334 }
335 }
336 }
337
338 private class InnerPortPairGroupListener implements PortPairGroupListener {
339
340 @Override
341 public void event(PortPairGroupEvent event) {
342 checkNotNull(event, EVENT_NOT_NULL);
343 PortPairGroup portPairGroup = event.subject();
344 if (PortPairGroupEvent.Type.PORT_PAIR_GROUP_PUT == event.type()) {
345 notifyListeners(new VtnRscEvent(
346 VtnRscEvent.Type.PORT_PAIR_GROUP_PUT,
347 new VtnRscEventFeedback(portPairGroup)));
348 } else if (PortPairGroupEvent.Type.PORT_PAIR_GROUP_DELETE == event.type()) {
349 notifyListeners(new VtnRscEvent(
350 VtnRscEvent.Type.PORT_PAIR_GROUP_DELETE,
351 new VtnRscEventFeedback(portPairGroup)));
352 } else if (PortPairGroupEvent.Type.PORT_PAIR_GROUP_UPDATE == event.type()) {
353 notifyListeners(new VtnRscEvent(
354 VtnRscEvent.Type.PORT_PAIR_GROUP_UPDATE,
355 new VtnRscEventFeedback(portPairGroup)));
356 }
357 }
358 }
359
360 private class InnerFlowClassifierListener implements FlowClassifierListener {
361
362 @Override
363 public void event(FlowClassifierEvent event) {
364 checkNotNull(event, EVENT_NOT_NULL);
365 FlowClassifier flowClassifier = event.subject();
366 if (FlowClassifierEvent.Type.FLOW_CLASSIFIER_PUT == event.type()) {
367 notifyListeners(new VtnRscEvent(
368 VtnRscEvent.Type.FLOW_CLASSIFIER_PUT,
369 new VtnRscEventFeedback(flowClassifier)));
370 } else if (FlowClassifierEvent.Type.FLOW_CLASSIFIER_DELETE == event.type()) {
371 notifyListeners(new VtnRscEvent(
372 VtnRscEvent.Type.FLOW_CLASSIFIER_DELETE,
373 new VtnRscEventFeedback(flowClassifier)));
374 } else if (FlowClassifierEvent.Type.FLOW_CLASSIFIER_UPDATE == event.type()) {
375 notifyListeners(new VtnRscEvent(
376 VtnRscEvent.Type.FLOW_CLASSIFIER_UPDATE,
377 new VtnRscEventFeedback(flowClassifier)));
378 }
379 }
380 }
381
382 private class InnerPortChainListener implements PortChainListener {
383
384 @Override
385 public void event(PortChainEvent event) {
386 checkNotNull(event, EVENT_NOT_NULL);
387 PortChain portChain = event.subject();
388 if (PortChainEvent.Type.PORT_CHAIN_PUT == event.type()) {
389 notifyListeners(new VtnRscEvent(
390 VtnRscEvent.Type.PORT_CHAIN_PUT,
391 new VtnRscEventFeedback(portChain)));
392 } else if (PortChainEvent.Type.PORT_CHAIN_DELETE == event.type()) {
393 notifyListeners(new VtnRscEvent(
394 VtnRscEvent.Type.PORT_CHAIN_DELETE,
395 new VtnRscEventFeedback(portChain)));
396 } else if (PortChainEvent.Type.PORT_CHAIN_UPDATE == event.type()) {
397 notifyListeners(new VtnRscEvent(
398 VtnRscEvent.Type.PORT_CHAIN_UPDATE,
399 new VtnRscEventFeedback(portChain)));
400 }
401 }
402 }
403
jiangruif675cd42015-11-27 15:03:59 +0800404 @Override
405 public Iterator<Device> getClassifierOfTenant(TenantId tenantId) {
406 checkNotNull(tenantId, TENANTID_NOT_NULL);
407 Set<DeviceId> deviceIdSet = classifierOvsMap.get(tenantId);
408 Set<Device> deviceSet = new HashSet<>();
409 if (deviceIdSet != null) {
410 for (DeviceId deviceId : deviceIdSet) {
411 deviceSet.add(deviceService.getDevice(deviceId));
412 }
413 }
414 return deviceSet.iterator();
415 }
416
417 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -0700418 public Iterator<Device> getSffOfTenant(TenantId tenantId) {
jiangruif675cd42015-11-27 15:03:59 +0800419 checkNotNull(tenantId, TENANTID_NOT_NULL);
420 Set<DeviceId> deviceIdSet = sffOvsMap.get(tenantId);
421 Set<Device> deviceSet = new HashSet<>();
422 if (deviceIdSet != null) {
423 for (DeviceId deviceId : deviceIdSet) {
424 deviceSet.add(deviceService.getDevice(deviceId));
425 }
426 }
427 return deviceSet.iterator();
428 }
429
430 @Override
431 public MacAddress getGatewayMac(HostId hostId) {
432 checkNotNull(hostId, "hostId cannot be null");
433 Host host = hostService.getHost(hostId);
434 String ifaceId = host.annotations().value(IFACEID);
435 VirtualPortId hPortId = VirtualPortId.portId(ifaceId);
436 VirtualPort hPort = virtualPortService.getPort(hPortId);
437 SubnetId subnetId = hPort.fixedIps().iterator().next().subnetId();
438 Subnet subnet = subnetService.getSubnet(subnetId);
439 IpAddress gatewayIp = subnet.gatewayIp();
440 Iterable<VirtualPort> virtualPorts = virtualPortService.getPorts();
441 MacAddress macAddress = null;
442 for (VirtualPort port : virtualPorts) {
443 Set<FixedIp> fixedIpSet = port.fixedIps();
444 for (FixedIp fixedIp : fixedIpSet) {
445 if (fixedIp.ip().equals(gatewayIp)) {
446 macAddress = port.macAddress();
447 }
448 }
449 }
450 return macAddress;
451 }
452
453 @Override
454 public boolean isServiceFunction(VirtualPortId portId) {
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530455 return portPairService.exists(PortPairId.of(portId.portId()));
jiangruif675cd42015-11-27 15:03:59 +0800456 }
457
458 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -0700459 public DeviceId getSfToSffMaping(VirtualPortId portId) {
jiangruif675cd42015-11-27 15:03:59 +0800460 checkNotNull(portId, "portId cannot be null");
461 VirtualPort vmPort = virtualPortService.getPort(portId);
462 Set<Host> hostSet = hostService.getHostsByMac(vmPort.macAddress());
463 for (Host host : hostSet) {
Satish K01a60f22015-11-28 15:21:42 +0530464 if (host.annotations().value(IFACEID).equals(vmPort.portId().portId())) {
jiangruif675cd42015-11-27 15:03:59 +0800465 return host.location().deviceId();
466 }
467 }
468 return null;
469 }
470
lishuaib43dbf72016-01-06 11:11:35 +0800471 @Override
472 public void addDeviceIdOfOvsMap(VirtualPortId virtualPortId,
473 TenantId tenantId, DeviceId deviceId) {
474 checkNotNull(virtualPortId, VIRTUALPORTID_NOT_NULL);
475 checkNotNull(tenantId, TENANTID_NOT_NULL);
476 checkNotNull(deviceId, DEVICEID_NOT_NULL);
477 if (isServiceFunction(virtualPortId)) {
478 addDeviceIdToSpecificMap(tenantId, deviceId, sffOvsMap);
479 } else {
480 addDeviceIdToSpecificMap(tenantId, deviceId, classifierOvsMap);
481 }
482 }
483
484 @Override
485 public void removeDeviceIdOfOvsMap(Host host, TenantId tenantId, DeviceId deviceId) {
486 checkNotNull(host, HOST_NOT_NULL);
487 checkNotNull(tenantId, TENANTID_NOT_NULL);
488 checkNotNull(deviceId, DEVICEID_NOT_NULL);
489 if (isLastSFHostOfTenant(host, deviceId, tenantId)) {
490 removeDeviceIdToSpecificMap(tenantId, deviceId, sffOvsMap);
491 }
492 if (isLastClassifierHostOfTenant(host, deviceId, tenantId)) {
493 removeDeviceIdToSpecificMap(tenantId, deviceId, classifierOvsMap);
494 }
495 }
496
jiangruif675cd42015-11-27 15:03:59 +0800497 /**
498 * Checks whether the last Service Function host of a specific tenant in
499 * this device.
500 *
501 * @param host the host on device
502 * @param deviceId the device identifier
503 * @param tenantId the tenant identifier
504 * @return true or false
505 */
506 private boolean isLastSFHostOfTenant(Host host, DeviceId deviceId,
507 TenantId tenantId) {
jiangruif675cd42015-11-27 15:03:59 +0800508 Set<Host> hostSet = hostService.getConnectedHosts(deviceId);
lishuai1fbd41b2015-12-10 16:44:57 +0800509 if (hostSet != null) {
510 for (Host h : hostSet) {
511 String ifaceId = h.annotations().value(IFACEID);
512 if (ifaceId != null) {
513 VirtualPortId hPortId = VirtualPortId.portId(ifaceId);
514 if (virtualPortService.getPort(hPortId).tenantId().tenantId()
515 .equals(tenantId.tenantId())
516 && isServiceFunction(hPortId)) {
lishuaib43dbf72016-01-06 11:11:35 +0800517 if (!h.equals(host)) {
518 return false;
519 }
lishuai1fbd41b2015-12-10 16:44:57 +0800520 }
jiangruif675cd42015-11-27 15:03:59 +0800521 }
522 }
523 }
lishuaib43dbf72016-01-06 11:11:35 +0800524 return true;
jiangruif675cd42015-11-27 15:03:59 +0800525 }
526
527 /**
528 * Checks whether the last Classifier host of a specific tenant in this
529 * device.
530 *
531 * @param host the host on device
532 * @param deviceId the device identifier
533 * @param tenantId the tenant identifier
534 * @return true or false
535 */
536 private boolean isLastClassifierHostOfTenant(Host host, DeviceId deviceId,
537 TenantId tenantId) {
jiangruif675cd42015-11-27 15:03:59 +0800538 Set<Host> hostSet = hostService.getConnectedHosts(deviceId);
lishuai1fbd41b2015-12-10 16:44:57 +0800539 if (hostSet != null) {
540 for (Host h : hostSet) {
541 String ifaceId = h.annotations().value(IFACEID);
542 if (ifaceId != null) {
543 VirtualPortId hPortId = VirtualPortId.portId(ifaceId);
544 if (virtualPortService.getPort(hPortId).tenantId().tenantId()
545 .equals(tenantId.tenantId())
546 && !isServiceFunction(hPortId)) {
lishuaib43dbf72016-01-06 11:11:35 +0800547 if (!h.equals(host)) {
548 return false;
549 }
lishuai1fbd41b2015-12-10 16:44:57 +0800550 }
jiangruif675cd42015-11-27 15:03:59 +0800551 }
552 }
553 }
lishuaib43dbf72016-01-06 11:11:35 +0800554 return true;
jiangruif675cd42015-11-27 15:03:59 +0800555 }
556
557 /**
558 * Adds specify Device identifier to OvsMap.
559 *
560 * @param tenantId the tenant identifier
561 * @param deviceId the device identifier
562 * @param ovsMap the instance of map to store device identifier
563 */
lishuaib43dbf72016-01-06 11:11:35 +0800564 private void addDeviceIdToSpecificMap(TenantId tenantId,
jiangruif675cd42015-11-27 15:03:59 +0800565 DeviceId deviceId,
566 EventuallyConsistentMap<TenantId, Set<DeviceId>> ovsMap) {
jiangruif675cd42015-11-27 15:03:59 +0800567 if (ovsMap.containsKey(tenantId)) {
568 Set<DeviceId> deviceIdSet = ovsMap.get(tenantId);
569 deviceIdSet.add(deviceId);
570 ovsMap.put(tenantId, deviceIdSet);
571 } else {
572 Set<DeviceId> deviceIdSet = new HashSet<>();
573 deviceIdSet.add(deviceId);
574 ovsMap.put(tenantId, deviceIdSet);
575 }
576 }
577
578 /**
579 * Removes specify Device identifier from 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 removeDeviceIdToSpecificMap(TenantId tenantId,
jiangruif675cd42015-11-27 15:03:59 +0800586 DeviceId deviceId,
587 EventuallyConsistentMap<TenantId, Set<DeviceId>> ovsMap) {
jiangruif675cd42015-11-27 15:03:59 +0800588 Set<DeviceId> deviceIdSet = ovsMap.get(tenantId);
lishuaib43dbf72016-01-06 11:11:35 +0800589 if (deviceIdSet != null && deviceIdSet.size() > 1) {
jiangruif675cd42015-11-27 15:03:59 +0800590 deviceIdSet.remove(deviceId);
591 ovsMap.put(tenantId, deviceIdSet);
592 } else {
593 ovsMap.remove(tenantId);
594 }
595 }
596
597 /**
598 * Notifies specify event to all listeners.
599 *
600 * @param event VtnRsc event
601 */
602 private void notifyListeners(VtnRscEvent event) {
603 checkNotNull(event, EVENT_NOT_NULL);
Mahesh Poojary Scc11f722015-11-29 16:09:56 +0530604 post(event);
jiangruif675cd42015-11-27 15:03:59 +0800605 }
606}