blob: f692a6228d388705a97608009e9afa1f24ccc726 [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
Mahesh Poojary Sba827292016-05-09 11:31:12 +053022import java.util.List;
23import java.util.Map;
24import java.util.stream.Collectors;
25
26import org.apache.felix.scr.annotations.Activate;
27import org.apache.felix.scr.annotations.Component;
28import org.apache.felix.scr.annotations.Deactivate;
29import org.apache.felix.scr.annotations.Reference;
30import org.apache.felix.scr.annotations.ReferenceCardinality;
31import org.apache.felix.scr.annotations.Service;
32
33import org.onlab.util.KryoNamespace;
34import org.onosproject.incubator.net.tunnel.TunnelId;
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053035import org.onosproject.incubator.net.resource.label.LabelResource;
Mahesh Poojary Sba827292016-05-09 11:31:12 +053036import org.onosproject.incubator.net.resource.label.LabelResourceId;
Mahesh Poojary S33536202016-05-30 07:22:36 +053037import org.onosproject.net.intent.constraint.BandwidthConstraint;
Mahesh Poojary Sba827292016-05-09 11:31:12 +053038import org.onosproject.net.DeviceId;
39import org.onosproject.net.Link;
40import org.onosproject.net.resource.ResourceConsumer;
Mahesh Poojary S33536202016-05-30 07:22:36 +053041import org.onosproject.pce.pceservice.constraint.CostConstraint;
Mahesh Poojary Sba827292016-05-09 11:31:12 +053042import org.onosproject.pce.pceservice.TunnelConsumerId;
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053043import org.onosproject.pce.pceservice.LspType;
Mahesh Poojary Sba827292016-05-09 11:31:12 +053044import org.onosproject.pce.pcestore.api.LspLocalLabelInfo;
45import org.onosproject.pce.pcestore.api.PceStore;
46import org.onosproject.store.serializers.KryoNamespaces;
47import org.onosproject.store.service.ConsistentMap;
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053048import org.onosproject.store.service.DistributedSet;
Mahesh Poojary Sba827292016-05-09 11:31:12 +053049import org.onosproject.store.service.Serializer;
50import org.onosproject.store.service.StorageService;
51
52import org.slf4j.Logger;
53import org.slf4j.LoggerFactory;
54
55/**
56 * Manages the pool of available labels to devices, links and tunnels.
57 */
58@Component(immediate = true)
59@Service
60public class DistributedPceStore implements PceStore {
61
62 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053063 private static final String DEVICE_LABEL_STORE_INFO_NULL = "Device Label Store cannot be null";
64 private static final String LABEL_RESOURCE_ID_NULL = "Label Resource Id cannot be null";
65 private static final String LABEL_RESOURCE_LIST_NULL = "Label Resource List cannot be null";
66 private static final String LABEL_RESOURCE_NULL = "Label Resource cannot be null";
Mahesh Poojary Sba827292016-05-09 11:31:12 +053067 private static final String LINK_NULL = "LINK cannot be null";
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053068 private static final String LSP_LOCAL_LABEL_INFO_NULL = "LSP Local Label Info cannot be null";
69 private static final String PATH_INFO_NULL = "Path Info cannot be null";
Mahesh Poojary Sba827292016-05-09 11:31:12 +053070 private static final String PCECC_TUNNEL_INFO_NULL = "PCECC Tunnel Info cannot be null";
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053071 private static final String TUNNEL_ID_NULL = "Tunnel Id cannot be null";
Mahesh Poojary Sba827292016-05-09 11:31:12 +053072 private static final String TUNNEL_CONSUMER_ID_NULL = "Tunnel consumer Id cannot be null";
73
74 private final Logger log = LoggerFactory.getLogger(getClass());
75
76 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
77 protected StorageService storageService;
78
79 // Mapping device with global node label
80 private ConsistentMap<DeviceId, LabelResourceId> globalNodeLabelMap;
81
82 // Mapping link with adjacency label
83 private ConsistentMap<Link, LabelResourceId> adjLabelMap;
84
85 // Mapping tunnel with device local info with tunnel consumer id
86 private ConsistentMap<TunnelId, PceccTunnelInfo> tunnelInfoMap;
87
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053088 // List of Failed path info
89 private DistributedSet<PcePathInfo> failedPathSet;
90
Mahesh Poojary Sba827292016-05-09 11:31:12 +053091 @Activate
92 protected void activate() {
93 globalNodeLabelMap = storageService.<DeviceId, LabelResourceId>consistentMapBuilder()
94 .withName("onos-pce-globalnodelabelmap")
95 .withSerializer(Serializer.using(
96 new KryoNamespace.Builder()
97 .register(KryoNamespaces.API)
98 .register(LabelResourceId.class)
99 .build()))
100 .build();
101
102 adjLabelMap = storageService.<Link, LabelResourceId>consistentMapBuilder()
103 .withName("onos-pce-adjlabelmap")
104 .withSerializer(Serializer.using(
105 new KryoNamespace.Builder()
106 .register(KryoNamespaces.API)
107 .register(Link.class,
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +0530108 LabelResource.class,
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530109 LabelResourceId.class)
110 .build()))
111 .build();
112
113 tunnelInfoMap = storageService.<TunnelId, PceccTunnelInfo>consistentMapBuilder()
114 .withName("onos-pce-tunnelinfomap")
115 .withSerializer(Serializer.using(
116 new KryoNamespace.Builder()
117 .register(KryoNamespaces.API)
118 .register(TunnelId.class,
119 PceccTunnelInfo.class,
120 DefaultLspLocalLabelInfo.class,
121 TunnelConsumerId.class,
122 LabelResourceId.class)
123 .build()))
124 .build();
125
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +0530126 failedPathSet = storageService.<PcePathInfo>setBuilder()
127 .withName("failed-path-info")
128 .withSerializer(Serializer.using(
129 new KryoNamespace.Builder()
130 .register(KryoNamespaces.API)
131 .register(PcePathInfo.class,
Mahesh Poojary S33536202016-05-30 07:22:36 +0530132 CostConstraint.class,
133 CostConstraint.Type.class,
134 BandwidthConstraint.class,
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +0530135 LspType.class)
136 .build()))
137
138 .build()
139 .asDistributedSet();
140
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530141 log.info("Started");
142 }
143
144 @Deactivate
145 protected void deactivate() {
146 log.info("Stopped");
147 }
148
149 @Override
150 public boolean existsGlobalNodeLabel(DeviceId id) {
151 checkNotNull(id, DEVICE_ID_NULL);
152 return globalNodeLabelMap.containsKey(id);
153 }
154
155 @Override
156 public boolean existsAdjLabel(Link link) {
157 checkNotNull(link, LINK_NULL);
158 return adjLabelMap.containsKey(link);
159 }
160
161 @Override
162 public boolean existsTunnelInfo(TunnelId tunnelId) {
163 checkNotNull(tunnelId, TUNNEL_ID_NULL);
164 return tunnelInfoMap.containsKey(tunnelId);
165 }
166
167 @Override
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +0530168 public boolean existsFailedPathInfo(PcePathInfo failedPathInfo) {
169 checkNotNull(failedPathInfo, PATH_INFO_NULL);
170 return failedPathSet.contains(failedPathInfo);
171 }
172
173 @Override
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530174 public int getGlobalNodeLabelCount() {
175 return globalNodeLabelMap.size();
176 }
177
178 @Override
179 public int getAdjLabelCount() {
180 return adjLabelMap.size();
181 }
182
183 @Override
184 public int getTunnelInfoCount() {
185 return tunnelInfoMap.size();
186 }
187
188 @Override
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +0530189 public int getFailedPathInfoCount() {
190 return failedPathSet.size();
191 }
192
193 @Override
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530194 public Map<DeviceId, LabelResourceId> getGlobalNodeLabels() {
195 return globalNodeLabelMap.entrySet().stream()
196 .collect(Collectors.toMap(Map.Entry::getKey, e -> (LabelResourceId) e.getValue().value()));
197 }
198
199 @Override
200 public Map<Link, LabelResourceId> getAdjLabels() {
201 return adjLabelMap.entrySet().stream()
202 .collect(Collectors.toMap(Map.Entry::getKey, e -> (LabelResourceId) e.getValue().value()));
203 }
204
205 @Override
206 public Map<TunnelId, PceccTunnelInfo> getTunnelInfos() {
207 return tunnelInfoMap.entrySet().stream()
208 .collect(Collectors.toMap(Map.Entry::getKey, e -> (PceccTunnelInfo) e.getValue().value()));
209 }
210
211 @Override
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +0530212 public Iterable<PcePathInfo> getFailedPathInfos() {
213 return ImmutableSet.copyOf(failedPathSet);
214 }
215
216 @Override
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530217 public LabelResourceId getGlobalNodeLabel(DeviceId id) {
218 checkNotNull(id, DEVICE_ID_NULL);
219 return globalNodeLabelMap.get(id).value();
220 }
221
222 @Override
223 public LabelResourceId getAdjLabel(Link link) {
224 checkNotNull(link, LINK_NULL);
225 return adjLabelMap.get(link).value();
226 }
227
228 @Override
229 public PceccTunnelInfo getTunnelInfo(TunnelId tunnelId) {
230 checkNotNull(tunnelId, TUNNEL_ID_NULL);
231 return tunnelInfoMap.get(tunnelId).value();
232 }
233
234 @Override
235 public void addGlobalNodeLabel(DeviceId deviceId, LabelResourceId labelId) {
236 checkNotNull(deviceId, DEVICE_ID_NULL);
237 checkNotNull(labelId, LABEL_RESOURCE_ID_NULL);
238
239 globalNodeLabelMap.put(deviceId, labelId);
240 }
241
242 @Override
243 public void addAdjLabel(Link link, LabelResourceId labelId) {
244 checkNotNull(link, LINK_NULL);
245 checkNotNull(labelId, LABEL_RESOURCE_ID_NULL);
246
247 adjLabelMap.put(link, labelId);
248 }
249
250 @Override
251 public void addTunnelInfo(TunnelId tunnelId, PceccTunnelInfo pceccTunnelInfo) {
252 checkNotNull(tunnelId, TUNNEL_ID_NULL);
253 checkNotNull(pceccTunnelInfo, PCECC_TUNNEL_INFO_NULL);
254
255 tunnelInfoMap.put(tunnelId, pceccTunnelInfo);
256 }
257
258 @Override
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +0530259 public void addFailedPathInfo(PcePathInfo failedPathInfo) {
260 checkNotNull(failedPathInfo, PATH_INFO_NULL);
261 failedPathSet.add(failedPathInfo);
262 }
263
264 @Override
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530265 public boolean updateTunnelInfo(TunnelId tunnelId, List<LspLocalLabelInfo> lspLocalLabelInfoList) {
266 checkNotNull(tunnelId, TUNNEL_ID_NULL);
267 checkNotNull(lspLocalLabelInfoList, LSP_LOCAL_LABEL_INFO_NULL);
268
269 if (!tunnelInfoMap.containsKey((tunnelId))) {
270 log.debug("Tunnel info does not exist whose tunnel id is {}.", tunnelId.toString());
271 return false;
272 }
273
274 PceccTunnelInfo tunnelInfo = tunnelInfoMap.get(tunnelId).value();
275 tunnelInfo.lspLocalLabelInfoList(lspLocalLabelInfoList);
276 tunnelInfoMap.put(tunnelId, tunnelInfo);
277
278 return true;
279 }
280
281 @Override
282 public boolean updateTunnelInfo(TunnelId tunnelId, ResourceConsumer tunnelConsumerId) {
283 checkNotNull(tunnelId, TUNNEL_ID_NULL);
284 checkNotNull(tunnelConsumerId, TUNNEL_CONSUMER_ID_NULL);
285
286 if (!tunnelInfoMap.containsKey((tunnelId))) {
287 log.debug("Tunnel info does not exist whose tunnel id is {}.", tunnelId.toString());
288 return false;
289 }
290
291 PceccTunnelInfo tunnelInfo = tunnelInfoMap.get(tunnelId).value();
292 tunnelInfo.tunnelConsumerId(tunnelConsumerId);
293 tunnelInfoMap.put(tunnelId, tunnelInfo);
294
295 return true;
296 }
297
298 @Override
299 public boolean removeGlobalNodeLabel(DeviceId id) {
300 checkNotNull(id, DEVICE_ID_NULL);
301
302 if (globalNodeLabelMap.remove(id) == null) {
303 log.error("SR-TE node label deletion for device {} has failed.", id.toString());
304 return false;
305 }
306 return true;
307 }
308
309 @Override
310 public boolean removeAdjLabel(Link link) {
311 checkNotNull(link, LINK_NULL);
312
313 if (adjLabelMap.remove(link) == null) {
314 log.error("Adjacency label deletion for link {} hash failed.", link.toString());
315 return false;
316 }
317 return true;
318 }
319
320 @Override
321 public boolean removeTunnelInfo(TunnelId tunnelId) {
322 checkNotNull(tunnelId, TUNNEL_ID_NULL);
323
324 if (tunnelInfoMap.remove(tunnelId) == null) {
325 log.error("Tunnel info deletion for tunnel id {} has failed.", tunnelId.toString());
326 return false;
327 }
328 return true;
329 }
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +0530330
331 @Override
332 public boolean removeFailedPathInfo(PcePathInfo failedPathInfo) {
333 checkNotNull(failedPathInfo, PATH_INFO_NULL);
334
335 if (!failedPathSet.remove(failedPathInfo)) {
336 log.error("Failed path info {} deletion has failed.", failedPathInfo.toString());
337 return false;
338 }
339 return true;
340 }
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530341}