janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present 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.l3vpn.netl3vpn.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
| 25 | import org.onlab.util.KryoNamespace; |
| 26 | import org.onosproject.l3vpn.netl3vpn.AccessInfo; |
| 27 | import org.onosproject.l3vpn.netl3vpn.BgpInfo; |
| 28 | import org.onosproject.l3vpn.netl3vpn.DeviceInfo; |
| 29 | import org.onosproject.l3vpn.netl3vpn.FullMeshVpnConfig; |
| 30 | import org.onosproject.l3vpn.netl3vpn.HubSpokeVpnConfig; |
| 31 | import org.onosproject.l3vpn.netl3vpn.InterfaceInfo; |
janani b | 36b1d77 | 2017-03-27 15:01:28 +0530 | [diff] [blame] | 32 | import org.onosproject.l3vpn.netl3vpn.NetL3VpnStore; |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 33 | import org.onosproject.l3vpn.netl3vpn.ProtocolInfo; |
| 34 | import org.onosproject.l3vpn.netl3vpn.RouteProtocol; |
| 35 | import org.onosproject.l3vpn.netl3vpn.VpnConfig; |
| 36 | import org.onosproject.l3vpn.netl3vpn.VpnInstance; |
| 37 | import org.onosproject.l3vpn.netl3vpn.VpnType; |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 38 | import org.onosproject.net.DeviceId; |
| 39 | import org.onosproject.store.serializers.KryoNamespaces; |
| 40 | import org.onosproject.store.service.ConsistentMap; |
| 41 | import org.onosproject.store.service.DistributedSet; |
| 42 | import org.onosproject.store.service.Serializer; |
| 43 | import org.onosproject.store.service.StorageService; |
| 44 | import org.onosproject.yang.model.LeafListKey; |
| 45 | import org.onosproject.yang.model.ListKey; |
| 46 | import org.onosproject.yang.model.NodeKey; |
| 47 | import org.onosproject.yang.model.ResourceId; |
| 48 | import org.onosproject.yang.model.SchemaId; |
| 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 | */ |
| 61 | @Component(immediate = true) |
| 62 | @Service |
| 63 | public class DistributedNetL3VpnStore implements NetL3VpnStore { |
| 64 | |
| 65 | private static final Serializer L3VPN_SERIALIZER = Serializer |
| 66 | .using(new KryoNamespace.Builder().register(KryoNamespaces.API) |
| 67 | .register(KryoNamespaces.API) |
| 68 | .register(VpnInstance.class) |
| 69 | .register(VpnType.class) |
| 70 | .register(VpnConfig.class) |
| 71 | .register(FullMeshVpnConfig.class) |
| 72 | .register(HubSpokeVpnConfig.class) |
| 73 | .register(DeviceInfo.class) |
| 74 | .register(ResourceId.class) |
| 75 | .register(NodeKey.class) |
| 76 | .register(SchemaId.class) |
| 77 | .register(LeafListKey.class) |
| 78 | .register(ListKey.class) |
| 79 | .register(AccessInfo.class) |
janani b | 36b1d77 | 2017-03-27 15:01:28 +0530 | [diff] [blame] | 80 | .register(InterfaceInfo.class) |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 81 | .register(BgpInfo.class) |
| 82 | .register(RouteProtocol.class) |
| 83 | .register(ProtocolInfo.class) |
| 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 | |
| 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 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 | |
| 119 | @Activate |
| 120 | protected void activate() { |
| 121 | vpnInsMap = storageService.<String, VpnInstance>consistentMapBuilder() |
| 122 | .withName("onos-l3vpn-instance-map") |
| 123 | .withSerializer(L3VPN_SERIALIZER) |
| 124 | .build(); |
| 125 | |
| 126 | intInfoMap = storageService |
| 127 | .<AccessInfo, InterfaceInfo>consistentMapBuilder() |
| 128 | .withName("onos-l3vpn-int-info-map") |
| 129 | .withSerializer(L3VPN_SERIALIZER) |
| 130 | .build(); |
| 131 | |
| 132 | bgpInfoMap = storageService.<BgpInfo, DeviceId>consistentMapBuilder() |
| 133 | .withName("onos-l3vpn-bgp-info-map") |
| 134 | .withSerializer(L3VPN_SERIALIZER) |
| 135 | .build(); |
| 136 | |
| 137 | freedIdList = storageService.<Long>setBuilder() |
| 138 | .withName("onos-l3vpn-id-freed-list") |
| 139 | .withSerializer(Serializer.using(KryoNamespaces.API)) |
| 140 | .build() |
| 141 | .asDistributedSet(); |
| 142 | |
| 143 | log.info("Started"); |
| 144 | } |
| 145 | |
| 146 | @Deactivate |
| 147 | protected void deactivate() { |
| 148 | log.info("Stopped"); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public Iterable<Long> getFreedIdList() { |
| 153 | return ImmutableSet.copyOf(freedIdList); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public Map<String, VpnInstance> getVpnInstances() { |
| 158 | return vpnInsMap.entrySet().stream() |
| 159 | .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue() |
| 160 | .value())); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public Map<BgpInfo, DeviceId> getBgpInfo() { |
| 165 | return bgpInfoMap.entrySet().stream() |
| 166 | .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue() |
| 167 | .value())); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public Map<AccessInfo, InterfaceInfo> getInterfaceInfo() { |
| 172 | return intInfoMap.entrySet().stream() |
| 173 | .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue() |
| 174 | .value())); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void addIdToFreeList(Long id) { |
| 179 | checkNotNull(id, FREE_ID_NULL); |
| 180 | freedIdList.add(id); |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public void addVpnInsIfAbsent(String name, VpnInstance instance) { |
| 185 | checkNotNull(name, VPN_NAME_NULL); |
| 186 | checkNotNull(instance, VPN_INS_NULL); |
| 187 | vpnInsMap.putIfAbsent(name, instance); |
| 188 | } |
| 189 | |
| 190 | @Override |
janani b | 36b1d77 | 2017-03-27 15:01:28 +0530 | [diff] [blame] | 191 | public void addVpnIns(String name, VpnInstance instance) { |
| 192 | checkNotNull(name, VPN_NAME_NULL); |
| 193 | checkNotNull(instance, VPN_INS_NULL); |
| 194 | vpnInsMap.put(name, instance); |
| 195 | } |
| 196 | |
| 197 | @Override |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 198 | public void addInterfaceInfo(AccessInfo accessInfo, InterfaceInfo intInfo) { |
| 199 | checkNotNull(accessInfo, ACCESS_INFO_NULL); |
| 200 | checkNotNull(intInfo, INT_INFO_NULL); |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame] | 201 | intInfoMap.put(accessInfo, intInfo); |
janani b | f41dec3 | 2017-03-24 18:44:07 +0530 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public void addBgpInfo(BgpInfo bgpInfo, DeviceId devId) { |
| 206 | checkNotNull(devId, BGP_INFO_NULL); |
| 207 | checkNotNull(devId, DEV_ID_NULL); |
| 208 | bgpInfoMap.put(bgpInfo, devId); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public boolean removeInterfaceInfo(AccessInfo accessInfo) { |
| 213 | checkNotNull(accessInfo, ACCESS_INFO_NULL); |
| 214 | |
| 215 | if (intInfoMap.remove(accessInfo) == null) { |
| 216 | log.error("Interface info deletion for access info {} has failed.", |
| 217 | accessInfo.toString()); |
| 218 | return false; |
| 219 | } |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public boolean removeVpnInstance(String vpnName) { |
| 225 | checkNotNull(vpnName, VPN_NAME_NULL); |
| 226 | |
| 227 | if (vpnInsMap.remove(vpnName) == null) { |
| 228 | log.error("Vpn instance deletion for vpn name {} has failed.", |
| 229 | vpnName); |
| 230 | return false; |
| 231 | } |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public boolean removeIdFromFreeList(Long id) { |
| 237 | checkNotNull(id, FREE_ID_NULL); |
| 238 | |
| 239 | if (!freedIdList.remove(id)) { |
| 240 | log.error("Id from free id list {} deletion has failed.", |
| 241 | id.toString()); |
| 242 | return false; |
| 243 | } |
| 244 | return true; |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public boolean removeBgpInfo(BgpInfo bgpInfo) { |
| 249 | checkNotNull(bgpInfo, BGP_INFO_NULL); |
| 250 | |
| 251 | if (bgpInfoMap.remove(bgpInfo) == null) { |
| 252 | log.error("Device id deletion for BGP info {} has failed.", |
| 253 | bgpInfo.toString()); |
| 254 | return false; |
| 255 | } |
| 256 | return true; |
| 257 | } |
| 258 | } |