blob: 3902593362cc3022ca8b4f231e715b0bc1e08eba [file] [log] [blame]
jiangrui9c6db862015-11-27 14:50:46 +08001/*
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 */
16package org.onosproject.vtnrsc.floatingip.impl;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19import static org.slf4j.LoggerFactory.getLogger;
20
21import java.util.Collection;
22import java.util.Collections;
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.util.KryoNamespace;
33import org.onosproject.core.ApplicationId;
34import org.onosproject.core.CoreService;
35import org.onosproject.store.serializers.KryoNamespaces;
36import org.onosproject.store.service.EventuallyConsistentMap;
37import org.onosproject.store.service.EventuallyConsistentMapEvent;
38import org.onosproject.store.service.EventuallyConsistentMapListener;
39import org.onosproject.store.service.StorageService;
40import org.onosproject.store.service.WallClockTimestamp;
41import org.onosproject.vtnrsc.DefaultFloatingIp;
42import org.onosproject.vtnrsc.FloatingIp;
43import org.onosproject.vtnrsc.FloatingIpId;
44import org.onosproject.vtnrsc.TenantId;
45import org.onosproject.vtnrsc.TenantNetworkId;
46import org.onosproject.vtnrsc.VirtualPortId;
47import org.onosproject.vtnrsc.RouterId;
48import org.onosproject.vtnrsc.floatingip.FloatingIpEvent;
49import org.onosproject.vtnrsc.floatingip.FloatingIpListener;
50import org.onosproject.vtnrsc.floatingip.FloatingIpService;
51import org.onosproject.vtnrsc.router.RouterService;
52import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService;
53import org.onosproject.vtnrsc.virtualport.VirtualPortService;
54import org.slf4j.Logger;
55
56import com.google.common.collect.Sets;
57
58/**
59 * Provides implementation of the FloatingIp service.
60 */
61@Component(immediate = true)
62@Service
63public 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";
lishuai762df812016-01-08 11:51:15 +080066 private static final String FLOATINGIPSTORE = "vtn-floatingip-store";
67 private static final String FLOATINGIPBINDSTORE = "vtn-floatingip-bind-store";
jiangrui9c6db862015-11-27 14:50:46 +080068 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;
lishuai762df812016-01-08 11:51:15 +080078 protected EventuallyConsistentMap<FloatingIpId, FloatingIp> floatingIpBindStore;
jiangrui9c6db862015-11-27 14:50:46 +080079 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()
lishuai762df812016-01-08 11:51:15 +0800108 .withName(FLOATINGIPSTORE).withSerializer(serializer)
109 .withTimestampProvider((k, v) -> new WallClockTimestamp())
110 .build();
111 floatingIpBindStore = storageService
112 .<FloatingIpId, FloatingIp>eventuallyConsistentMapBuilder()
113 .withName(FLOATINGIPBINDSTORE).withSerializer(serializer)
jiangrui9c6db862015-11-27 14:50:46 +0800114 .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();
lishuai762df812016-01-08 11:51:15 +0800124 floatingIpBindStore.destroy();
jiangrui9c6db862015-11-27 14:50:46 +0800125 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);
lishuaib43dbf72016-01-06 11:11:35 +0800187 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;
jiangrui9c6db862015-11-27 14:50:46 +0800192 }
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 Ke3005812015-11-28 15:35:56 +0530201 for (FloatingIp floatingIp : floatingIps) {
202 verifyFloatingIpData(floatingIp);
lishuai762df812016-01-08 11:51:15 +0800203 FloatingIp oldFloatingIp = floatingIpStore.get(floatingIp.id());
204 floatingIpBindStore.put(floatingIp.id(), oldFloatingIp);
lishuaib43dbf72016-01-06 11:11:35 +0800205 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;
jiangrui9c6db862015-11-27 14:50:46 +0800210 }
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 Ke3005812015-11-28 15:35:56 +0530219 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);
lishuaib43dbf72016-01-06 11:11:35 +0800227 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 Ke3005812015-11-28 15:35:56 +0530232 floatingIpStore.remove(floatingIpId, floatingIp);
lishuai762df812016-01-08 11:51:15 +0800233 floatingIpBindStore.remove(floatingIpId);
Satish Ke3005812015-11-28 15:35:56 +0530234 if (floatingIpStore.containsKey(floatingIpId)) {
235 log.debug("The floating Ip is deleted failed whose identifier is {}",
236 floatingIpId.toString());
237 result = false;
jiangrui9c6db862015-11-27 14:50:46 +0800238 }
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));
lishuai762df812016-01-08 11:51:15 +0800317 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 }
jiangrui9c6db862015-11-27 14:50:46 +0800329 }
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}