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