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