Merge "BUG-2329 Add test for anyxmls inside rpc resonse for netcfon-connector"
[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 public class NodeTable implements Serializable {
23
24     private static final long serialVersionUID = 1L;
25     public static final Short SPECIALNODETABLEID = (short) 0;
26
27     /**
28      * Enum-like static class created with the purpose of identifing
29      * multiple type of nodes in the SDN network. The type is
30      * necessary to figure out to later on correctly use the
31      * nodeTableID. Using a static class instead of an Enum so we can add
32      * dynamically new types without changing anything in the
33      * surround.
34      */
35     public static final class NodeTableIDType {
36         private static final ConcurrentHashMap<String, Class<? extends Object>> compatibleType =
37                 new ConcurrentHashMap<String, Class<? extends Object>>();
38         /**
39          * These are in compliance with the compatibility types in 'Node'
40          */
41         public static String OPENFLOW = "OF";
42         public static String PCEP = "PE";
43         public static String ONEPK = "PK";
44         public static String PRODUCTION = "PR";
45
46         // Pre-populated types, just here for convenience and ease of
47         // unit-testing, but certainly those could live also outside.
48         // Currently we allow these 4. It can be changed later.
49         static {
50             compatibleType.put(OPENFLOW, Byte.class);
51             compatibleType.put(PCEP, UUID.class);
52             compatibleType.put(ONEPK, String.class);
53             compatibleType.put(PRODUCTION, String.class);
54         }
55
56         /**
57          * Return the type of the class expected for the
58          * NodeTableID, it's used for validity check in the constructor
59          *
60          * @param type, the type of the NodeTable for which we
61          * want to retrieve the compatible class to be used as ID.
62          *
63          * @return The Class which is supposed to instantiate the ID
64          * for the NodeTableID
65          */
66         public static Class<?> getClassType(String type) {
67             return compatibleType.get(type);
68         }
69
70         /**
71          * Returns all the registered nodeTableIDTypes currently available
72          *
73          * @return The current registered NodeTableIDTypes
74          */
75         public static Set<String> values() {
76             return compatibleType.keySet();
77         }
78
79         /**
80          * Register a new ID for which NodeTable can be created
81          *
82          * @param type, the new type being registered
83          * @param compatibleID, the type of class to be accepted as ID
84          *
85          * @return true if registered, false otherwise
86          */
87         public static boolean registerIDType(String type,
88                 Class<? extends Object> compatibleID) {
89             if (compatibleType.get(type) != null) {
90                 return false;
91             }  else {
92                 compatibleType.put(type, compatibleID);
93                 return true;
94             }
95         }
96
97         /**
98          * UNRegister a new ID for which Node can be created
99          *
100          * @param type, the type being UN-registered
101          *
102          */
103         public static void unRegisterIDType(String type) {
104             compatibleType.remove(type);
105         }
106     }
107
108     // Elements that constitute the NodeTable
109     private Object nodeTableID;
110     private String nodeTableType;
111     @XmlElement(name = "node")
112     private Node nodeTableNode;
113
114     // Helper field for JAXB
115     private String nodeTableIDString;
116
117     /**
118      * Private constructor used for JAXB mapping
119      */
120     @SuppressWarnings("unused")
121     private NodeTable() {
122         this.nodeTableIDString = null;
123         this.nodeTableID = null;
124         this.nodeTableType = null;
125         this.nodeTableNode = null;
126     }
127
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;
135         } else {
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());
140         }
141     }
142
143     /**
144      * Copy constructor for NodeTable
145      *
146      * @param src NodeTable to copy from
147      *
148      */
149     public NodeTable(NodeTable src) throws ConstructionException {
150         if (src != null) {
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
155             // of it
156             this.nodeTableID = src.getID();
157             this.nodeTableNode = new Node(src.getNode());
158         } else {
159             throw
160             new ConstructionException("Null incoming object to copy from");
161         }
162     }
163
164     /**
165      * @return the nodeTableID
166      */
167     public Object getID() {
168         return this.nodeTableID;
169     }
170
171     /**
172      * @return the nodeTableType
173      */
174     public String getType() {
175         return this.nodeTableType;
176     }
177
178     /**
179      * Private setter for nodeConnectorType to be called by JAXB not by anyone
180      * else, NodeConnector is immutable
181      *
182      * @param type the nodeTableType to set
183      */
184     @SuppressWarnings("unused")
185     private void setType(String type) {
186         this.nodeTableType = type;
187         if (this.nodeTableIDString != null) {
188             this.fillmeFromString(type, this.nodeTableIDString);
189         }
190     }
191
192     /**
193      * @return the nodeTableNode
194      */
195     public Node getNode() {
196         return this.nodeTableNode;
197     }
198
199     /**
200      * @param nodeTableNode the nodeTableNode to set
201      */
202     public void setNodeTableNode(Node nodeTableNode) {
203         this.nodeTableNode = nodeTableNode;
204     }
205
206     /**
207      * @return the nodeTableIDString
208      */
209     @XmlElement(name = "id")
210     public String getNodeTableIDString() {
211         return this.nodeTableIDString != null? this.nodeTableIDString : nodeTableID.toString();
212     }
213
214     /**
215      * @param nodeTableIDString the nodeTableIDString to set
216      */
217     @SuppressWarnings("unused")
218     private void setNodeTableIDString(String IDStr) {
219         this.nodeTableIDString = IDStr;
220         if (this.nodeTableType != null) {
221             this.fillmeFromString(this.nodeTableType, IDStr);
222         }
223     }
224
225     /**
226      * fill the current object from the string parameters passed, will
227      * be only used by JAXB
228      *
229      * @param typeStr string representing the type of the Node
230      * @param IDStr String representation of the ID
231      */
232     private void fillmeFromString(String typeStr, String IDStr) {
233         if (typeStr == null) {
234             return;
235         }
236
237         if (IDStr == null) {
238             return;
239         }
240
241         this.nodeTableType = typeStr;
242         this.nodeTableID = (byte) Byte.parseByte(IDStr);
243     }
244
245     @Override
246     public int hashCode() {
247         final int prime = 31;
248         int result = 1;
249         result = prime * result
250                 + ((nodeTableID == null) ? 0 : nodeTableID.hashCode());
251         result = prime
252                 * result
253                 + ((nodeTableNode == null) ? 0 : nodeTableNode
254                         .hashCode());
255         result = prime
256                 * result
257                 + ((nodeTableType == null) ? 0 : nodeTableType
258                         .hashCode());
259         return result;
260     }
261
262     @Override
263     public boolean equals(Object obj) {
264         if (this == obj)
265             return true;
266         if (obj == null)
267             return false;
268         if (getClass() != obj.getClass())
269             return false;
270         NodeTable other = (NodeTable) obj;
271         if (nodeTableID == null) {
272             if (other.nodeTableID != null)
273                 return false;
274         } else if (!nodeTableID.equals(other.nodeTableID))
275             return false;
276         if (nodeTableNode == null) {
277             if (other.nodeTableNode != null)
278                 return false;
279         } else if (!nodeTableNode.equals(other.nodeTableNode))
280             return false;
281         if (nodeTableType == null) {
282             if (other.nodeTableType != null)
283                 return false;
284         } else if (!nodeTableType.equals(other.nodeTableType))
285             return false;
286         return true;
287     }
288
289     @Override
290     public String toString() {
291         return this.getNodeTableIdAsString() + "@" + this.nodeTableNode;
292     }
293
294     public String getNodeTableIdAsString() {
295         return this.nodeTableType + "|"
296                 + this.nodeTableID.toString();
297     }
298
299 }