blob: 7bfafed2bb2499bff8fbd83e3c98859f11083ad8 [file] [log] [blame]
jiangruib80e4c72015-11-27 15:00:51 +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.router.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.util.KryoNamespace;
32import org.onosproject.core.ApplicationId;
33import org.onosproject.core.CoreService;
34import org.onosproject.store.serializers.KryoNamespaces;
35import org.onosproject.store.service.EventuallyConsistentMap;
36import org.onosproject.store.service.EventuallyConsistentMapEvent;
37import org.onosproject.store.service.EventuallyConsistentMapListener;
38import org.onosproject.store.service.StorageService;
39import org.onosproject.store.service.WallClockTimestamp;
40import org.onosproject.vtnrsc.DefaultRouter;
41import org.onosproject.vtnrsc.FixedIp;
42import org.onosproject.vtnrsc.Router;
43import org.onosproject.vtnrsc.RouterGateway;
44import org.onosproject.vtnrsc.RouterId;
45import org.onosproject.vtnrsc.SubnetId;
46import org.onosproject.vtnrsc.TenantId;
47import org.onosproject.vtnrsc.TenantNetworkId;
48import org.onosproject.vtnrsc.VirtualPortId;
49import org.onosproject.vtnrsc.router.RouterEvent;
50import org.onosproject.vtnrsc.router.RouterListener;
51import org.onosproject.vtnrsc.router.RouterService;
52import org.onosproject.vtnrsc.subnet.SubnetService;
53import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService;
54import org.onosproject.vtnrsc.virtualport.VirtualPortService;
55import org.slf4j.Logger;
56
57import com.google.common.collect.Sets;
58
59/**
60 * Provides implementation of the Router service.
61 */
62@Component(immediate = true)
63@Service
64public class RouterManager implements RouterService {
65
66 private static final String ROUTER_ID_NULL = "Router ID cannot be null";
67 private static final String ROUTER_NOT_NULL = "Router cannot be null";
68 private static final String ROUTER = "vtn-router-store";
69 private static final String VTNRSC_APP = "org.onosproject.vtnrsc";
70 private static final String LISTENER_NOT_NULL = "Listener cannot be null";
71 private static final String EVENT_NOT_NULL = "event cannot be null";
72
73 private final Logger log = getLogger(getClass());
74 private final Set<RouterListener> listeners = Sets.newCopyOnWriteArraySet();
75 private EventuallyConsistentMapListener<RouterId, Router> routerListener = new InnerRouterStoreListener();
76 protected EventuallyConsistentMap<RouterId, Router> routerStore;
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 SubnetService subnetService;
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(Router.class, RouterId.class, DefaultRouter.class,
101 TenantNetworkId.class, TenantId.class,
102 VirtualPortId.class, DefaultRouter.class,
103 RouterGateway.class, Router.Status.class,
104 SubnetId.class);
105 routerStore = storageService
106 .<RouterId, Router>eventuallyConsistentMapBuilder()
107 .withName(ROUTER).withSerializer(serializer)
108 .withTimestampProvider((k, v) -> new WallClockTimestamp())
109 .build();
110 routerStore.addListener(routerListener);
111 log.info("Started");
112 }
113
114 @Deactivate
115 public void deactivate() {
116 routerStore.removeListener(routerListener);
117 routerStore.destroy();
118 listeners.clear();
119 log.info("Stopped");
120 }
121
122 @Override
123 public boolean exists(RouterId routerId) {
124 checkNotNull(routerId, ROUTER_ID_NULL);
125 return routerStore.containsKey(routerId);
126 }
127
128 @Override
129 public Collection<Router> getRouters() {
130 return Collections.unmodifiableCollection(routerStore.values());
131 }
132
133 @Override
134 public Router getRouter(RouterId routerId) {
135 checkNotNull(routerId, ROUTER_ID_NULL);
136 return routerStore.get(routerId);
137 }
138
139 @Override
140 public boolean createRouters(Collection<Router> routers) {
141 checkNotNull(routers, ROUTER_NOT_NULL);
142 for (Router router : routers) {
143 verifyRouterData(router);
144 routerStore.put(router.id(), router);
145 if (!routerStore.containsKey(router.id())) {
146 log.debug("The router is created failed whose identifier is {}",
147 router.id().toString());
148 return false;
149 }
150 }
151 return true;
152 }
153
154 @Override
155 public boolean updateRouters(Collection<Router> routers) {
156 checkNotNull(routers, ROUTER_NOT_NULL);
157 if (routers != null) {
158 for (Router router : routers) {
159 if (!routerStore.containsKey(router.id())) {
160 log.debug("The routers is not exist whose identifier is {}",
161 router.id().toString());
162 throw new IllegalArgumentException(
163 "routers ID doesn't exist");
164 }
165 verifyRouterData(router);
166 routerStore.put(router.id(), router);
167 if (!router.equals(routerStore.get(router.id()))) {
168 log.debug("The router is updated failed whose identifier is {}",
169 router.id().toString());
170 return false;
171 }
172 }
173 }
174 return true;
175 }
176
177 @Override
178 public boolean removeRouters(Collection<RouterId> routerIds) {
179 checkNotNull(routerIds, ROUTER_ID_NULL);
180 if (routerIds != null) {
181 for (RouterId routerId : routerIds) {
182 if (!routerStore.containsKey(routerId)) {
183 log.debug("The router is not exist whose identifier is {}",
184 routerId.toString());
185 throw new IllegalArgumentException(
186 "router ID doesn't exist");
187 }
188 Router router = routerStore.get(routerId);
189 routerStore.remove(routerId, router);
190 if (routerStore.containsKey(routerId)) {
191 log.debug("The router deleted is failed whose identifier is {}",
192 routerId.toString());
193 return false;
194 }
195 }
196 }
197 return true;
198 }
199
200 @Override
201 public void addListener(RouterListener listener) {
202 checkNotNull(listener, LISTENER_NOT_NULL);
203 listeners.add(listener);
204 }
205
206 @Override
207 public void removeListener(RouterListener listener) {
208 checkNotNull(listener, LISTENER_NOT_NULL);
209 listeners.remove(listener);
210 }
211
212 /**
213 * Verifies validity of Router data.
214 *
215 * @param routers router instance
216 */
217 private void verifyRouterData(Router routers) {
218 checkNotNull(routers, ROUTER_NOT_NULL);
219 if (routers.gatewayPortid() != null
220 && !virtualPortService.exists(routers.gatewayPortid())) {
221 log.debug("The gateway port ID is not exist whose identifier is {}",
222 routers.gatewayPortid().toString());
223 throw new IllegalArgumentException("gateway port ID doesn't exist");
224 }
225
226 if (routers.externalGatewayInfo() != null) {
227 RouterGateway routerGateway = routers.externalGatewayInfo();
228 if (!tenantNetworkService.exists(routerGateway.networkId())) {
229 log.debug("The network ID of gateway info is not exist whose identifier is {}",
230 routers.id().toString());
231 throw new IllegalArgumentException(
232 "network ID of gateway info doesn't exist");
233 }
234 Iterable<FixedIp> fixedIps = routerGateway.externalFixedIps();
235 for (FixedIp fixedIp : fixedIps) {
236 if (!subnetService.exists(fixedIp.subnetId())) {
237 log.debug("The subnet ID of gateway info is not exist whose identifier is {}",
238 routers.id().toString());
239 throw new IllegalArgumentException(
240 "subnet ID of gateway info doesn't exist");
241 }
242 }
243 }
244 }
245
246 private class InnerRouterStoreListener
247 implements EventuallyConsistentMapListener<RouterId, Router> {
248
249 @Override
250 public void event(EventuallyConsistentMapEvent<RouterId, Router> event) {
251 checkNotNull(event, EVENT_NOT_NULL);
252 Router router = event.value();
253 if (EventuallyConsistentMapEvent.Type.PUT == event.type()) {
254 notifyListeners(new RouterEvent(RouterEvent.Type.ROUTER_PUT,
255 router));
256 }
257 if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) {
258 notifyListeners(new RouterEvent(RouterEvent.Type.ROUTER_DELETE,
259 router));
260 }
261 }
262 }
263
264 /**
265 * Notifies specify event to all listeners.
266 *
267 * @param event Floating IP event
268 */
269 private void notifyListeners(RouterEvent event) {
270 checkNotNull(event, EVENT_NOT_NULL);
271 listeners.forEach(listener -> listener.event(event));
272 }
273}