blob: 8276f3dbdb7459906db32c1b982f7bfe80117c0b [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
20import java.util.List;
21import java.util.Map;
22import java.util.stream.Collectors;
23
24import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
27import org.apache.felix.scr.annotations.Reference;
28import org.apache.felix.scr.annotations.ReferenceCardinality;
29import org.apache.felix.scr.annotations.Service;
30
31import org.onlab.util.KryoNamespace;
32import org.onosproject.incubator.net.tunnel.TunnelId;
33import org.onosproject.incubator.net.resource.label.LabelResourceId;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.Link;
36import org.onosproject.net.resource.ResourceConsumer;
37import org.onosproject.pce.pceservice.TunnelConsumerId;
38import org.onosproject.pce.pcestore.api.LspLocalLabelInfo;
39import org.onosproject.pce.pcestore.api.PceStore;
40import org.onosproject.store.serializers.KryoNamespaces;
41import org.onosproject.store.service.ConsistentMap;
42import org.onosproject.store.service.Serializer;
43import org.onosproject.store.service.StorageService;
44
45import org.slf4j.Logger;
46import org.slf4j.LoggerFactory;
47
48/**
49 * Manages the pool of available labels to devices, links and tunnels.
50 */
51@Component(immediate = true)
52@Service
53public class DistributedPceStore implements PceStore {
54
55 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
56 private static final String LINK_NULL = "LINK cannot be null";
57 private static final String TUNNEL_ID_NULL = "Tunnel ID cannot be null";
58 private static final String LABEL_RESOURCE_ID_NULL = "Label Resource ID cannot be null";
59 private static final String PCECC_TUNNEL_INFO_NULL = "PCECC Tunnel Info cannot be null";
60 private static final String LSP_LOCAL_LABEL_INFO_NULL = "LSP Local Label info cannot be null";
61 private static final String TUNNEL_CONSUMER_ID_NULL = "Tunnel consumer Id cannot be null";
62
63 private final Logger log = LoggerFactory.getLogger(getClass());
64
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected StorageService storageService;
67
68 // Mapping device with global node label
69 private ConsistentMap<DeviceId, LabelResourceId> globalNodeLabelMap;
70
71 // Mapping link with adjacency label
72 private ConsistentMap<Link, LabelResourceId> adjLabelMap;
73
74 // Mapping tunnel with device local info with tunnel consumer id
75 private ConsistentMap<TunnelId, PceccTunnelInfo> tunnelInfoMap;
76
77 @Activate
78 protected void activate() {
79 globalNodeLabelMap = storageService.<DeviceId, LabelResourceId>consistentMapBuilder()
80 .withName("onos-pce-globalnodelabelmap")
81 .withSerializer(Serializer.using(
82 new KryoNamespace.Builder()
83 .register(KryoNamespaces.API)
84 .register(LabelResourceId.class)
85 .build()))
86 .build();
87
88 adjLabelMap = storageService.<Link, LabelResourceId>consistentMapBuilder()
89 .withName("onos-pce-adjlabelmap")
90 .withSerializer(Serializer.using(
91 new KryoNamespace.Builder()
92 .register(KryoNamespaces.API)
93 .register(Link.class,
94 LabelResourceId.class)
95 .build()))
96 .build();
97
98 tunnelInfoMap = storageService.<TunnelId, PceccTunnelInfo>consistentMapBuilder()
99 .withName("onos-pce-tunnelinfomap")
100 .withSerializer(Serializer.using(
101 new KryoNamespace.Builder()
102 .register(KryoNamespaces.API)
103 .register(TunnelId.class,
104 PceccTunnelInfo.class,
105 DefaultLspLocalLabelInfo.class,
106 TunnelConsumerId.class,
107 LabelResourceId.class)
108 .build()))
109 .build();
110
111 log.info("Started");
112 }
113
114 @Deactivate
115 protected void deactivate() {
116 log.info("Stopped");
117 }
118
119 @Override
120 public boolean existsGlobalNodeLabel(DeviceId id) {
121 checkNotNull(id, DEVICE_ID_NULL);
122 return globalNodeLabelMap.containsKey(id);
123 }
124
125 @Override
126 public boolean existsAdjLabel(Link link) {
127 checkNotNull(link, LINK_NULL);
128 return adjLabelMap.containsKey(link);
129 }
130
131 @Override
132 public boolean existsTunnelInfo(TunnelId tunnelId) {
133 checkNotNull(tunnelId, TUNNEL_ID_NULL);
134 return tunnelInfoMap.containsKey(tunnelId);
135 }
136
137 @Override
138 public int getGlobalNodeLabelCount() {
139 return globalNodeLabelMap.size();
140 }
141
142 @Override
143 public int getAdjLabelCount() {
144 return adjLabelMap.size();
145 }
146
147 @Override
148 public int getTunnelInfoCount() {
149 return tunnelInfoMap.size();
150 }
151
152 @Override
153 public Map<DeviceId, LabelResourceId> getGlobalNodeLabels() {
154 return globalNodeLabelMap.entrySet().stream()
155 .collect(Collectors.toMap(Map.Entry::getKey, e -> (LabelResourceId) e.getValue().value()));
156 }
157
158 @Override
159 public Map<Link, LabelResourceId> getAdjLabels() {
160 return adjLabelMap.entrySet().stream()
161 .collect(Collectors.toMap(Map.Entry::getKey, e -> (LabelResourceId) e.getValue().value()));
162 }
163
164 @Override
165 public Map<TunnelId, PceccTunnelInfo> getTunnelInfos() {
166 return tunnelInfoMap.entrySet().stream()
167 .collect(Collectors.toMap(Map.Entry::getKey, e -> (PceccTunnelInfo) e.getValue().value()));
168 }
169
170 @Override
171 public LabelResourceId getGlobalNodeLabel(DeviceId id) {
172 checkNotNull(id, DEVICE_ID_NULL);
173 return globalNodeLabelMap.get(id).value();
174 }
175
176 @Override
177 public LabelResourceId getAdjLabel(Link link) {
178 checkNotNull(link, LINK_NULL);
179 return adjLabelMap.get(link).value();
180 }
181
182 @Override
183 public PceccTunnelInfo getTunnelInfo(TunnelId tunnelId) {
184 checkNotNull(tunnelId, TUNNEL_ID_NULL);
185 return tunnelInfoMap.get(tunnelId).value();
186 }
187
188 @Override
189 public void addGlobalNodeLabel(DeviceId deviceId, LabelResourceId labelId) {
190 checkNotNull(deviceId, DEVICE_ID_NULL);
191 checkNotNull(labelId, LABEL_RESOURCE_ID_NULL);
192
193 globalNodeLabelMap.put(deviceId, labelId);
194 }
195
196 @Override
197 public void addAdjLabel(Link link, LabelResourceId labelId) {
198 checkNotNull(link, LINK_NULL);
199 checkNotNull(labelId, LABEL_RESOURCE_ID_NULL);
200
201 adjLabelMap.put(link, labelId);
202 }
203
204 @Override
205 public void addTunnelInfo(TunnelId tunnelId, PceccTunnelInfo pceccTunnelInfo) {
206 checkNotNull(tunnelId, TUNNEL_ID_NULL);
207 checkNotNull(pceccTunnelInfo, PCECC_TUNNEL_INFO_NULL);
208
209 tunnelInfoMap.put(tunnelId, pceccTunnelInfo);
210 }
211
212 @Override
213 public boolean updateTunnelInfo(TunnelId tunnelId, List<LspLocalLabelInfo> lspLocalLabelInfoList) {
214 checkNotNull(tunnelId, TUNNEL_ID_NULL);
215 checkNotNull(lspLocalLabelInfoList, LSP_LOCAL_LABEL_INFO_NULL);
216
217 if (!tunnelInfoMap.containsKey((tunnelId))) {
218 log.debug("Tunnel info does not exist whose tunnel id is {}.", tunnelId.toString());
219 return false;
220 }
221
222 PceccTunnelInfo tunnelInfo = tunnelInfoMap.get(tunnelId).value();
223 tunnelInfo.lspLocalLabelInfoList(lspLocalLabelInfoList);
224 tunnelInfoMap.put(tunnelId, tunnelInfo);
225
226 return true;
227 }
228
229 @Override
230 public boolean updateTunnelInfo(TunnelId tunnelId, ResourceConsumer tunnelConsumerId) {
231 checkNotNull(tunnelId, TUNNEL_ID_NULL);
232 checkNotNull(tunnelConsumerId, TUNNEL_CONSUMER_ID_NULL);
233
234 if (!tunnelInfoMap.containsKey((tunnelId))) {
235 log.debug("Tunnel info does not exist whose tunnel id is {}.", tunnelId.toString());
236 return false;
237 }
238
239 PceccTunnelInfo tunnelInfo = tunnelInfoMap.get(tunnelId).value();
240 tunnelInfo.tunnelConsumerId(tunnelConsumerId);
241 tunnelInfoMap.put(tunnelId, tunnelInfo);
242
243 return true;
244 }
245
246 @Override
247 public boolean removeGlobalNodeLabel(DeviceId id) {
248 checkNotNull(id, DEVICE_ID_NULL);
249
250 if (globalNodeLabelMap.remove(id) == null) {
251 log.error("SR-TE node label deletion for device {} has failed.", id.toString());
252 return false;
253 }
254 return true;
255 }
256
257 @Override
258 public boolean removeAdjLabel(Link link) {
259 checkNotNull(link, LINK_NULL);
260
261 if (adjLabelMap.remove(link) == null) {
262 log.error("Adjacency label deletion for link {} hash failed.", link.toString());
263 return false;
264 }
265 return true;
266 }
267
268 @Override
269 public boolean removeTunnelInfo(TunnelId tunnelId) {
270 checkNotNull(tunnelId, TUNNEL_ID_NULL);
271
272 if (tunnelInfoMap.remove(tunnelId) == null) {
273 log.error("Tunnel info deletion for tunnel id {} has failed.", tunnelId.toString());
274 return false;
275 }
276 return true;
277 }
278}