blob: 93170d1c53c5d905043551bcba37bfcf45df4718 [file] [log] [blame]
Yixiao Chen68bfab22016-11-11 11:04:10 -05001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Yixiao Chen68bfab22016-11-11 11:04:10 -05003 *
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 */
16package org.onosproject.tetopology.management.impl;
17
18import java.util.List;
19import java.util.Map;
20
21import org.apache.commons.collections.MapUtils;
22import org.onlab.packet.Ip4Address;
23import org.onosproject.tetopology.management.api.DefaultNetwork;
24import org.onosproject.tetopology.management.api.KeyId;
25import org.onosproject.tetopology.management.api.Network;
26import org.onosproject.tetopology.management.api.TeTopology;
27import org.onosproject.tetopology.management.api.TeTopologyId;
28import org.onosproject.tetopology.management.api.TeTopologyKey;
29import org.onosproject.tetopology.management.api.link.DefaultNetworkLink;
30import org.onosproject.tetopology.management.api.link.NetworkLink;
31import org.onosproject.tetopology.management.api.link.NetworkLinkKey;
32import org.onosproject.tetopology.management.api.link.TeLink;
33import org.onosproject.tetopology.management.api.link.TeLinkTpGlobalKey;
34import org.onosproject.tetopology.management.api.link.TeLinkTpKey;
35import org.onosproject.tetopology.management.api.node.DefaultNetworkNode;
36import org.onosproject.tetopology.management.api.node.DefaultTerminationPoint;
37import org.onosproject.tetopology.management.api.node.NetworkNode;
38import org.onosproject.tetopology.management.api.node.NetworkNodeKey;
39import org.onosproject.tetopology.management.api.node.NodeTpKey;
40import org.onosproject.tetopology.management.api.node.TeNode;
41import org.onosproject.tetopology.management.api.node.TeNodeKey;
42import org.onosproject.tetopology.management.api.node.TerminationPoint;
43
44import com.google.common.collect.Lists;
45import com.google.common.collect.Maps;
46
47/**
48 * TE Topology Manager utility functions.
49 */
50public 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()),
Yixiao Chen29f06332016-12-07 16:14:29 -0500115 KeyId.keyId(Ip4Address
116 .valueOf((int) teNodeKey.teNodeId())
117 .toString()));
Yixiao Chen68bfab22016-11-11 11:04:10 -0500118 }
119
120 /**
121 * Returns the network link key for a TE link global key.
122 *
123 * @param teLinkKey TE link global key
124 * @return value of network link key
125 */
126 public static NetworkLinkKey networkLinkKey(TeLinkTpGlobalKey teLinkKey) {
127 return new NetworkLinkKey(toNetworkId(teLinkKey.teTopologyKey()),
128 toNetworkLinkId(teLinkKey.teLinkTpKey()));
129 }
130
131 /**
132 * Returns the TE topology id for a TE topology.
133 *
134 * @param teTopology an instance of TE topology
135 * @return value of TE topology id
136 */
137 public static TeTopologyId teTopologyId(TeTopology teTopology) {
138 return new TeTopologyId(teTopology.teTopologyId().providerId(),
139 teTopology.teTopologyId().clientId(),
140 teTopology.teTopologyIdStringValue());
141 }
142
143 /**
144 * Returns a default instance of termination point for a TE termination point id.
145 *
146 * @param teTpId TE termination point id
147 * @return an instance of termination point
148 */
149 private static TerminationPoint tpBuilder(long teTpId) {
150 return new DefaultTerminationPoint(KeyId.keyId(Long.toString(teTpId)), null, teTpId);
151 }
152
153 /**
154 * Returns an instance of network node for a TE node.
155 *
156 * @param id value of the network node id
157 * @param teNode value of TE node
158 * @return an instance of network node
159 */
160 public static NetworkNode nodeBuilder(KeyId id, TeNode teNode) {
161 List<NetworkNodeKey> supportingNodeIds = null;
162 if (teNode.supportingTeNodeId() != null) {
163 supportingNodeIds = Lists.newArrayList(networkNodeKey(teNode.supportingTeNodeId()));
164 }
165 Map<KeyId, TerminationPoint> tps = Maps.newConcurrentMap();
166 for (Long teTpid : teNode.teTerminationPointIds()) {
167 tps.put(KeyId.keyId(Long.toString(teTpid)), tpBuilder(teTpid));
168 }
169 return new DefaultNetworkNode(id, supportingNodeIds, teNode, tps);
170 }
171
172 /**
173 * Returns the network node termination point key for a TE link end point key.
174 *
175 * @param teLinkKey TE link end point key
176 * @return value of network node termination point key
177 */
178 public static NodeTpKey nodeTpKey(TeLinkTpKey teLinkKey) {
Yixiao Chen29f06332016-12-07 16:14:29 -0500179 return new NodeTpKey(KeyId.keyId(Ip4Address
180 .valueOf((int) teLinkKey.teNodeId()).toString()),
Yixiao Chen68bfab22016-11-11 11:04:10 -0500181 KeyId.keyId(Long.toString(teLinkKey.teLinkTpId())));
182 }
183
184 /**
185 * Returns an instance of network link for a TE link.
186 *
187 * @param id value of the network link id
188 * @param teLink value of TE link
189 * @return an instance of network link
190 */
191 public static NetworkLink linkBuilder(KeyId id, TeLink teLink) {
192 NodeTpKey source = nodeTpKey(teLink.teLinkKey());
193 NodeTpKey destination = null;
194 if (teLink.peerTeLinkKey() != null) {
195 destination = nodeTpKey(teLink.peerTeLinkKey());
196 }
197 List<NetworkLinkKey> supportingLinkIds = null;
198 if (teLink.supportingTeLinkId() != null) {
199 supportingLinkIds = Lists.newArrayList(networkLinkKey(teLink.supportingTeLinkId()));
200 }
201 return new DefaultNetworkLink(id, source, destination, supportingLinkIds, teLink);
202 }
203
204 /**
205 * Returns an instance of network for a TE topology.
206 *
207 * @param teTopology value of TE topology
208 * @return an instance of network
209 */
210 public static Network networkBuilder(TeTopology teTopology) {
211 KeyId networkId = TeMgrUtil.toNetworkId(teTopology.teTopologyId());
212 TeTopologyId topologyId = teTopologyId(teTopology);
213 Map<KeyId, NetworkNode> nodes = null;
214 if (MapUtils.isNotEmpty(teTopology.teNodes())) {
215 nodes = Maps.newHashMap();
216 for (TeNode tenode : teTopology.teNodes().values()) {
Yixiao Chen29f06332016-12-07 16:14:29 -0500217 KeyId key = KeyId.keyId(Ip4Address
218 .valueOf((int) tenode.teNodeId()).toString());
Yixiao Chen68bfab22016-11-11 11:04:10 -0500219 nodes.put(key, nodeBuilder(key, tenode));
220 }
221 }
222 Map<KeyId, NetworkLink> links = null;
223 if (MapUtils.isNotEmpty(teTopology.teLinks())) {
224 links = Maps.newHashMap();
225 for (TeLink telink : teTopology.teLinks().values()) {
Yixiao Chen29f06332016-12-07 16:14:29 -0500226 KeyId key = toNetworkLinkId(telink.teLinkKey());
Yixiao Chen68bfab22016-11-11 11:04:10 -0500227 links.put(key, linkBuilder(key, telink));
228
229 }
230 }
231 return new DefaultNetwork(networkId, null, nodes, links,
Aihua Guo0bc12092017-02-10 09:53:55 -0500232 topologyId, false, teTopology.ownerId(),
233 teTopology.optimization());
Yixiao Chen68bfab22016-11-11 11:04:10 -0500234 }
235
236}