Committed by
Gerrit Code Review
[ONOS-2819] add implementation of routerInterfaceService api
Change-Id: I65febe22acb199f20989a2b5bdac00d8fe69c2bc
Showing
2 changed files
with
255 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.routerinterface.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.RouterId; | ||
41 | +import org.onosproject.vtnrsc.RouterInterface; | ||
42 | +import org.onosproject.vtnrsc.SubnetId; | ||
43 | +import org.onosproject.vtnrsc.TenantId; | ||
44 | +import org.onosproject.vtnrsc.VirtualPortId; | ||
45 | +import org.onosproject.vtnrsc.router.RouterService; | ||
46 | +import org.onosproject.vtnrsc.routerinterface.RouterInterfaceEvent; | ||
47 | +import org.onosproject.vtnrsc.routerinterface.RouterInterfaceListener; | ||
48 | +import org.onosproject.vtnrsc.routerinterface.RouterInterfaceService; | ||
49 | +import org.onosproject.vtnrsc.subnet.SubnetService; | ||
50 | +import org.onosproject.vtnrsc.virtualport.VirtualPortService; | ||
51 | +import org.slf4j.Logger; | ||
52 | + | ||
53 | +import com.google.common.collect.Sets; | ||
54 | + | ||
55 | +/** | ||
56 | + * Provides implementation of the Router interface service. | ||
57 | + */ | ||
58 | +@Component(immediate = true) | ||
59 | +@Service | ||
60 | +public class RouterInterfaceManager implements RouterInterfaceService { | ||
61 | + private static final String SUBNET_ID_NULL = "Subnet ID cannot be null"; | ||
62 | + private static final String ROUTER_INTERFACE_NULL = "Router Interface cannot be null"; | ||
63 | + private static final String ROUTER_INTERFACE = "vtn-router-interface-store"; | ||
64 | + private static final String VTNRSC_APP = "org.onosproject.vtnrsc"; | ||
65 | + private static final String LISTENER_NOT_NULL = "Listener cannot be null"; | ||
66 | + private static final String EVENT_NOT_NULL = "event cannot be null"; | ||
67 | + | ||
68 | + private final Logger log = getLogger(getClass()); | ||
69 | + private final Set<RouterInterfaceListener> listeners = Sets | ||
70 | + .newCopyOnWriteArraySet(); | ||
71 | + private EventuallyConsistentMapListener<SubnetId, RouterInterface> routerInterfaceListener = | ||
72 | + new InnerRouterInterfaceStoreListener(); | ||
73 | + protected EventuallyConsistentMap<SubnetId, RouterInterface> routerInterfaceStore; | ||
74 | + protected ApplicationId appId; | ||
75 | + | ||
76 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
77 | + protected StorageService storageService; | ||
78 | + | ||
79 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
80 | + protected CoreService coreService; | ||
81 | + | ||
82 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
83 | + protected VirtualPortService virtualPortService; | ||
84 | + | ||
85 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
86 | + protected SubnetService subnetService; | ||
87 | + | ||
88 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
89 | + protected RouterService routerService; | ||
90 | + | ||
91 | + @Activate | ||
92 | + public void activate() { | ||
93 | + appId = coreService.registerApplication(VTNRSC_APP); | ||
94 | + KryoNamespace.Builder serializer = KryoNamespace | ||
95 | + .newBuilder() | ||
96 | + .register(KryoNamespaces.API) | ||
97 | + .register(RouterId.class, TenantId.class, VirtualPortId.class, | ||
98 | + RouterInterface.class, SubnetId.class); | ||
99 | + routerInterfaceStore = storageService | ||
100 | + .<SubnetId, RouterInterface>eventuallyConsistentMapBuilder() | ||
101 | + .withName(ROUTER_INTERFACE).withSerializer(serializer) | ||
102 | + .withTimestampProvider((k, v) -> new WallClockTimestamp()) | ||
103 | + .build(); | ||
104 | + routerInterfaceStore.addListener(routerInterfaceListener); | ||
105 | + log.info("Started"); | ||
106 | + } | ||
107 | + | ||
108 | + @Deactivate | ||
109 | + public void deactivate() { | ||
110 | + routerInterfaceStore.removeListener(routerInterfaceListener); | ||
111 | + routerInterfaceStore.destroy(); | ||
112 | + listeners.clear(); | ||
113 | + log.info("Stopped"); | ||
114 | + } | ||
115 | + | ||
116 | + @Override | ||
117 | + public boolean exists(SubnetId subnetId) { | ||
118 | + checkNotNull(subnetId, SUBNET_ID_NULL); | ||
119 | + return routerInterfaceStore.containsKey(subnetId); | ||
120 | + } | ||
121 | + | ||
122 | + @Override | ||
123 | + public Collection<RouterInterface> getRouterInterfaces() { | ||
124 | + return Collections | ||
125 | + .unmodifiableCollection(routerInterfaceStore.values()); | ||
126 | + } | ||
127 | + | ||
128 | + @Override | ||
129 | + public RouterInterface getRouterInterface(SubnetId subnetId) { | ||
130 | + checkNotNull(subnetId, SUBNET_ID_NULL); | ||
131 | + return routerInterfaceStore.get(subnetId); | ||
132 | + } | ||
133 | + | ||
134 | + @Override | ||
135 | + public boolean addRouterInterface(RouterInterface routerInterface) { | ||
136 | + checkNotNull(routerInterface, ROUTER_INTERFACE_NULL); | ||
137 | + verifyRouterInterfaceData(routerInterface); | ||
138 | + routerInterfaceStore.put(routerInterface.subnetId(), routerInterface); | ||
139 | + if (!routerInterfaceStore.containsKey(routerInterface.subnetId())) { | ||
140 | + log.debug("The router interface is created failed whose identifier is {}", | ||
141 | + routerInterface.subnetId().toString()); | ||
142 | + return false; | ||
143 | + } | ||
144 | + return true; | ||
145 | + } | ||
146 | + | ||
147 | + @Override | ||
148 | + public boolean removeRouterInterface(RouterInterface routerInterface) { | ||
149 | + checkNotNull(routerInterface, ROUTER_INTERFACE_NULL); | ||
150 | + if (!routerInterfaceStore.containsKey(routerInterface.subnetId())) { | ||
151 | + log.debug("The router interface is not exist whose identifier is {}", | ||
152 | + routerInterface.subnetId().toString()); | ||
153 | + throw new IllegalArgumentException("subnet ID doesn't exist"); | ||
154 | + } | ||
155 | + verifyRouterInterfaceData(routerInterface); | ||
156 | + routerInterfaceStore | ||
157 | + .remove(routerInterface.subnetId(), routerInterface); | ||
158 | + if (routerInterfaceStore.containsKey(routerInterface.subnetId())) { | ||
159 | + log.debug("The router interface deleted is failed whose identifier is {}", | ||
160 | + routerInterface.subnetId().toString()); | ||
161 | + return false; | ||
162 | + } | ||
163 | + return true; | ||
164 | + } | ||
165 | + | ||
166 | + @Override | ||
167 | + public void addListener(RouterInterfaceListener listener) { | ||
168 | + checkNotNull(listener, LISTENER_NOT_NULL); | ||
169 | + listeners.add(listener); | ||
170 | + } | ||
171 | + | ||
172 | + @Override | ||
173 | + public void removeListener(RouterInterfaceListener listener) { | ||
174 | + checkNotNull(listener, LISTENER_NOT_NULL); | ||
175 | + listeners.remove(listener); | ||
176 | + } | ||
177 | + | ||
178 | + /** | ||
179 | + * Verifies validity of Router interface data. | ||
180 | + * | ||
181 | + * @param routers router instance | ||
182 | + */ | ||
183 | + private void verifyRouterInterfaceData(RouterInterface routerInterface) { | ||
184 | + checkNotNull(routerInterface, ROUTER_INTERFACE_NULL); | ||
185 | + if (!subnetService.exists(routerInterface.subnetId())) { | ||
186 | + log.debug("The subnet ID of interface is not exist whose identifier is {}", | ||
187 | + routerInterface.subnetId().toString()); | ||
188 | + throw new IllegalArgumentException( | ||
189 | + "subnet ID of interface doesn't exist"); | ||
190 | + } | ||
191 | + if (!virtualPortService.exists(routerInterface.portId())) { | ||
192 | + log.debug("The port ID of interface is not exist whose identifier is {}", | ||
193 | + routerInterface.portId().toString()); | ||
194 | + throw new IllegalArgumentException( | ||
195 | + "port ID of interface doesn't exist"); | ||
196 | + } | ||
197 | + if (!routerService.exists(routerInterface.routerId())) { | ||
198 | + log.debug("The router ID of interface is not exist whose identifier is {}", | ||
199 | + routerInterface.routerId().toString()); | ||
200 | + throw new IllegalArgumentException( | ||
201 | + "router ID of interface doesn't exist"); | ||
202 | + } | ||
203 | + } | ||
204 | + | ||
205 | + private class InnerRouterInterfaceStoreListener | ||
206 | + implements | ||
207 | + EventuallyConsistentMapListener<SubnetId, RouterInterface> { | ||
208 | + | ||
209 | + @Override | ||
210 | + public void event(EventuallyConsistentMapEvent<SubnetId, RouterInterface> event) { | ||
211 | + checkNotNull(event, EVENT_NOT_NULL); | ||
212 | + RouterInterface routerInterface = event.value(); | ||
213 | + if (EventuallyConsistentMapEvent.Type.PUT == event.type()) { | ||
214 | + notifyListeners(new RouterInterfaceEvent( | ||
215 | + RouterInterfaceEvent.Type.ROUTER_INTERFACE_PUT, | ||
216 | + routerInterface)); | ||
217 | + } | ||
218 | + if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) { | ||
219 | + notifyListeners(new RouterInterfaceEvent( | ||
220 | + RouterInterfaceEvent.Type.ROUTER_INTERFACE_DELETE, | ||
221 | + routerInterface)); | ||
222 | + } | ||
223 | + } | ||
224 | + } | ||
225 | + | ||
226 | + /** | ||
227 | + * Notifies specify event to all listeners. | ||
228 | + * | ||
229 | + * @param event Floating IP event | ||
230 | + */ | ||
231 | + private void notifyListeners(RouterInterfaceEvent event) { | ||
232 | + checkNotNull(event, EVENT_NOT_NULL); | ||
233 | + listeners.forEach(listener -> listener.event(event)); | ||
234 | + } | ||
235 | +} |
apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/routerinterface/impl/package-info.java
0 → 100644
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 RouterInterface service. | ||
19 | + */ | ||
20 | +package org.onosproject.vtnrsc.routerinterface.impl; |
-
Please register or login to post a comment