Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [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.tetopology.management.impl; |
| 17 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 18 | import com.google.common.collect.Lists; |
| 19 | import com.google.common.collect.Maps; |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 20 | import org.apache.commons.collections.CollectionUtils; |
| 21 | import org.apache.commons.collections.MapUtils; |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 22 | import org.onlab.util.KryoNamespace; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.store.AbstractStore; |
| 25 | import org.onosproject.store.serializers.KryoNamespaces; |
| 26 | import org.onosproject.store.service.AtomicCounter; |
| 27 | import org.onosproject.store.service.ConsistentMap; |
| 28 | import org.onosproject.store.service.MapEvent; |
| 29 | import org.onosproject.store.service.MapEventListener; |
| 30 | import org.onosproject.store.service.Serializer; |
| 31 | import org.onosproject.store.service.StorageService; |
| 32 | import org.onosproject.tetopology.management.api.CommonTopologyData; |
| 33 | import org.onosproject.tetopology.management.api.DefaultNetwork; |
| 34 | import org.onosproject.tetopology.management.api.DefaultTeTopologies; |
| 35 | import org.onosproject.tetopology.management.api.DefaultTeTopology; |
| 36 | import org.onosproject.tetopology.management.api.EncodingType; |
| 37 | import org.onosproject.tetopology.management.api.KeyId; |
| 38 | import org.onosproject.tetopology.management.api.LongValue; |
| 39 | import org.onosproject.tetopology.management.api.Network; |
| 40 | import org.onosproject.tetopology.management.api.OptimizationType; |
| 41 | import org.onosproject.tetopology.management.api.ProviderClientId; |
| 42 | import org.onosproject.tetopology.management.api.SwitchingType; |
| 43 | import org.onosproject.tetopology.management.api.TeConstants; |
| 44 | import org.onosproject.tetopology.management.api.TeStatus; |
| 45 | import org.onosproject.tetopology.management.api.TeTopologies; |
| 46 | import org.onosproject.tetopology.management.api.TeTopology; |
| 47 | import org.onosproject.tetopology.management.api.TeTopologyEvent; |
| 48 | import org.onosproject.tetopology.management.api.TeTopologyEvent.Type; |
| 49 | import org.onosproject.tetopology.management.api.TeTopologyId; |
| 50 | import org.onosproject.tetopology.management.api.TeTopologyKey; |
| 51 | import org.onosproject.tetopology.management.api.TeUtils; |
| 52 | import org.onosproject.tetopology.management.api.link.AsNumber; |
| 53 | import org.onosproject.tetopology.management.api.link.CommonLinkData; |
Yixiao Chen | 29f0633 | 2016-12-07 16:14:29 -0500 | [diff] [blame] | 54 | import org.onosproject.tetopology.management.api.link.ConnectivityMatrixId; |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 55 | import org.onosproject.tetopology.management.api.link.DefaultNetworkLink; |
| 56 | import org.onosproject.tetopology.management.api.link.DefaultTeLink; |
| 57 | import org.onosproject.tetopology.management.api.link.ElementType; |
| 58 | import org.onosproject.tetopology.management.api.link.ExternalLink; |
| 59 | import org.onosproject.tetopology.management.api.link.Label; |
| 60 | import org.onosproject.tetopology.management.api.link.LinkBandwidth; |
| 61 | import org.onosproject.tetopology.management.api.link.NetworkLink; |
| 62 | import org.onosproject.tetopology.management.api.link.NetworkLinkKey; |
| 63 | import org.onosproject.tetopology.management.api.link.OduResource; |
| 64 | import org.onosproject.tetopology.management.api.link.PathElement; |
| 65 | import org.onosproject.tetopology.management.api.link.TeIpv4; |
| 66 | import org.onosproject.tetopology.management.api.link.TeIpv6; |
| 67 | import org.onosproject.tetopology.management.api.link.TeLink; |
Yixiao Chen | 29f0633 | 2016-12-07 16:14:29 -0500 | [diff] [blame] | 68 | import org.onosproject.tetopology.management.api.link.TeLinkId; |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 69 | import org.onosproject.tetopology.management.api.link.TeLinkTpGlobalKey; |
| 70 | import org.onosproject.tetopology.management.api.link.TeLinkTpKey; |
| 71 | import org.onosproject.tetopology.management.api.link.TePathAttributes; |
| 72 | import org.onosproject.tetopology.management.api.link.TunnelProtectionType; |
| 73 | import org.onosproject.tetopology.management.api.link.UnderlayAbstractPath; |
| 74 | import org.onosproject.tetopology.management.api.link.UnderlayBackupPath; |
| 75 | import org.onosproject.tetopology.management.api.link.UnderlayPath; |
| 76 | import org.onosproject.tetopology.management.api.link.UnderlayPrimaryPath; |
| 77 | import org.onosproject.tetopology.management.api.link.UnnumberedLink; |
| 78 | import org.onosproject.tetopology.management.api.node.CommonNodeData; |
| 79 | import org.onosproject.tetopology.management.api.node.ConnectivityMatrix; |
| 80 | import org.onosproject.tetopology.management.api.node.ConnectivityMatrixKey; |
| 81 | import org.onosproject.tetopology.management.api.node.DefaultNetworkNode; |
| 82 | import org.onosproject.tetopology.management.api.node.DefaultTeNode; |
| 83 | import org.onosproject.tetopology.management.api.node.DefaultTerminationPoint; |
| 84 | import org.onosproject.tetopology.management.api.node.DefaultTunnelTerminationPoint; |
| 85 | import org.onosproject.tetopology.management.api.node.LocalLinkConnectivity; |
| 86 | import org.onosproject.tetopology.management.api.node.NetworkNode; |
| 87 | import org.onosproject.tetopology.management.api.node.NetworkNodeKey; |
| 88 | import org.onosproject.tetopology.management.api.node.NodeTpKey; |
| 89 | import org.onosproject.tetopology.management.api.node.TeNode; |
| 90 | import org.onosproject.tetopology.management.api.node.TeNodeKey; |
| 91 | import org.onosproject.tetopology.management.api.node.TerminationPoint; |
| 92 | import org.onosproject.tetopology.management.api.node.TerminationPointKey; |
| 93 | import org.onosproject.tetopology.management.api.node.TtpKey; |
| 94 | import org.onosproject.tetopology.management.api.node.TunnelTerminationPoint; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 95 | import org.osgi.service.component.annotations.Activate; |
| 96 | import org.osgi.service.component.annotations.Component; |
| 97 | import org.osgi.service.component.annotations.Deactivate; |
| 98 | import org.osgi.service.component.annotations.Reference; |
| 99 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 100 | import org.slf4j.Logger; |
| 101 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 102 | import java.util.BitSet; |
| 103 | import java.util.List; |
| 104 | import java.util.Map; |
| 105 | import java.util.concurrent.BlockingQueue; |
| 106 | import java.util.concurrent.LinkedBlockingQueue; |
| 107 | |
| 108 | import static org.onosproject.tetopology.management.api.OptimizationType.NOT_OPTIMIZED; |
| 109 | import static org.onosproject.tetopology.management.api.TeTopologyEvent.Type.LINK_ADDED; |
| 110 | import static org.onosproject.tetopology.management.api.TeTopologyEvent.Type.LINK_REMOVED; |
| 111 | import static org.onosproject.tetopology.management.api.TeTopologyEvent.Type.LINK_UPDATED; |
| 112 | import static org.onosproject.tetopology.management.api.TeTopologyEvent.Type.NETWORK_ADDED; |
| 113 | import static org.onosproject.tetopology.management.api.TeTopologyEvent.Type.NETWORK_REMOVED; |
| 114 | import static org.onosproject.tetopology.management.api.TeTopologyEvent.Type.NETWORK_UPDATED; |
| 115 | import static org.onosproject.tetopology.management.api.TeTopologyEvent.Type.NODE_ADDED; |
| 116 | import static org.onosproject.tetopology.management.api.TeTopologyEvent.Type.NODE_REMOVED; |
| 117 | import static org.onosproject.tetopology.management.api.TeTopologyEvent.Type.NODE_UPDATED; |
| 118 | import static org.onosproject.tetopology.management.api.TeTopologyEvent.Type.TE_LINK_ADDED; |
| 119 | import static org.onosproject.tetopology.management.api.TeTopologyEvent.Type.TE_LINK_REMOVED; |
| 120 | import static org.onosproject.tetopology.management.api.TeTopologyEvent.Type.TE_LINK_UPDATED; |
| 121 | import static org.onosproject.tetopology.management.api.TeTopologyEvent.Type.TE_NODE_ADDED; |
| 122 | import static org.onosproject.tetopology.management.api.TeTopologyEvent.Type.TE_NODE_REMOVED; |
| 123 | import static org.onosproject.tetopology.management.api.TeTopologyEvent.Type.TE_NODE_UPDATED; |
| 124 | import static org.onosproject.tetopology.management.api.TeTopologyEvent.Type.TE_TOPOLOGY_ADDED; |
| 125 | import static org.onosproject.tetopology.management.api.TeTopologyEvent.Type.TE_TOPOLOGY_REMOVED; |
| 126 | import static org.onosproject.tetopology.management.api.TeTopologyEvent.Type.TE_TOPOLOGY_UPDATED; |
| 127 | import static org.slf4j.LoggerFactory.getLogger; |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * Implementation of the TE network store. |
| 131 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 132 | @Component(immediate = true, service = TeTopologyStore.class) |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 133 | public class DistributedTeTopologyStore |
| 134 | extends AbstractStore<TeTopologyEvent, TeTopologyStoreDelegate> |
| 135 | implements TeTopologyStore { |
| 136 | private static final String STORE_NAME = "TE_NETWORK_TOPOLOGY_STORE"; |
Jordan Halterman | 279773e | 2017-06-15 11:17:21 -0700 | [diff] [blame] | 137 | private static final String COUNTER_NAME = "TeTopology-TeTopologyId"; |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 138 | private static final String TETOPOLOGYKEY_INTERNALTETOPOLOGY = "TeTopologyKey-InternalTeTopology"; |
Yixiao Chen | 265b3bb | 2017-01-13 10:17:03 -0500 | [diff] [blame] | 139 | private static final String NETWORKID_NETWORK = "NetworkId-InternalNetwork"; |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 140 | private static final String TENODEKEY_INTERNALTENODE = "TeNodeKey-InternalTeNode"; |
| 141 | private static final String CONNMATRIXKEY_CONNECTIVITYMATRIX = "ConnMatrixKey-ConnectivityMatrix"; |
| 142 | private static final String NETWORKNODEKEY_INTERNALNETWORKNODE = "NetworkNodeKey-InternalNetworkNode"; |
| 143 | private static final String TELINKGLOBALKEY_INTERNALTELINK = "TeLinkGlobalKey-InternalTeLink"; |
| 144 | private static final String NETWORKLINKKEY_INTERNALNETWORKLINK = "NetworkLinkKey-InternalNetworkLink"; |
| 145 | private static final String TPKEY_INTERNALTERMINATIONPOINT = "tpKey-InternalTerminationPoint"; |
| 146 | private static final String TELINKTPGLOBALKEY_TERMINATIONPOINTKEY = "TeLinkGlobalKey-TerminationPointKey"; |
| 147 | private static final String TTPKEY_TUNNELTERMINATIONPOINT = "TtpKey-TunnelTerminationPoint"; |
| 148 | private final Logger log = getLogger(getClass()); |
| 149 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 150 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 151 | protected StorageService storageService; |
| 152 | // Track TE topologies by TE Topology key |
| 153 | private ConsistentMap<TeTopologyKey, InternalTeTopology> teTopologyConsistentMap; |
| 154 | private Map<TeTopologyKey, InternalTeTopology> teTopologyMap; |
| 155 | private AtomicCounter nextTeTopologyId; |
| 156 | // Listener for te topology events |
| 157 | private final MapEventListener<TeTopologyKey, InternalTeTopology> teTopologyListener = |
| 158 | new InternalTeTopologyListener(); |
| 159 | // Track networks by network Id |
| 160 | private ConsistentMap<KeyId, InternalNetwork> networkConsistentMap; |
| 161 | private Map<KeyId, InternalNetwork> networkMap; |
| 162 | // Listener for network events |
| 163 | private final MapEventListener<KeyId, InternalNetwork> networkListener = |
| 164 | new InternalNetworkListener(); |
| 165 | // Track TE nodes by TE node key |
| 166 | private ConsistentMap<TeNodeKey, InternalTeNode> teNodeConsistentMap; |
| 167 | private Map<TeNodeKey, InternalTeNode> teNodeMap; |
| 168 | // Track ConnectivityMatrix by its key |
| 169 | private ConsistentMap<ConnectivityMatrixKey, ConnectivityMatrix> connMatrixConsistentMap; |
| 170 | private Map<ConnectivityMatrixKey, ConnectivityMatrix> connMatrixMap; |
| 171 | // Track Tunnel Termination Points by its key |
| 172 | private ConsistentMap<TtpKey, TunnelTerminationPoint> ttpConsistentMap; |
| 173 | private Map<TtpKey, TunnelTerminationPoint> ttpMap; |
| 174 | // Listener for TE node events |
| 175 | private final MapEventListener<TeNodeKey, InternalTeNode> teNodeListener = |
| 176 | new InternalTeNodeListener(); |
| 177 | // Track network nodes by network node key |
| 178 | private ConsistentMap<NetworkNodeKey, InternalNetworkNode> networkNodeConsistentMap; |
| 179 | private Map<NetworkNodeKey, InternalNetworkNode> networkNodeMap; |
| 180 | // Listener for network node events |
| 181 | private final MapEventListener<NetworkNodeKey, InternalNetworkNode> networkNodeListener = |
| 182 | new InternalNetworkNodeListener(); |
| 183 | // Track TE links by its key |
| 184 | private ConsistentMap<TeLinkTpGlobalKey, InternalTeLink> teLinkConsistentMap; |
| 185 | private Map<TeLinkTpGlobalKey, InternalTeLink> teLinkMap; |
| 186 | // Listener for te link events |
| 187 | private final MapEventListener<TeLinkTpGlobalKey, InternalTeLink> teLinkListener = |
| 188 | new InternalTeLinkListener(); |
| 189 | // Track network links by network link key |
| 190 | private ConsistentMap<NetworkLinkKey, InternalNetworkLink> networkLinkConsistentMap; |
| 191 | private Map<NetworkLinkKey, InternalNetworkLink> networkLinkMap; |
| 192 | // Listener for network link events |
| 193 | private final MapEventListener<NetworkLinkKey, InternalNetworkLink> networkLinkListener = |
| 194 | new InternalNetworkLinkListener(); |
| 195 | // Track Termination points by termination point key |
| 196 | private ConsistentMap<TerminationPointKey, InternalTerminationPoint> tpConsistentMap; |
| 197 | private Map<TerminationPointKey, InternalTerminationPoint> tpMap; |
| 198 | // Track termination point keys by TE termination point Key |
| 199 | private ConsistentMap<TeLinkTpGlobalKey, TerminationPointKey> tpKeyConsistentMap; |
| 200 | private Map<TeLinkTpGlobalKey, TerminationPointKey> tpKeyMap; |
Yixiao Chen | 265b3bb | 2017-01-13 10:17:03 -0500 | [diff] [blame] | 201 | private final BlockingQueue<TeTopologyMapEvent> mapEventQueue = new LinkedBlockingQueue<>(); |
| 202 | |
Yixiao Chen | 29f0633 | 2016-12-07 16:14:29 -0500 | [diff] [blame] | 203 | private long providerId; |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 204 | private static final Serializer TETOPOLOGY_SERIALIZER = Serializer |
| 205 | .using(new KryoNamespace.Builder().register(KryoNamespaces.API) |
| 206 | .register(TeTopologyKey.class) |
| 207 | .register(ProviderClientId.class) |
| 208 | .register(TeNodeKey.class) |
| 209 | .register(TeLinkTpGlobalKey.class) |
| 210 | .register(CommonTopologyData.class) |
| 211 | .register(KeyId.class) |
| 212 | .register(OptimizationType.class) |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 213 | .register(InternalTeTopology.class) |
| 214 | .register(InternalNetwork.class) |
| 215 | .register(InternalTerminationPoint.class) |
| 216 | .register(InternalTeNode.class) |
| 217 | .register(InternalNetworkNode.class) |
| 218 | .register(CommonNodeData.class) |
| 219 | .register(ConnectivityMatrixKey.class) |
| 220 | .register(ConnectivityMatrix.class) |
| 221 | .register(TtpKey.class) |
| 222 | .register(NetworkNodeKey.class) |
| 223 | .register(TeStatus.class) |
| 224 | .register(ElementType.class) |
| 225 | .register(TeIpv4.class) |
| 226 | .register(TeIpv6.class) |
| 227 | .register(AsNumber.class) |
| 228 | .register(Label.class) |
| 229 | .register(UnnumberedLink.class) |
Yixiao Chen | 29f0633 | 2016-12-07 16:14:29 -0500 | [diff] [blame] | 230 | .register(TeLinkId.class) |
| 231 | .register(ConnectivityMatrixId.class) |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 232 | .register(InternalTeLink.class) |
| 233 | .register(InternalNetworkLink.class) |
| 234 | .register(TeLinkTpKey.class) |
| 235 | .register(NetworkLinkKey.class) |
| 236 | .register(NodeTpKey.class) |
| 237 | .register(CommonLinkData.class) |
| 238 | .register(SwitchingType.class) |
| 239 | .register(EncodingType.class) |
| 240 | .register(ExternalLink.class) |
| 241 | .register(UnderlayPath.class) |
| 242 | .register(LinkBandwidth.class) |
| 243 | .register(OduResource.class) |
| 244 | .register(PathElement.class) |
| 245 | .register(UnderlayAbstractPath.class) |
| 246 | .register(UnderlayBackupPath.class) |
| 247 | .register(UnderlayPrimaryPath.class) |
| 248 | .register(TePathAttributes.class) |
| 249 | .register(TerminationPoint.class) |
| 250 | .register(TunnelTerminationPoint.class) |
| 251 | .register(DefaultTunnelTerminationPoint.class) |
| 252 | .register(TerminationPointKey.class) |
| 253 | .register(TunnelProtectionType.class) |
| 254 | .register(LongValue.class) |
| 255 | .register(LocalLinkConnectivity.class) |
| 256 | .build()); |
| 257 | |
| 258 | /** |
| 259 | * Distributed network store service activate method. |
| 260 | */ |
| 261 | @Activate |
| 262 | public void activate() { |
| 263 | teTopologyConsistentMap = storageService |
| 264 | .<TeTopologyKey, InternalTeTopology>consistentMapBuilder() |
| 265 | .withSerializer(TETOPOLOGY_SERIALIZER) |
| 266 | .withName(TETOPOLOGYKEY_INTERNALTETOPOLOGY) |
| 267 | .withRelaxedReadConsistency() |
| 268 | .build(); |
| 269 | teTopologyConsistentMap.addListener(teTopologyListener); |
| 270 | teTopologyMap = teTopologyConsistentMap.asJavaMap(); |
| 271 | networkConsistentMap = storageService |
| 272 | .<KeyId, InternalNetwork>consistentMapBuilder() |
| 273 | .withSerializer(TETOPOLOGY_SERIALIZER) |
| 274 | .withName(NETWORKID_NETWORK) |
| 275 | .withRelaxedReadConsistency() |
| 276 | .build(); |
| 277 | networkConsistentMap.addListener(networkListener); |
| 278 | networkMap = networkConsistentMap.asJavaMap(); |
| 279 | teNodeConsistentMap = storageService |
| 280 | .<TeNodeKey, InternalTeNode>consistentMapBuilder() |
| 281 | .withSerializer(TETOPOLOGY_SERIALIZER) |
| 282 | .withName(TENODEKEY_INTERNALTENODE) |
| 283 | .withRelaxedReadConsistency() |
| 284 | .build(); |
| 285 | teNodeConsistentMap.addListener(teNodeListener); |
| 286 | teNodeMap = teNodeConsistentMap.asJavaMap(); |
| 287 | connMatrixConsistentMap = storageService |
| 288 | .<ConnectivityMatrixKey, ConnectivityMatrix>consistentMapBuilder() |
| 289 | .withSerializer(TETOPOLOGY_SERIALIZER) |
| 290 | .withName(CONNMATRIXKEY_CONNECTIVITYMATRIX) |
| 291 | .withRelaxedReadConsistency() |
| 292 | .build(); |
| 293 | connMatrixMap = connMatrixConsistentMap.asJavaMap(); |
| 294 | networkNodeConsistentMap = storageService |
| 295 | .<NetworkNodeKey, InternalNetworkNode>consistentMapBuilder() |
| 296 | .withSerializer(TETOPOLOGY_SERIALIZER) |
| 297 | .withName(NETWORKNODEKEY_INTERNALNETWORKNODE) |
| 298 | .withRelaxedReadConsistency() |
| 299 | .build(); |
| 300 | networkNodeConsistentMap.addListener(networkNodeListener); |
| 301 | networkNodeMap = networkNodeConsistentMap.asJavaMap(); |
| 302 | teLinkConsistentMap = storageService |
| 303 | .<TeLinkTpGlobalKey, InternalTeLink>consistentMapBuilder() |
| 304 | .withSerializer(TETOPOLOGY_SERIALIZER) |
| 305 | .withName(TELINKGLOBALKEY_INTERNALTELINK) |
| 306 | .withRelaxedReadConsistency() |
| 307 | .build(); |
| 308 | teLinkConsistentMap.addListener(teLinkListener); |
| 309 | teLinkMap = teLinkConsistentMap.asJavaMap(); |
| 310 | networkLinkConsistentMap = storageService |
| 311 | .<NetworkLinkKey, InternalNetworkLink>consistentMapBuilder() |
| 312 | .withSerializer(TETOPOLOGY_SERIALIZER) |
| 313 | .withName(NETWORKLINKKEY_INTERNALNETWORKLINK) |
| 314 | .withRelaxedReadConsistency() |
| 315 | .build(); |
| 316 | networkLinkConsistentMap.addListener(networkLinkListener); |
| 317 | networkLinkMap = networkLinkConsistentMap.asJavaMap(); |
| 318 | tpConsistentMap = storageService |
| 319 | .<TerminationPointKey, InternalTerminationPoint>consistentMapBuilder() |
| 320 | .withSerializer(TETOPOLOGY_SERIALIZER) |
| 321 | .withName(TPKEY_INTERNALTERMINATIONPOINT) |
| 322 | .withRelaxedReadConsistency() |
| 323 | .build(); |
| 324 | tpMap = tpConsistentMap.asJavaMap(); |
| 325 | tpKeyConsistentMap = storageService |
| 326 | .<TeLinkTpGlobalKey, TerminationPointKey>consistentMapBuilder() |
| 327 | .withSerializer(TETOPOLOGY_SERIALIZER) |
| 328 | .withName(TELINKTPGLOBALKEY_TERMINATIONPOINTKEY) |
| 329 | .withRelaxedReadConsistency() |
| 330 | .build(); |
| 331 | tpKeyMap = tpKeyConsistentMap.asJavaMap(); |
| 332 | ttpConsistentMap = storageService |
| 333 | .<TtpKey, TunnelTerminationPoint>consistentMapBuilder() |
| 334 | .withSerializer(TETOPOLOGY_SERIALIZER) |
| 335 | .withName(TTPKEY_TUNNELTERMINATIONPOINT) |
| 336 | .withRelaxedReadConsistency() |
| 337 | .build(); |
| 338 | ttpMap = ttpConsistentMap.asJavaMap(); |
| 339 | |
Jordan Halterman | 279773e | 2017-06-15 11:17:21 -0700 | [diff] [blame] | 340 | nextTeTopologyId = storageService.getAtomicCounter(COUNTER_NAME); |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 341 | log.info("Started"); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Distributed network store service deactivate method. |
| 346 | */ |
| 347 | @Deactivate |
| 348 | public void deactivate() { |
| 349 | teTopologyConsistentMap.removeListener(teTopologyListener); |
| 350 | teTopologyConsistentMap.destroy(); |
| 351 | teTopologyMap.clear(); |
| 352 | networkConsistentMap.removeListener(networkListener); |
| 353 | networkConsistentMap.destroy(); |
| 354 | networkMap.clear(); |
| 355 | teNodeConsistentMap.removeListener(teNodeListener); |
| 356 | teNodeConsistentMap.destroy(); |
| 357 | teNodeMap.clear(); |
| 358 | connMatrixConsistentMap.destroy(); |
| 359 | connMatrixMap.clear(); |
| 360 | networkNodeConsistentMap.destroy(); |
| 361 | networkNodeConsistentMap.removeListener(networkNodeListener); |
| 362 | networkNodeMap.clear(); |
| 363 | teLinkConsistentMap.removeListener(teLinkListener); |
| 364 | teLinkConsistentMap.destroy(); |
| 365 | teLinkMap.clear(); |
| 366 | networkLinkConsistentMap.destroy(); |
| 367 | networkLinkConsistentMap.removeListener(networkLinkListener); |
| 368 | networkLinkMap.clear(); |
| 369 | tpConsistentMap.destroy(); |
| 370 | tpMap.clear(); |
| 371 | tpKeyConsistentMap.destroy(); |
| 372 | tpKeyMap.clear(); |
| 373 | ttpConsistentMap.destroy(); |
| 374 | ttpMap.clear(); |
Yixiao Chen | 265b3bb | 2017-01-13 10:17:03 -0500 | [diff] [blame] | 375 | mapEventQueue.clear(); |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 376 | log.info("Stopped"); |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Listener class to map listener map events to the TETOPOLOGY events. |
| 381 | */ |
| 382 | private class InternalTeTopologyListener implements MapEventListener<TeTopologyKey, InternalTeTopology> { |
| 383 | @Override |
| 384 | public void event(MapEvent<TeTopologyKey, InternalTeTopology> event) { |
| 385 | Type type = null; |
| 386 | switch (event.type()) { |
| 387 | case INSERT: |
| 388 | type = TE_TOPOLOGY_ADDED; |
| 389 | break; |
| 390 | case UPDATE: |
| 391 | if (event.newValue().value().childUpdate()) { |
| 392 | // Masked by the child events (e.g. Removal) |
| 393 | break; |
| 394 | } |
| 395 | type = TE_TOPOLOGY_UPDATED; |
| 396 | break; |
| 397 | case REMOVE: |
| 398 | type = TE_TOPOLOGY_REMOVED; |
| 399 | break; |
| 400 | default: |
| 401 | log.error("Unsupported event type: {}", event.type()); |
| 402 | } |
| 403 | if (type != null) { |
| 404 | TeTopologyMapEvent mapEvent = new TeTopologyMapEvent(type); |
| 405 | mapEvent.setTeTopologyKey(event.key()); |
| 406 | try { |
| 407 | mapEventQueue.put(mapEvent); |
| 408 | } catch (InterruptedException e) { |
| 409 | log.warn("Unable to queue event {} ", mapEvent, e); |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 410 | Thread.currentThread().interrupt(); |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Listener class to map listener map events to the network events. |
| 418 | */ |
| 419 | private class InternalNetworkListener implements MapEventListener<KeyId, InternalNetwork> { |
| 420 | @Override |
| 421 | public void event(MapEvent<KeyId, InternalNetwork> event) { |
| 422 | Type type = null; |
| 423 | switch (event.type()) { |
| 424 | case INSERT: |
| 425 | type = NETWORK_ADDED; |
| 426 | break; |
| 427 | case UPDATE: |
| 428 | if (event.newValue().value().childUpdate()) { |
| 429 | // Masked by the child events (e.g. Removal) |
| 430 | break; |
| 431 | } |
| 432 | type = NETWORK_UPDATED; |
| 433 | break; |
| 434 | case REMOVE: |
| 435 | type = NETWORK_REMOVED; |
| 436 | break; |
| 437 | default: |
| 438 | log.error("Unsupported event type: {}", event.type()); |
| 439 | } |
| 440 | if (type != null) { |
| 441 | TeTopologyMapEvent mapEvent = new TeTopologyMapEvent(type); |
| 442 | mapEvent.setNetworkKey(event.key()); |
| 443 | try { |
| 444 | mapEventQueue.put(mapEvent); |
| 445 | } catch (InterruptedException e) { |
| 446 | log.warn("Unable to queue event {} ", mapEvent, e); |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 447 | Thread.currentThread().interrupt(); |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Listener class to map listener map events to the TENODE events. |
| 455 | */ |
| 456 | private class InternalTeNodeListener implements MapEventListener<TeNodeKey, InternalTeNode> { |
| 457 | @Override |
| 458 | public void event(MapEvent<TeNodeKey, InternalTeNode> event) { |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 459 | Type type = null; |
| 460 | switch (event.type()) { |
| 461 | case INSERT: |
| 462 | if (event.newValue().value().parentUpdate()) { |
| 463 | // Masked by the parent event (e.g. Add) |
| 464 | break; |
| 465 | } |
| 466 | type = TE_NODE_ADDED; |
| 467 | break; |
| 468 | case UPDATE: |
| 469 | if (event.newValue().value().childUpdate() || |
| 470 | event.newValue().value().parentUpdate()) { |
| 471 | // Masked by the child event (e.g. Removal) or parent event |
| 472 | break; |
| 473 | } |
| 474 | type = TE_NODE_UPDATED; |
| 475 | break; |
| 476 | case REMOVE: |
| 477 | type = TE_NODE_REMOVED; |
| 478 | break; |
| 479 | default: |
| 480 | log.error("Unsupported event type: {}", event.type()); |
| 481 | } |
| 482 | if (type != null) { |
| 483 | TeTopologyMapEvent mapEvent = new TeTopologyMapEvent(type); |
| 484 | mapEvent.setTeNodeKey(event.key()); |
| 485 | try { |
| 486 | mapEventQueue.put(mapEvent); |
| 487 | } catch (InterruptedException e) { |
| 488 | log.warn("Unable to queue event {} ", mapEvent, e); |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 489 | Thread.currentThread().interrupt(); |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Listener class to map listener map events to the NETWORK NODE events. |
| 497 | */ |
| 498 | private class InternalNetworkNodeListener implements MapEventListener<NetworkNodeKey, InternalNetworkNode> { |
| 499 | @Override |
| 500 | public void event(MapEvent<NetworkNodeKey, InternalNetworkNode> event) { |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 501 | Type type = null; |
| 502 | switch (event.type()) { |
| 503 | case INSERT: |
| 504 | if (event.newValue().value().parentUpdate()) { |
| 505 | // Masked by the parent event (e.g. Add) |
| 506 | break; |
| 507 | } |
| 508 | type = NODE_ADDED; |
| 509 | break; |
| 510 | case UPDATE: |
| 511 | if (event.newValue().value().childUpdate() || |
| 512 | event.newValue().value().parentUpdate()) { |
| 513 | // Masked by the child event (e.g. Removal) or parent event |
| 514 | break; |
| 515 | } |
| 516 | type = NODE_UPDATED; |
| 517 | break; |
| 518 | case REMOVE: |
| 519 | type = NODE_REMOVED; |
| 520 | break; |
| 521 | default: |
| 522 | log.error("Unsupported event type: {}", event.type()); |
| 523 | } |
| 524 | if (type != null) { |
| 525 | TeTopologyMapEvent mapEvent = new TeTopologyMapEvent(type); |
| 526 | mapEvent.setNetworkNodeKey(event.key()); |
| 527 | try { |
| 528 | mapEventQueue.put(mapEvent); |
| 529 | } catch (InterruptedException e) { |
| 530 | log.warn("Unable to queue event {} ", mapEvent, e); |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 531 | Thread.currentThread().interrupt(); |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Listener class to map listener map events to the TELINK events. |
| 539 | */ |
| 540 | private class InternalTeLinkListener implements MapEventListener<TeLinkTpGlobalKey, InternalTeLink> { |
| 541 | @Override |
| 542 | public void event(MapEvent<TeLinkTpGlobalKey, InternalTeLink> event) { |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 543 | Type type = null; |
| 544 | switch (event.type()) { |
| 545 | case INSERT: |
| 546 | if (event.newValue().value().parentUpdate()) { |
| 547 | // Masked by the parent event (e.g. Add) |
| 548 | break; |
| 549 | } |
| 550 | type = TE_LINK_ADDED; |
| 551 | break; |
| 552 | case UPDATE: |
| 553 | if (event.newValue().value().parentUpdate()) { |
| 554 | // Masked by parent event |
| 555 | break; |
| 556 | } |
| 557 | type = TE_LINK_UPDATED; |
| 558 | break; |
| 559 | case REMOVE: |
| 560 | type = TE_LINK_REMOVED; |
| 561 | break; |
| 562 | default: |
| 563 | log.error("Unsupported event type: {}", event.type()); |
| 564 | } |
| 565 | if (type != null) { |
| 566 | TeTopologyMapEvent mapEvent = new TeTopologyMapEvent(type); |
| 567 | mapEvent.setTeLinkKey(event.key()); |
| 568 | try { |
| 569 | mapEventQueue.put(mapEvent); |
| 570 | } catch (InterruptedException e) { |
| 571 | log.warn("Unable to queue event {} ", mapEvent, e); |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 572 | Thread.currentThread().interrupt(); |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | /** |
| 579 | * Listener class to map listener map events to the NETWORK LINK events. |
| 580 | */ |
| 581 | private class InternalNetworkLinkListener implements MapEventListener<NetworkLinkKey, InternalNetworkLink> { |
| 582 | @Override |
| 583 | public void event(MapEvent<NetworkLinkKey, InternalNetworkLink> event) { |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 584 | Type type = null; |
| 585 | switch (event.type()) { |
| 586 | case INSERT: |
| 587 | if (event.newValue().value().parentUpdate()) { |
| 588 | // Masked by the parent event (e.g. Add) |
| 589 | break; |
| 590 | } |
| 591 | type = LINK_ADDED; |
| 592 | break; |
| 593 | case UPDATE: |
| 594 | if (event.newValue().value().parentUpdate()) { |
| 595 | // Masked by the child event (e.g. Removal) or parent event |
| 596 | break; |
| 597 | } |
| 598 | type = LINK_UPDATED; |
| 599 | break; |
| 600 | case REMOVE: |
| 601 | type = LINK_REMOVED; |
| 602 | break; |
| 603 | default: |
| 604 | log.error("Unsupported event type: {}", event.type()); |
| 605 | } |
| 606 | if (type != null) { |
| 607 | TeTopologyMapEvent mapEvent = new TeTopologyMapEvent(type); |
| 608 | mapEvent.setNetworkLinkKey(event.key()); |
| 609 | try { |
| 610 | mapEventQueue.put(mapEvent); |
| 611 | } catch (InterruptedException e) { |
| 612 | log.warn("Unable to queue event {} ", mapEvent, e); |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 613 | Thread.currentThread().interrupt(); |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | @Override |
| 620 | public TeTopologies teTopologies() { |
| 621 | Map<TeTopologyKey, TeTopology> teTopologies = Maps.newHashMap(); |
| 622 | if (MapUtils.isNotEmpty(teTopologyMap)) { |
| 623 | for (TeTopologyKey key : teTopologyMap.keySet()) { |
| 624 | teTopologies.put(key, teTopology(key)); |
| 625 | } |
| 626 | } |
| 627 | return new DefaultTeTopologies(STORE_NAME, teTopologies); |
| 628 | } |
| 629 | |
| 630 | private TeTopology teTopology(TeTopologyKey topologyId, |
| 631 | InternalTeTopology intTopology) { |
| 632 | if (intTopology == null) { |
| 633 | return null; |
| 634 | } |
| 635 | Map<Long, TeNode> teNodes = null; |
| 636 | if (CollectionUtils.isNotEmpty(intTopology.teNodeKeys())) { |
| 637 | teNodes = Maps.newHashMap(); |
| 638 | for (TeNodeKey key : intTopology.teNodeKeys()) { |
| 639 | teNodes.put(key.teNodeId(), teNode(key)); |
| 640 | } |
| 641 | } |
| 642 | Map<TeLinkTpKey, TeLink> teLinks = null; |
| 643 | if (CollectionUtils.isNotEmpty(intTopology.teLinkKeys())) { |
| 644 | teLinks = Maps.newHashMap(); |
| 645 | for (TeLinkTpGlobalKey key : intTopology.teLinkKeys()) { |
| 646 | teLinks.put(key.teLinkTpKey(), teLink(key)); |
| 647 | } |
| 648 | } |
| 649 | return new DefaultTeTopology(topologyId, teNodes, teLinks, |
| 650 | intTopology.teTopologyId(), intTopology.topologyData()); |
| 651 | } |
| 652 | |
| 653 | @Override |
| 654 | public TeTopology teTopology(TeTopologyKey topologyId) { |
| 655 | InternalTeTopology intTopology = teTopologyMap.get(topologyId); |
| 656 | return teTopology(topologyId, intTopology); |
| 657 | } |
| 658 | |
| 659 | private void removeTopologyeMapEntrys(InternalTeTopology curTopology) { |
| 660 | // Remove TE nodes |
| 661 | if (CollectionUtils.isNotEmpty(curTopology.teNodeKeys())) { |
| 662 | for (TeNodeKey key : curTopology.teNodeKeys()) { |
| 663 | removeTeNode(key, true); |
| 664 | } |
| 665 | } |
| 666 | // Remove TE Links |
| 667 | if (CollectionUtils.isNotEmpty(curTopology.teLinkKeys())) { |
| 668 | for (TeLinkTpGlobalKey key : curTopology.teLinkKeys()) { |
| 669 | removeTeLink(key, true); |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | @Override |
| 675 | public void updateTeTopology(TeTopology teTopology) { |
| 676 | InternalTeTopology curTopology = teTopologyMap.get(teTopology.teTopologyId()); |
| 677 | // Update TE nodes |
| 678 | List<NetworkNodeKey> nodeIds = null; |
| 679 | if (MapUtils.isNotEmpty(teTopology.teNodes())) { |
| 680 | nodeIds = Lists.newArrayList(); |
| 681 | for (Map.Entry<Long, TeNode> entry : teTopology.teNodes().entrySet()) { |
| 682 | TeNodeKey teNodeKey = new TeNodeKey(teTopology.teTopologyId(), entry.getKey()); |
| 683 | NetworkNodeKey nodeKey = TeMgrUtil.networkNodeKey(teNodeKey); |
| 684 | updateTeNode(teNodeKey, entry.getValue(), true, true, nodeKey); |
| 685 | nodeIds.add(nodeKey); |
| 686 | } |
| 687 | } |
| 688 | // Update TE links |
| 689 | List<NetworkLinkKey> linkIds = null; |
| 690 | if (MapUtils.isNotEmpty(teTopology.teLinks())) { |
| 691 | linkIds = Lists.newArrayList(); |
| 692 | for (Map.Entry<TeLinkTpKey, TeLink> entry : teTopology.teLinks().entrySet()) { |
| 693 | TeLinkTpGlobalKey teLinkKey = new TeLinkTpGlobalKey(teTopology.teTopologyId(), |
| 694 | entry.getKey()); |
| 695 | NetworkLinkKey linkKey = TeMgrUtil.networkLinkKey(teLinkKey); |
| 696 | updateTeLink(teLinkKey, entry.getValue(), true, true, linkKey); |
| 697 | linkIds.add(linkKey); |
| 698 | } |
| 699 | } |
| 700 | // Finally Update teTopologyMap |
| 701 | InternalTeTopology newTopology = new InternalTeTopology(teTopology); |
| 702 | teTopologyMap.put(teTopology.teTopologyId(), newTopology); |
| 703 | |
| 704 | if (curTopology == null) { |
| 705 | // New topology, update networkMap |
| 706 | InternalNetwork intNetwork = new InternalNetwork(); |
| 707 | intNetwork.setServerProvided(false); |
| 708 | intNetwork.setTeTopologyKey(teTopology.teTopologyId()); |
| 709 | intNetwork.setNodeIds(nodeIds); |
| 710 | intNetwork.setLinkIds(linkIds); |
| 711 | networkMap.put(teTopology.networkId(), intNetwork); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | @Override |
| 716 | public void removeTeTopology(TeTopologyKey topologyId) { |
| 717 | // Remove it from teTopologyMap |
| 718 | InternalTeTopology topology = teTopologyMap.remove(topologyId); |
| 719 | if (topology != null) { |
| 720 | removeTopologyeMapEntrys(topology); |
| 721 | // Remove it from networkMap; |
| 722 | networkMap.remove(topology.topologyData().networkId()); |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | @Override |
| 727 | public List<Network> networks() { |
| 728 | if (MapUtils.isEmpty(networkMap)) { |
| 729 | return null; |
| 730 | } |
| 731 | List<Network> networks = Lists.newArrayList(); |
| 732 | for (KeyId networkId : networkMap.keySet()) { |
| 733 | networks.add(network(networkId)); |
| 734 | } |
| 735 | return networks; |
| 736 | } |
| 737 | |
| 738 | private Network network(KeyId networkId, InternalNetwork curNetwork) { |
| 739 | if (curNetwork == null) { |
| 740 | return null; |
| 741 | } |
| 742 | List<KeyId> supportingNetworkIds = curNetwork.supportingNetworkIds(); |
| 743 | Map<KeyId, NetworkNode> nodes = null; |
| 744 | if (CollectionUtils.isNotEmpty(curNetwork.nodeIds())) { |
| 745 | nodes = Maps.newHashMap(); |
| 746 | for (NetworkNodeKey key : curNetwork.nodeIds()) { |
| 747 | nodes.put(key.nodeId(), networkNode(key)); |
| 748 | } |
| 749 | } |
| 750 | Map<KeyId, NetworkLink> links = null; |
| 751 | if (CollectionUtils.isNotEmpty(curNetwork.linkIds())) { |
| 752 | links = Maps.newHashMap(); |
| 753 | for (NetworkLinkKey key : curNetwork.linkIds()) { |
| 754 | links.put(key.linkId(), networkLink(key)); |
| 755 | } |
| 756 | } |
| 757 | TeTopologyId topologyId = null; |
| 758 | DeviceId ownerId = null; |
Aihua Guo | 0bc1209 | 2017-02-10 09:53:55 -0500 | [diff] [blame] | 759 | OptimizationType opt = NOT_OPTIMIZED; |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 760 | if (curNetwork.teTopologyKey() != null && |
| 761 | teTopologyMap.get(curNetwork.teTopologyKey()) != null) { |
| 762 | topologyId = new TeTopologyId(curNetwork.teTopologyKey().providerId(), |
| 763 | curNetwork.teTopologyKey().clientId(), |
Aihua Guo | 0bc1209 | 2017-02-10 09:53:55 -0500 | [diff] [blame] | 764 | teTopologyMap.get(curNetwork.teTopologyKey()) |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 765 | .teTopologyId()); |
| 766 | ownerId = teTopologyMap.get(curNetwork.teTopologyKey()) |
| 767 | .topologyData().ownerId(); |
Aihua Guo | 0bc1209 | 2017-02-10 09:53:55 -0500 | [diff] [blame] | 768 | opt = teTopologyMap.get(curNetwork.teTopologyKey()).topologyData().optimization(); |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 769 | } |
| 770 | return new DefaultNetwork(networkId, supportingNetworkIds, nodes, links, |
Aihua Guo | 0bc1209 | 2017-02-10 09:53:55 -0500 | [diff] [blame] | 771 | topologyId, curNetwork.serverProvided(), ownerId, opt); |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | @Override |
| 775 | public Network network(KeyId networkId) { |
| 776 | InternalNetwork curNetwork = networkMap.get(networkId); |
| 777 | return network(networkId, curNetwork); |
| 778 | } |
| 779 | |
| 780 | private void removeNetworkMapEntrys(InternalNetwork curNetwork, boolean teRemove) { |
| 781 | // Remove TE nodes |
| 782 | if (CollectionUtils.isNotEmpty(curNetwork.nodeIds())) { |
| 783 | for (NetworkNodeKey key : curNetwork.nodeIds()) { |
| 784 | removeNetworkNode(key, teRemove); |
| 785 | } |
| 786 | } |
| 787 | // Remove TE Links |
| 788 | if (CollectionUtils.isNotEmpty(curNetwork.linkIds())) { |
| 789 | for (NetworkLinkKey key : curNetwork.linkIds()) { |
| 790 | removeNetworkLink(key, teRemove); |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | private TeTopologyKey newTeTopologyKey(TeTopologyId teTopologyId) { |
| 796 | long idValue; |
| 797 | try { |
| 798 | idValue = Long.parseLong(teTopologyId.topologyId()); |
| 799 | } catch (NumberFormatException e) { |
| 800 | // Can't get the long value from the string. |
| 801 | // Use an assigned id value from local id pool, |
| 802 | // Ideally id should be assigned per provider base. |
| 803 | idValue = nextTeTopologyId(); |
| 804 | } |
| 805 | return new TeTopologyKey(teTopologyId.providerId(), teTopologyId.clientId(), idValue); |
| 806 | } |
| 807 | |
| 808 | @Override |
| 809 | public void updateNetwork(Network network) { |
Yixiao Chen | 265b3bb | 2017-01-13 10:17:03 -0500 | [diff] [blame] | 810 | log.debug("updateNetwork {}", network); |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 811 | InternalNetwork curNetwork = networkMap.get(network.networkId()); |
| 812 | TeTopologyKey topoKey = null; |
| 813 | if (network.teTopologyId() != null) { |
| 814 | topoKey = newTeTopologyKey(network.teTopologyId()); |
| 815 | } |
| 816 | // Update TE nodes |
| 817 | List<TeNodeKey> teNodeKeys = null; |
| 818 | if (MapUtils.isNotEmpty(network.nodes())) { |
| 819 | teNodeKeys = Lists.newArrayList(); |
| 820 | for (Map.Entry<KeyId, NetworkNode> entry : network.nodes().entrySet()) { |
| 821 | NetworkNodeKey nodeKey = new NetworkNodeKey(network.networkId(), entry.getKey()); |
| 822 | TeNodeKey teNodeKey = null; |
| 823 | if (topoKey != null && entry.getValue().teNode() != null) { |
| 824 | teNodeKey = new TeNodeKey(topoKey, entry.getValue().teNode().teNodeId()); |
| 825 | } |
| 826 | updateNetworkNode(nodeKey, entry.getValue(), true, false, teNodeKey); |
| 827 | teNodeKeys.add(teNodeKey); |
| 828 | } |
| 829 | } |
| 830 | // Update TE links |
| 831 | List<TeLinkTpGlobalKey> teLinkKeys = null; |
| 832 | if (MapUtils.isNotEmpty(network.links())) { |
| 833 | teLinkKeys = Lists.newArrayList(); |
| 834 | for (Map.Entry<KeyId, NetworkLink> entry : network.links().entrySet()) { |
| 835 | NetworkLinkKey linkKey = new NetworkLinkKey(network.networkId(), entry.getKey()); |
| 836 | TeLinkTpGlobalKey teLinkKey = null; |
| 837 | if (topoKey != null && entry.getValue().teLink() != null) { |
| 838 | teLinkKey = new TeLinkTpGlobalKey(topoKey, entry.getValue().teLink().teLinkKey()); |
| 839 | } |
| 840 | updateNetworkLink(linkKey, entry.getValue(), true, false, teLinkKey); |
| 841 | teLinkKeys.add(teLinkKey); |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | // New network, update TE Topology first |
| 846 | if (curNetwork == null) { |
| 847 | InternalTeTopology intTopo = new InternalTeTopology(network.teTopologyId().topologyId()); |
| 848 | intTopo.setTeNodeKeys(teNodeKeys); |
| 849 | intTopo.setTeLinkKeys(teLinkKeys); |
| 850 | BitSet flags = new BitSet(TeConstants.FLAG_MAX_BITS); |
| 851 | flags.set(TeTopology.BIT_LEARNT); |
Yixiao Chen | 29f0633 | 2016-12-07 16:14:29 -0500 | [diff] [blame] | 852 | if (network.teTopologyId().clientId() == providerId) { |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 853 | // Hard rule for now |
| 854 | flags.set(TeTopology.BIT_CUSTOMIZED); |
| 855 | } |
| 856 | CommonTopologyData common = new CommonTopologyData(network.networkId(), |
Aihua Guo | 0bc1209 | 2017-02-10 09:53:55 -0500 | [diff] [blame] | 857 | network.optimization(), |
| 858 | flags, |
| 859 | network.ownerId()); |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 860 | intTopo.setTopologydata(common); |
| 861 | teTopologyMap.put(topoKey, intTopo); |
| 862 | } |
| 863 | // Finally Update networkMap |
| 864 | InternalNetwork newNetwork = new InternalNetwork(network); |
| 865 | newNetwork.setTeTopologyKey(topoKey); |
| 866 | networkMap.put(network.networkId(), newNetwork); |
| 867 | } |
| 868 | |
| 869 | @Override |
| 870 | public void removeNetwork(KeyId networkId) { |
| 871 | // Remove it from networkMap |
| 872 | InternalNetwork network = networkMap.remove(networkId); |
| 873 | if (network != null && network.teTopologyKey() != null) { |
| 874 | removeNetworkMapEntrys(network, false); |
| 875 | teTopologyMap.remove(network.teTopologyKey()); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | private TeNode teNode(TeNodeKey nodeKey, InternalTeNode intNode) { |
| 880 | if (intNode == null) { |
| 881 | return null; |
| 882 | } |
| 883 | Map<Long, ConnectivityMatrix> connMatrices = null; |
| 884 | if (CollectionUtils.isNotEmpty(intNode.connMatrixKeys())) { |
| 885 | connMatrices = Maps.newHashMap(); |
| 886 | for (ConnectivityMatrixKey key : intNode.connMatrixKeys()) { |
| 887 | connMatrices.put(key.entryId(), connMatrixMap.get(key)); |
| 888 | } |
| 889 | } |
| 890 | List<Long> teLinkIds = null; |
| 891 | if (CollectionUtils.isNotEmpty(intNode.teLinkTpKeys())) { |
| 892 | teLinkIds = Lists.newArrayList(); |
| 893 | for (TeLinkTpGlobalKey key : intNode.teLinkTpKeys()) { |
| 894 | teLinkIds = TeUtils.addListElement(teLinkIds, key.teLinkTpId()); |
| 895 | } |
| 896 | } |
| 897 | List<Long> tps = null; |
| 898 | if (CollectionUtils.isNotEmpty(intNode.teTpKeys())) { |
| 899 | tps = Lists.newArrayList(); |
| 900 | for (TeLinkTpGlobalKey key : intNode.teTpKeys()) { |
| 901 | tps = TeUtils.addListElement(tps, key.teLinkTpId()); |
| 902 | } |
| 903 | } |
| 904 | Map<Long, TunnelTerminationPoint> ttps = null; |
| 905 | if (CollectionUtils.isNotEmpty(intNode.ttpKeys())) { |
| 906 | ttps = Maps.newHashMap(); |
| 907 | for (TtpKey key : intNode.ttpKeys()) { |
| 908 | ttps.put(key.ttpId(), ttpMap.get(key)); |
| 909 | } |
| 910 | } |
| 911 | return new DefaultTeNode(nodeKey.teNodeId(), |
| 912 | intNode.underlayTopologyKey(), |
| 913 | intNode.supportNodeKey(), |
| 914 | intNode.sourceTeNodeKey(), |
| 915 | intNode.teData(), |
| 916 | connMatrices, teLinkIds, ttps, tps); |
| 917 | } |
| 918 | |
| 919 | @Override |
| 920 | public TeNode teNode(TeNodeKey nodeKey) { |
| 921 | InternalTeNode intNode = teNodeMap.get(nodeKey); |
| 922 | return teNode(nodeKey, intNode); |
| 923 | } |
| 924 | |
| 925 | private void removeTeNodeMapEntrys(InternalTeNode intNode) { |
| 926 | // Remove connMatrixMap entries for the node |
| 927 | if (CollectionUtils.isNotEmpty(intNode.connMatrixKeys())) { |
| 928 | for (ConnectivityMatrixKey key : intNode.connMatrixKeys()) { |
| 929 | connMatrixMap.remove(key); |
| 930 | } |
| 931 | } |
| 932 | // Remove ttpMap entries for the node |
| 933 | if (CollectionUtils.isNotEmpty(intNode.ttpKeys())) { |
| 934 | for (TtpKey key : intNode.ttpKeys()) { |
| 935 | ttpMap.remove(key); |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | private void updateTeNode(TeNodeKey nodeKey, TeNode node, boolean parentUpdate, |
| 941 | boolean teNodeUpdate, NetworkNodeKey networkNodeKey) { |
| 942 | InternalTeTopology intTopo = teTopologyMap.get(nodeKey.teTopologyKey()); |
| 943 | if (intTopo == null && !parentUpdate) { |
| 944 | log.error("TE Topology is not in dataStore for nodeUpdate {}", nodeKey); |
| 945 | return; |
| 946 | } |
| 947 | InternalTeNode curNode = teNodeMap.get(nodeKey); |
| 948 | // Update connMatrixMap |
| 949 | if (MapUtils.isNotEmpty(node.connectivityMatrices())) { |
| 950 | for (Map.Entry<Long, ConnectivityMatrix> entry : node.connectivityMatrices().entrySet()) { |
| 951 | connMatrixMap.put(new ConnectivityMatrixKey(nodeKey, entry.getKey()), |
| 952 | entry.getValue()); |
| 953 | } |
| 954 | } |
| 955 | // Update ttpMap |
| 956 | if (MapUtils.isNotEmpty(node.tunnelTerminationPoints())) { |
| 957 | for (Map.Entry<Long, TunnelTerminationPoint> entry : node.tunnelTerminationPoints().entrySet()) { |
| 958 | ttpMap.put(new TtpKey(nodeKey, entry.getKey()), entry.getValue()); |
| 959 | } |
| 960 | } |
| 961 | // Update teNodeMap |
| 962 | InternalTeNode intNode = new InternalTeNode(nodeKey, node, networkNodeKey, parentUpdate); |
| 963 | teNodeMap.put(nodeKey, intNode); |
| 964 | if (curNode == null && !parentUpdate && intTopo != null) { |
| 965 | // Update InternalTeTopology |
| 966 | intTopo.setChildUpdate(true); |
| 967 | TeUtils.addListElement(intTopo.teNodeKeys(), nodeKey); |
| 968 | } |
| 969 | // Update networkNodeMap |
| 970 | if (teNodeUpdate) { |
| 971 | updateNetworkNode(networkNodeKey, networkNode(node), parentUpdate, |
| 972 | teNodeUpdate, nodeKey); |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | private NetworkNode networkNode(TeNode node) { |
| 977 | KeyId nodeId = KeyId.keyId(Long.toString(node.teNodeId())); |
| 978 | List<NetworkNodeKey> supportingNodeIds = null; |
| 979 | if (node.supportingTeNodeId() != null) { |
| 980 | supportingNodeIds = Lists.newArrayList(); |
| 981 | supportingNodeIds.add(new NetworkNodeKey( |
| 982 | TeMgrUtil.toNetworkId((node.supportingTeNodeId().teTopologyKey())), |
| 983 | KeyId.keyId(Long.toString(node.supportingTeNodeId().teNodeId())))); |
| 984 | } |
| 985 | Map<KeyId, TerminationPoint> tps = null; |
| 986 | if (node.teTerminationPointIds() != null) { |
| 987 | tps = Maps.newHashMap(); |
| 988 | for (Long teTpId : node.teTerminationPointIds()) { |
| 989 | tps.put(KeyId.keyId(Long.toString(teTpId)), |
| 990 | new DefaultTerminationPoint(KeyId.keyId(Long.toString(teTpId)), |
| 991 | null, teTpId)); |
| 992 | } |
| 993 | } |
| 994 | return new DefaultNetworkNode(nodeId, supportingNodeIds, node, tps); |
| 995 | } |
| 996 | |
| 997 | @Override |
| 998 | public void updateTeNode(TeNodeKey nodeKey, TeNode node) { |
| 999 | updateTeNode(nodeKey, node, false, true, TeMgrUtil.networkNodeKey(nodeKey)); |
| 1000 | } |
| 1001 | |
| 1002 | private void removeTeNode(TeNodeKey nodeKey, boolean teNodeRemove) { |
| 1003 | // Remove it from InternalTeTopology first |
| 1004 | InternalTeTopology intTopo = teTopologyMap.get(nodeKey.teTopologyKey()); |
| 1005 | if (intTopo != null && CollectionUtils.isNotEmpty(intTopo.teNodeKeys())) { |
| 1006 | intTopo.setChildUpdate(true); |
| 1007 | intTopo.teNodeKeys().remove(nodeKey); |
| 1008 | } |
| 1009 | // Then remove it from teNodeMap |
| 1010 | InternalTeNode node = teNodeMap.remove(nodeKey); |
Palash Kala | 1ddfea0 | 2017-04-12 13:20:02 +0900 | [diff] [blame] | 1011 | |
| 1012 | if (node == null) { |
| 1013 | log.error("No node found for nodeKey {}", nodeKey); |
| 1014 | return; |
| 1015 | } |
| 1016 | |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 1017 | removeTeNodeMapEntrys(node); |
| 1018 | // Remove it from networkNodeMap |
| 1019 | if (teNodeRemove && node != null) { |
| 1020 | removeNetworkNode(node.networkNodeKey(), teNodeRemove); |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | @Override |
| 1025 | public void removeTeNode(TeNodeKey nodeKey) { |
| 1026 | removeTeNode(nodeKey, true); |
| 1027 | } |
| 1028 | |
| 1029 | private NetworkNode networkNode(NetworkNodeKey nodeKey, InternalNetworkNode intNode) { |
| 1030 | if (intNode == null) { |
| 1031 | return null; |
| 1032 | } |
| 1033 | Map<KeyId, TerminationPoint> tps = Maps.newHashMap(); |
| 1034 | for (KeyId tpId : intNode.tpIds()) { |
| 1035 | tps.put(tpId, terminationPoint( |
| 1036 | new TerminationPointKey(nodeKey, tpId))); |
| 1037 | |
| 1038 | } |
| 1039 | return new DefaultNetworkNode(nodeKey.nodeId(), intNode.supportingNodeIds(), |
| 1040 | teNode(intNode.teNodeKey()), tps); |
| 1041 | } |
| 1042 | |
| 1043 | @Override |
| 1044 | public NetworkNode networkNode(NetworkNodeKey nodeKey) { |
| 1045 | InternalNetworkNode intNode = networkNodeMap.get(nodeKey); |
| 1046 | return networkNode(nodeKey, intNode); |
| 1047 | } |
| 1048 | |
| 1049 | private void updateNetworkNode(NetworkNodeKey nodeKey, NetworkNode node, |
| 1050 | boolean parentUpdate, boolean teNodeUpdate, TeNodeKey teNodeKey) { |
| 1051 | InternalNetwork intNework = null; |
| 1052 | if (!parentUpdate) { |
| 1053 | intNework = networkMap.get(nodeKey.networkId()); |
| 1054 | if (intNework == null) { |
| 1055 | log.error("Network is not in dataStore for nodeUpdate {}", nodeKey); |
| 1056 | return; |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | InternalNetworkNode exNode = networkNodeMap.get(nodeKey); |
| 1061 | if (exNode != null && CollectionUtils.isNotEmpty(exNode.tpIds())) { |
| 1062 | // Remove the TerminationPoints first |
| 1063 | for (KeyId tpId : exNode.tpIds()) { |
| 1064 | removeTerminationPoint(new TerminationPointKey(nodeKey, tpId)); |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | if (MapUtils.isNotEmpty(node.terminationPoints())) { |
| 1069 | // Update with new TerminationPoints |
| 1070 | for (Map.Entry<KeyId, TerminationPoint> entry : node.terminationPoints().entrySet()) { |
| 1071 | updateTerminationPoint(new TerminationPointKey(nodeKey, entry.getKey()), |
| 1072 | entry.getValue(), parentUpdate, teNodeKey); |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | // Update teNodeMap first |
| 1077 | if (!teNodeUpdate && teNodeKey != null && node.teNode() != null) { |
| 1078 | updateTeNode(teNodeKey, node.teNode(), parentUpdate, teNodeUpdate, nodeKey); |
| 1079 | } |
| 1080 | // Update networkNodeMap |
| 1081 | InternalNetworkNode intNode = new InternalNetworkNode(node, parentUpdate); |
| 1082 | intNode.setTeNodeKey(teNodeKey); |
| 1083 | networkNodeMap.put(nodeKey, intNode); |
| 1084 | if (exNode == null && !parentUpdate && intNework != null) { |
| 1085 | // Update the InternalNetwork |
| 1086 | intNework.setChildUpdate(true); |
| 1087 | TeUtils.addListElement(intNework.nodeIds(), nodeKey); |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | @Override |
| 1092 | public void updateNetworkNode(NetworkNodeKey nodeKey, NetworkNode node) { |
| 1093 | TeNodeKey teNodeKey = null; |
| 1094 | if (node.teNode() != null) { |
| 1095 | teNodeKey = new TeNodeKey(networkMap.get(nodeKey.networkId()).teTopologyKey(), |
| 1096 | node.teNode().teNodeId()); |
| 1097 | } |
| 1098 | updateNetworkNode(nodeKey, node, false, false, teNodeKey); |
| 1099 | } |
| 1100 | |
| 1101 | private void removeNetworkNode(NetworkNodeKey nodeKey, boolean teNodeRemove) { |
| 1102 | // Update the InternalNetwork |
| 1103 | InternalNetwork intNework = networkMap.get(nodeKey.networkId()); |
| 1104 | if (intNework != null && CollectionUtils.isNotEmpty(intNework.nodeIds())) { |
| 1105 | intNework.setChildUpdate(true); |
Ray Milkey | fa6002c | 2018-01-19 15:50:37 -0800 | [diff] [blame] | 1106 | intNework.nodeIds().remove(nodeKey); |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 1107 | } |
| 1108 | InternalNetworkNode intNode = networkNodeMap.remove(nodeKey); |
| 1109 | if (intNode != null && CollectionUtils.isNotEmpty(intNode.tpIds())) { |
| 1110 | // Remove the TerminationPoints first |
| 1111 | for (KeyId tpId : intNode.tpIds()) { |
| 1112 | removeTerminationPoint(new TerminationPointKey(nodeKey, tpId)); |
| 1113 | } |
| 1114 | } |
| 1115 | if (!teNodeRemove && intNode != null) { |
| 1116 | // Now remove it from teNodeMap |
| 1117 | removeTeNode(intNode.teNodeKey(), teNodeRemove); |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | @Override |
| 1122 | public void removeNetworkNode(NetworkNodeKey nodeKey) { |
| 1123 | removeNetworkNode(nodeKey, false); |
| 1124 | } |
| 1125 | |
| 1126 | private TeLink teLink(TeLinkTpGlobalKey linkKey, InternalTeLink intLink) { |
| 1127 | if (intLink == null) { |
| 1128 | return null; |
| 1129 | } |
| 1130 | return new DefaultTeLink(linkKey.teLinkTpKey(), |
| 1131 | intLink.peerTeLinkKey(), |
| 1132 | intLink.underlayTopologyKey(), |
| 1133 | intLink.supportingLinkKey(), |
| 1134 | intLink.sourceTeLinkKey(), |
| 1135 | intLink.teData()); |
| 1136 | } |
| 1137 | |
| 1138 | @Override |
| 1139 | public TeLink teLink(TeLinkTpGlobalKey linkKey) { |
| 1140 | InternalTeLink intLink = teLinkMap.get(linkKey); |
| 1141 | return teLink(linkKey, intLink); |
| 1142 | } |
| 1143 | |
| 1144 | private void updateTeLink(TeLinkTpGlobalKey linkKey, TeLink link, |
| 1145 | boolean parentUpdate, boolean teLinkUpdate, NetworkLinkKey networkLinkKey) { |
| 1146 | InternalTeTopology intTopo = teTopologyMap.get(linkKey.teTopologyKey()); |
| 1147 | if (intTopo == null && !parentUpdate) { |
| 1148 | log.error("TE Topology is not in dataStore for linkUpdate {}", linkKey); |
| 1149 | return; |
| 1150 | } |
| 1151 | InternalTeNode intNode = teNodeMap.get(linkKey.teNodeKey()); |
| 1152 | if (intNode == null && !parentUpdate) { |
| 1153 | log.error("TE node is not in dataStore for linkUpdate {}", linkKey); |
| 1154 | return; |
| 1155 | } |
| 1156 | InternalTeLink exLink = teLinkMap.get(linkKey); |
| 1157 | |
| 1158 | // Update teLinkMap |
| 1159 | InternalTeLink intLink = new InternalTeLink(link, parentUpdate); |
| 1160 | intLink.setNetworkLinkKey(networkLinkKey); |
| 1161 | teLinkMap.put(linkKey, intLink); |
| 1162 | if (exLink == null && !parentUpdate) { |
| 1163 | if (intTopo != null) { |
| 1164 | // Update the InternalTeTopology |
| 1165 | intTopo.setChildUpdate(true); |
| 1166 | intTopo.setTeLinkKeys(TeUtils.addListElement(intTopo.teLinkKeys(), linkKey)); |
| 1167 | } |
| 1168 | if (intNode != null) { |
| 1169 | // Update the InternalNode |
| 1170 | intNode.setChildUpdate(true); |
| 1171 | intNode.setTeLinkTpKeys(TeUtils.addListElement(intNode.teLinkTpKeys(), linkKey)); |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | // Update networkLinkMap |
| 1176 | if (teLinkUpdate) { |
| 1177 | updateNetworkLink(networkLinkKey, networkLink(link), parentUpdate, |
| 1178 | teLinkUpdate, linkKey); |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | private NetworkLink networkLink(TeLink link) { |
| 1183 | KeyId linkId = TeMgrUtil.toNetworkLinkId(link.teLinkKey()); |
| 1184 | NodeTpKey source = null; |
| 1185 | if (link.teLinkKey() != null) { |
| 1186 | source = new NodeTpKey(KeyId.keyId(Long.toString(link.teLinkKey().teNodeId())), |
| 1187 | KeyId.keyId(Long.toString(link.teLinkKey().teLinkTpId()))); |
| 1188 | } |
| 1189 | NodeTpKey dest = null; |
| 1190 | if (link.peerTeLinkKey() != null) { |
| 1191 | dest = new NodeTpKey(KeyId.keyId(Long.toString(link.peerTeLinkKey().teNodeId())), |
| 1192 | KeyId.keyId(Long.toString(link.peerTeLinkKey().teLinkTpId()))); |
| 1193 | } |
| 1194 | List<NetworkLinkKey> supportingLinkIds = null; |
| 1195 | if (link.supportingTeLinkId() != null) { |
| 1196 | supportingLinkIds = Lists.newArrayList(); |
| 1197 | supportingLinkIds.add(new NetworkLinkKey( |
| 1198 | TeMgrUtil.toNetworkId(link.supportingTeLinkId().teTopologyKey()), |
| 1199 | TeMgrUtil.toNetworkLinkId(link.supportingTeLinkId().teLinkTpKey()))); |
| 1200 | } |
| 1201 | return new DefaultNetworkLink(linkId, source, dest, supportingLinkIds, link); |
| 1202 | } |
| 1203 | |
| 1204 | @Override |
| 1205 | public void updateTeLink(TeLinkTpGlobalKey linkKey, TeLink link) { |
| 1206 | updateTeLink(linkKey, link, false, true, TeMgrUtil.networkLinkKey(linkKey)); |
| 1207 | } |
| 1208 | |
| 1209 | private void removeTeLink(TeLinkTpGlobalKey linkKey, boolean teLinkRemove) { |
| 1210 | // Remove it from InternalTeTopology first |
| 1211 | InternalTeTopology intTopo = teTopologyMap.get(linkKey.teTopologyKey()); |
| 1212 | if (intTopo != null && CollectionUtils.isNotEmpty(intTopo.teLinkKeys())) { |
| 1213 | intTopo.setChildUpdate(true); |
| 1214 | intTopo.teLinkKeys().remove(linkKey); |
| 1215 | } |
| 1216 | // Remove it from InternalTeNode |
| 1217 | InternalTeNode intNode = teNodeMap.get(linkKey.teNodeKey()); |
| 1218 | if (intNode != null && CollectionUtils.isNotEmpty(intNode.teLinkTpKeys())) { |
| 1219 | intNode.setChildUpdate(true); |
| 1220 | intNode.teLinkTpKeys().remove(linkKey); |
| 1221 | } |
| 1222 | // Then remove it from teLinkMap |
| 1223 | InternalTeLink link = teLinkMap.remove(linkKey); |
| 1224 | if (teLinkRemove && link != null) { |
| 1225 | // Remove it from networkLinkMap |
| 1226 | removeNetworkLink(link.networkLinkKey(), teLinkRemove); |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | @Override |
| 1231 | public void removeTeLink(TeLinkTpGlobalKey linkKey) { |
| 1232 | removeTeLink(linkKey, true); |
| 1233 | } |
| 1234 | |
| 1235 | private NetworkLink networkLink(NetworkLinkKey linkKey, InternalNetworkLink intLink) { |
| 1236 | if (intLink == null) { |
| 1237 | return null; |
| 1238 | } |
| 1239 | return new DefaultNetworkLink(linkKey.linkId(), intLink.source(), |
| 1240 | intLink.destination(), intLink.supportingLinkIds(), teLink(intLink.teLinkKey())); |
| 1241 | } |
| 1242 | |
| 1243 | @Override |
| 1244 | public NetworkLink networkLink(NetworkLinkKey linkKey) { |
| 1245 | InternalNetworkLink intLink = networkLinkMap.get(linkKey); |
| 1246 | return networkLink(linkKey, intLink); |
| 1247 | } |
| 1248 | |
| 1249 | private void updateNetworkLink(NetworkLinkKey linkKey, NetworkLink link, |
| 1250 | boolean parentUpdate, boolean teLinkUpdate, TeLinkTpGlobalKey teLinkKey) { |
| 1251 | InternalNetwork intNework = null; |
| 1252 | if (!parentUpdate) { |
| 1253 | intNework = networkMap.get(linkKey.networkId()); |
| 1254 | if (intNework == null) { |
| 1255 | log.error("Network is not in dataStore for linkUpdate {}", linkKey); |
| 1256 | return; |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | InternalNetworkLink exLink = networkLinkMap.get(linkKey); |
| 1261 | |
| 1262 | // Now update teLinkMap first |
| 1263 | if (!teLinkUpdate && teLinkKey != null) { |
| 1264 | updateTeLink(teLinkKey, link.teLink(), parentUpdate, teLinkUpdate, linkKey); |
| 1265 | } |
| 1266 | // Update networkLinkMap |
| 1267 | InternalNetworkLink intLink = new InternalNetworkLink(link, parentUpdate); |
| 1268 | intLink.setTeLinkKey(teLinkKey); |
| 1269 | networkLinkMap.put(linkKey, intLink); |
| 1270 | if (exLink == null && !parentUpdate && intNework != null) { |
| 1271 | // Update the InternalNetwork |
| 1272 | intNework.setChildUpdate(true); |
| 1273 | TeUtils.addListElement(intNework.linkIds(), linkKey); |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | @Override |
| 1278 | public void updateNetworkLink(NetworkLinkKey linkKey, NetworkLink link) { |
| 1279 | TeLinkTpGlobalKey teLinkKey = null; |
| 1280 | if (link.teLink() != null) { |
| 1281 | teLinkKey = new TeLinkTpGlobalKey(networkMap.get(linkKey.networkId()).teTopologyKey(), |
| 1282 | link.teLink().teLinkKey()); |
| 1283 | } |
| 1284 | |
| 1285 | updateNetworkLink(linkKey, link, false, false, teLinkKey); |
| 1286 | } |
| 1287 | |
| 1288 | private void removeNetworkLink(NetworkLinkKey linkKey, boolean teLinkRemove) { |
| 1289 | // Update the InternalNetwork |
| 1290 | InternalNetwork intNework = networkMap.get(linkKey.networkId()); |
| 1291 | if (intNework != null && CollectionUtils.isNotEmpty(intNework.linkIds())) { |
| 1292 | intNework.setChildUpdate(true); |
Ray Milkey | fa6002c | 2018-01-19 15:50:37 -0800 | [diff] [blame] | 1293 | intNework.linkIds().remove(linkKey); |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 1294 | } |
| 1295 | // Remove it from networkLinkMap |
| 1296 | InternalNetworkLink intLink = networkLinkMap.remove(linkKey); |
| 1297 | if (!teLinkRemove && intLink != null && intLink.teLinkKey() != null) { |
| 1298 | // Now remove it from teLinkMap |
| 1299 | removeTeLink(intLink.teLinkKey(), teLinkRemove); |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | @Override |
| 1304 | public void removeNetworkLink(NetworkLinkKey linkKey) { |
| 1305 | removeNetworkLink(linkKey, false); |
| 1306 | } |
| 1307 | |
| 1308 | private TerminationPoint terminationPoint(TerminationPointKey tpKey) { |
| 1309 | InternalTerminationPoint intTp = tpMap.get(tpKey); |
| 1310 | if (intTp == null) { |
| 1311 | return null; |
| 1312 | } |
| 1313 | return new DefaultTerminationPoint(tpKey.tpId(), intTp.supportingTpIds(), |
| 1314 | intTp.teTpKey().teLinkTpId()); |
| 1315 | } |
| 1316 | |
| 1317 | private void updateTerminationPoint(TerminationPointKey tpKey, |
| 1318 | TerminationPoint tp, boolean parentUpdate, TeNodeKey teNodeKey) { |
| 1319 | TeNodeKey myTeNodeKey; |
| 1320 | InternalNetworkNode intNode = null; |
| 1321 | if (!parentUpdate) { |
Yuta HIGUCHI | 488a94c | 2018-01-26 17:24:09 -0800 | [diff] [blame] | 1322 | intNode = networkNodeMap.get(tpKey); |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 1323 | if (intNode == null) { |
| 1324 | log.error(" node is not in dataStore for tp update {}", tpKey); |
| 1325 | return; |
| 1326 | } |
| 1327 | myTeNodeKey = intNode.teNodeKey(); |
| 1328 | } else { |
| 1329 | myTeNodeKey = teNodeKey; |
| 1330 | } |
| 1331 | TeLinkTpGlobalKey teTpKey = new TeLinkTpGlobalKey(myTeNodeKey, tp.teTpId()); |
| 1332 | |
| 1333 | boolean newTp = tpMap.get(tpKey) == null; |
| 1334 | InternalTerminationPoint intTp = new InternalTerminationPoint(tp); |
| 1335 | intTp.setTeTpKey(teTpKey); |
| 1336 | tpMap.put(tpKey, intTp); |
| 1337 | if (newTp) { |
| 1338 | // Update tpKeyMap |
| 1339 | tpKeyMap.put(teTpKey, tpKey); |
| 1340 | if (!parentUpdate && intNode != null) { |
| 1341 | // Update InternalNetworkNode |
| 1342 | intNode.setChildUpdate(true); |
| 1343 | intNode.setTpIds(TeUtils.addListElement(intNode.tpIds(), tpKey.tpId())); |
| 1344 | } |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | @Override |
| 1349 | public void updateTerminationPoint(TerminationPointKey tpKey, |
| 1350 | TerminationPoint tp) { |
| 1351 | updateTerminationPoint(tpKey, tp, false, null); |
| 1352 | } |
| 1353 | |
| 1354 | @Override |
| 1355 | public void removeTerminationPoint(TerminationPointKey tpKey) { |
| 1356 | // Update InternalNetworkNode |
Yuta HIGUCHI | 488a94c | 2018-01-26 17:24:09 -0800 | [diff] [blame] | 1357 | InternalNetworkNode intNode = networkNodeMap.get(tpKey); |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 1358 | if (intNode != null && CollectionUtils.isNotEmpty(intNode.tpIds())) { |
| 1359 | intNode.setChildUpdate(true); |
| 1360 | intNode.tpIds().remove(tpKey.tpId()); |
| 1361 | } |
| 1362 | // Remove it from tpMap |
| 1363 | InternalTerminationPoint tp = tpMap.remove(tpKey); |
| 1364 | // Remove it from tpKeyMap |
| 1365 | if (tp != null) { |
| 1366 | tpKeyMap.remove(tp.teTpKey()); |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | @Override |
| 1371 | public TunnelTerminationPoint tunnelTerminationPoint(TtpKey ttpId) { |
| 1372 | return ttpMap.get(ttpId); |
| 1373 | } |
| 1374 | |
| 1375 | @Override |
| 1376 | public long nextTeTopologyId() { |
| 1377 | return nextTeTopologyId.getAndIncrement(); |
| 1378 | } |
| 1379 | |
| 1380 | @Override |
| 1381 | public long nextTeNodeId(TeTopologyKey topoKey) { |
| 1382 | return teTopologyMap.get(topoKey).nextTeNodeId(); |
| 1383 | } |
| 1384 | |
| 1385 | @Override |
| 1386 | public void setNextTeNodeId(TeTopologyKey topoKey, long nextNodeId) { |
| 1387 | teTopologyMap.get(topoKey).setNextTeNodeId(nextNodeId); |
| 1388 | } |
| 1389 | |
| 1390 | @Override |
| 1391 | public KeyId networkId(TeTopologyKey teTopologyKey) { |
Yixiao Chen | 29f0633 | 2016-12-07 16:14:29 -0500 | [diff] [blame] | 1392 | return teTopologyMap.get(teTopologyKey) == null || |
| 1393 | teTopologyMap.get(teTopologyKey).topologyData() == null ? null : |
| 1394 | teTopologyMap.get(teTopologyKey).topologyData().networkId(); |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | @Override |
| 1398 | public NetworkNodeKey nodeKey(TeNodeKey teNodeKey) { |
Yixiao Chen | 29f0633 | 2016-12-07 16:14:29 -0500 | [diff] [blame] | 1399 | return teNodeMap.get(teNodeKey) == null ? null : |
| 1400 | teNodeMap.get(teNodeKey).networkNodeKey(); |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | @Override |
| 1404 | public NetworkLinkKey linkKey(TeLinkTpGlobalKey teLinkKey) { |
Yixiao Chen | 29f0633 | 2016-12-07 16:14:29 -0500 | [diff] [blame] | 1405 | return teLinkMap.get(teLinkKey) == null ? null : |
| 1406 | teLinkMap.get(teLinkKey).networkLinkKey(); |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | @Override |
| 1410 | public TerminationPointKey terminationPointKey(TeLinkTpGlobalKey teTpKey) { |
| 1411 | return tpKeyMap.get(teTpKey); |
| 1412 | } |
| 1413 | |
| 1414 | @Override |
Yixiao Chen | 265b3bb | 2017-01-13 10:17:03 -0500 | [diff] [blame] | 1415 | public BlockingQueue<TeTopologyMapEvent> mapEventQueue() { |
| 1416 | return mapEventQueue; |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 1417 | } |
Yixiao Chen | 29f0633 | 2016-12-07 16:14:29 -0500 | [diff] [blame] | 1418 | |
| 1419 | @Override |
| 1420 | public void setProviderId(long providerId) { |
| 1421 | this.providerId = providerId; |
| 1422 | } |
Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [diff] [blame] | 1423 | } |
| 1424 | |