blob: baf3bc44d3b59dfb282a36180cfbd58d2912bbaa [file] [log] [blame]
janani bf41dec32017-03-24 18:44:07 +05301/*
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 */
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;
32import org.onosproject.l3vpn.netl3vpn.ProtocolInfo;
33import org.onosproject.l3vpn.netl3vpn.RouteProtocol;
34import org.onosproject.l3vpn.netl3vpn.VpnConfig;
35import org.onosproject.l3vpn.netl3vpn.VpnInstance;
36import org.onosproject.l3vpn.netl3vpn.VpnType;
37import org.onosproject.l3vpn.netl3vpn.NetL3VpnStore;
38import org.onosproject.net.DeviceId;
39import org.onosproject.store.serializers.KryoNamespaces;
40import org.onosproject.store.service.ConsistentMap;
41import org.onosproject.store.service.DistributedSet;
42import org.onosproject.store.service.Serializer;
43import org.onosproject.store.service.StorageService;
44import org.onosproject.yang.model.LeafListKey;
45import org.onosproject.yang.model.ListKey;
46import org.onosproject.yang.model.NodeKey;
47import org.onosproject.yang.model.ResourceId;
48import org.onosproject.yang.model.SchemaId;
49import 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 */
61@Component(immediate = true)
62@Service
63public 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)
80 .register(BgpInfo.class)
81 .register(RouteProtocol.class)
82 .register(ProtocolInfo.class)
83 .build());
84
85 private static final String FREE_ID_NULL = "Free ID cannot be null";
86 private static final String VPN_NAME_NULL = "VPN name cannot be null";
87 private static final String VPN_INS_NULL = "VPN instance cannot be null";
88 private static final String ACCESS_INFO_NULL = "Access info cannot be null";
89 private static final String BGP_INFO_NULL = "BGP info cannot be null";
90 private static final String INT_INFO_NULL = "Interface info cannot be null";
91 private static final String DEV_ID_NULL = "Device Id cannot be null";
92
93 private final Logger log = getLogger(getClass());
94
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected StorageService storageService;
97
98 /**
99 * Freed id list of NET L3VPN.
100 */
101 private DistributedSet<Long> freedIdList;
102
103 /**
104 * Map of interface info with access info as key.
105 */
106 private ConsistentMap<AccessInfo, InterfaceInfo> intInfoMap;
107
108 /**
109 * Map of VPN instance with VPN name as key.
110 */
111 private ConsistentMap<String, VpnInstance> vpnInsMap;
112
113 /**
114 * Map of BGP information and the device id.
115 */
116 private ConsistentMap<BgpInfo, DeviceId> bgpInfoMap;
117
118 @Activate
119 protected void activate() {
120 vpnInsMap = storageService.<String, VpnInstance>consistentMapBuilder()
121 .withName("onos-l3vpn-instance-map")
122 .withSerializer(L3VPN_SERIALIZER)
123 .build();
124
125 intInfoMap = storageService
126 .<AccessInfo, InterfaceInfo>consistentMapBuilder()
127 .withName("onos-l3vpn-int-info-map")
128 .withSerializer(L3VPN_SERIALIZER)
129 .build();
130
131 bgpInfoMap = storageService.<BgpInfo, DeviceId>consistentMapBuilder()
132 .withName("onos-l3vpn-bgp-info-map")
133 .withSerializer(L3VPN_SERIALIZER)
134 .build();
135
136 freedIdList = storageService.<Long>setBuilder()
137 .withName("onos-l3vpn-id-freed-list")
138 .withSerializer(Serializer.using(KryoNamespaces.API))
139 .build()
140 .asDistributedSet();
141
142 log.info("Started");
143 }
144
145 @Deactivate
146 protected void deactivate() {
147 log.info("Stopped");
148 }
149
150 @Override
151 public Iterable<Long> getFreedIdList() {
152 return ImmutableSet.copyOf(freedIdList);
153 }
154
155 @Override
156 public Map<String, VpnInstance> getVpnInstances() {
157 return vpnInsMap.entrySet().stream()
158 .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue()
159 .value()));
160 }
161
162 @Override
163 public Map<BgpInfo, DeviceId> getBgpInfo() {
164 return bgpInfoMap.entrySet().stream()
165 .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue()
166 .value()));
167 }
168
169 @Override
170 public Map<AccessInfo, InterfaceInfo> getInterfaceInfo() {
171 return intInfoMap.entrySet().stream()
172 .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue()
173 .value()));
174 }
175
176 @Override
177 public void addIdToFreeList(Long id) {
178 checkNotNull(id, FREE_ID_NULL);
179 freedIdList.add(id);
180 }
181
182 @Override
183 public void addVpnInsIfAbsent(String name, VpnInstance instance) {
184 checkNotNull(name, VPN_NAME_NULL);
185 checkNotNull(instance, VPN_INS_NULL);
186 vpnInsMap.putIfAbsent(name, instance);
187 }
188
189 @Override
190 public void addInterfaceInfo(AccessInfo accessInfo, InterfaceInfo intInfo) {
191 checkNotNull(accessInfo, ACCESS_INFO_NULL);
192 checkNotNull(intInfo, INT_INFO_NULL);
193 intInfoMap.putIfAbsent(accessInfo, intInfo);
194 }
195
196 @Override
197 public void addBgpInfo(BgpInfo bgpInfo, DeviceId devId) {
198 checkNotNull(devId, BGP_INFO_NULL);
199 checkNotNull(devId, DEV_ID_NULL);
200 bgpInfoMap.put(bgpInfo, devId);
201 }
202
203 @Override
204 public boolean removeInterfaceInfo(AccessInfo accessInfo) {
205 checkNotNull(accessInfo, ACCESS_INFO_NULL);
206
207 if (intInfoMap.remove(accessInfo) == null) {
208 log.error("Interface info deletion for access info {} has failed.",
209 accessInfo.toString());
210 return false;
211 }
212 return true;
213 }
214
215 @Override
216 public boolean removeVpnInstance(String vpnName) {
217 checkNotNull(vpnName, VPN_NAME_NULL);
218
219 if (vpnInsMap.remove(vpnName) == null) {
220 log.error("Vpn instance deletion for vpn name {} has failed.",
221 vpnName);
222 return false;
223 }
224 return true;
225 }
226
227 @Override
228 public boolean removeIdFromFreeList(Long id) {
229 checkNotNull(id, FREE_ID_NULL);
230
231 if (!freedIdList.remove(id)) {
232 log.error("Id from free id list {} deletion has failed.",
233 id.toString());
234 return false;
235 }
236 return true;
237 }
238
239 @Override
240 public boolean removeBgpInfo(BgpInfo bgpInfo) {
241 checkNotNull(bgpInfo, BGP_INFO_NULL);
242
243 if (bgpInfoMap.remove(bgpInfo) == null) {
244 log.error("Device id deletion for BGP info {} has failed.",
245 bgpInfo.toString());
246 return false;
247 }
248 return true;
249 }
250}