2 * Copyright (c) 2013 Big Switch Networks, Inc. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sal.core;
10 import java.io.Serializable;
12 import java.util.UUID;
13 import java.util.concurrent.ConcurrentHashMap;
15 import javax.xml.bind.annotation.XmlAccessType;
16 import javax.xml.bind.annotation.XmlAccessorType;
17 import javax.xml.bind.annotation.XmlAttribute;
18 import javax.xml.bind.annotation.XmlElement;
19 import javax.xml.bind.annotation.XmlRootElement;
22 @XmlAccessorType(XmlAccessType.NONE)
23 public class NodeTable implements Serializable {
25 private static final long serialVersionUID = 1L;
26 public static final Short SPECIALNODETABLEID = (short) 0;
29 * Enum-like static class created with the purpose of identifing
30 * multiple type of nodes in the SDN network. The type is
31 * necessary to figure out to later on correctly use the
32 * nodeTableID. Using a static class instead of an Enum so we can add
33 * dynamically new types without changing anything in the
36 public static final class NodeTableIDType {
37 private static final ConcurrentHashMap<String, Class<? extends Object>> compatibleType =
38 new ConcurrentHashMap<String, Class<? extends Object>>();
40 * These are in compliance with the compatibility types in 'Node'
42 public static String OPENFLOW = "OF";
43 public static String PCEP = "PE";
44 public static String ONEPK = "PK";
45 public static String PRODUCTION = "PR";
47 // Pre-populated types, just here for convenience and ease of
48 // unit-testing, but certainly those could live also outside.
49 // Currently we allow these 4. It can be changed later.
51 compatibleType.put(OPENFLOW, Byte.class);
52 compatibleType.put(PCEP, UUID.class);
53 compatibleType.put(ONEPK, String.class);
54 compatibleType.put(PRODUCTION, String.class);
58 * Return the type of the class expected for the
59 * NodeTableID, it's used for validity check in the constructor
61 * @param type, the type of the NodeTable for which we
62 * want to retrieve the compatible class to be used as ID.
64 * @return The Class which is supposed to instantiate the ID
67 public static Class<?> getClassType(String type) {
68 return compatibleType.get(type);
72 * Returns all the registered nodeTableIDTypes currently available
74 * @return The current registered NodeTableIDTypes
76 public static Set<String> values() {
77 return compatibleType.keySet();
81 * Register a new ID for which NodeTable can be created
83 * @param type, the new type being registered
84 * @param compatibleID, the type of class to be accepted as ID
86 * @return true if registered, false otherwise
88 public static boolean registerIDType(String type,
89 Class<? extends Object> compatibleID) {
90 if (compatibleType.get(type) != null) {
93 compatibleType.put(type, compatibleID);
99 * UNRegister a new ID for which Node can be created
101 * @param type, the type being UN-registered
104 public static void unRegisterIDType(String type) {
105 compatibleType.remove(type);
109 // Elements that constitute the NodeTable
110 private Object nodeTableID;
111 private String nodeTableType;
112 @XmlElement(name = "node")
113 private Node nodeTableNode;
115 // Helper field for JAXB
116 private String nodeTableIDString;
119 * Private constructor used for JAXB mapping
121 private NodeTable() {
122 this.nodeTableIDString = null;
123 this.nodeTableID = null;
124 this.nodeTableType = null;
125 this.nodeTableNode = null;
128 public NodeTable(String nodeTableType, Object id, Node node) throws ConstructionException {
129 if (NodeTableIDType.getClassType(nodeTableType) != null &&
130 NodeTableIDType.getClassType(nodeTableType).isInstance(id) &&
131 node.getType().equals(nodeTableType)) {
132 this.nodeTableType = nodeTableType;
133 this.nodeTableID = id;
134 this.nodeTableNode = node;
136 throw new ConstructionException("Type of incoming object:"
137 + id.getClass() + " not compatible with expected type:"
138 + NodeTableIDType.getClassType(nodeTableType)
139 + " or Node type incompatible:" + node.getType());
144 * Copy constructor for NodeTable
146 * @param src NodeTable to copy from
149 public NodeTable(NodeTable src) throws ConstructionException {
151 this.nodeTableType = src.getType();
152 // Here we can reference the object because that is
153 // supposed to be an immutable identifier as well like a
154 // UUID/Integer and so on, hence no need to create a copy
156 this.nodeTableID = src.getID();
157 this.nodeTableNode = new Node(src.getNode());
160 new ConstructionException("Null incoming object to copy from");
165 * @return the nodeTableID
167 public Object getID() {
168 return this.nodeTableID;
172 * @return the nodeTableType
174 public String getType() {
175 return this.nodeTableType;
179 * @param type the nodeTableType to set
181 * Private setter for nodeConnectorType to be called by JAXB not by anyone
182 * else, NodeConnector is immutable
184 private void setType(String type) {
185 this.nodeTableType = type;
186 if (this.nodeTableIDString != null) {
187 this.fillmeFromString(type, this.nodeTableIDString);
192 * @return the nodeTableNode
194 public Node getNode() {
195 return this.nodeTableNode;
199 * @param nodeTableNode the nodeTableNode to set
201 public void setNodeTableNode(Node nodeTableNode) {
202 this.nodeTableNode = nodeTableNode;
206 * @return the nodeTableIDString
208 @XmlAttribute(name = "id")
209 public String getNodeTableIDString() {
210 return this.nodeTableIDString.toString();
214 * @param nodeTableIDString the nodeTableIDString to set
216 @SuppressWarnings("unused")
217 private void setNodeTableIDString(String IDStr) {
218 this.nodeTableIDString = IDStr;
219 if (this.nodeTableType != null) {
220 this.fillmeFromString(this.nodeTableType, IDStr);
225 * fill the current object from the string parameters passed, will
226 * be only used by JAXB
228 * @param typeStr string representing the type of the Node
229 * @param IDStr String representation of the ID
231 private void fillmeFromString(String typeStr, String IDStr) {
232 if (typeStr == null) {
240 this.nodeTableType = typeStr;
241 this.nodeTableID = (byte) Byte.parseByte(IDStr);
245 public int hashCode() {
246 final int prime = 31;
248 result = prime * result
249 + ((nodeTableID == null) ? 0 : nodeTableID.hashCode());
252 + ((nodeTableNode == null) ? 0 : nodeTableNode
256 + ((nodeTableType == null) ? 0 : nodeTableType
262 public boolean equals(Object obj) {
267 if (getClass() != obj.getClass())
269 NodeTable other = (NodeTable) obj;
270 if (nodeTableID == null) {
271 if (other.nodeTableID != null)
273 } else if (!nodeTableID.equals(other.nodeTableID))
275 if (nodeTableNode == null) {
276 if (other.nodeTableNode != null)
278 } else if (!nodeTableNode.equals(other.nodeTableNode))
280 if (nodeTableType == null) {
281 if (other.nodeTableType != null)
283 } else if (!nodeTableType.equals(other.nodeTableType))
289 public String toString() {
290 return this.getNodeTableIdAsString() + "@" + this.nodeTableNode;
293 public String getNodeTableIdAsString() {
294 return this.nodeTableType.toString() + "|"
295 + this.nodeTableID.toString();