jiangrui | 9c6db86 | 2015-11-27 14:50:46 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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.floatingip.impl; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import java.util.Collection; |
| 22 | import java.util.Collections; |
| 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.util.KryoNamespace; |
| 33 | import org.onosproject.core.ApplicationId; |
| 34 | import org.onosproject.core.CoreService; |
| 35 | import org.onosproject.store.serializers.KryoNamespaces; |
| 36 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 37 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 38 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
| 39 | import org.onosproject.store.service.StorageService; |
| 40 | import org.onosproject.store.service.WallClockTimestamp; |
| 41 | import org.onosproject.vtnrsc.DefaultFloatingIp; |
| 42 | import org.onosproject.vtnrsc.FloatingIp; |
| 43 | import org.onosproject.vtnrsc.FloatingIpId; |
| 44 | import org.onosproject.vtnrsc.TenantId; |
| 45 | import org.onosproject.vtnrsc.TenantNetworkId; |
| 46 | import org.onosproject.vtnrsc.VirtualPortId; |
| 47 | import org.onosproject.vtnrsc.RouterId; |
| 48 | import org.onosproject.vtnrsc.floatingip.FloatingIpEvent; |
| 49 | import org.onosproject.vtnrsc.floatingip.FloatingIpListener; |
| 50 | import org.onosproject.vtnrsc.floatingip.FloatingIpService; |
| 51 | import org.onosproject.vtnrsc.router.RouterService; |
| 52 | import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService; |
| 53 | import org.onosproject.vtnrsc.virtualport.VirtualPortService; |
| 54 | import org.slf4j.Logger; |
| 55 | |
| 56 | import com.google.common.collect.Sets; |
| 57 | |
| 58 | /** |
| 59 | * Provides implementation of the FloatingIp service. |
| 60 | */ |
| 61 | @Component(immediate = true) |
| 62 | @Service |
| 63 | public class FloatingIpManager implements FloatingIpService { |
| 64 | private static final String FLOATINGIP_ID_NOT_NULL = "Floatingip ID cannot be null"; |
| 65 | private static final String FLOATINGIP_NOT_NULL = "Floatingip cannot be null"; |
lishuai | 762df81 | 2016-01-08 11:51:15 +0800 | [diff] [blame^] | 66 | private static final String FLOATINGIPSTORE = "vtn-floatingip-store"; |
| 67 | private static final String FLOATINGIPBINDSTORE = "vtn-floatingip-bind-store"; |
jiangrui | 9c6db86 | 2015-11-27 14:50:46 +0800 | [diff] [blame] | 68 | private static final String VTNRSC_APP = "org.onosproject.vtnrsc"; |
| 69 | private static final String LISTENER_NOT_NULL = "Listener cannot be null"; |
| 70 | private static final String EVENT_NOT_NULL = "event cannot be null"; |
| 71 | |
| 72 | private final Logger log = getLogger(getClass()); |
| 73 | private final Set<FloatingIpListener> listeners = Sets |
| 74 | .newCopyOnWriteArraySet(); |
| 75 | private EventuallyConsistentMapListener<FloatingIpId, FloatingIp> floatingIpListener = |
| 76 | new InnerFloatingIpStoreListener(); |
| 77 | protected EventuallyConsistentMap<FloatingIpId, FloatingIp> floatingIpStore; |
lishuai | 762df81 | 2016-01-08 11:51:15 +0800 | [diff] [blame^] | 78 | protected EventuallyConsistentMap<FloatingIpId, FloatingIp> floatingIpBindStore; |
jiangrui | 9c6db86 | 2015-11-27 14:50:46 +0800 | [diff] [blame] | 79 | protected ApplicationId appId; |
| 80 | |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 82 | protected StorageService storageService; |
| 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 85 | protected CoreService coreService; |
| 86 | |
| 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 88 | protected TenantNetworkService tenantNetworkService; |
| 89 | |
| 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 91 | protected VirtualPortService virtualPortService; |
| 92 | |
| 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 94 | protected RouterService routerService; |
| 95 | |
| 96 | @Activate |
| 97 | public void activate() { |
| 98 | appId = coreService.registerApplication(VTNRSC_APP); |
| 99 | KryoNamespace.Builder serializer = KryoNamespace |
| 100 | .newBuilder() |
| 101 | .register(KryoNamespaces.API) |
| 102 | .register(FloatingIp.class, FloatingIpId.class, |
| 103 | TenantNetworkId.class, TenantId.class, |
| 104 | FloatingIp.Status.class, RouterId.class, |
| 105 | VirtualPortId.class, DefaultFloatingIp.class); |
| 106 | floatingIpStore = storageService |
| 107 | .<FloatingIpId, FloatingIp>eventuallyConsistentMapBuilder() |
lishuai | 762df81 | 2016-01-08 11:51:15 +0800 | [diff] [blame^] | 108 | .withName(FLOATINGIPSTORE).withSerializer(serializer) |
| 109 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 110 | .build(); |
| 111 | floatingIpBindStore = storageService |
| 112 | .<FloatingIpId, FloatingIp>eventuallyConsistentMapBuilder() |
| 113 | .withName(FLOATINGIPBINDSTORE).withSerializer(serializer) |
jiangrui | 9c6db86 | 2015-11-27 14:50:46 +0800 | [diff] [blame] | 114 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 115 | .build(); |
| 116 | floatingIpStore.addListener(floatingIpListener); |
| 117 | log.info("Started"); |
| 118 | } |
| 119 | |
| 120 | @Deactivate |
| 121 | public void deactivate() { |
| 122 | floatingIpStore.removeListener(floatingIpListener); |
| 123 | floatingIpStore.destroy(); |
lishuai | 762df81 | 2016-01-08 11:51:15 +0800 | [diff] [blame^] | 124 | floatingIpBindStore.destroy(); |
jiangrui | 9c6db86 | 2015-11-27 14:50:46 +0800 | [diff] [blame] | 125 | listeners.clear(); |
| 126 | log.info("Stopped"); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public Collection<FloatingIp> getFloatingIps() { |
| 131 | return Collections.unmodifiableCollection(floatingIpStore.values()); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public FloatingIp getFloatingIp(FloatingIpId floatingIpId) { |
| 136 | checkNotNull(floatingIpId, FLOATINGIP_ID_NOT_NULL); |
| 137 | return floatingIpStore.get(floatingIpId); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public boolean exists(FloatingIpId floatingIpId) { |
| 142 | checkNotNull(floatingIpId, FLOATINGIP_ID_NOT_NULL); |
| 143 | return floatingIpStore.containsKey(floatingIpId); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public boolean floatingIpIsUsed(IpAddress floatingIpAddr, |
| 148 | FloatingIpId floatingIpId) { |
| 149 | checkNotNull(floatingIpAddr, "Floating IP address cannot be null"); |
| 150 | checkNotNull(floatingIpId, "Floating IP Id cannot be null"); |
| 151 | Collection<FloatingIp> floatingIps = getFloatingIps(); |
| 152 | for (FloatingIp floatingIp : floatingIps) { |
| 153 | if (floatingIp.floatingIp().equals(floatingIpAddr) |
| 154 | && !floatingIp.id().equals(floatingIpId)) { |
| 155 | return true; |
| 156 | } |
| 157 | } |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public boolean fixedIpIsUsed(IpAddress fixedIpAddr, TenantId tenantId, |
| 163 | FloatingIpId floatingIpId) { |
| 164 | checkNotNull(fixedIpAddr, "Fixed IP address cannot be null"); |
| 165 | checkNotNull(tenantId, "Tenant Id cannot be null"); |
| 166 | checkNotNull(floatingIpId, "Floating IP Id cannot be null"); |
| 167 | Collection<FloatingIp> floatingIps = getFloatingIps(); |
| 168 | for (FloatingIp floatingIp : floatingIps) { |
| 169 | IpAddress fixedIp = floatingIp.fixedIp(); |
| 170 | if (fixedIp != null) { |
| 171 | if (fixedIp.equals(fixedIpAddr) |
| 172 | && floatingIp.tenantId().equals(tenantId) |
| 173 | && !floatingIp.id().equals(floatingIpId)) { |
| 174 | return true; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public boolean createFloatingIps(Collection<FloatingIp> floatingIps) { |
| 183 | checkNotNull(floatingIps, FLOATINGIP_NOT_NULL); |
| 184 | boolean result = true; |
| 185 | for (FloatingIp floatingIp : floatingIps) { |
| 186 | verifyFloatingIpData(floatingIp); |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 187 | floatingIpStore.put(floatingIp.id(), floatingIp); |
| 188 | if (!floatingIpStore.containsKey(floatingIp.id())) { |
| 189 | log.debug("The floating Ip is created failed whose identifier is {}", |
| 190 | floatingIp.id().toString()); |
| 191 | result = false; |
jiangrui | 9c6db86 | 2015-11-27 14:50:46 +0800 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | return result; |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public boolean updateFloatingIps(Collection<FloatingIp> floatingIps) { |
| 199 | checkNotNull(floatingIps, FLOATINGIP_NOT_NULL); |
| 200 | boolean result = true; |
Satish K | e300581 | 2015-11-28 15:35:56 +0530 | [diff] [blame] | 201 | for (FloatingIp floatingIp : floatingIps) { |
| 202 | verifyFloatingIpData(floatingIp); |
lishuai | 762df81 | 2016-01-08 11:51:15 +0800 | [diff] [blame^] | 203 | FloatingIp oldFloatingIp = floatingIpStore.get(floatingIp.id()); |
| 204 | floatingIpBindStore.put(floatingIp.id(), oldFloatingIp); |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 205 | floatingIpStore.put(floatingIp.id(), floatingIp); |
| 206 | if (!floatingIpStore.containsKey(floatingIp.id())) { |
| 207 | log.debug("The floating Ip is updated failed whose identifier is {}", |
| 208 | floatingIp.id().toString()); |
| 209 | result = false; |
jiangrui | 9c6db86 | 2015-11-27 14:50:46 +0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | return result; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public boolean removeFloatingIps(Collection<FloatingIpId> floatingIpIds) { |
| 217 | checkNotNull(floatingIpIds, FLOATINGIP_ID_NOT_NULL); |
| 218 | boolean result = true; |
Satish K | e300581 | 2015-11-28 15:35:56 +0530 | [diff] [blame] | 219 | for (FloatingIpId floatingIpId : floatingIpIds) { |
| 220 | if (!floatingIpStore.containsKey(floatingIpId)) { |
| 221 | log.debug("The floatingIp is not exist whose identifier is {}", |
| 222 | floatingIpId.toString()); |
| 223 | throw new IllegalArgumentException( |
| 224 | "FloatingIP ID doesn't exist"); |
| 225 | } |
| 226 | FloatingIp floatingIp = floatingIpStore.get(floatingIpId); |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 227 | if (floatingIp.portId() != null) { |
| 228 | log.debug("The floating Ip is uesd by the port whose identifier is {}", |
| 229 | floatingIp.portId().toString()); |
| 230 | return false; |
| 231 | } |
Satish K | e300581 | 2015-11-28 15:35:56 +0530 | [diff] [blame] | 232 | floatingIpStore.remove(floatingIpId, floatingIp); |
lishuai | 762df81 | 2016-01-08 11:51:15 +0800 | [diff] [blame^] | 233 | floatingIpBindStore.remove(floatingIpId); |
Satish K | e300581 | 2015-11-28 15:35:56 +0530 | [diff] [blame] | 234 | if (floatingIpStore.containsKey(floatingIpId)) { |
| 235 | log.debug("The floating Ip is deleted failed whose identifier is {}", |
| 236 | floatingIpId.toString()); |
| 237 | result = false; |
jiangrui | 9c6db86 | 2015-11-27 14:50:46 +0800 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | return result; |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public void addListener(FloatingIpListener listener) { |
| 245 | checkNotNull(listener, LISTENER_NOT_NULL); |
| 246 | listeners.add(listener); |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public void removeListener(FloatingIpListener listener) { |
| 251 | checkNotNull(listener, LISTENER_NOT_NULL); |
| 252 | listeners.add(listener); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Verifies validity of FloatingIp data. |
| 257 | * |
| 258 | * @param floatingIps floatingIp instance |
| 259 | */ |
| 260 | private void verifyFloatingIpData(FloatingIp floatingIps) { |
| 261 | checkNotNull(floatingIps, FLOATINGIP_NOT_NULL); |
| 262 | if (!tenantNetworkService.exists(floatingIps.networkId())) { |
| 263 | log.debug("The network identifier {} that the floating Ip {} create for is not exist", |
| 264 | floatingIps.networkId().toString(), floatingIps.id() |
| 265 | .toString()); |
| 266 | throw new IllegalArgumentException( |
| 267 | "Floating network ID doesn't exist"); |
| 268 | } |
| 269 | |
| 270 | VirtualPortId portId = floatingIps.portId(); |
| 271 | if (portId != null && !virtualPortService.exists(portId)) { |
| 272 | log.debug("The port identifier {} that the floating Ip {} create for is not exist", |
| 273 | floatingIps.portId().toString(), floatingIps.id() |
| 274 | .toString()); |
| 275 | throw new IllegalArgumentException("Port ID doesn't exist"); |
| 276 | } |
| 277 | |
| 278 | RouterId routerId = floatingIps.routerId(); |
| 279 | if (routerId != null && !routerService.exists(routerId)) { |
| 280 | log.debug("The router identifier {} that the floating Ip {} create for is not exist", |
| 281 | floatingIps.routerId().toString(), floatingIps.id() |
| 282 | .toString()); |
| 283 | throw new IllegalArgumentException("Router ID doesn't exist"); |
| 284 | } |
| 285 | |
| 286 | if (floatingIpIsUsed(floatingIps.floatingIp(), floatingIps.id())) { |
| 287 | log.debug("The floaing Ip {} that the floating Ip {} create for is used", |
| 288 | floatingIps.floatingIp().toString(), floatingIps.id() |
| 289 | .toString()); |
| 290 | throw new IllegalArgumentException( |
| 291 | "The floating IP address is used"); |
| 292 | } |
| 293 | |
| 294 | IpAddress fixedIp = floatingIps.fixedIp(); |
| 295 | if (fixedIp != null |
| 296 | && fixedIpIsUsed(fixedIp, floatingIps.tenantId(), |
| 297 | floatingIps.id())) { |
| 298 | log.debug("The fixed Ip {} that the floating Ip {} create for is used", |
| 299 | floatingIps.fixedIp().toString(), floatingIps.id() |
| 300 | .toString()); |
| 301 | throw new IllegalArgumentException("The fixed IP address is used"); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | private class InnerFloatingIpStoreListener |
| 306 | implements |
| 307 | EventuallyConsistentMapListener<FloatingIpId, FloatingIp> { |
| 308 | |
| 309 | @Override |
| 310 | public void event(EventuallyConsistentMapEvent<FloatingIpId, FloatingIp> event) { |
| 311 | checkNotNull(event, EVENT_NOT_NULL); |
| 312 | FloatingIp floatingIp = event.value(); |
| 313 | if (EventuallyConsistentMapEvent.Type.PUT == event.type()) { |
| 314 | notifyListeners(new FloatingIpEvent( |
| 315 | FloatingIpEvent.Type.FLOATINGIP_PUT, |
| 316 | floatingIp)); |
lishuai | 762df81 | 2016-01-08 11:51:15 +0800 | [diff] [blame^] | 317 | if (floatingIp.portId() != null) { |
| 318 | notifyListeners(new FloatingIpEvent( |
| 319 | FloatingIpEvent.Type.FLOATINGIP_BIND, |
| 320 | floatingIp)); |
| 321 | } else { |
| 322 | FloatingIp oldFloatingIp = floatingIpBindStore.get(floatingIp.id()); |
| 323 | if (oldFloatingIp != null) { |
| 324 | notifyListeners(new FloatingIpEvent( |
| 325 | FloatingIpEvent.Type.FLOATINGIP_UNBIND, |
| 326 | oldFloatingIp)); |
| 327 | } |
| 328 | } |
jiangrui | 9c6db86 | 2015-11-27 14:50:46 +0800 | [diff] [blame] | 329 | } |
| 330 | if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) { |
| 331 | notifyListeners(new FloatingIpEvent( |
| 332 | FloatingIpEvent.Type.FLOATINGIP_DELETE, |
| 333 | floatingIp)); |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Notifies specify event to all listeners. |
| 340 | * |
| 341 | * @param event Floating IP event |
| 342 | */ |
| 343 | private void notifyListeners(FloatingIpEvent event) { |
| 344 | checkNotNull(event, EVENT_NOT_NULL); |
| 345 | listeners.forEach(listener -> listener.event(event)); |
| 346 | } |
| 347 | } |