Committed by
Gerrit Code Review
[ONOS-2818] add implementation of RouterService api
Change-Id: Iea2ddbc8a5de66aa091d63fdbb506bc6f0a23f05
Showing
2 changed files
with
293 additions
and
0 deletions
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.router.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.util.KryoNamespace; | ||
32 | +import org.onosproject.core.ApplicationId; | ||
33 | +import org.onosproject.core.CoreService; | ||
34 | +import org.onosproject.store.serializers.KryoNamespaces; | ||
35 | +import org.onosproject.store.service.EventuallyConsistentMap; | ||
36 | +import org.onosproject.store.service.EventuallyConsistentMapEvent; | ||
37 | +import org.onosproject.store.service.EventuallyConsistentMapListener; | ||
38 | +import org.onosproject.store.service.StorageService; | ||
39 | +import org.onosproject.store.service.WallClockTimestamp; | ||
40 | +import org.onosproject.vtnrsc.DefaultRouter; | ||
41 | +import org.onosproject.vtnrsc.FixedIp; | ||
42 | +import org.onosproject.vtnrsc.Router; | ||
43 | +import org.onosproject.vtnrsc.RouterGateway; | ||
44 | +import org.onosproject.vtnrsc.RouterId; | ||
45 | +import org.onosproject.vtnrsc.SubnetId; | ||
46 | +import org.onosproject.vtnrsc.TenantId; | ||
47 | +import org.onosproject.vtnrsc.TenantNetworkId; | ||
48 | +import org.onosproject.vtnrsc.VirtualPortId; | ||
49 | +import org.onosproject.vtnrsc.router.RouterEvent; | ||
50 | +import org.onosproject.vtnrsc.router.RouterListener; | ||
51 | +import org.onosproject.vtnrsc.router.RouterService; | ||
52 | +import org.onosproject.vtnrsc.subnet.SubnetService; | ||
53 | +import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService; | ||
54 | +import org.onosproject.vtnrsc.virtualport.VirtualPortService; | ||
55 | +import org.slf4j.Logger; | ||
56 | + | ||
57 | +import com.google.common.collect.Sets; | ||
58 | + | ||
59 | +/** | ||
60 | + * Provides implementation of the Router service. | ||
61 | + */ | ||
62 | +@Component(immediate = true) | ||
63 | +@Service | ||
64 | +public 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 | +} |
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 | + | ||
17 | +/** | ||
18 | + * Provides implementation of the Router service. | ||
19 | + */ | ||
20 | +package org.onosproject.vtnrsc.router.impl; |
-
Please register or login to post a comment