3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
13 * @brief Describe a generic network element in multiple SDNs technologies
15 * Describe a generic network element in multiple SDNs technologies. A
16 * Node is identified by the pair (NodeType, NodeID), the nodetype are
17 * needed in order to further specify the nodeID
19 package org.opendaylight.controller.sal.core;
21 import java.io.Serializable;
22 import java.math.BigInteger;
24 import java.util.UUID;
25 import java.util.concurrent.ConcurrentHashMap;
27 import javax.xml.bind.annotation.XmlAccessType;
28 import javax.xml.bind.annotation.XmlAccessorType;
29 import javax.xml.bind.annotation.XmlAttribute;
30 import javax.xml.bind.annotation.XmlRootElement;
32 import org.opendaylight.controller.sal.utils.HexEncode;
33 import org.opendaylight.controller.sal.utils.INodeFactory;
34 import org.opendaylight.controller.sal.utils.ServiceHelper;
37 * Describe a generic network element in multiple SDNs technologies. A
38 * Node is identified by the pair (NodeType, NodeID), the nodetype are
39 * needed in order to further specify the nodeID
42 @XmlAccessorType(XmlAccessType.NONE)
44 public class Node implements Serializable {
45 private static final long serialVersionUID = 1L;
48 * Enum-like static class created with the purpose of identifing
49 * multiple type of nodes in the SDN network. The type is
50 * necessary to figure out to later on correctly use the
51 * nodeID. Using a static class instead of an Enum so we can add
52 * dynamically new types without changing anything in the
55 public static final class NodeIDType {
56 private static final ConcurrentHashMap<String, Class<? extends Object>> compatibleType =
57 new ConcurrentHashMap<String, Class<? extends Object>>();
59 * Identifier for an OpenFlow node
61 public static String OPENFLOW = "OF";
63 * Identifier for a PCEP node
65 public static String PCEP = "PE";
67 * Identifier for a ONEPK node
69 public static String ONEPK = "PK";
71 * Identifier for a node in a non-SDN network
73 public static String PRODUCTION = "PR";
75 // Pre-populated types, just here for convenience and ease of
76 // unit-testing, but certainly those could live also outside.
78 compatibleType.put(OPENFLOW, Long.class);
79 compatibleType.put(PCEP, UUID.class);
80 compatibleType.put(ONEPK, String.class);
81 compatibleType.put(PRODUCTION, String.class);
85 * Return the type of the class expected for the
86 * NodeID, it's used for validity check in the constructor
88 * @param nodeType the type of the node we want to check
91 * @return The Class which is supposed to instantiate the ID
94 public static Class<?> getClassType(String nodeType) {
95 return compatibleType.get(nodeType);
99 * Returns all the registered nodeIDTypes currently available
101 * @return The current registered NodeIDTypes
103 public static Set<String> values() {
104 return compatibleType.keySet();
108 * Register a new ID for which Node can be created
110 * @param type, the new type being registered
111 * @param compatibleID, the type of class to be accepted as ID
113 * @return true if registered, false otherwise
115 public static boolean registerIDType(String type,
116 Class<? extends Object> compatibleID) {
117 if (compatibleType.get(type) != null) {
120 compatibleType.put(type, compatibleID);
126 * UNRegister a new ID for which Node can be created
128 * @param type, the type being UN-registered
131 public static void unRegisterIDType(String type) {
132 compatibleType.remove(type);
136 // This is the identity of the Node a (Type,ID) pair!, the full
137 // essence of this class.
138 private Object nodeID;
139 private String nodeType;
141 // Shadow value for unmarshalling
142 private String nodeIDString;
145 * Private constructor used for JAXB mapping
149 this.nodeType = null;
150 this.nodeIDString = null;
154 * Constructor for the Node objects, it validate the input so if
155 * the ID passed is not of the type expected accordingly to the
156 * type an exception is raised.
158 * @param nodeType Type of the node we are building
159 * @param id ID used by the SDN technology to identify the node
162 public Node(String nodeType, Object id) throws ConstructionException {
163 if (NodeIDType.getClassType(nodeType) != null &&
164 NodeIDType.getClassType(nodeType).isInstance(id)) {
165 this.nodeType = nodeType;
168 throw new ConstructionException("Type of incoming object:"
169 + id.getClass() + " not compatible with expected type:"
170 + NodeIDType.getClassType(nodeType));
175 * Copy Constructor for the Node objects.
177 * @param src type of nodes to copy from
180 public Node(Node src) throws ConstructionException {
182 this.nodeType = src.getType();
183 // Here we can reference the object because that is
184 // supposed to be an immutable identifier as well like a
185 // UUID/Integer and so on, hence no need to create a copy
187 this.nodeID = src.getID();
190 new ConstructionException("Null incoming object to copy from");
195 * getter for node type
198 * @return The node Type for this Node object
200 @XmlAttribute(name = "type")
201 public String getType() {
202 return this.nodeType;
206 * fill the current object from the string parameters passed, will
207 * be only used by JAXB
209 * @param typeStr string representing the type of the Node
210 * @param IDStr String representation of the ID
212 private void fillmeFromString(String typeStr, String IDStr) {
213 if (typeStr == null) {
221 this.nodeType = typeStr;
222 if (typeStr.equals(NodeIDType.OPENFLOW)) {
223 this.nodeID = Long.valueOf(HexEncode.stringToLong(IDStr));
224 } else if (typeStr.equals(NodeIDType.ONEPK)) {
226 } else if (typeStr.equals(NodeIDType.PCEP)) {
227 this.nodeID = UUID.fromString(IDStr);
228 } else if (typeStr.equals(NodeIDType.PRODUCTION)) {
231 //Use plugin's method to get appropriate conversion from IDStr to nodeID
232 INodeFactory f = (INodeFactory) ServiceHelper
233 .getGlobalInstance(INodeFactory.class, new Node(), "(protocolName="+typeStr+")");
235 Node n = f.fromString(typeStr, IDStr);
236 this.nodeID = n.nodeID;
242 * Private setter for nodeType to be called by JAXB not by anyone
243 * else, Node is immutable
245 * @param type of node to be set
247 private void setType(String type) {
248 this.nodeType = type;
249 if (this.nodeIDString != null) {
250 this.fillmeFromString(type, this.nodeIDString);
258 * @return The node ID for this Node object
260 public Object getID() {
265 * Getter for the node ID in string format
267 * @return The nodeID in string format
269 @XmlAttribute(name = "id")
270 public String getNodeIDString() {
271 if (this.nodeType.equals(NodeIDType.OPENFLOW)) {
272 return HexEncode.longToHexString((Long) this.nodeID);
274 return this.nodeID.toString();
279 * private setter to be used by JAXB
281 * @param nodeIDString String representation for NodeID
283 private void setNodeIDString(String nodeIDString) {
284 this.nodeIDString = nodeIDString;
285 if (this.nodeType != null) {
286 this.fillmeFromString(this.nodeType, nodeIDString);
291 public int hashCode() {
292 final int prime = 31;
294 result = prime * result + ((nodeID == null) ? 0 : nodeID.hashCode());
295 result = prime * result
296 + ((nodeType == null) ? 0 : nodeType.hashCode());
301 public boolean equals(Object obj) {
306 if (getClass() != obj.getClass())
308 Node other = (Node) obj;
309 if (nodeID == null) {
310 if (other.nodeID != null)
312 } else if (!nodeID.equals(other.nodeID))
314 if (nodeType == null) {
315 if (other.nodeType != null)
317 } else if (!nodeType.equals(other.nodeType))
323 public String toString() {
324 if (this.nodeType.equals(NodeIDType.OPENFLOW)) {
325 return this.nodeType.toString() + "|"
326 + HexEncode.longToHexString((Long) this.nodeID);
328 return this.nodeType.toString() + "|" + this.nodeID.toString();
333 * Static method to get back a Node from a string
335 * @param str string formatted in toString mode that can be
336 * converted back to a Node format.
338 * @return a Node if succed or null if no
340 public static Node fromString(String str) {
345 String parts[] = str.split("\\|");
346 if (parts.length != 2) {
347 // Try to guess from a String formatted as a long because
348 // for long time openflow has been prime citizen so lets
349 // keep this legacy for now
350 String numStr = str.toUpperCase();
352 Long ofNodeID = null;
353 if (numStr.startsWith("0X")) {
354 // Try as an hex number
356 BigInteger b = new BigInteger(
357 numStr.replaceFirst("0X", ""), 16);
358 ofNodeID = Long.valueOf(b.longValue());
359 } catch (Exception ex) {
363 // Try as a decimal number
365 BigInteger b = new BigInteger(numStr);
366 ofNodeID = Long.valueOf(b.longValue());
367 } catch (Exception ex) {
372 // Startegy #3 parse as HexLong
373 if (ofNodeID == null) {
375 ofNodeID = Long.valueOf(HexEncode.stringToLong(numStr));
376 } catch (Exception ex) {
381 // We ran out of ideas ... return null
382 if (ofNodeID == null) {
386 // Lets return the cooked up NodeID
388 return new Node(NodeIDType.OPENFLOW, ofNodeID);
389 } catch (ConstructionException ex) {
394 String typeStr = parts[0];
395 String IDStr = parts[1];
397 return fromString(typeStr, IDStr);
401 * Static method to get back a Node from a pair of strings, the
402 * first one being the Type representation, the second one being
403 * the ID string representation, expected to be heavily used in
406 * @param type, the type of the node we are parsing
407 * @param id, the string representation of the node id
409 * @return a Node if succed or null if no
411 public static Node fromString(String typeStr, String IDStr) {
412 if (typeStr == null) {
420 if (typeStr.equals(NodeIDType.OPENFLOW)) {
422 Long ID = Long.valueOf(HexEncode.stringToLong(IDStr));
423 return new Node(typeStr, ID);
424 } catch (Exception ex) {
427 } else if (typeStr.equals(NodeIDType.ONEPK)) {
429 return new Node(typeStr, IDStr);
430 } catch (Exception ex) {
433 } else if (typeStr.equals(NodeIDType.PCEP)) {
435 UUID ID = UUID.fromString(IDStr);
436 return new Node(typeStr, ID);
437 } catch (Exception ex) {
440 } else if (typeStr.equals(NodeIDType.PRODUCTION)) {
442 return new Node(typeStr, IDStr);
443 } catch (Exception ex) {
447 //Use INodeFactory to create a Node of registered Node type.
448 //The protocol plugin being used depends on typeStr.
449 INodeFactory f = (INodeFactory) ServiceHelper
450 .getGlobalInstance(INodeFactory.class, new Node(), "(protocolName="+typeStr+")");
453 return f.fromString(typeStr, IDStr);