blob: 1c8a2712d3ca9e43bcd1dc1ad5f4b0c979dd446f [file] [log] [blame]
jiangruib80e4c72015-11-27 15:00:51 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
jiangruib80e4c72015-11-27 15:00:51 +08003 *
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,
drlb04ca992016-03-21 20:42:43 -0400104 SubnetId.class, FixedIp.class);
jiangruib80e4c72015-11-27 15:00:51 +0800105 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);
Satish K4a83f922015-11-28 15:50:12 +0530157 for (Router router : routers) {
158 if (!routerStore.containsKey(router.id())) {
159 log.debug("The routers is not exist whose identifier is {}",
160 router.id().toString());
161 throw new IllegalArgumentException(
162 "routers ID doesn't exist");
163 }
164 verifyRouterData(router);
165 routerStore.put(router.id(), router);
166 if (!router.equals(routerStore.get(router.id()))) {
167 log.debug("The router is updated failed whose identifier is {}",
168 router.id().toString());
169 return false;
jiangruib80e4c72015-11-27 15:00:51 +0800170 }
171 }
172 return true;
173 }
174
175 @Override
176 public boolean removeRouters(Collection<RouterId> routerIds) {
177 checkNotNull(routerIds, ROUTER_ID_NULL);
Satish K4a83f922015-11-28 15:50:12 +0530178 for (RouterId routerId : routerIds) {
179 if (!routerStore.containsKey(routerId)) {
180 log.debug("The router is not exist whose identifier is {}",
181 routerId.toString());
182 throw new IllegalArgumentException(
183 "router ID doesn't exist");
184 }
185 Router router = routerStore.get(routerId);
186 routerStore.remove(routerId, router);
187 if (routerStore.containsKey(routerId)) {
188 log.debug("The router deleted is failed whose identifier is {}",
189 routerId.toString());
190 return false;
jiangruib80e4c72015-11-27 15:00:51 +0800191 }
192 }
193 return true;
194 }
195
196 @Override
197 public void addListener(RouterListener listener) {
198 checkNotNull(listener, LISTENER_NOT_NULL);
199 listeners.add(listener);
200 }
201
202 @Override
203 public void removeListener(RouterListener listener) {
204 checkNotNull(listener, LISTENER_NOT_NULL);
205 listeners.remove(listener);
206 }
207
208 /**
209 * Verifies validity of Router data.
210 *
211 * @param routers router instance
212 */
213 private void verifyRouterData(Router routers) {
214 checkNotNull(routers, ROUTER_NOT_NULL);
215 if (routers.gatewayPortid() != null
216 && !virtualPortService.exists(routers.gatewayPortid())) {
217 log.debug("The gateway port ID is not exist whose identifier is {}",
218 routers.gatewayPortid().toString());
219 throw new IllegalArgumentException("gateway port ID doesn't exist");
220 }
221
222 if (routers.externalGatewayInfo() != null) {
223 RouterGateway routerGateway = routers.externalGatewayInfo();
224 if (!tenantNetworkService.exists(routerGateway.networkId())) {
225 log.debug("The network ID of gateway info is not exist whose identifier is {}",
226 routers.id().toString());
227 throw new IllegalArgumentException(
228 "network ID of gateway info doesn't exist");
229 }
230 Iterable<FixedIp> fixedIps = routerGateway.externalFixedIps();
231 for (FixedIp fixedIp : fixedIps) {
232 if (!subnetService.exists(fixedIp.subnetId())) {
233 log.debug("The subnet ID of gateway info is not exist whose identifier is {}",
234 routers.id().toString());
235 throw new IllegalArgumentException(
236 "subnet ID of gateway info doesn't exist");
237 }
238 }
239 }
240 }
241
242 private class InnerRouterStoreListener
243 implements EventuallyConsistentMapListener<RouterId, Router> {
244
245 @Override
246 public void event(EventuallyConsistentMapEvent<RouterId, Router> event) {
247 checkNotNull(event, EVENT_NOT_NULL);
248 Router router = event.value();
249 if (EventuallyConsistentMapEvent.Type.PUT == event.type()) {
250 notifyListeners(new RouterEvent(RouterEvent.Type.ROUTER_PUT,
251 router));
252 }
253 if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) {
254 notifyListeners(new RouterEvent(RouterEvent.Type.ROUTER_DELETE,
255 router));
256 }
257 }
258 }
259
260 /**
261 * Notifies specify event to all listeners.
262 *
263 * @param event Floating IP event
264 */
265 private void notifyListeners(RouterEvent event) {
266 checkNotNull(event, EVENT_NOT_NULL);
267 listeners.forEach(listener -> listener.event(event));
268 }
269}