jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 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 | package org.onosproject.vtnrsc.service.impl; |
| 17 | |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 18 | import org.onlab.packet.IpAddress; |
| 19 | import org.onlab.packet.MacAddress; |
| 20 | import org.onlab.util.KryoNamespace; |
| 21 | import org.onosproject.core.CoreService; |
Mahesh Poojary S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 22 | import org.onosproject.event.AbstractListenerManager; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 23 | import org.onosproject.net.Device; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.Host; |
| 26 | import org.onosproject.net.HostId; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 27 | import org.onosproject.net.device.DeviceService; |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 28 | import org.onosproject.net.host.HostService; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 29 | import org.onosproject.store.serializers.KryoNamespaces; |
| 30 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 31 | import org.onosproject.store.service.LogicalClockService; |
| 32 | import org.onosproject.store.service.StorageService; |
| 33 | import org.onosproject.vtnrsc.FixedIp; |
| 34 | import org.onosproject.vtnrsc.FloatingIp; |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 35 | import org.onosproject.vtnrsc.FlowClassifier; |
| 36 | import org.onosproject.vtnrsc.PortChain; |
| 37 | import org.onosproject.vtnrsc.PortPair; |
| 38 | import org.onosproject.vtnrsc.PortPairGroup; |
| 39 | import org.onosproject.vtnrsc.PortPairId; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 40 | import org.onosproject.vtnrsc.Router; |
Wu wenbin | acc10ea | 2016-05-06 16:48:56 +0800 | [diff] [blame] | 41 | import org.onosproject.vtnrsc.RouterId; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 42 | import org.onosproject.vtnrsc.RouterInterface; |
| 43 | import org.onosproject.vtnrsc.SegmentationId; |
| 44 | import org.onosproject.vtnrsc.Subnet; |
| 45 | import org.onosproject.vtnrsc.SubnetId; |
| 46 | import org.onosproject.vtnrsc.TenantId; |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 47 | import org.onosproject.vtnrsc.TenantRouter; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 48 | import org.onosproject.vtnrsc.VirtualPort; |
| 49 | import org.onosproject.vtnrsc.VirtualPortId; |
| 50 | import org.onosproject.vtnrsc.event.VtnRscEvent; |
| 51 | import org.onosproject.vtnrsc.event.VtnRscEventFeedback; |
| 52 | import org.onosproject.vtnrsc.event.VtnRscListener; |
| 53 | import org.onosproject.vtnrsc.floatingip.FloatingIpEvent; |
| 54 | import org.onosproject.vtnrsc.floatingip.FloatingIpListener; |
| 55 | import org.onosproject.vtnrsc.floatingip.FloatingIpService; |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 56 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierEvent; |
| 57 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierListener; |
| 58 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService; |
| 59 | import org.onosproject.vtnrsc.portchain.PortChainEvent; |
| 60 | import org.onosproject.vtnrsc.portchain.PortChainListener; |
| 61 | import org.onosproject.vtnrsc.portchain.PortChainService; |
| 62 | import org.onosproject.vtnrsc.portpair.PortPairEvent; |
| 63 | import org.onosproject.vtnrsc.portpair.PortPairListener; |
| 64 | import org.onosproject.vtnrsc.portpair.PortPairService; |
| 65 | import org.onosproject.vtnrsc.portpairgroup.PortPairGroupEvent; |
| 66 | import org.onosproject.vtnrsc.portpairgroup.PortPairGroupListener; |
| 67 | import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 68 | import org.onosproject.vtnrsc.router.RouterEvent; |
| 69 | import org.onosproject.vtnrsc.router.RouterListener; |
| 70 | import org.onosproject.vtnrsc.router.RouterService; |
| 71 | import org.onosproject.vtnrsc.routerinterface.RouterInterfaceEvent; |
| 72 | import org.onosproject.vtnrsc.routerinterface.RouterInterfaceListener; |
| 73 | import org.onosproject.vtnrsc.routerinterface.RouterInterfaceService; |
| 74 | import org.onosproject.vtnrsc.service.VtnRscService; |
| 75 | import org.onosproject.vtnrsc.subnet.SubnetService; |
| 76 | import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService; |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 77 | import org.onosproject.vtnrsc.virtualport.VirtualPortEvent; |
| 78 | import org.onosproject.vtnrsc.virtualport.VirtualPortListener; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 79 | import org.onosproject.vtnrsc.virtualport.VirtualPortService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 80 | import org.osgi.service.component.annotations.Activate; |
| 81 | import org.osgi.service.component.annotations.Component; |
| 82 | import org.osgi.service.component.annotations.Deactivate; |
| 83 | import org.osgi.service.component.annotations.Reference; |
| 84 | import org.osgi.service.component.annotations.ReferenceCardinality; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 85 | import org.slf4j.Logger; |
| 86 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 87 | import java.util.HashSet; |
| 88 | import java.util.Iterator; |
| 89 | import java.util.Set; |
| 90 | |
| 91 | import static com.google.common.base.Preconditions.checkNotNull; |
| 92 | import static org.slf4j.LoggerFactory.getLogger; |
| 93 | |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 94 | /** |
| 95 | * Provides implementation of the VtnRsc service. |
| 96 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 97 | @Component(immediate = true, service = VtnRscService.class) |
Mahesh Poojary S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 98 | public class VtnRscManager extends AbstractListenerManager<VtnRscEvent, VtnRscListener> |
| 99 | implements VtnRscService { |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 101 | protected CoreService coreService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 103 | protected StorageService storageService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 105 | protected LogicalClockService clockService; |
| 106 | |
| 107 | private final Logger log = getLogger(getClass()); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 108 | private FloatingIpListener floatingIpListener = new InnerFloatingIpListener(); |
| 109 | private RouterListener routerListener = new InnerRouterListener(); |
| 110 | private RouterInterfaceListener routerInterfaceListener = new InnerRouterInterfaceListener(); |
Mahesh Poojary S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 111 | private PortPairListener portPairListener = new InnerPortPairListener(); |
| 112 | private PortPairGroupListener portPairGroupListener = new InnerPortPairGroupListener(); |
| 113 | private FlowClassifierListener flowClassifierListener = new InnerFlowClassifierListener(); |
| 114 | private PortChainListener portChainListener = new InnerPortChainListener(); |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 115 | private VirtualPortListener virtualPortListener = new InnerVirtualPortListener(); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 116 | |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 117 | private EventuallyConsistentMap<TenantId, SegmentationId> l3vniTenantMap; |
| 118 | private EventuallyConsistentMap<TenantRouter, SegmentationId> l3vniTenantRouterMap; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 119 | 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"; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 124 | 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"; |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 127 | private static final String VIRTUALPORTID_NOT_NULL = "virtualPortId cannot be null"; |
| 128 | private static final String HOST_NOT_NULL = "host cannot be null"; |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 129 | private static final String L3VNITENANTMAP = "l3vniTenantMap"; |
| 130 | private static final String L3VNITENANTROUTERMAP = "l3vniTenantRouterMap"; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 131 | private static final String CLASSIFIEROVSMAP = "classifierOvsMap"; |
| 132 | private static final String SFFOVSMAP = "sffOvsMap"; |
| 133 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 134 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 135 | protected RouterService routerService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 136 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 137 | protected FloatingIpService floatingIpService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 138 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 139 | protected RouterInterfaceService routerInterfaceService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 140 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 141 | protected VirtualPortService virtualPortService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 142 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 143 | protected HostService hostService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 144 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 145 | protected SubnetService subnetService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 146 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 147 | protected TenantNetworkService tenantNetworkService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 148 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 149 | protected DeviceService deviceService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 150 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Mahesh Poojary S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 151 | protected PortPairService portPairService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 152 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Mahesh Poojary S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 153 | protected PortPairGroupService portPairGroupService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 154 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Mahesh Poojary S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 155 | protected FlowClassifierService flowClassifierService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 156 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Mahesh Poojary S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 157 | protected PortChainService portChainService; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 158 | |
| 159 | @Activate |
| 160 | public void activate() { |
Phaneendra Manda | 5f32687 | 2015-12-07 19:25:09 +0530 | [diff] [blame] | 161 | eventDispatcher.addSink(VtnRscEvent.class, listenerRegistry); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 162 | floatingIpService.addListener(floatingIpListener); |
| 163 | routerService.addListener(routerListener); |
| 164 | routerInterfaceService.addListener(routerInterfaceListener); |
Mahesh Poojary S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 165 | portPairService.addListener(portPairListener); |
| 166 | portPairGroupService.addListener(portPairGroupListener); |
| 167 | flowClassifierService.addListener(flowClassifierListener); |
| 168 | portChainService.addListener(portChainListener); |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 169 | virtualPortService.addListener(virtualPortListener); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 170 | |
| 171 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 172 | .register(KryoNamespaces.API) |
Ray Milkey | 5dae922 | 2017-06-15 11:55:51 -0700 | [diff] [blame] | 173 | .register(TenantId.class, SegmentationId.class, |
Wu wenbin | acc10ea | 2016-05-06 16:48:56 +0800 | [diff] [blame] | 174 | TenantRouter.class, RouterId.class); |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 175 | l3vniTenantMap = storageService |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 176 | .<TenantId, SegmentationId>eventuallyConsistentMapBuilder() |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 177 | .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) |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 184 | .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 Manda | 5f32687 | 2015-12-07 19:25:09 +0530 | [diff] [blame] | 202 | eventDispatcher.removeSink(VtnRscEvent.class); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 203 | floatingIpService.removeListener(floatingIpListener); |
| 204 | routerService.removeListener(routerListener); |
| 205 | routerInterfaceService.removeListener(routerInterfaceListener); |
Mahesh Poojary S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 206 | portPairService.removeListener(portPairListener); |
| 207 | portPairGroupService.removeListener(portPairGroupListener); |
| 208 | flowClassifierService.removeListener(flowClassifierListener); |
| 209 | portChainService.removeListener(portChainListener); |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 210 | virtualPortService.removeListener(virtualPortListener); |
Mahesh Poojary S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 211 | |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 212 | l3vniTenantMap.destroy(); |
| 213 | l3vniTenantRouterMap.destroy(); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 214 | classifierOvsMap.destroy(); |
| 215 | sffOvsMap.destroy(); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 216 | log.info("Stopped"); |
| 217 | } |
| 218 | |
| 219 | @Override |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 220 | public SegmentationId getL3vni(TenantId tenantId) { |
| 221 | checkNotNull(tenantId, "tenantId cannot be null"); |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 222 | SegmentationId l3vni = l3vniTenantMap.get(tenantId); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 223 | if (l3vni == null) { |
| 224 | long segmentationId = coreService.getIdGenerator(RUNNELOPTOPOIC) |
| 225 | .getNewId(); |
| 226 | l3vni = SegmentationId.segmentationId(String |
| 227 | .valueOf(segmentationId)); |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 228 | 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); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 243 | } |
| 244 | return l3vni; |
| 245 | } |
| 246 | |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 247 | 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 | } |
lishuai | 762df81 | 2016-01-08 11:51:15 +0800 | [diff] [blame] | 265 | 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 | } |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 277 | } |
| 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 S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 320 | 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 Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 407 | 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 | |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 423 | @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 Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 437 | public Iterator<Device> getSffOfTenant(TenantId tenantId) { |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 438 | 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 S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 474 | return portPairService.exists(PortPairId.of(portId.portId())); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | @Override |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 478 | public DeviceId getSfToSffMaping(VirtualPortId portId) { |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 479 | 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 K | 01a60f2 | 2015-11-28 15:21:42 +0530 | [diff] [blame] | 483 | if (host.annotations().value(IFACEID).equals(vmPort.portId().portId())) { |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 484 | return host.location().deviceId(); |
| 485 | } |
| 486 | } |
| 487 | return null; |
| 488 | } |
| 489 | |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 490 | @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 | |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 516 | /** |
| 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) { |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 527 | Set<Host> hostSet = hostService.getConnectedHosts(deviceId); |
lishuai | 1fbd41b | 2015-12-10 16:44:57 +0800 | [diff] [blame] | 528 | 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)) { |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 536 | if (!h.equals(host)) { |
| 537 | return false; |
| 538 | } |
lishuai | 1fbd41b | 2015-12-10 16:44:57 +0800 | [diff] [blame] | 539 | } |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | } |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 543 | return true; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 544 | } |
| 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) { |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 557 | Set<Host> hostSet = hostService.getConnectedHosts(deviceId); |
lishuai | 1fbd41b | 2015-12-10 16:44:57 +0800 | [diff] [blame] | 558 | 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)) { |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 566 | if (!h.equals(host)) { |
| 567 | return false; |
| 568 | } |
lishuai | 1fbd41b | 2015-12-10 16:44:57 +0800 | [diff] [blame] | 569 | } |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | } |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 573 | return true; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 574 | } |
| 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 | */ |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 583 | private void addDeviceIdToSpecificMap(TenantId tenantId, |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 584 | DeviceId deviceId, |
| 585 | EventuallyConsistentMap<TenantId, Set<DeviceId>> ovsMap) { |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 586 | 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 | */ |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 604 | private void removeDeviceIdToSpecificMap(TenantId tenantId, |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 605 | DeviceId deviceId, |
| 606 | EventuallyConsistentMap<TenantId, Set<DeviceId>> ovsMap) { |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 607 | Set<DeviceId> deviceIdSet = ovsMap.get(tenantId); |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 608 | if (deviceIdSet != null && deviceIdSet.size() > 1) { |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 609 | 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 S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 623 | post(event); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 624 | } |
| 625 | } |