Mark AD-SAL interfaces as deprecated
[controller.git] / opendaylight / adsal / sal / api / src / main / java / org / opendaylight / controller / sal / core / NodeTable.java
1 /*
2  * Copyright (c) 2013 Big Switch Networks, Inc.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.core;
9
10 import java.io.Serializable;
11 import java.util.Set;
12 import java.util.UUID;
13 import java.util.concurrent.ConcurrentHashMap;
14
15 import javax.xml.bind.annotation.XmlAccessType;
16 import javax.xml.bind.annotation.XmlAccessorType;
17 import javax.xml.bind.annotation.XmlElement;
18 import javax.xml.bind.annotation.XmlRootElement;
19
20 @XmlRootElement
21 @XmlAccessorType(XmlAccessType.NONE)
22 @Deprecated
23 public class NodeTable implements Serializable {
24
25     private static final long serialVersionUID = 1L;
26     public static final Short SPECIALNODETABLEID = (short) 0;
27
28     /**
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
34      * surround.
35      */
36     public static final class NodeTableIDType {
37         private static final ConcurrentHashMap<String, Class<? extends Object>> compatibleType =
38                 new ConcurrentHashMap<String, Class<? extends Object>>();
39         /**
40          * These are in compliance with the compatibility types in 'Node'
41          */
42         public static String OPENFLOW = "OF";
43         public static String PCEP = "PE";
44         public static String ONEPK = "PK";
45         public static String PRODUCTION = "PR";
46
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.
50         static {
51             compatibleType.put(OPENFLOW, Byte.class);
52             compatibleType.put(PCEP, UUID.class);
53             compatibleType.put(ONEPK, String.class);
54             compatibleType.put(PRODUCTION, String.class);
55         }
56
57         /**
58          * Return the type of the class expected for the
59          * NodeTableID, it's used for validity check in the constructor
60          *
61          * @param type, the type of the NodeTable for which we
62          * want to retrieve the compatible class to be used as ID.
63          *
64          * @return The Class which is supposed to instantiate the ID
65          * for the NodeTableID
66          */
67         public static Class<?> getClassType(String type) {
68             return compatibleType.get(type);
69         }
70
71         /**
72          * Returns all the registered nodeTableIDTypes currently available
73          *
74          * @return The current registered NodeTableIDTypes
75          */
76         public static Set<String> values() {
77             return compatibleType.keySet();
78         }
79
80         /**
81          * Register a new ID for which NodeTable can be created
82          *
83          * @param type, the new type being registered
84          * @param compatibleID, the type of class to be accepted as ID
85          *
86          * @return true if registered, false otherwise
87          */
88         public static boolean registerIDType(String type,
89                 Class<? extends Object> compatibleID) {
90             if (compatibleType.get(type) != null) {
91                 return false;
92             }  else {
93                 compatibleType.put(type, compatibleID);
94                 return true;
95             }
96         }
97
98         /**
99          * UNRegister a new ID for which Node can be created
100          *
101          * @param type, the type being UN-registered
102          *
103          */
104         public static void unRegisterIDType(String type) {
105             compatibleType.remove(type);
106         }
107     }
108
109     // Elements that constitute the NodeTable
110     private Object nodeTableID;
111     private String nodeTableType;
112     @XmlElement(name = "node")
113     private Node nodeTableNode;
114
115     // Helper field for JAXB
116     private String nodeTableIDString;
117
118     /**
119      * Private constructor used for JAXB mapping
120      */
121     @SuppressWarnings("unused")
122     private NodeTable() {
123         this.nodeTableIDString = null;
124         this.nodeTableID = null;
125         this.nodeTableType = null;
126         this.nodeTableNode = null;
127     }
128
129     public NodeTable(String nodeTableType, Object id, Node node) throws ConstructionException {
130         if (NodeTableIDType.getClassType(nodeTableType) != null &&
131                 NodeTableIDType.getClassType(nodeTableType).isInstance(id) &&
132                 node.getType().equals(nodeTableType)) {
133             this.nodeTableType = nodeTableType;
134             this.nodeTableID = id;
135             this.nodeTableNode = node;
136         } else {
137             throw new ConstructionException("Type of incoming object:"
138                     + id.getClass() + " not compatible with expected type:"
139                     + NodeTableIDType.getClassType(nodeTableType)
140                     + " or Node type incompatible:" + node.getType());
141         }
142     }
143
144     /**
145      * Copy constructor for NodeTable
146      *
147      * @param src NodeTable to copy from
148      *
149      */
150     public NodeTable(NodeTable src) throws ConstructionException {
151         if (src != null) {
152             this.nodeTableType = src.getType();
153             // Here we can reference the object because that is
154             // supposed to be an immutable identifier as well like a
155             // UUID/Integer and so on, hence no need to create a copy
156             // of it
157             this.nodeTableID = src.getID();
158             this.nodeTableNode = new Node(src.getNode());
159         } else {
160             throw
161             new ConstructionException("Null incoming object to copy from");
162         }
163     }
164
165     /**
166      * @return the nodeTableID
167      */
168     public Object getID() {
169         return this.nodeTableID;
170     }
171
172     /**
173      * @return the nodeTableType
174      */
175     public String getType() {
176         return this.nodeTableType;
177     }
178
179     /**
180      * Private setter for nodeConnectorType to be called by JAXB not by anyone
181      * else, NodeConnector is immutable
182      *
183      * @param type the nodeTableType to set
184      */
185     @SuppressWarnings("unused")
186     private void setType(String type) {
187         this.nodeTableType = type;
188         if (this.nodeTableIDString != null) {
189             this.fillmeFromString(type, this.nodeTableIDString);
190         }
191     }
192
193     /**
194      * @return the nodeTableNode
195      */
196     public Node getNode() {
197         return this.nodeTableNode;
198     }
199
200     /**
201      * @param nodeTableNode the nodeTableNode to set
202      */
203     public void setNodeTableNode(Node nodeTableNode) {
204         this.nodeTableNode = nodeTableNode;
205     }
206
207     /**
208      * @return the nodeTableIDString
209      */
210     @XmlElement(name = "id")
211     public String getNodeTableIDString() {
212         return this.nodeTableIDString != null? this.nodeTableIDString : nodeTableID.toString();
213     }
214
215     /**
216      * @param nodeTableIDString the nodeTableIDString to set
217      */
218     @SuppressWarnings("unused")
219     private void setNodeTableIDString(String IDStr) {
220         this.nodeTableIDString = IDStr;
221         if (this.nodeTableType != null) {
222             this.fillmeFromString(this.nodeTableType, IDStr);
223         }
224     }
225
226     /**
227      * fill the current object from the string parameters passed, will
228      * be only used by JAXB
229      *
230      * @param typeStr string representing the type of the Node
231      * @param IDStr String representation of the ID
232      */
233     private void fillmeFromString(String typeStr, String IDStr) {
234         if (typeStr == null) {
235             return;
236         }
237
238         if (IDStr == null) {
239             return;
240         }
241
242         this.nodeTableType = typeStr;
243         this.nodeTableID = (byte) Byte.parseByte(IDStr);
244     }
245
246     @Override
247     public int hashCode() {
248         final int prime = 31;
249         int result = 1;
250         result = prime * result
251                 + ((nodeTableID == null) ? 0 : nodeTableID.hashCode());
252         result = prime
253                 * result
254                 + ((nodeTableNode == null) ? 0 : nodeTableNode
255                         .hashCode());
256         result = prime
257                 * result
258                 + ((nodeTableType == null) ? 0 : nodeTableType
259                         .hashCode());
260         return result;
261     }
262
263     @Override
264     public boolean equals(Object obj) {
265         if (this == obj)
266             return true;
267         if (obj == null)
268             return false;
269         if (getClass() != obj.getClass())
270             return false;
271         NodeTable other = (NodeTable) obj;
272         if (nodeTableID == null) {
273             if (other.nodeTableID != null)
274                 return false;
275         } else if (!nodeTableID.equals(other.nodeTableID))
276             return false;
277         if (nodeTableNode == null) {
278             if (other.nodeTableNode != null)
279                 return false;
280         } else if (!nodeTableNode.equals(other.nodeTableNode))
281             return false;
282         if (nodeTableType == null) {
283             if (other.nodeTableType != null)
284                 return false;
285         } else if (!nodeTableType.equals(other.nodeTableType))
286             return false;
287         return true;
288     }
289
290     @Override
291     public String toString() {
292         return this.getNodeTableIdAsString() + "@" + this.nodeTableNode;
293     }
294
295     public String getNodeTableIdAsString() {
296         return this.nodeTableType + "|"
297                 + this.nodeTableID.toString();
298     }
299
300 }