Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 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.pcelabelstore; |
| 17 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 18 | import org.onlab.util.KryoNamespace; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 19 | import org.onosproject.incubator.net.resource.label.LabelResource; |
| 20 | import org.onosproject.incubator.net.resource.label.LabelResourceId; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 21 | import org.onosproject.incubator.net.tunnel.TunnelId; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.Link; |
| 24 | import org.onosproject.pcelabelstore.api.LspLocalLabelInfo; |
| 25 | import org.onosproject.pcelabelstore.api.PceLabelStore; |
| 26 | import org.onosproject.store.serializers.KryoNamespaces; |
| 27 | import org.onosproject.store.service.ConsistentMap; |
| 28 | import org.onosproject.store.service.Serializer; |
| 29 | import org.onosproject.store.service.StorageService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 30 | import org.osgi.service.component.annotations.Activate; |
| 31 | import org.osgi.service.component.annotations.Component; |
| 32 | import org.osgi.service.component.annotations.Deactivate; |
| 33 | import org.osgi.service.component.annotations.Reference; |
| 34 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 38 | import java.util.HashMap; |
| 39 | import java.util.HashSet; |
| 40 | import java.util.List; |
| 41 | import java.util.Map; |
| 42 | import java.util.stream.Collectors; |
| 43 | |
| 44 | import static com.google.common.base.Preconditions.checkNotNull; |
| 45 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 46 | /** |
| 47 | * Manages the pool of available labels to devices, links and tunnels. |
| 48 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 49 | @Component(immediate = true, service = PceLabelStore.class) |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 50 | public class DistributedPceLabelStore implements PceLabelStore { |
| 51 | |
| 52 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 53 | private static final String LABEL_RESOURCE_ID_NULL = "Label Resource Id cannot be null"; |
| 54 | private static final String LINK_NULL = "LINK cannot be null"; |
| 55 | private static final String PCECC_TUNNEL_INFO_NULL = "PCECC Tunnel Info cannot be null"; |
| 56 | private static final String TUNNEL_ID_NULL = "Tunnel Id cannot be null"; |
| 57 | |
| 58 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 59 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 61 | protected StorageService storageService; |
| 62 | |
| 63 | // Mapping device with global node label |
| 64 | private ConsistentMap<DeviceId, LabelResourceId> globalNodeLabelMap; |
| 65 | |
| 66 | // Mapping link with adjacency label |
| 67 | private ConsistentMap<Link, LabelResourceId> adjLabelMap; |
| 68 | |
| 69 | // Mapping tunnel id with local labels. |
| 70 | private ConsistentMap<TunnelId, List<LspLocalLabelInfo>> tunnelLabelInfoMap; |
| 71 | |
| 72 | // Locally maintain LSRID to device id mapping for better performance. |
| 73 | private Map<String, DeviceId> lsrIdDeviceIdMap = new HashMap<>(); |
| 74 | |
| 75 | // List of PCC LSR ids whose BGP device information was not available to perform |
| 76 | // label db sync. |
| 77 | private HashSet<DeviceId> pendinglabelDbSyncPccMap = new HashSet<>(); |
| 78 | |
| 79 | @Activate |
| 80 | protected void activate() { |
| 81 | globalNodeLabelMap = storageService.<DeviceId, LabelResourceId>consistentMapBuilder() |
| 82 | .withName("onos-pce-globalnodelabelmap") |
| 83 | .withSerializer(Serializer.using( |
| 84 | new KryoNamespace.Builder() |
| 85 | .register(KryoNamespaces.API) |
| 86 | .register(LabelResourceId.class) |
| 87 | .build())) |
| 88 | .build(); |
| 89 | |
| 90 | adjLabelMap = storageService.<Link, LabelResourceId>consistentMapBuilder() |
| 91 | .withName("onos-pce-adjlabelmap") |
| 92 | .withSerializer(Serializer.using( |
| 93 | new KryoNamespace.Builder() |
| 94 | .register(KryoNamespaces.API) |
| 95 | .register(Link.class, |
| 96 | LabelResource.class, |
| 97 | LabelResourceId.class) |
| 98 | .build())) |
| 99 | .build(); |
| 100 | |
| 101 | tunnelLabelInfoMap = storageService.<TunnelId, List<LspLocalLabelInfo>>consistentMapBuilder() |
| 102 | .withName("onos-pce-tunnellabelinfomap") |
| 103 | .withSerializer(Serializer.using( |
| 104 | new KryoNamespace.Builder() |
| 105 | .register(KryoNamespaces.API) |
| 106 | .register(TunnelId.class, |
| 107 | DefaultLspLocalLabelInfo.class, |
| 108 | LabelResourceId.class, |
| 109 | DeviceId.class) |
| 110 | .build())) |
| 111 | .build(); |
| 112 | |
| 113 | log.info("Started"); |
| 114 | } |
| 115 | |
| 116 | @Deactivate |
| 117 | protected void deactivate() { |
| 118 | log.info("Stopped"); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public boolean existsGlobalNodeLabel(DeviceId id) { |
| 123 | checkNotNull(id, DEVICE_ID_NULL); |
| 124 | return globalNodeLabelMap.containsKey(id); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public boolean existsAdjLabel(Link link) { |
| 129 | checkNotNull(link, LINK_NULL); |
| 130 | return adjLabelMap.containsKey(link); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public boolean existsTunnelInfo(TunnelId tunnelId) { |
| 135 | checkNotNull(tunnelId, TUNNEL_ID_NULL); |
| 136 | return tunnelLabelInfoMap.containsKey(tunnelId); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public int getGlobalNodeLabelCount() { |
| 141 | return globalNodeLabelMap.size(); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public int getAdjLabelCount() { |
| 146 | return adjLabelMap.size(); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public int getTunnelInfoCount() { |
| 151 | return tunnelLabelInfoMap.size(); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public Map<DeviceId, LabelResourceId> getGlobalNodeLabels() { |
| 156 | return globalNodeLabelMap.entrySet().stream() |
| 157 | .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().value())); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Map<Link, LabelResourceId> getAdjLabels() { |
| 162 | return adjLabelMap.entrySet().stream() |
| 163 | .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().value())); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public Map<TunnelId, List<LspLocalLabelInfo>> getTunnelInfos() { |
| 168 | return tunnelLabelInfoMap.entrySet().stream() |
| 169 | .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().value())); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public LabelResourceId getGlobalNodeLabel(DeviceId id) { |
| 174 | checkNotNull(id, DEVICE_ID_NULL); |
| 175 | return globalNodeLabelMap.get(id) == null ? null : globalNodeLabelMap.get(id).value(); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public LabelResourceId getAdjLabel(Link link) { |
| 180 | checkNotNull(link, LINK_NULL); |
| 181 | return adjLabelMap.get(link) == null ? null : adjLabelMap.get(link).value(); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public List<LspLocalLabelInfo> getTunnelInfo(TunnelId tunnelId) { |
| 186 | checkNotNull(tunnelId, TUNNEL_ID_NULL); |
| 187 | return tunnelLabelInfoMap.get(tunnelId) == null ? null : tunnelLabelInfoMap.get(tunnelId).value(); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void addGlobalNodeLabel(DeviceId deviceId, LabelResourceId labelId) { |
| 192 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 193 | checkNotNull(labelId, LABEL_RESOURCE_ID_NULL); |
| 194 | |
| 195 | globalNodeLabelMap.put(deviceId, labelId); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public void addAdjLabel(Link link, LabelResourceId labelId) { |
| 200 | checkNotNull(link, LINK_NULL); |
| 201 | checkNotNull(labelId, LABEL_RESOURCE_ID_NULL); |
| 202 | |
| 203 | adjLabelMap.put(link, labelId); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void addTunnelInfo(TunnelId tunnelId, List<LspLocalLabelInfo> lspLocalLabelInfoList) { |
| 208 | checkNotNull(tunnelId, TUNNEL_ID_NULL); |
| 209 | checkNotNull(lspLocalLabelInfoList, PCECC_TUNNEL_INFO_NULL); |
| 210 | |
| 211 | tunnelLabelInfoMap.put(tunnelId, lspLocalLabelInfoList); |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public boolean removeGlobalNodeLabel(DeviceId id) { |
| 216 | checkNotNull(id, DEVICE_ID_NULL); |
| 217 | |
| 218 | if (globalNodeLabelMap.remove(id) == null) { |
| 219 | log.error("SR-TE node label deletion for device {} has failed.", id.toString()); |
| 220 | return false; |
| 221 | } |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public boolean removeAdjLabel(Link link) { |
| 227 | checkNotNull(link, LINK_NULL); |
| 228 | |
| 229 | if (adjLabelMap.remove(link) == null) { |
| 230 | log.error("Adjacency label deletion for link {} hash failed.", link.toString()); |
| 231 | return false; |
| 232 | } |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public boolean removeTunnelInfo(TunnelId tunnelId) { |
| 238 | checkNotNull(tunnelId, TUNNEL_ID_NULL); |
| 239 | |
| 240 | if (tunnelLabelInfoMap.remove(tunnelId) == null) { |
| 241 | log.error("Tunnel info deletion for tunnel id {} has failed.", tunnelId.toString()); |
| 242 | return false; |
| 243 | } |
| 244 | return true; |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public boolean addLsrIdDevice(String lsrId, DeviceId deviceId) { |
| 249 | checkNotNull(lsrId); |
| 250 | checkNotNull(deviceId); |
| 251 | |
| 252 | lsrIdDeviceIdMap.put(lsrId, deviceId); |
| 253 | return true; |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public boolean removeLsrIdDevice(String lsrId) { |
| 258 | checkNotNull(lsrId); |
| 259 | |
| 260 | lsrIdDeviceIdMap.remove(lsrId); |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public DeviceId getLsrIdDevice(String lsrId) { |
| 266 | checkNotNull(lsrId); |
| 267 | |
| 268 | return lsrIdDeviceIdMap.get(lsrId); |
| 269 | |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public boolean addPccLsr(DeviceId lsrId) { |
| 274 | checkNotNull(lsrId); |
| 275 | pendinglabelDbSyncPccMap.add(lsrId); |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public boolean removePccLsr(DeviceId lsrId) { |
| 281 | checkNotNull(lsrId); |
| 282 | pendinglabelDbSyncPccMap.remove(lsrId); |
| 283 | return true; |
| 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public boolean hasPccLsr(DeviceId lsrId) { |
| 288 | checkNotNull(lsrId); |
| 289 | return pendinglabelDbSyncPccMap.contains(lsrId); |
| 290 | |
| 291 | } |
| 292 | } |