jiangrui | b150da5 | 2015-11-27 14:55:03 +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 | b150da5 | 2015-11-27 14:55:03 +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.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); |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 137 | if (!virtualPortService.exists(routerInterface.portId())) { |
| 138 | log.debug("The port ID of interface is not exist whose identifier is {}", |
| 139 | routerInterface.portId().toString()); |
| 140 | throw new IllegalArgumentException( |
| 141 | "port ID of interface doesn't exist"); |
| 142 | } |
jiangrui | b150da5 | 2015-11-27 14:55:03 +0800 | [diff] [blame] | 143 | verifyRouterInterfaceData(routerInterface); |
| 144 | routerInterfaceStore.put(routerInterface.subnetId(), routerInterface); |
| 145 | if (!routerInterfaceStore.containsKey(routerInterface.subnetId())) { |
| 146 | log.debug("The router interface is created failed whose identifier is {}", |
| 147 | routerInterface.subnetId().toString()); |
| 148 | return false; |
| 149 | } |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public boolean removeRouterInterface(RouterInterface routerInterface) { |
| 155 | checkNotNull(routerInterface, ROUTER_INTERFACE_NULL); |
| 156 | if (!routerInterfaceStore.containsKey(routerInterface.subnetId())) { |
| 157 | log.debug("The router interface is not exist whose identifier is {}", |
| 158 | routerInterface.subnetId().toString()); |
| 159 | throw new IllegalArgumentException("subnet ID doesn't exist"); |
| 160 | } |
| 161 | verifyRouterInterfaceData(routerInterface); |
| 162 | routerInterfaceStore |
| 163 | .remove(routerInterface.subnetId(), routerInterface); |
| 164 | if (routerInterfaceStore.containsKey(routerInterface.subnetId())) { |
| 165 | log.debug("The router interface deleted is failed whose identifier is {}", |
| 166 | routerInterface.subnetId().toString()); |
| 167 | return false; |
| 168 | } |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public void addListener(RouterInterfaceListener listener) { |
| 174 | checkNotNull(listener, LISTENER_NOT_NULL); |
| 175 | listeners.add(listener); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void removeListener(RouterInterfaceListener listener) { |
| 180 | checkNotNull(listener, LISTENER_NOT_NULL); |
| 181 | listeners.remove(listener); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Verifies validity of Router interface data. |
| 186 | * |
| 187 | * @param routers router instance |
| 188 | */ |
| 189 | private void verifyRouterInterfaceData(RouterInterface routerInterface) { |
| 190 | checkNotNull(routerInterface, ROUTER_INTERFACE_NULL); |
| 191 | if (!subnetService.exists(routerInterface.subnetId())) { |
| 192 | log.debug("The subnet ID of interface is not exist whose identifier is {}", |
| 193 | routerInterface.subnetId().toString()); |
| 194 | throw new IllegalArgumentException( |
| 195 | "subnet ID of interface doesn't exist"); |
| 196 | } |
jiangrui | b150da5 | 2015-11-27 14:55:03 +0800 | [diff] [blame] | 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 | } |