blob: 91e149a698b4b11a8b7162f19100740d7df4de9f [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";
66 private static final String FLOATINGIP = "vtn-floatingip-store";
67 private static final String VTNRSC_APP = "org.onosproject.vtnrsc";
68 private static final String LISTENER_NOT_NULL = "Listener cannot be null";
69 private static final String EVENT_NOT_NULL = "event cannot be null";
70
71 private final Logger log = getLogger(getClass());
72 private final Set<FloatingIpListener> listeners = Sets
73 .newCopyOnWriteArraySet();
74 private EventuallyConsistentMapListener<FloatingIpId, FloatingIp> floatingIpListener =
75 new InnerFloatingIpStoreListener();
76 protected EventuallyConsistentMap<FloatingIpId, FloatingIp> floatingIpStore;
77 protected ApplicationId appId;
78
79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected StorageService storageService;
81
82 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
83 protected CoreService coreService;
84
85 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected TenantNetworkService tenantNetworkService;
87
88 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
89 protected VirtualPortService virtualPortService;
90
91 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
92 protected RouterService routerService;
93
94 @Activate
95 public void activate() {
96 appId = coreService.registerApplication(VTNRSC_APP);
97 KryoNamespace.Builder serializer = KryoNamespace
98 .newBuilder()
99 .register(KryoNamespaces.API)
100 .register(FloatingIp.class, FloatingIpId.class,
101 TenantNetworkId.class, TenantId.class,
102 FloatingIp.Status.class, RouterId.class,
103 VirtualPortId.class, DefaultFloatingIp.class);
104 floatingIpStore = storageService
105 .<FloatingIpId, FloatingIp>eventuallyConsistentMapBuilder()
106 .withName(FLOATINGIP).withSerializer(serializer)
107 .withTimestampProvider((k, v) -> new WallClockTimestamp())
108 .build();
109 floatingIpStore.addListener(floatingIpListener);
110 log.info("Started");
111 }
112
113 @Deactivate
114 public void deactivate() {
115 floatingIpStore.removeListener(floatingIpListener);
116 floatingIpStore.destroy();
117 listeners.clear();
118 log.info("Stopped");
119 }
120
121 @Override
122 public Collection<FloatingIp> getFloatingIps() {
123 return Collections.unmodifiableCollection(floatingIpStore.values());
124 }
125
126 @Override
127 public FloatingIp getFloatingIp(FloatingIpId floatingIpId) {
128 checkNotNull(floatingIpId, FLOATINGIP_ID_NOT_NULL);
129 return floatingIpStore.get(floatingIpId);
130 }
131
132 @Override
133 public boolean exists(FloatingIpId floatingIpId) {
134 checkNotNull(floatingIpId, FLOATINGIP_ID_NOT_NULL);
135 return floatingIpStore.containsKey(floatingIpId);
136 }
137
138 @Override
139 public boolean floatingIpIsUsed(IpAddress floatingIpAddr,
140 FloatingIpId floatingIpId) {
141 checkNotNull(floatingIpAddr, "Floating IP address cannot be null");
142 checkNotNull(floatingIpId, "Floating IP Id cannot be null");
143 Collection<FloatingIp> floatingIps = getFloatingIps();
144 for (FloatingIp floatingIp : floatingIps) {
145 if (floatingIp.floatingIp().equals(floatingIpAddr)
146 && !floatingIp.id().equals(floatingIpId)) {
147 return true;
148 }
149 }
150 return false;
151 }
152
153 @Override
154 public boolean fixedIpIsUsed(IpAddress fixedIpAddr, TenantId tenantId,
155 FloatingIpId floatingIpId) {
156 checkNotNull(fixedIpAddr, "Fixed IP address cannot be null");
157 checkNotNull(tenantId, "Tenant Id cannot be null");
158 checkNotNull(floatingIpId, "Floating IP Id cannot be null");
159 Collection<FloatingIp> floatingIps = getFloatingIps();
160 for (FloatingIp floatingIp : floatingIps) {
161 IpAddress fixedIp = floatingIp.fixedIp();
162 if (fixedIp != null) {
163 if (fixedIp.equals(fixedIpAddr)
164 && floatingIp.tenantId().equals(tenantId)
165 && !floatingIp.id().equals(floatingIpId)) {
166 return true;
167 }
168 }
169 }
170 return false;
171 }
172
173 @Override
174 public boolean createFloatingIps(Collection<FloatingIp> floatingIps) {
175 checkNotNull(floatingIps, FLOATINGIP_NOT_NULL);
176 boolean result = true;
177 for (FloatingIp floatingIp : floatingIps) {
178 verifyFloatingIpData(floatingIp);
lishuaib43dbf72016-01-06 11:11:35 +0800179 floatingIpStore.put(floatingIp.id(), floatingIp);
180 if (!floatingIpStore.containsKey(floatingIp.id())) {
181 log.debug("The floating Ip is created failed whose identifier is {}",
182 floatingIp.id().toString());
183 result = false;
jiangrui9c6db862015-11-27 14:50:46 +0800184 }
185 }
186 return result;
187 }
188
189 @Override
190 public boolean updateFloatingIps(Collection<FloatingIp> floatingIps) {
191 checkNotNull(floatingIps, FLOATINGIP_NOT_NULL);
192 boolean result = true;
Satish Ke3005812015-11-28 15:35:56 +0530193 for (FloatingIp floatingIp : floatingIps) {
194 verifyFloatingIpData(floatingIp);
lishuaib43dbf72016-01-06 11:11:35 +0800195 floatingIpStore.put(floatingIp.id(), floatingIp);
196 if (!floatingIpStore.containsKey(floatingIp.id())) {
197 log.debug("The floating Ip is updated failed whose identifier is {}",
198 floatingIp.id().toString());
199 result = false;
jiangrui9c6db862015-11-27 14:50:46 +0800200 }
201 }
202 return result;
203 }
204
205 @Override
206 public boolean removeFloatingIps(Collection<FloatingIpId> floatingIpIds) {
207 checkNotNull(floatingIpIds, FLOATINGIP_ID_NOT_NULL);
208 boolean result = true;
Satish Ke3005812015-11-28 15:35:56 +0530209 for (FloatingIpId floatingIpId : floatingIpIds) {
210 if (!floatingIpStore.containsKey(floatingIpId)) {
211 log.debug("The floatingIp is not exist whose identifier is {}",
212 floatingIpId.toString());
213 throw new IllegalArgumentException(
214 "FloatingIP ID doesn't exist");
215 }
216 FloatingIp floatingIp = floatingIpStore.get(floatingIpId);
lishuaib43dbf72016-01-06 11:11:35 +0800217 if (floatingIp.portId() != null) {
218 log.debug("The floating Ip is uesd by the port whose identifier is {}",
219 floatingIp.portId().toString());
220 return false;
221 }
Satish Ke3005812015-11-28 15:35:56 +0530222 floatingIpStore.remove(floatingIpId, floatingIp);
223 if (floatingIpStore.containsKey(floatingIpId)) {
224 log.debug("The floating Ip is deleted failed whose identifier is {}",
225 floatingIpId.toString());
226 result = false;
jiangrui9c6db862015-11-27 14:50:46 +0800227 }
228 }
229 return result;
230 }
231
232 @Override
233 public void addListener(FloatingIpListener listener) {
234 checkNotNull(listener, LISTENER_NOT_NULL);
235 listeners.add(listener);
236 }
237
238 @Override
239 public void removeListener(FloatingIpListener listener) {
240 checkNotNull(listener, LISTENER_NOT_NULL);
241 listeners.add(listener);
242 }
243
244 /**
245 * Verifies validity of FloatingIp data.
246 *
247 * @param floatingIps floatingIp instance
248 */
249 private void verifyFloatingIpData(FloatingIp floatingIps) {
250 checkNotNull(floatingIps, FLOATINGIP_NOT_NULL);
251 if (!tenantNetworkService.exists(floatingIps.networkId())) {
252 log.debug("The network identifier {} that the floating Ip {} create for is not exist",
253 floatingIps.networkId().toString(), floatingIps.id()
254 .toString());
255 throw new IllegalArgumentException(
256 "Floating network ID doesn't exist");
257 }
258
259 VirtualPortId portId = floatingIps.portId();
260 if (portId != null && !virtualPortService.exists(portId)) {
261 log.debug("The port identifier {} that the floating Ip {} create for is not exist",
262 floatingIps.portId().toString(), floatingIps.id()
263 .toString());
264 throw new IllegalArgumentException("Port ID doesn't exist");
265 }
266
267 RouterId routerId = floatingIps.routerId();
268 if (routerId != null && !routerService.exists(routerId)) {
269 log.debug("The router identifier {} that the floating Ip {} create for is not exist",
270 floatingIps.routerId().toString(), floatingIps.id()
271 .toString());
272 throw new IllegalArgumentException("Router ID doesn't exist");
273 }
274
275 if (floatingIpIsUsed(floatingIps.floatingIp(), floatingIps.id())) {
276 log.debug("The floaing Ip {} that the floating Ip {} create for is used",
277 floatingIps.floatingIp().toString(), floatingIps.id()
278 .toString());
279 throw new IllegalArgumentException(
280 "The floating IP address is used");
281 }
282
283 IpAddress fixedIp = floatingIps.fixedIp();
284 if (fixedIp != null
285 && fixedIpIsUsed(fixedIp, floatingIps.tenantId(),
286 floatingIps.id())) {
287 log.debug("The fixed Ip {} that the floating Ip {} create for is used",
288 floatingIps.fixedIp().toString(), floatingIps.id()
289 .toString());
290 throw new IllegalArgumentException("The fixed IP address is used");
291 }
292 }
293
294 private class InnerFloatingIpStoreListener
295 implements
296 EventuallyConsistentMapListener<FloatingIpId, FloatingIp> {
297
298 @Override
299 public void event(EventuallyConsistentMapEvent<FloatingIpId, FloatingIp> event) {
300 checkNotNull(event, EVENT_NOT_NULL);
301 FloatingIp floatingIp = event.value();
302 if (EventuallyConsistentMapEvent.Type.PUT == event.type()) {
303 notifyListeners(new FloatingIpEvent(
304 FloatingIpEvent.Type.FLOATINGIP_PUT,
305 floatingIp));
306 }
307 if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) {
308 notifyListeners(new FloatingIpEvent(
309 FloatingIpEvent.Type.FLOATINGIP_DELETE,
310 floatingIp));
311 }
312 }
313 }
314
315 /**
316 * Notifies specify event to all listeners.
317 *
318 * @param event Floating IP event
319 */
320 private void notifyListeners(FloatingIpEvent event) {
321 checkNotNull(event, EVENT_NOT_NULL);
322 listeners.forEach(listener -> listener.event(event));
323 }
324}