blob: 5178d5e863e50c7bcf30021c8ffb6f8fc4b17339 [file] [log] [blame]
janani bf41dec32017-03-24 18:44:07 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
janani bf41dec32017-03-24 18:44:07 +05303 *
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 */
16package org.onosproject.l3vpn.netl3vpn.impl;
17
18import com.google.common.collect.ImmutableSet;
janani bf41dec32017-03-24 18:44:07 +053019import org.onlab.util.KryoNamespace;
20import org.onosproject.l3vpn.netl3vpn.AccessInfo;
21import org.onosproject.l3vpn.netl3vpn.BgpInfo;
22import org.onosproject.l3vpn.netl3vpn.DeviceInfo;
23import org.onosproject.l3vpn.netl3vpn.FullMeshVpnConfig;
24import org.onosproject.l3vpn.netl3vpn.HubSpokeVpnConfig;
25import org.onosproject.l3vpn.netl3vpn.InterfaceInfo;
janani b36b1d772017-03-27 15:01:28 +053026import org.onosproject.l3vpn.netl3vpn.NetL3VpnStore;
janani bf41dec32017-03-24 18:44:07 +053027import org.onosproject.l3vpn.netl3vpn.ProtocolInfo;
28import org.onosproject.l3vpn.netl3vpn.RouteProtocol;
janani b9ed76be2017-08-29 19:11:33 +053029import org.onosproject.l3vpn.netl3vpn.TunnelInfo;
janani bf41dec32017-03-24 18:44:07 +053030import org.onosproject.l3vpn.netl3vpn.VpnConfig;
31import org.onosproject.l3vpn.netl3vpn.VpnInstance;
32import org.onosproject.l3vpn.netl3vpn.VpnType;
janani bf41dec32017-03-24 18:44:07 +053033import org.onosproject.net.DeviceId;
34import org.onosproject.store.serializers.KryoNamespaces;
35import org.onosproject.store.service.ConsistentMap;
36import org.onosproject.store.service.DistributedSet;
37import org.onosproject.store.service.Serializer;
38import org.onosproject.store.service.StorageService;
39import org.onosproject.yang.model.LeafListKey;
40import org.onosproject.yang.model.ListKey;
41import org.onosproject.yang.model.NodeKey;
42import org.onosproject.yang.model.ResourceId;
43import org.onosproject.yang.model.SchemaId;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070044import org.osgi.service.component.annotations.Activate;
45import org.osgi.service.component.annotations.Component;
46import org.osgi.service.component.annotations.Deactivate;
47import org.osgi.service.component.annotations.Reference;
48import org.osgi.service.component.annotations.ReferenceCardinality;
janani bf41dec32017-03-24 18:44:07 +053049import org.slf4j.Logger;
50
51import java.util.Map;
52import java.util.stream.Collectors;
53
54import static com.google.common.base.Preconditions.checkNotNull;
55import 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 Milkeyd84f89b2018-08-17 14:54:17 -070061@Component(immediate = true, service = NetL3VpnStore.class)
janani bf41dec32017-03-24 18:44:07 +053062public 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 b36b1d772017-03-27 15:01:28 +053079 .register(InterfaceInfo.class)
janani bf41dec32017-03-24 18:44:07 +053080 .register(BgpInfo.class)
81 .register(RouteProtocol.class)
82 .register(ProtocolInfo.class)
janani b9ed76be2017-08-29 19:11:33 +053083 .register(TunnelInfo.class)
janani bf41dec32017-03-24 18:44:07 +053084 .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 Milkeyd84f89b2018-08-17 14:54:17 -070096 @Reference(cardinality = ReferenceCardinality.MANDATORY)
janani bf41dec32017-03-24 18:44:07 +053097 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 b9ed76be2017-08-29 19:11:33 +0530119 /**
120 * Map of device id and tunnel count.
121 */
122 private ConsistentMap<DeviceId, Integer> tunnelInfoMap;
123
janani bf41dec32017-03-24 18:44:07 +0530124 @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 b9ed76be2017-08-29 19:11:33 +0530142 tunnelInfoMap = storageService.<DeviceId, Integer>consistentMapBuilder()
143 .withName("onos-l3vpn-tnl-info-map")
144 .withSerializer(L3VPN_SERIALIZER)
145 .build();
146
janani bf41dec32017-03-24 18:44:07 +0530147 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 b9ed76be2017-08-29 19:11:33 +0530188 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 bf41dec32017-03-24 18:44:07 +0530195 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 b36b1d772017-03-27 15:01:28 +0530208 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 bf41dec32017-03-24 18:44:07 +0530215 public void addInterfaceInfo(AccessInfo accessInfo, InterfaceInfo intInfo) {
216 checkNotNull(accessInfo, ACCESS_INFO_NULL);
217 checkNotNull(intInfo, INT_INFO_NULL);
janani bd821b182017-03-30 16:34:49 +0530218 intInfoMap.put(accessInfo, intInfo);
janani bf41dec32017-03-24 18:44:07 +0530219 }
220
221 @Override
222 public void addBgpInfo(BgpInfo bgpInfo, DeviceId devId) {
janani b9ed76be2017-08-29 19:11:33 +0530223 checkNotNull(bgpInfo, BGP_INFO_NULL);
janani bf41dec32017-03-24 18:44:07 +0530224 checkNotNull(devId, DEV_ID_NULL);
225 bgpInfoMap.put(bgpInfo, devId);
226 }
227
228 @Override
janani b9ed76be2017-08-29 19:11:33 +0530229 public void addTunnelInfo(DeviceId devId, Integer count) {
230 checkNotNull(devId, DEV_ID_NULL);
231 tunnelInfoMap.put(devId, count);
232 }
233
234 @Override
janani bf41dec32017-03-24 18:44:07 +0530235 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 b9ed76be2017-08-29 19:11:33 +0530281
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 bf41dec32017-03-24 18:44:07 +0530292}