jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import java.util.HashSet; |
| 22 | import java.util.Iterator; |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import org.apache.felix.scr.annotations.Activate; |
| 26 | import org.apache.felix.scr.annotations.Component; |
| 27 | import org.apache.felix.scr.annotations.Deactivate; |
| 28 | import org.apache.felix.scr.annotations.Reference; |
| 29 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 30 | import org.apache.felix.scr.annotations.Service; |
| 31 | import org.onlab.packet.IpAddress; |
| 32 | import org.onlab.packet.MacAddress; |
| 33 | import org.onlab.util.KryoNamespace; |
| 34 | import org.onosproject.core.CoreService; |
Mahesh Poojary S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 35 | import org.onosproject.event.AbstractListenerManager; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 36 | import org.onosproject.net.Device; |
| 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.Host; |
| 39 | import org.onosproject.net.HostId; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 40 | import org.onosproject.net.device.DeviceService; |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 41 | import org.onosproject.net.host.HostService; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 42 | import org.onosproject.store.serializers.KryoNamespaces; |
| 43 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 44 | import org.onosproject.store.service.LogicalClockService; |
| 45 | import org.onosproject.store.service.StorageService; |
| 46 | import org.onosproject.vtnrsc.FixedIp; |
| 47 | import org.onosproject.vtnrsc.FloatingIp; |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 48 | import org.onosproject.vtnrsc.FlowClassifier; |
| 49 | import org.onosproject.vtnrsc.PortChain; |
| 50 | import org.onosproject.vtnrsc.PortPair; |
| 51 | import org.onosproject.vtnrsc.PortPairGroup; |
| 52 | import org.onosproject.vtnrsc.PortPairId; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 53 | import org.onosproject.vtnrsc.Router; |
Wu wenbin | acc10ea | 2016-05-06 16:48:56 +0800 | [diff] [blame] | 54 | import org.onosproject.vtnrsc.RouterId; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 55 | import org.onosproject.vtnrsc.RouterInterface; |
| 56 | import org.onosproject.vtnrsc.SegmentationId; |
| 57 | import org.onosproject.vtnrsc.Subnet; |
| 58 | import org.onosproject.vtnrsc.SubnetId; |
| 59 | import org.onosproject.vtnrsc.TenantId; |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 60 | import org.onosproject.vtnrsc.TenantRouter; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 61 | import org.onosproject.vtnrsc.VirtualPort; |
| 62 | import org.onosproject.vtnrsc.VirtualPortId; |
| 63 | import org.onosproject.vtnrsc.event.VtnRscEvent; |
| 64 | import org.onosproject.vtnrsc.event.VtnRscEventFeedback; |
| 65 | import org.onosproject.vtnrsc.event.VtnRscListener; |
| 66 | import org.onosproject.vtnrsc.floatingip.FloatingIpEvent; |
| 67 | import org.onosproject.vtnrsc.floatingip.FloatingIpListener; |
| 68 | import org.onosproject.vtnrsc.floatingip.FloatingIpService; |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 69 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierEvent; |
| 70 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierListener; |
| 71 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService; |
| 72 | import org.onosproject.vtnrsc.portchain.PortChainEvent; |
| 73 | import org.onosproject.vtnrsc.portchain.PortChainListener; |
| 74 | import org.onosproject.vtnrsc.portchain.PortChainService; |
| 75 | import org.onosproject.vtnrsc.portpair.PortPairEvent; |
| 76 | import org.onosproject.vtnrsc.portpair.PortPairListener; |
| 77 | import org.onosproject.vtnrsc.portpair.PortPairService; |
| 78 | import org.onosproject.vtnrsc.portpairgroup.PortPairGroupEvent; |
| 79 | import org.onosproject.vtnrsc.portpairgroup.PortPairGroupListener; |
| 80 | import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 81 | import org.onosproject.vtnrsc.router.RouterEvent; |
| 82 | import org.onosproject.vtnrsc.router.RouterListener; |
| 83 | import org.onosproject.vtnrsc.router.RouterService; |
| 84 | import org.onosproject.vtnrsc.routerinterface.RouterInterfaceEvent; |
| 85 | import org.onosproject.vtnrsc.routerinterface.RouterInterfaceListener; |
| 86 | import org.onosproject.vtnrsc.routerinterface.RouterInterfaceService; |
| 87 | import org.onosproject.vtnrsc.service.VtnRscService; |
| 88 | import org.onosproject.vtnrsc.subnet.SubnetService; |
| 89 | import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService; |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 90 | import org.onosproject.vtnrsc.virtualport.VirtualPortEvent; |
| 91 | import org.onosproject.vtnrsc.virtualport.VirtualPortListener; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 92 | import org.onosproject.vtnrsc.virtualport.VirtualPortService; |
| 93 | import org.slf4j.Logger; |
| 94 | |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 95 | /** |
| 96 | * Provides implementation of the VtnRsc service. |
| 97 | */ |
| 98 | @Component(immediate = true) |
| 99 | @Service |
Mahesh Poojary S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 100 | public class VtnRscManager extends AbstractListenerManager<VtnRscEvent, VtnRscListener> |
| 101 | implements VtnRscService { |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 103 | protected CoreService coreService; |
| 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 105 | protected StorageService storageService; |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected LogicalClockService clockService; |
| 108 | |
| 109 | private final Logger log = getLogger(getClass()); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 110 | private FloatingIpListener floatingIpListener = new InnerFloatingIpListener(); |
| 111 | private RouterListener routerListener = new InnerRouterListener(); |
| 112 | private RouterInterfaceListener routerInterfaceListener = new InnerRouterInterfaceListener(); |
Mahesh Poojary S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 113 | private PortPairListener portPairListener = new InnerPortPairListener(); |
| 114 | private PortPairGroupListener portPairGroupListener = new InnerPortPairGroupListener(); |
| 115 | private FlowClassifierListener flowClassifierListener = new InnerFlowClassifierListener(); |
| 116 | private PortChainListener portChainListener = new InnerPortChainListener(); |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 117 | private VirtualPortListener virtualPortListener = new InnerVirtualPortListener(); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 118 | |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 119 | private EventuallyConsistentMap<TenantId, SegmentationId> l3vniTenantMap; |
| 120 | private EventuallyConsistentMap<TenantRouter, SegmentationId> l3vniTenantRouterMap; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 121 | private EventuallyConsistentMap<TenantId, Set<DeviceId>> classifierOvsMap; |
| 122 | private EventuallyConsistentMap<TenantId, Set<DeviceId>> sffOvsMap; |
| 123 | |
| 124 | private static final String IFACEID = "ifaceid"; |
| 125 | private static final String RUNNELOPTOPOIC = "tunnel-ops-ids"; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 126 | private static final String EVENT_NOT_NULL = "event cannot be null"; |
| 127 | private static final String TENANTID_NOT_NULL = "tenantId cannot be null"; |
| 128 | private static final String DEVICEID_NOT_NULL = "deviceId cannot be null"; |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 129 | private static final String VIRTUALPORTID_NOT_NULL = "virtualPortId cannot be null"; |
| 130 | private static final String HOST_NOT_NULL = "host cannot be null"; |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 131 | private static final String L3VNITENANTMAP = "l3vniTenantMap"; |
| 132 | private static final String L3VNITENANTROUTERMAP = "l3vniTenantRouterMap"; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 133 | private static final String CLASSIFIEROVSMAP = "classifierOvsMap"; |
| 134 | private static final String SFFOVSMAP = "sffOvsMap"; |
| 135 | |
| 136 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 137 | protected RouterService routerService; |
| 138 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 139 | protected FloatingIpService floatingIpService; |
| 140 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 141 | protected RouterInterfaceService routerInterfaceService; |
| 142 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 143 | protected VirtualPortService virtualPortService; |
| 144 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 145 | protected HostService hostService; |
| 146 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 147 | protected SubnetService subnetService; |
| 148 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 149 | protected TenantNetworkService tenantNetworkService; |
| 150 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 151 | protected DeviceService deviceService; |
Mahesh Poojary S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 152 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 153 | protected PortPairService portPairService; |
| 154 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 155 | protected PortPairGroupService portPairGroupService; |
| 156 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 157 | protected FlowClassifierService flowClassifierService; |
| 158 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 159 | protected PortChainService portChainService; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 160 | |
| 161 | @Activate |
| 162 | public void activate() { |
Phaneendra Manda | 5f32687 | 2015-12-07 19:25:09 +0530 | [diff] [blame] | 163 | eventDispatcher.addSink(VtnRscEvent.class, listenerRegistry); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 164 | floatingIpService.addListener(floatingIpListener); |
| 165 | routerService.addListener(routerListener); |
| 166 | routerInterfaceService.addListener(routerInterfaceListener); |
Mahesh Poojary S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 167 | portPairService.addListener(portPairListener); |
| 168 | portPairGroupService.addListener(portPairGroupListener); |
| 169 | flowClassifierService.addListener(flowClassifierListener); |
| 170 | portChainService.addListener(portChainListener); |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 171 | virtualPortService.addListener(virtualPortListener); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 172 | |
| 173 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 174 | .register(KryoNamespaces.API) |
Ray Milkey | 5dae922 | 2017-06-15 11:55:51 -0700 | [diff] [blame^] | 175 | .register(TenantId.class, SegmentationId.class, |
Wu wenbin | acc10ea | 2016-05-06 16:48:56 +0800 | [diff] [blame] | 176 | TenantRouter.class, RouterId.class); |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 177 | l3vniTenantMap = storageService |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 178 | .<TenantId, SegmentationId>eventuallyConsistentMapBuilder() |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 179 | .withName(L3VNITENANTMAP).withSerializer(serializer) |
| 180 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
| 181 | .build(); |
| 182 | |
| 183 | l3vniTenantRouterMap = storageService |
| 184 | .<TenantRouter, SegmentationId>eventuallyConsistentMapBuilder() |
| 185 | .withName(L3VNITENANTROUTERMAP).withSerializer(serializer) |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 186 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
| 187 | .build(); |
| 188 | |
| 189 | classifierOvsMap = storageService |
| 190 | .<TenantId, Set<DeviceId>>eventuallyConsistentMapBuilder() |
| 191 | .withName(CLASSIFIEROVSMAP).withSerializer(serializer) |
| 192 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
| 193 | .build(); |
| 194 | |
| 195 | sffOvsMap = storageService |
| 196 | .<TenantId, Set<DeviceId>>eventuallyConsistentMapBuilder() |
| 197 | .withName(SFFOVSMAP).withSerializer(serializer) |
| 198 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
| 199 | .build(); |
| 200 | } |
| 201 | |
| 202 | @Deactivate |
| 203 | public void deactivate() { |
Phaneendra Manda | 5f32687 | 2015-12-07 19:25:09 +0530 | [diff] [blame] | 204 | eventDispatcher.removeSink(VtnRscEvent.class); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 205 | floatingIpService.removeListener(floatingIpListener); |
| 206 | routerService.removeListener(routerListener); |
| 207 | routerInterfaceService.removeListener(routerInterfaceListener); |
Mahesh Poojary S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 208 | portPairService.removeListener(portPairListener); |
| 209 | portPairGroupService.removeListener(portPairGroupListener); |
| 210 | flowClassifierService.removeListener(flowClassifierListener); |
| 211 | portChainService.removeListener(portChainListener); |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 212 | virtualPortService.removeListener(virtualPortListener); |
Mahesh Poojary S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 213 | |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 214 | l3vniTenantMap.destroy(); |
| 215 | l3vniTenantRouterMap.destroy(); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 216 | classifierOvsMap.destroy(); |
| 217 | sffOvsMap.destroy(); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 218 | log.info("Stopped"); |
| 219 | } |
| 220 | |
| 221 | @Override |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 222 | public SegmentationId getL3vni(TenantId tenantId) { |
| 223 | checkNotNull(tenantId, "tenantId cannot be null"); |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 224 | SegmentationId l3vni = l3vniTenantMap.get(tenantId); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 225 | if (l3vni == null) { |
| 226 | long segmentationId = coreService.getIdGenerator(RUNNELOPTOPOIC) |
| 227 | .getNewId(); |
| 228 | l3vni = SegmentationId.segmentationId(String |
| 229 | .valueOf(segmentationId)); |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 230 | l3vniTenantMap.put(tenantId, l3vni); |
| 231 | } |
| 232 | return l3vni; |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public SegmentationId getL3vni(TenantRouter tenantRouter) { |
| 237 | checkNotNull(tenantRouter, "tenantRouter cannot be null"); |
| 238 | SegmentationId l3vni = l3vniTenantRouterMap.get(tenantRouter); |
| 239 | if (l3vni == null) { |
| 240 | long segmentationId = coreService.getIdGenerator(RUNNELOPTOPOIC) |
| 241 | .getNewId(); |
| 242 | l3vni = SegmentationId.segmentationId(String |
| 243 | .valueOf(segmentationId)); |
| 244 | l3vniTenantRouterMap.put(tenantRouter, l3vni); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 245 | } |
| 246 | return l3vni; |
| 247 | } |
| 248 | |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 249 | private class InnerFloatingIpListener implements FloatingIpListener { |
| 250 | |
| 251 | @Override |
| 252 | public void event(FloatingIpEvent event) { |
| 253 | checkNotNull(event, EVENT_NOT_NULL); |
| 254 | FloatingIp floatingIp = event.subject(); |
| 255 | if (FloatingIpEvent.Type.FLOATINGIP_PUT == event.type()) { |
| 256 | notifyListeners(new VtnRscEvent( |
| 257 | VtnRscEvent.Type.FLOATINGIP_PUT, |
| 258 | new VtnRscEventFeedback( |
| 259 | floatingIp))); |
| 260 | } |
| 261 | if (FloatingIpEvent.Type.FLOATINGIP_DELETE == event.type()) { |
| 262 | notifyListeners(new VtnRscEvent( |
| 263 | VtnRscEvent.Type.FLOATINGIP_DELETE, |
| 264 | new VtnRscEventFeedback( |
| 265 | floatingIp))); |
| 266 | } |
lishuai | 762df81 | 2016-01-08 11:51:15 +0800 | [diff] [blame] | 267 | if (FloatingIpEvent.Type.FLOATINGIP_BIND == event.type()) { |
| 268 | notifyListeners(new VtnRscEvent( |
| 269 | VtnRscEvent.Type.FLOATINGIP_BIND, |
| 270 | new VtnRscEventFeedback( |
| 271 | floatingIp))); |
| 272 | } |
| 273 | if (FloatingIpEvent.Type.FLOATINGIP_UNBIND == event.type()) { |
| 274 | notifyListeners(new VtnRscEvent( |
| 275 | VtnRscEvent.Type.FLOATINGIP_UNBIND, |
| 276 | new VtnRscEventFeedback( |
| 277 | floatingIp))); |
| 278 | } |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | private class InnerRouterListener implements RouterListener { |
| 283 | |
| 284 | @Override |
| 285 | public void event(RouterEvent event) { |
| 286 | checkNotNull(event, EVENT_NOT_NULL); |
| 287 | Router router = event.subject(); |
| 288 | if (RouterEvent.Type.ROUTER_PUT == event.type()) { |
| 289 | notifyListeners(new VtnRscEvent(VtnRscEvent.Type.ROUTER_PUT, |
| 290 | new VtnRscEventFeedback(router))); |
| 291 | } |
| 292 | if (RouterEvent.Type.ROUTER_DELETE == event.type()) { |
| 293 | notifyListeners(new VtnRscEvent(VtnRscEvent.Type.ROUTER_DELETE, |
| 294 | new VtnRscEventFeedback(router))); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | private class InnerRouterInterfaceListener |
| 300 | implements RouterInterfaceListener { |
| 301 | |
| 302 | @Override |
| 303 | public void event(RouterInterfaceEvent event) { |
| 304 | checkNotNull(event, EVENT_NOT_NULL); |
| 305 | RouterInterface routerInterface = event.subject(); |
| 306 | if (RouterInterfaceEvent.Type.ROUTER_INTERFACE_PUT == event.type()) { |
| 307 | notifyListeners(new VtnRscEvent( |
| 308 | VtnRscEvent.Type.ROUTER_INTERFACE_PUT, |
| 309 | new VtnRscEventFeedback( |
| 310 | routerInterface))); |
| 311 | } |
| 312 | if (RouterInterfaceEvent.Type.ROUTER_INTERFACE_DELETE == event |
| 313 | .type()) { |
| 314 | notifyListeners(new VtnRscEvent( |
| 315 | VtnRscEvent.Type.ROUTER_INTERFACE_DELETE, |
| 316 | new VtnRscEventFeedback( |
| 317 | routerInterface))); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
Mahesh Poojary S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 322 | private class InnerPortPairListener implements PortPairListener { |
| 323 | |
| 324 | @Override |
| 325 | public void event(PortPairEvent event) { |
| 326 | checkNotNull(event, EVENT_NOT_NULL); |
| 327 | PortPair portPair = event.subject(); |
| 328 | if (PortPairEvent.Type.PORT_PAIR_PUT == event.type()) { |
| 329 | notifyListeners(new VtnRscEvent(VtnRscEvent.Type.PORT_PAIR_PUT, |
| 330 | new VtnRscEventFeedback(portPair))); |
| 331 | } else if (PortPairEvent.Type.PORT_PAIR_DELETE == event.type()) { |
| 332 | notifyListeners(new VtnRscEvent( |
| 333 | VtnRscEvent.Type.PORT_PAIR_DELETE, |
| 334 | new VtnRscEventFeedback(portPair))); |
| 335 | } else if (PortPairEvent.Type.PORT_PAIR_UPDATE == event.type()) { |
| 336 | notifyListeners(new VtnRscEvent( |
| 337 | VtnRscEvent.Type.PORT_PAIR_UPDATE, |
| 338 | new VtnRscEventFeedback(portPair))); |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | private class InnerPortPairGroupListener implements PortPairGroupListener { |
| 344 | |
| 345 | @Override |
| 346 | public void event(PortPairGroupEvent event) { |
| 347 | checkNotNull(event, EVENT_NOT_NULL); |
| 348 | PortPairGroup portPairGroup = event.subject(); |
| 349 | if (PortPairGroupEvent.Type.PORT_PAIR_GROUP_PUT == event.type()) { |
| 350 | notifyListeners(new VtnRscEvent( |
| 351 | VtnRscEvent.Type.PORT_PAIR_GROUP_PUT, |
| 352 | new VtnRscEventFeedback(portPairGroup))); |
| 353 | } else if (PortPairGroupEvent.Type.PORT_PAIR_GROUP_DELETE == event.type()) { |
| 354 | notifyListeners(new VtnRscEvent( |
| 355 | VtnRscEvent.Type.PORT_PAIR_GROUP_DELETE, |
| 356 | new VtnRscEventFeedback(portPairGroup))); |
| 357 | } else if (PortPairGroupEvent.Type.PORT_PAIR_GROUP_UPDATE == event.type()) { |
| 358 | notifyListeners(new VtnRscEvent( |
| 359 | VtnRscEvent.Type.PORT_PAIR_GROUP_UPDATE, |
| 360 | new VtnRscEventFeedback(portPairGroup))); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | private class InnerFlowClassifierListener implements FlowClassifierListener { |
| 366 | |
| 367 | @Override |
| 368 | public void event(FlowClassifierEvent event) { |
| 369 | checkNotNull(event, EVENT_NOT_NULL); |
| 370 | FlowClassifier flowClassifier = event.subject(); |
| 371 | if (FlowClassifierEvent.Type.FLOW_CLASSIFIER_PUT == event.type()) { |
| 372 | notifyListeners(new VtnRscEvent( |
| 373 | VtnRscEvent.Type.FLOW_CLASSIFIER_PUT, |
| 374 | new VtnRscEventFeedback(flowClassifier))); |
| 375 | } else if (FlowClassifierEvent.Type.FLOW_CLASSIFIER_DELETE == event.type()) { |
| 376 | notifyListeners(new VtnRscEvent( |
| 377 | VtnRscEvent.Type.FLOW_CLASSIFIER_DELETE, |
| 378 | new VtnRscEventFeedback(flowClassifier))); |
| 379 | } else if (FlowClassifierEvent.Type.FLOW_CLASSIFIER_UPDATE == event.type()) { |
| 380 | notifyListeners(new VtnRscEvent( |
| 381 | VtnRscEvent.Type.FLOW_CLASSIFIER_UPDATE, |
| 382 | new VtnRscEventFeedback(flowClassifier))); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | private class InnerPortChainListener implements PortChainListener { |
| 388 | |
| 389 | @Override |
| 390 | public void event(PortChainEvent event) { |
| 391 | checkNotNull(event, EVENT_NOT_NULL); |
| 392 | PortChain portChain = event.subject(); |
| 393 | if (PortChainEvent.Type.PORT_CHAIN_PUT == event.type()) { |
| 394 | notifyListeners(new VtnRscEvent( |
| 395 | VtnRscEvent.Type.PORT_CHAIN_PUT, |
| 396 | new VtnRscEventFeedback(portChain))); |
| 397 | } else if (PortChainEvent.Type.PORT_CHAIN_DELETE == event.type()) { |
| 398 | notifyListeners(new VtnRscEvent( |
| 399 | VtnRscEvent.Type.PORT_CHAIN_DELETE, |
| 400 | new VtnRscEventFeedback(portChain))); |
| 401 | } else if (PortChainEvent.Type.PORT_CHAIN_UPDATE == event.type()) { |
| 402 | notifyListeners(new VtnRscEvent( |
| 403 | VtnRscEvent.Type.PORT_CHAIN_UPDATE, |
| 404 | new VtnRscEventFeedback(portChain))); |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 409 | private class InnerVirtualPortListener implements VirtualPortListener { |
| 410 | |
| 411 | @Override |
| 412 | public void event(VirtualPortEvent event) { |
| 413 | checkNotNull(event, EVENT_NOT_NULL); |
| 414 | VirtualPort virtualPort = event.subject(); |
| 415 | if (VirtualPortEvent.Type.VIRTUAL_PORT_PUT == event.type()) { |
| 416 | notifyListeners(new VtnRscEvent(VtnRscEvent.Type.VIRTUAL_PORT_PUT, |
| 417 | new VtnRscEventFeedback(virtualPort))); |
| 418 | } else if (VirtualPortEvent.Type.VIRTUAL_PORT_DELETE == event.type()) { |
| 419 | notifyListeners(new VtnRscEvent(VtnRscEvent.Type.VIRTUAL_PORT_DELETE, |
| 420 | new VtnRscEventFeedback(virtualPort))); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 425 | @Override |
| 426 | public Iterator<Device> getClassifierOfTenant(TenantId tenantId) { |
| 427 | checkNotNull(tenantId, TENANTID_NOT_NULL); |
| 428 | Set<DeviceId> deviceIdSet = classifierOvsMap.get(tenantId); |
| 429 | Set<Device> deviceSet = new HashSet<>(); |
| 430 | if (deviceIdSet != null) { |
| 431 | for (DeviceId deviceId : deviceIdSet) { |
| 432 | deviceSet.add(deviceService.getDevice(deviceId)); |
| 433 | } |
| 434 | } |
| 435 | return deviceSet.iterator(); |
| 436 | } |
| 437 | |
| 438 | @Override |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 439 | public Iterator<Device> getSffOfTenant(TenantId tenantId) { |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 440 | checkNotNull(tenantId, TENANTID_NOT_NULL); |
| 441 | Set<DeviceId> deviceIdSet = sffOvsMap.get(tenantId); |
| 442 | Set<Device> deviceSet = new HashSet<>(); |
| 443 | if (deviceIdSet != null) { |
| 444 | for (DeviceId deviceId : deviceIdSet) { |
| 445 | deviceSet.add(deviceService.getDevice(deviceId)); |
| 446 | } |
| 447 | } |
| 448 | return deviceSet.iterator(); |
| 449 | } |
| 450 | |
| 451 | @Override |
| 452 | public MacAddress getGatewayMac(HostId hostId) { |
| 453 | checkNotNull(hostId, "hostId cannot be null"); |
| 454 | Host host = hostService.getHost(hostId); |
| 455 | String ifaceId = host.annotations().value(IFACEID); |
| 456 | VirtualPortId hPortId = VirtualPortId.portId(ifaceId); |
| 457 | VirtualPort hPort = virtualPortService.getPort(hPortId); |
| 458 | SubnetId subnetId = hPort.fixedIps().iterator().next().subnetId(); |
| 459 | Subnet subnet = subnetService.getSubnet(subnetId); |
| 460 | IpAddress gatewayIp = subnet.gatewayIp(); |
| 461 | Iterable<VirtualPort> virtualPorts = virtualPortService.getPorts(); |
| 462 | MacAddress macAddress = null; |
| 463 | for (VirtualPort port : virtualPorts) { |
| 464 | Set<FixedIp> fixedIpSet = port.fixedIps(); |
| 465 | for (FixedIp fixedIp : fixedIpSet) { |
| 466 | if (fixedIp.ip().equals(gatewayIp)) { |
| 467 | macAddress = port.macAddress(); |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | return macAddress; |
| 472 | } |
| 473 | |
| 474 | @Override |
| 475 | public boolean isServiceFunction(VirtualPortId portId) { |
Mahesh Poojary S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 476 | return portPairService.exists(PortPairId.of(portId.portId())); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | @Override |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 480 | public DeviceId getSfToSffMaping(VirtualPortId portId) { |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 481 | checkNotNull(portId, "portId cannot be null"); |
| 482 | VirtualPort vmPort = virtualPortService.getPort(portId); |
| 483 | Set<Host> hostSet = hostService.getHostsByMac(vmPort.macAddress()); |
| 484 | for (Host host : hostSet) { |
Satish K | 01a60f2 | 2015-11-28 15:21:42 +0530 | [diff] [blame] | 485 | if (host.annotations().value(IFACEID).equals(vmPort.portId().portId())) { |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 486 | return host.location().deviceId(); |
| 487 | } |
| 488 | } |
| 489 | return null; |
| 490 | } |
| 491 | |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 492 | @Override |
| 493 | public void addDeviceIdOfOvsMap(VirtualPortId virtualPortId, |
| 494 | TenantId tenantId, DeviceId deviceId) { |
| 495 | checkNotNull(virtualPortId, VIRTUALPORTID_NOT_NULL); |
| 496 | checkNotNull(tenantId, TENANTID_NOT_NULL); |
| 497 | checkNotNull(deviceId, DEVICEID_NOT_NULL); |
| 498 | if (isServiceFunction(virtualPortId)) { |
| 499 | addDeviceIdToSpecificMap(tenantId, deviceId, sffOvsMap); |
| 500 | } else { |
| 501 | addDeviceIdToSpecificMap(tenantId, deviceId, classifierOvsMap); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | @Override |
| 506 | public void removeDeviceIdOfOvsMap(Host host, TenantId tenantId, DeviceId deviceId) { |
| 507 | checkNotNull(host, HOST_NOT_NULL); |
| 508 | checkNotNull(tenantId, TENANTID_NOT_NULL); |
| 509 | checkNotNull(deviceId, DEVICEID_NOT_NULL); |
| 510 | if (isLastSFHostOfTenant(host, deviceId, tenantId)) { |
| 511 | removeDeviceIdToSpecificMap(tenantId, deviceId, sffOvsMap); |
| 512 | } |
| 513 | if (isLastClassifierHostOfTenant(host, deviceId, tenantId)) { |
| 514 | removeDeviceIdToSpecificMap(tenantId, deviceId, classifierOvsMap); |
| 515 | } |
| 516 | } |
| 517 | |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 518 | /** |
| 519 | * Checks whether the last Service Function host of a specific tenant in |
| 520 | * this device. |
| 521 | * |
| 522 | * @param host the host on device |
| 523 | * @param deviceId the device identifier |
| 524 | * @param tenantId the tenant identifier |
| 525 | * @return true or false |
| 526 | */ |
| 527 | private boolean isLastSFHostOfTenant(Host host, DeviceId deviceId, |
| 528 | TenantId tenantId) { |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 529 | Set<Host> hostSet = hostService.getConnectedHosts(deviceId); |
lishuai | 1fbd41b | 2015-12-10 16:44:57 +0800 | [diff] [blame] | 530 | if (hostSet != null) { |
| 531 | for (Host h : hostSet) { |
| 532 | String ifaceId = h.annotations().value(IFACEID); |
| 533 | if (ifaceId != null) { |
| 534 | VirtualPortId hPortId = VirtualPortId.portId(ifaceId); |
| 535 | if (virtualPortService.getPort(hPortId).tenantId().tenantId() |
| 536 | .equals(tenantId.tenantId()) |
| 537 | && isServiceFunction(hPortId)) { |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 538 | if (!h.equals(host)) { |
| 539 | return false; |
| 540 | } |
lishuai | 1fbd41b | 2015-12-10 16:44:57 +0800 | [diff] [blame] | 541 | } |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | } |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 545 | return true; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /** |
| 549 | * Checks whether the last Classifier host of a specific tenant in this |
| 550 | * device. |
| 551 | * |
| 552 | * @param host the host on device |
| 553 | * @param deviceId the device identifier |
| 554 | * @param tenantId the tenant identifier |
| 555 | * @return true or false |
| 556 | */ |
| 557 | private boolean isLastClassifierHostOfTenant(Host host, DeviceId deviceId, |
| 558 | TenantId tenantId) { |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 559 | Set<Host> hostSet = hostService.getConnectedHosts(deviceId); |
lishuai | 1fbd41b | 2015-12-10 16:44:57 +0800 | [diff] [blame] | 560 | if (hostSet != null) { |
| 561 | for (Host h : hostSet) { |
| 562 | String ifaceId = h.annotations().value(IFACEID); |
| 563 | if (ifaceId != null) { |
| 564 | VirtualPortId hPortId = VirtualPortId.portId(ifaceId); |
| 565 | if (virtualPortService.getPort(hPortId).tenantId().tenantId() |
| 566 | .equals(tenantId.tenantId()) |
| 567 | && !isServiceFunction(hPortId)) { |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 568 | if (!h.equals(host)) { |
| 569 | return false; |
| 570 | } |
lishuai | 1fbd41b | 2015-12-10 16:44:57 +0800 | [diff] [blame] | 571 | } |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | } |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 575 | return true; |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /** |
| 579 | * Adds specify Device identifier to OvsMap. |
| 580 | * |
| 581 | * @param tenantId the tenant identifier |
| 582 | * @param deviceId the device identifier |
| 583 | * @param ovsMap the instance of map to store device identifier |
| 584 | */ |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 585 | private void addDeviceIdToSpecificMap(TenantId tenantId, |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 586 | DeviceId deviceId, |
| 587 | EventuallyConsistentMap<TenantId, Set<DeviceId>> ovsMap) { |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 588 | if (ovsMap.containsKey(tenantId)) { |
| 589 | Set<DeviceId> deviceIdSet = ovsMap.get(tenantId); |
| 590 | deviceIdSet.add(deviceId); |
| 591 | ovsMap.put(tenantId, deviceIdSet); |
| 592 | } else { |
| 593 | Set<DeviceId> deviceIdSet = new HashSet<>(); |
| 594 | deviceIdSet.add(deviceId); |
| 595 | ovsMap.put(tenantId, deviceIdSet); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * Removes specify Device identifier from OvsMap. |
| 601 | * |
| 602 | * @param tenantId the tenant identifier |
| 603 | * @param deviceId the device identifier |
| 604 | * @param ovsMap the instance of map to store device identifier |
| 605 | */ |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 606 | private void removeDeviceIdToSpecificMap(TenantId tenantId, |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 607 | DeviceId deviceId, |
| 608 | EventuallyConsistentMap<TenantId, Set<DeviceId>> ovsMap) { |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 609 | Set<DeviceId> deviceIdSet = ovsMap.get(tenantId); |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 610 | if (deviceIdSet != null && deviceIdSet.size() > 1) { |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 611 | deviceIdSet.remove(deviceId); |
| 612 | ovsMap.put(tenantId, deviceIdSet); |
| 613 | } else { |
| 614 | ovsMap.remove(tenantId); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * Notifies specify event to all listeners. |
| 620 | * |
| 621 | * @param event VtnRsc event |
| 622 | */ |
| 623 | private void notifyListeners(VtnRscEvent event) { |
| 624 | checkNotNull(event, EVENT_NOT_NULL); |
Mahesh Poojary S | cc11f72 | 2015-11-29 16:09:56 +0530 | [diff] [blame] | 625 | post(event); |
jiangrui | f675cd4 | 2015-11-27 15:03:59 +0800 | [diff] [blame] | 626 | } |
| 627 | } |