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