jiangrui | b80e4c7 | 2015-11-27 15:00:51 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Laboratory |
jiangrui | b80e4c7 | 2015-11-27 15:00:51 +0800 | [diff] [blame] | 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, |
drl | b04ca99 | 2016-03-21 20:42:43 -0400 | [diff] [blame] | 104 | SubnetId.class, FixedIp.class); |
jiangrui | b80e4c7 | 2015-11-27 15:00:51 +0800 | [diff] [blame] | 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); |
Satish K | 4a83f92 | 2015-11-28 15:50:12 +0530 | [diff] [blame] | 157 | 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; |
jiangrui | b80e4c7 | 2015-11-27 15:00:51 +0800 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public boolean removeRouters(Collection<RouterId> routerIds) { |
| 177 | checkNotNull(routerIds, ROUTER_ID_NULL); |
Satish K | 4a83f92 | 2015-11-28 15:50:12 +0530 | [diff] [blame] | 178 | 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; |
jiangrui | b80e4c7 | 2015-11-27 15:00:51 +0800 | [diff] [blame] | 191 | } |
| 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 | } |