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