janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [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.l3vpn.netl3vpn.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 19 | import org.onlab.util.KryoNamespace; |
| 20 | import org.onosproject.l3vpn.netl3vpn.AccessInfo; |
| 21 | import org.onosproject.l3vpn.netl3vpn.BgpInfo; |
| 22 | import org.onosproject.l3vpn.netl3vpn.DeviceInfo; |
| 23 | import org.onosproject.l3vpn.netl3vpn.FullMeshVpnConfig; |
| 24 | import org.onosproject.l3vpn.netl3vpn.HubSpokeVpnConfig; |
| 25 | import org.onosproject.l3vpn.netl3vpn.InterfaceInfo; |
janani b | 36b1d77 | 2017-03-27 15:01:28 +0530 | [diff] [blame] | 26 | import org.onosproject.l3vpn.netl3vpn.NetL3VpnStore; |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 27 | import org.onosproject.l3vpn.netl3vpn.ProtocolInfo; |
| 28 | import org.onosproject.l3vpn.netl3vpn.RouteProtocol; |
janani b | 9ed76be | 2017-08-29 19:11:33 +0530 | [diff] [blame] | 29 | import org.onosproject.l3vpn.netl3vpn.TunnelInfo; |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 30 | import org.onosproject.l3vpn.netl3vpn.VpnConfig; |
| 31 | import org.onosproject.l3vpn.netl3vpn.VpnInstance; |
| 32 | import org.onosproject.l3vpn.netl3vpn.VpnType; |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.store.serializers.KryoNamespaces; |
| 35 | import org.onosproject.store.service.ConsistentMap; |
| 36 | import org.onosproject.store.service.DistributedSet; |
| 37 | import org.onosproject.store.service.Serializer; |
| 38 | import org.onosproject.store.service.StorageService; |
| 39 | import org.onosproject.yang.model.LeafListKey; |
| 40 | import org.onosproject.yang.model.ListKey; |
| 41 | import org.onosproject.yang.model.NodeKey; |
| 42 | import org.onosproject.yang.model.ResourceId; |
| 43 | import org.onosproject.yang.model.SchemaId; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 44 | import org.osgi.service.component.annotations.Activate; |
| 45 | import org.osgi.service.component.annotations.Component; |
| 46 | import org.osgi.service.component.annotations.Deactivate; |
| 47 | import org.osgi.service.component.annotations.Reference; |
| 48 | import org.osgi.service.component.annotations.ReferenceCardinality; |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 49 | import org.slf4j.Logger; |
| 50 | |
| 51 | import java.util.Map; |
| 52 | import java.util.stream.Collectors; |
| 53 | |
| 54 | import static com.google.common.base.Preconditions.checkNotNull; |
| 55 | import static org.slf4j.LoggerFactory.getLogger; |
| 56 | |
| 57 | /** |
| 58 | * Manages the pool of available VPN instances and its associated devices |
| 59 | * and interface information. |
| 60 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 61 | @Component(immediate = true, service = NetL3VpnStore.class) |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 62 | public class DistributedNetL3VpnStore implements NetL3VpnStore { |
| 63 | |
| 64 | private static final Serializer L3VPN_SERIALIZER = Serializer |
| 65 | .using(new KryoNamespace.Builder().register(KryoNamespaces.API) |
| 66 | .register(KryoNamespaces.API) |
| 67 | .register(VpnInstance.class) |
| 68 | .register(VpnType.class) |
| 69 | .register(VpnConfig.class) |
| 70 | .register(FullMeshVpnConfig.class) |
| 71 | .register(HubSpokeVpnConfig.class) |
| 72 | .register(DeviceInfo.class) |
| 73 | .register(ResourceId.class) |
| 74 | .register(NodeKey.class) |
| 75 | .register(SchemaId.class) |
| 76 | .register(LeafListKey.class) |
| 77 | .register(ListKey.class) |
| 78 | .register(AccessInfo.class) |
janani b | 36b1d77 | 2017-03-27 15:01:28 +0530 | [diff] [blame] | 79 | .register(InterfaceInfo.class) |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 80 | .register(BgpInfo.class) |
| 81 | .register(RouteProtocol.class) |
| 82 | .register(ProtocolInfo.class) |
janani b | 9ed76be | 2017-08-29 19:11:33 +0530 | [diff] [blame] | 83 | .register(TunnelInfo.class) |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 84 | .build()); |
| 85 | |
| 86 | private static final String FREE_ID_NULL = "Free ID cannot be null"; |
| 87 | private static final String VPN_NAME_NULL = "VPN name cannot be null"; |
| 88 | private static final String VPN_INS_NULL = "VPN instance cannot be null"; |
| 89 | private static final String ACCESS_INFO_NULL = "Access info cannot be null"; |
| 90 | private static final String BGP_INFO_NULL = "BGP info cannot be null"; |
| 91 | private static final String INT_INFO_NULL = "Interface info cannot be null"; |
| 92 | private static final String DEV_ID_NULL = "Device Id cannot be null"; |
| 93 | |
| 94 | private final Logger log = getLogger(getClass()); |
| 95 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 97 | protected StorageService storageService; |
| 98 | |
| 99 | /** |
| 100 | * Freed id list of NET L3VPN. |
| 101 | */ |
| 102 | private DistributedSet<Long> freedIdList; |
| 103 | |
| 104 | /** |
| 105 | * Map of interface info with access info as key. |
| 106 | */ |
| 107 | private ConsistentMap<AccessInfo, InterfaceInfo> intInfoMap; |
| 108 | |
| 109 | /** |
| 110 | * Map of VPN instance with VPN name as key. |
| 111 | */ |
| 112 | private ConsistentMap<String, VpnInstance> vpnInsMap; |
| 113 | |
| 114 | /** |
| 115 | * Map of BGP information and the device id. |
| 116 | */ |
| 117 | private ConsistentMap<BgpInfo, DeviceId> bgpInfoMap; |
| 118 | |
janani b | 9ed76be | 2017-08-29 19:11:33 +0530 | [diff] [blame] | 119 | /** |
| 120 | * Map of device id and tunnel count. |
| 121 | */ |
| 122 | private ConsistentMap<DeviceId, Integer> tunnelInfoMap; |
| 123 | |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 124 | @Activate |
| 125 | protected void activate() { |
| 126 | vpnInsMap = storageService.<String, VpnInstance>consistentMapBuilder() |
| 127 | .withName("onos-l3vpn-instance-map") |
| 128 | .withSerializer(L3VPN_SERIALIZER) |
| 129 | .build(); |
| 130 | |
| 131 | intInfoMap = storageService |
| 132 | .<AccessInfo, InterfaceInfo>consistentMapBuilder() |
| 133 | .withName("onos-l3vpn-int-info-map") |
| 134 | .withSerializer(L3VPN_SERIALIZER) |
| 135 | .build(); |
| 136 | |
| 137 | bgpInfoMap = storageService.<BgpInfo, DeviceId>consistentMapBuilder() |
| 138 | .withName("onos-l3vpn-bgp-info-map") |
| 139 | .withSerializer(L3VPN_SERIALIZER) |
| 140 | .build(); |
| 141 | |
janani b | 9ed76be | 2017-08-29 19:11:33 +0530 | [diff] [blame] | 142 | tunnelInfoMap = storageService.<DeviceId, Integer>consistentMapBuilder() |
| 143 | .withName("onos-l3vpn-tnl-info-map") |
| 144 | .withSerializer(L3VPN_SERIALIZER) |
| 145 | .build(); |
| 146 | |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 147 | freedIdList = storageService.<Long>setBuilder() |
| 148 | .withName("onos-l3vpn-id-freed-list") |
| 149 | .withSerializer(Serializer.using(KryoNamespaces.API)) |
| 150 | .build() |
| 151 | .asDistributedSet(); |
| 152 | |
| 153 | log.info("Started"); |
| 154 | } |
| 155 | |
| 156 | @Deactivate |
| 157 | protected void deactivate() { |
| 158 | log.info("Stopped"); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public Iterable<Long> getFreedIdList() { |
| 163 | return ImmutableSet.copyOf(freedIdList); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public Map<String, VpnInstance> getVpnInstances() { |
| 168 | return vpnInsMap.entrySet().stream() |
| 169 | .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue() |
| 170 | .value())); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public Map<BgpInfo, DeviceId> getBgpInfo() { |
| 175 | return bgpInfoMap.entrySet().stream() |
| 176 | .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue() |
| 177 | .value())); |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public Map<AccessInfo, InterfaceInfo> getInterfaceInfo() { |
| 182 | return intInfoMap.entrySet().stream() |
| 183 | .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue() |
| 184 | .value())); |
| 185 | } |
| 186 | |
| 187 | @Override |
janani b | 9ed76be | 2017-08-29 19:11:33 +0530 | [diff] [blame] | 188 | public Map<DeviceId, Integer> getTunnelInfo() { |
| 189 | return tunnelInfoMap.entrySet().stream() |
| 190 | .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue() |
| 191 | .value())); |
| 192 | } |
| 193 | |
| 194 | @Override |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 195 | public void addIdToFreeList(Long id) { |
| 196 | checkNotNull(id, FREE_ID_NULL); |
| 197 | freedIdList.add(id); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void addVpnInsIfAbsent(String name, VpnInstance instance) { |
| 202 | checkNotNull(name, VPN_NAME_NULL); |
| 203 | checkNotNull(instance, VPN_INS_NULL); |
| 204 | vpnInsMap.putIfAbsent(name, instance); |
| 205 | } |
| 206 | |
| 207 | @Override |
janani b | 36b1d77 | 2017-03-27 15:01:28 +0530 | [diff] [blame] | 208 | public void addVpnIns(String name, VpnInstance instance) { |
| 209 | checkNotNull(name, VPN_NAME_NULL); |
| 210 | checkNotNull(instance, VPN_INS_NULL); |
| 211 | vpnInsMap.put(name, instance); |
| 212 | } |
| 213 | |
| 214 | @Override |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 215 | public void addInterfaceInfo(AccessInfo accessInfo, InterfaceInfo intInfo) { |
| 216 | checkNotNull(accessInfo, ACCESS_INFO_NULL); |
| 217 | checkNotNull(intInfo, INT_INFO_NULL); |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame] | 218 | intInfoMap.put(accessInfo, intInfo); |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public void addBgpInfo(BgpInfo bgpInfo, DeviceId devId) { |
janani b | 9ed76be | 2017-08-29 19:11:33 +0530 | [diff] [blame] | 223 | checkNotNull(bgpInfo, BGP_INFO_NULL); |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 224 | checkNotNull(devId, DEV_ID_NULL); |
| 225 | bgpInfoMap.put(bgpInfo, devId); |
| 226 | } |
| 227 | |
| 228 | @Override |
janani b | 9ed76be | 2017-08-29 19:11:33 +0530 | [diff] [blame] | 229 | public void addTunnelInfo(DeviceId devId, Integer count) { |
| 230 | checkNotNull(devId, DEV_ID_NULL); |
| 231 | tunnelInfoMap.put(devId, count); |
| 232 | } |
| 233 | |
| 234 | @Override |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 235 | public boolean removeInterfaceInfo(AccessInfo accessInfo) { |
| 236 | checkNotNull(accessInfo, ACCESS_INFO_NULL); |
| 237 | |
| 238 | if (intInfoMap.remove(accessInfo) == null) { |
| 239 | log.error("Interface info deletion for access info {} has failed.", |
| 240 | accessInfo.toString()); |
| 241 | return false; |
| 242 | } |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public boolean removeVpnInstance(String vpnName) { |
| 248 | checkNotNull(vpnName, VPN_NAME_NULL); |
| 249 | |
| 250 | if (vpnInsMap.remove(vpnName) == null) { |
| 251 | log.error("Vpn instance deletion for vpn name {} has failed.", |
| 252 | vpnName); |
| 253 | return false; |
| 254 | } |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public boolean removeIdFromFreeList(Long id) { |
| 260 | checkNotNull(id, FREE_ID_NULL); |
| 261 | |
| 262 | if (!freedIdList.remove(id)) { |
| 263 | log.error("Id from free id list {} deletion has failed.", |
| 264 | id.toString()); |
| 265 | return false; |
| 266 | } |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public boolean removeBgpInfo(BgpInfo bgpInfo) { |
| 272 | checkNotNull(bgpInfo, BGP_INFO_NULL); |
| 273 | |
| 274 | if (bgpInfoMap.remove(bgpInfo) == null) { |
| 275 | log.error("Device id deletion for BGP info {} has failed.", |
| 276 | bgpInfo.toString()); |
| 277 | return false; |
| 278 | } |
| 279 | return true; |
| 280 | } |
janani b | 9ed76be | 2017-08-29 19:11:33 +0530 | [diff] [blame] | 281 | |
| 282 | @Override |
| 283 | public boolean removeTunnelInfo(DeviceId id) { |
| 284 | checkNotNull(id, DEV_ID_NULL); |
| 285 | |
| 286 | if (tunnelInfoMap.remove(id) == null) { |
| 287 | log.error("Device id deletion in tunnel info has failed."); |
| 288 | return false; |
| 289 | } |
| 290 | return true; |
| 291 | } |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 292 | } |