blob: 0cda2817c98f44854e4a815a888ba63d078300a3 [file] [log] [blame]
Mahesh Poojary Sba827292016-05-09 11:31:12 +05301/*
2 * Copyright 2016-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.pce.pcestore;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053020import com.google.common.collect.ImmutableSet;
21
Avantika-Huawei28b53752016-06-23 17:04:49 +053022import java.util.HashMap;
23import java.util.HashSet;
Mahesh Poojary Sba827292016-05-09 11:31:12 +053024import java.util.List;
25import java.util.Map;
26import java.util.stream.Collectors;
27
28import org.apache.felix.scr.annotations.Activate;
29import org.apache.felix.scr.annotations.Component;
30import org.apache.felix.scr.annotations.Deactivate;
31import org.apache.felix.scr.annotations.Reference;
32import org.apache.felix.scr.annotations.ReferenceCardinality;
33import org.apache.felix.scr.annotations.Service;
34
35import org.onlab.util.KryoNamespace;
36import org.onosproject.incubator.net.tunnel.TunnelId;
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053037import org.onosproject.incubator.net.resource.label.LabelResource;
Mahesh Poojary Sba827292016-05-09 11:31:12 +053038import org.onosproject.incubator.net.resource.label.LabelResourceId;
Mahesh Poojary S33536202016-05-30 07:22:36 +053039import org.onosproject.net.intent.constraint.BandwidthConstraint;
Mahesh Poojary Sba827292016-05-09 11:31:12 +053040import org.onosproject.net.DeviceId;
41import org.onosproject.net.Link;
42import org.onosproject.net.resource.ResourceConsumer;
Priyanka Bcdf9b102016-06-07 20:01:38 +053043import org.onosproject.pce.pceservice.constraint.CapabilityConstraint;
Mahesh Poojary S33536202016-05-30 07:22:36 +053044import org.onosproject.pce.pceservice.constraint.CostConstraint;
Mahesh Poojary Sba827292016-05-09 11:31:12 +053045import org.onosproject.pce.pceservice.TunnelConsumerId;
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053046import org.onosproject.pce.pceservice.LspType;
Mahesh Poojary Sba827292016-05-09 11:31:12 +053047import org.onosproject.pce.pcestore.api.LspLocalLabelInfo;
48import org.onosproject.pce.pcestore.api.PceStore;
49import org.onosproject.store.serializers.KryoNamespaces;
50import org.onosproject.store.service.ConsistentMap;
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053051import org.onosproject.store.service.DistributedSet;
Mahesh Poojary Sba827292016-05-09 11:31:12 +053052import org.onosproject.store.service.Serializer;
53import org.onosproject.store.service.StorageService;
54
55import org.slf4j.Logger;
56import org.slf4j.LoggerFactory;
57
58/**
59 * Manages the pool of available labels to devices, links and tunnels.
60 */
61@Component(immediate = true)
62@Service
63public class DistributedPceStore implements PceStore {
64
65 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053066 private static final String DEVICE_LABEL_STORE_INFO_NULL = "Device Label Store cannot be null";
67 private static final String LABEL_RESOURCE_ID_NULL = "Label Resource Id cannot be null";
68 private static final String LABEL_RESOURCE_LIST_NULL = "Label Resource List cannot be null";
69 private static final String LABEL_RESOURCE_NULL = "Label Resource cannot be null";
Mahesh Poojary Sba827292016-05-09 11:31:12 +053070 private static final String LINK_NULL = "LINK cannot be null";
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053071 private static final String LSP_LOCAL_LABEL_INFO_NULL = "LSP Local Label Info cannot be null";
72 private static final String PATH_INFO_NULL = "Path Info cannot be null";
Mahesh Poojary Sba827292016-05-09 11:31:12 +053073 private static final String PCECC_TUNNEL_INFO_NULL = "PCECC Tunnel Info cannot be null";
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053074 private static final String TUNNEL_ID_NULL = "Tunnel Id cannot be null";
Mahesh Poojary Sba827292016-05-09 11:31:12 +053075 private static final String TUNNEL_CONSUMER_ID_NULL = "Tunnel consumer Id cannot be null";
76
77 private final Logger log = LoggerFactory.getLogger(getClass());
78
79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected StorageService storageService;
81
82 // Mapping device with global node label
83 private ConsistentMap<DeviceId, LabelResourceId> globalNodeLabelMap;
84
85 // Mapping link with adjacency label
86 private ConsistentMap<Link, LabelResourceId> adjLabelMap;
87
88 // Mapping tunnel with device local info with tunnel consumer id
89 private ConsistentMap<TunnelId, PceccTunnelInfo> tunnelInfoMap;
90
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053091 // List of Failed path info
92 private DistributedSet<PcePathInfo> failedPathSet;
93
Avantika-Huawei28b53752016-06-23 17:04:49 +053094 // Locally maintain LSRID to device id mapping for better performance.
95 private Map<String, DeviceId> lsrIdDeviceIdMap = new HashMap<>();
96
97 // List of PCC LSR ids whose BGP device information was not available to perform
98 // label db sync.
99 private HashSet<DeviceId> pendinglabelDbSyncPccMap = new HashSet();
100
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530101 @Activate
102 protected void activate() {
103 globalNodeLabelMap = storageService.<DeviceId, LabelResourceId>consistentMapBuilder()
104 .withName("onos-pce-globalnodelabelmap")
105 .withSerializer(Serializer.using(
106 new KryoNamespace.Builder()
107 .register(KryoNamespaces.API)
108 .register(LabelResourceId.class)
109 .build()))
110 .build();
111
112 adjLabelMap = storageService.<Link, LabelResourceId>consistentMapBuilder()
113 .withName("onos-pce-adjlabelmap")
114 .withSerializer(Serializer.using(
115 new KryoNamespace.Builder()
116 .register(KryoNamespaces.API)
117 .register(Link.class,
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +0530118 LabelResource.class,
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530119 LabelResourceId.class)
120 .build()))
121 .build();
122
123 tunnelInfoMap = storageService.<TunnelId, PceccTunnelInfo>consistentMapBuilder()
124 .withName("onos-pce-tunnelinfomap")
125 .withSerializer(Serializer.using(
126 new KryoNamespace.Builder()
127 .register(KryoNamespaces.API)
128 .register(TunnelId.class,
129 PceccTunnelInfo.class,
130 DefaultLspLocalLabelInfo.class,
131 TunnelConsumerId.class,
132 LabelResourceId.class)
133 .build()))
134 .build();
135
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +0530136 failedPathSet = storageService.<PcePathInfo>setBuilder()
137 .withName("failed-path-info")
138 .withSerializer(Serializer.using(
139 new KryoNamespace.Builder()
140 .register(KryoNamespaces.API)
141 .register(PcePathInfo.class,
Mahesh Poojary S33536202016-05-30 07:22:36 +0530142 CostConstraint.class,
143 CostConstraint.Type.class,
144 BandwidthConstraint.class,
Priyanka Bcdf9b102016-06-07 20:01:38 +0530145 CapabilityConstraint.class,
146 CapabilityConstraint.CapabilityType.class,
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +0530147 LspType.class)
148 .build()))
149
150 .build()
151 .asDistributedSet();
152
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530153 log.info("Started");
154 }
155
156 @Deactivate
157 protected void deactivate() {
158 log.info("Stopped");
159 }
160
161 @Override
162 public boolean existsGlobalNodeLabel(DeviceId id) {
163 checkNotNull(id, DEVICE_ID_NULL);
164 return globalNodeLabelMap.containsKey(id);
165 }
166
167 @Override
168 public boolean existsAdjLabel(Link link) {
169 checkNotNull(link, LINK_NULL);
170 return adjLabelMap.containsKey(link);
171 }
172
173 @Override
174 public boolean existsTunnelInfo(TunnelId tunnelId) {
175 checkNotNull(tunnelId, TUNNEL_ID_NULL);
176 return tunnelInfoMap.containsKey(tunnelId);
177 }
178
179 @Override
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +0530180 public boolean existsFailedPathInfo(PcePathInfo failedPathInfo) {
181 checkNotNull(failedPathInfo, PATH_INFO_NULL);
182 return failedPathSet.contains(failedPathInfo);
183 }
184
185 @Override
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530186 public int getGlobalNodeLabelCount() {
187 return globalNodeLabelMap.size();
188 }
189
190 @Override
191 public int getAdjLabelCount() {
192 return adjLabelMap.size();
193 }
194
195 @Override
196 public int getTunnelInfoCount() {
197 return tunnelInfoMap.size();
198 }
199
200 @Override
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +0530201 public int getFailedPathInfoCount() {
202 return failedPathSet.size();
203 }
204
205 @Override
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530206 public Map<DeviceId, LabelResourceId> getGlobalNodeLabels() {
207 return globalNodeLabelMap.entrySet().stream()
208 .collect(Collectors.toMap(Map.Entry::getKey, e -> (LabelResourceId) e.getValue().value()));
209 }
210
211 @Override
212 public Map<Link, LabelResourceId> getAdjLabels() {
213 return adjLabelMap.entrySet().stream()
214 .collect(Collectors.toMap(Map.Entry::getKey, e -> (LabelResourceId) e.getValue().value()));
215 }
216
217 @Override
218 public Map<TunnelId, PceccTunnelInfo> getTunnelInfos() {
219 return tunnelInfoMap.entrySet().stream()
220 .collect(Collectors.toMap(Map.Entry::getKey, e -> (PceccTunnelInfo) e.getValue().value()));
221 }
222
223 @Override
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +0530224 public Iterable<PcePathInfo> getFailedPathInfos() {
225 return ImmutableSet.copyOf(failedPathSet);
226 }
227
228 @Override
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530229 public LabelResourceId getGlobalNodeLabel(DeviceId id) {
230 checkNotNull(id, DEVICE_ID_NULL);
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530231 return globalNodeLabelMap.get(id) == null ? null : globalNodeLabelMap.get(id).value();
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530232 }
233
234 @Override
235 public LabelResourceId getAdjLabel(Link link) {
236 checkNotNull(link, LINK_NULL);
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530237 return adjLabelMap.get(link) == null ? null : adjLabelMap.get(link).value();
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530238 }
239
240 @Override
241 public PceccTunnelInfo getTunnelInfo(TunnelId tunnelId) {
242 checkNotNull(tunnelId, TUNNEL_ID_NULL);
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530243 return tunnelInfoMap.get(tunnelId) == null ? null : tunnelInfoMap.get(tunnelId).value();
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530244 }
245
246 @Override
247 public void addGlobalNodeLabel(DeviceId deviceId, LabelResourceId labelId) {
248 checkNotNull(deviceId, DEVICE_ID_NULL);
249 checkNotNull(labelId, LABEL_RESOURCE_ID_NULL);
250
251 globalNodeLabelMap.put(deviceId, labelId);
252 }
253
254 @Override
255 public void addAdjLabel(Link link, LabelResourceId labelId) {
256 checkNotNull(link, LINK_NULL);
257 checkNotNull(labelId, LABEL_RESOURCE_ID_NULL);
258
259 adjLabelMap.put(link, labelId);
260 }
261
262 @Override
263 public void addTunnelInfo(TunnelId tunnelId, PceccTunnelInfo pceccTunnelInfo) {
264 checkNotNull(tunnelId, TUNNEL_ID_NULL);
265 checkNotNull(pceccTunnelInfo, PCECC_TUNNEL_INFO_NULL);
266
267 tunnelInfoMap.put(tunnelId, pceccTunnelInfo);
268 }
269
270 @Override
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +0530271 public void addFailedPathInfo(PcePathInfo failedPathInfo) {
272 checkNotNull(failedPathInfo, PATH_INFO_NULL);
273 failedPathSet.add(failedPathInfo);
274 }
275
276 @Override
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530277 public boolean updateTunnelInfo(TunnelId tunnelId, List<LspLocalLabelInfo> lspLocalLabelInfoList) {
278 checkNotNull(tunnelId, TUNNEL_ID_NULL);
279 checkNotNull(lspLocalLabelInfoList, LSP_LOCAL_LABEL_INFO_NULL);
280
281 if (!tunnelInfoMap.containsKey((tunnelId))) {
282 log.debug("Tunnel info does not exist whose tunnel id is {}.", tunnelId.toString());
283 return false;
284 }
285
286 PceccTunnelInfo tunnelInfo = tunnelInfoMap.get(tunnelId).value();
287 tunnelInfo.lspLocalLabelInfoList(lspLocalLabelInfoList);
288 tunnelInfoMap.put(tunnelId, tunnelInfo);
289
290 return true;
291 }
292
293 @Override
294 public boolean updateTunnelInfo(TunnelId tunnelId, ResourceConsumer tunnelConsumerId) {
295 checkNotNull(tunnelId, TUNNEL_ID_NULL);
296 checkNotNull(tunnelConsumerId, TUNNEL_CONSUMER_ID_NULL);
297
298 if (!tunnelInfoMap.containsKey((tunnelId))) {
299 log.debug("Tunnel info does not exist whose tunnel id is {}.", tunnelId.toString());
300 return false;
301 }
302
303 PceccTunnelInfo tunnelInfo = tunnelInfoMap.get(tunnelId).value();
304 tunnelInfo.tunnelConsumerId(tunnelConsumerId);
305 tunnelInfoMap.put(tunnelId, tunnelInfo);
306
307 return true;
308 }
309
310 @Override
311 public boolean removeGlobalNodeLabel(DeviceId id) {
312 checkNotNull(id, DEVICE_ID_NULL);
313
314 if (globalNodeLabelMap.remove(id) == null) {
315 log.error("SR-TE node label deletion for device {} has failed.", id.toString());
316 return false;
317 }
318 return true;
319 }
320
321 @Override
322 public boolean removeAdjLabel(Link link) {
323 checkNotNull(link, LINK_NULL);
324
325 if (adjLabelMap.remove(link) == null) {
326 log.error("Adjacency label deletion for link {} hash failed.", link.toString());
327 return false;
328 }
329 return true;
330 }
331
332 @Override
333 public boolean removeTunnelInfo(TunnelId tunnelId) {
334 checkNotNull(tunnelId, TUNNEL_ID_NULL);
335
336 if (tunnelInfoMap.remove(tunnelId) == null) {
337 log.error("Tunnel info deletion for tunnel id {} has failed.", tunnelId.toString());
338 return false;
339 }
340 return true;
341 }
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +0530342
343 @Override
344 public boolean removeFailedPathInfo(PcePathInfo failedPathInfo) {
345 checkNotNull(failedPathInfo, PATH_INFO_NULL);
346
347 if (!failedPathSet.remove(failedPathInfo)) {
348 log.error("Failed path info {} deletion has failed.", failedPathInfo.toString());
349 return false;
350 }
351 return true;
352 }
Avantika-Huawei28b53752016-06-23 17:04:49 +0530353
354 @Override
355 public boolean addLsrIdDevice(String lsrId, DeviceId deviceId) {
356 checkNotNull(lsrId);
357 checkNotNull(deviceId);
358
359 lsrIdDeviceIdMap.put(lsrId, deviceId);
360 return true;
361 }
362
363 @Override
364 public boolean removeLsrIdDevice(String lsrId) {
365 checkNotNull(lsrId);
366
367 lsrIdDeviceIdMap.remove(lsrId);
368 return true;
369 }
370
371 @Override
372 public DeviceId getLsrIdDevice(String lsrId) {
373 checkNotNull(lsrId);
374
375 return lsrIdDeviceIdMap.get(lsrId);
376
377 }
378
379 @Override
380 public boolean addPccLsr(DeviceId lsrId) {
381 checkNotNull(lsrId);
382 pendinglabelDbSyncPccMap.add(lsrId);
383 return true;
384 }
385
386 @Override
387 public boolean removePccLsr(DeviceId lsrId) {
388 checkNotNull(lsrId);
389 pendinglabelDbSyncPccMap.remove(lsrId);
390 return true;
391 }
392
393 @Override
394 public boolean hasPccLsr(DeviceId lsrId) {
395 checkNotNull(lsrId);
396 return pendinglabelDbSyncPccMap.contains(lsrId);
397
398 }
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530399}