Yixiao Chen | 68bfab2 | 2016-11-11 11:04:10 -0500 | [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.tetopology.management.impl; |
| 17 | |
| 18 | import java.util.List; |
| 19 | import java.util.Map; |
| 20 | |
| 21 | import org.apache.commons.collections.MapUtils; |
| 22 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onosproject.tetopology.management.api.DefaultNetwork; |
| 24 | import org.onosproject.tetopology.management.api.KeyId; |
| 25 | import org.onosproject.tetopology.management.api.Network; |
| 26 | import org.onosproject.tetopology.management.api.TeTopology; |
| 27 | import org.onosproject.tetopology.management.api.TeTopologyId; |
| 28 | import org.onosproject.tetopology.management.api.TeTopologyKey; |
| 29 | import org.onosproject.tetopology.management.api.link.DefaultNetworkLink; |
| 30 | import org.onosproject.tetopology.management.api.link.NetworkLink; |
| 31 | import org.onosproject.tetopology.management.api.link.NetworkLinkKey; |
| 32 | import org.onosproject.tetopology.management.api.link.TeLink; |
| 33 | import org.onosproject.tetopology.management.api.link.TeLinkTpGlobalKey; |
| 34 | import org.onosproject.tetopology.management.api.link.TeLinkTpKey; |
| 35 | import org.onosproject.tetopology.management.api.node.DefaultNetworkNode; |
| 36 | import org.onosproject.tetopology.management.api.node.DefaultTerminationPoint; |
| 37 | import org.onosproject.tetopology.management.api.node.NetworkNode; |
| 38 | import org.onosproject.tetopology.management.api.node.NetworkNodeKey; |
| 39 | import org.onosproject.tetopology.management.api.node.NodeTpKey; |
| 40 | import org.onosproject.tetopology.management.api.node.TeNode; |
| 41 | import org.onosproject.tetopology.management.api.node.TeNodeKey; |
| 42 | import org.onosproject.tetopology.management.api.node.TerminationPoint; |
| 43 | |
| 44 | import com.google.common.collect.Lists; |
| 45 | import com.google.common.collect.Maps; |
| 46 | |
| 47 | /** |
| 48 | * TE Topology Manager utility functions. |
| 49 | */ |
| 50 | public final class TeMgrUtil { |
| 51 | private static final String TENODE_ID = "teNodeId/"; |
| 52 | private static final String TELINK_ID = "/teLinkId/"; |
| 53 | private static final String PROVIDER_ID = "providerId/"; |
| 54 | private static final String CLIENT_ID = "/clientId/"; |
| 55 | private static final String TOPOLOGY_ID = "/topologyId/"; |
| 56 | |
| 57 | // no instantiation |
| 58 | private TeMgrUtil() { |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns the network link id for a TE link local key. |
| 63 | * |
| 64 | * @param key TE link local key |
| 65 | * @return value of network link id |
| 66 | */ |
| 67 | public static KeyId toNetworkLinkId(TeLinkTpKey key) { |
| 68 | return KeyId.keyId(new StringBuilder() |
| 69 | .append(TENODE_ID) |
| 70 | .append(Ip4Address.valueOf((int) key.teNodeId()).toString()) |
| 71 | .append(TELINK_ID) |
| 72 | .append(key.teLinkTpId()).toString()); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the network id for a TE topology id. |
| 77 | * |
| 78 | * @param teTopologyId TE topology id |
| 79 | * @return value of network id |
| 80 | */ |
| 81 | public static KeyId toNetworkId(TeTopologyId teTopologyId) { |
| 82 | return KeyId.keyId(new StringBuilder() |
| 83 | .append(PROVIDER_ID) |
| 84 | .append(teTopologyId.providerId()) |
| 85 | .append(CLIENT_ID) |
| 86 | .append(teTopologyId.clientId()) |
| 87 | .append(TOPOLOGY_ID) |
| 88 | .append(teTopologyId.topologyId()).toString()); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns the network id for a TE topology key. |
| 93 | * |
| 94 | * @param teTopologyKey TE topology key |
| 95 | * @return value of network id |
| 96 | */ |
| 97 | public static KeyId toNetworkId(TeTopologyKey teTopologyKey) { |
| 98 | return KeyId.keyId(new StringBuilder() |
| 99 | .append(PROVIDER_ID) |
| 100 | .append(teTopologyKey.providerId()) |
| 101 | .append(CLIENT_ID) |
| 102 | .append(teTopologyKey.clientId()) |
| 103 | .append(TOPOLOGY_ID) |
| 104 | .append(teTopologyKey.topologyId()).toString()); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns the network node key for a TE node global key. |
| 109 | * |
| 110 | * @param teNodeKey TE node global key |
| 111 | * @return value of network node key |
| 112 | */ |
| 113 | public static NetworkNodeKey networkNodeKey(TeNodeKey teNodeKey) { |
| 114 | return new NetworkNodeKey(toNetworkId(teNodeKey.teTopologyKey()), |
| 115 | KeyId.keyId(Long.toString(teNodeKey.teNodeId()))); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Returns the network link key for a TE link global key. |
| 120 | * |
| 121 | * @param teLinkKey TE link global key |
| 122 | * @return value of network link key |
| 123 | */ |
| 124 | public static NetworkLinkKey networkLinkKey(TeLinkTpGlobalKey teLinkKey) { |
| 125 | return new NetworkLinkKey(toNetworkId(teLinkKey.teTopologyKey()), |
| 126 | toNetworkLinkId(teLinkKey.teLinkTpKey())); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns the TE topology id for a TE topology. |
| 131 | * |
| 132 | * @param teTopology an instance of TE topology |
| 133 | * @return value of TE topology id |
| 134 | */ |
| 135 | public static TeTopologyId teTopologyId(TeTopology teTopology) { |
| 136 | return new TeTopologyId(teTopology.teTopologyId().providerId(), |
| 137 | teTopology.teTopologyId().clientId(), |
| 138 | teTopology.teTopologyIdStringValue()); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Returns a default instance of termination point for a TE termination point id. |
| 143 | * |
| 144 | * @param teTpId TE termination point id |
| 145 | * @return an instance of termination point |
| 146 | */ |
| 147 | private static TerminationPoint tpBuilder(long teTpId) { |
| 148 | return new DefaultTerminationPoint(KeyId.keyId(Long.toString(teTpId)), null, teTpId); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Returns an instance of network node for a TE node. |
| 153 | * |
| 154 | * @param id value of the network node id |
| 155 | * @param teNode value of TE node |
| 156 | * @return an instance of network node |
| 157 | */ |
| 158 | public static NetworkNode nodeBuilder(KeyId id, TeNode teNode) { |
| 159 | List<NetworkNodeKey> supportingNodeIds = null; |
| 160 | if (teNode.supportingTeNodeId() != null) { |
| 161 | supportingNodeIds = Lists.newArrayList(networkNodeKey(teNode.supportingTeNodeId())); |
| 162 | } |
| 163 | Map<KeyId, TerminationPoint> tps = Maps.newConcurrentMap(); |
| 164 | for (Long teTpid : teNode.teTerminationPointIds()) { |
| 165 | tps.put(KeyId.keyId(Long.toString(teTpid)), tpBuilder(teTpid)); |
| 166 | } |
| 167 | return new DefaultNetworkNode(id, supportingNodeIds, teNode, tps); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Returns the network node termination point key for a TE link end point key. |
| 172 | * |
| 173 | * @param teLinkKey TE link end point key |
| 174 | * @return value of network node termination point key |
| 175 | */ |
| 176 | public static NodeTpKey nodeTpKey(TeLinkTpKey teLinkKey) { |
| 177 | return new NodeTpKey(KeyId.keyId(Long.toString(teLinkKey.teNodeId())), |
| 178 | KeyId.keyId(Long.toString(teLinkKey.teLinkTpId()))); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Returns an instance of network link for a TE link. |
| 183 | * |
| 184 | * @param id value of the network link id |
| 185 | * @param teLink value of TE link |
| 186 | * @return an instance of network link |
| 187 | */ |
| 188 | public static NetworkLink linkBuilder(KeyId id, TeLink teLink) { |
| 189 | NodeTpKey source = nodeTpKey(teLink.teLinkKey()); |
| 190 | NodeTpKey destination = null; |
| 191 | if (teLink.peerTeLinkKey() != null) { |
| 192 | destination = nodeTpKey(teLink.peerTeLinkKey()); |
| 193 | } |
| 194 | List<NetworkLinkKey> supportingLinkIds = null; |
| 195 | if (teLink.supportingTeLinkId() != null) { |
| 196 | supportingLinkIds = Lists.newArrayList(networkLinkKey(teLink.supportingTeLinkId())); |
| 197 | } |
| 198 | return new DefaultNetworkLink(id, source, destination, supportingLinkIds, teLink); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Returns an instance of network for a TE topology. |
| 203 | * |
| 204 | * @param teTopology value of TE topology |
| 205 | * @return an instance of network |
| 206 | */ |
| 207 | public static Network networkBuilder(TeTopology teTopology) { |
| 208 | KeyId networkId = TeMgrUtil.toNetworkId(teTopology.teTopologyId()); |
| 209 | TeTopologyId topologyId = teTopologyId(teTopology); |
| 210 | Map<KeyId, NetworkNode> nodes = null; |
| 211 | if (MapUtils.isNotEmpty(teTopology.teNodes())) { |
| 212 | nodes = Maps.newHashMap(); |
| 213 | for (TeNode tenode : teTopology.teNodes().values()) { |
| 214 | KeyId key = KeyId.keyId(Long.toString(tenode.teNodeId())); |
| 215 | nodes.put(key, nodeBuilder(key, tenode)); |
| 216 | } |
| 217 | } |
| 218 | Map<KeyId, NetworkLink> links = null; |
| 219 | if (MapUtils.isNotEmpty(teTopology.teLinks())) { |
| 220 | links = Maps.newHashMap(); |
| 221 | for (TeLink telink : teTopology.teLinks().values()) { |
| 222 | KeyId key = TeMgrUtil.toNetworkLinkId(telink.teLinkKey()); |
| 223 | links.put(key, linkBuilder(key, telink)); |
| 224 | |
| 225 | } |
| 226 | } |
| 227 | return new DefaultNetwork(networkId, null, nodes, links, |
| 228 | topologyId, false, teTopology.ownerId()); |
| 229 | } |
| 230 | |
| 231 | } |