Merge "Bug 2347: DOMConcurrentDataCommitCoordinator uses wrong phase name"
[controller.git] / third-party / openflowj / src / main / java / org / openflow / protocol / queue / OFPacketQueue.java
1 package org.openflow.protocol.queue;
2
3 import java.nio.ByteBuffer;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.openflow.protocol.factory.OFQueuePropertyFactory;
8 import org.openflow.protocol.factory.OFQueuePropertyFactoryAware;
9 import org.openflow.util.U16;
10
11 /**
12  * Corresponds to the struct ofp_packet_queue OpenFlow structure
13  *
14  * @author David Erickson (daviderickson@cs.stanford.edu)
15  */
16 public class OFPacketQueue implements Cloneable, OFQueuePropertyFactoryAware {
17     public static int MINIMUM_LENGTH = 8;
18
19     protected OFQueuePropertyFactory queuePropertyFactory;
20
21     protected int queueId;
22     protected short length;
23     protected List<OFQueueProperty> properties;
24
25     /**
26      * @return the queueId
27      */
28     public int getQueueId() {
29         return queueId;
30     }
31
32     /**
33      * @param queueId the queueId to set
34      */
35     public OFPacketQueue setQueueId(int queueId) {
36         this.queueId = queueId;
37         return this;
38     }
39
40     /**
41      * @return the length
42      */
43     public short getLength() {
44         return length;
45     }
46
47     /**
48      * @param length the length to set
49      */
50     public void setLength(short length) {
51         this.length = length;
52     }
53
54     /**
55      * @return the properties
56      */
57     public List<OFQueueProperty> getProperties() {
58         return properties;
59     }
60
61     /**
62      * @param properties the properties to set
63      */
64     public OFPacketQueue setProperties(List<OFQueueProperty> properties) {
65         this.properties = properties;
66         return this;
67     }
68
69     public void readFrom(ByteBuffer data) {
70         this.queueId = data.getInt();
71         this.length = data.getShort();
72         data.getShort(); // pad
73         if (this.queuePropertyFactory == null)
74             throw new RuntimeException("OFQueuePropertyFactory not set");
75         this.properties = queuePropertyFactory.parseQueueProperties(data,
76                 U16.f(this.length) - MINIMUM_LENGTH);
77     }
78
79     public void writeTo(ByteBuffer data) {
80         data.putInt(this.queueId);
81         data.putShort(this.length);
82         data.putShort((short) 0); // pad
83         if (this.properties != null) {
84             for (OFQueueProperty queueProperty : this.properties) {
85                 queueProperty.writeTo(data);
86             }
87         }
88     }
89
90     @Override
91     public int hashCode() {
92         final int prime = 6367;
93         int result = 1;
94         result = prime * result + length;
95         result = prime * result
96                 + ((properties == null) ? 0 : properties.hashCode());
97         result = prime * result + queueId;
98         return result;
99     }
100
101     @Override
102     public boolean equals(Object obj) {
103         if (this == obj)
104             return true;
105         if (obj == null)
106             return false;
107         if (!(obj instanceof OFPacketQueue))
108             return false;
109         OFPacketQueue other = (OFPacketQueue) obj;
110         if (length != other.length)
111             return false;
112         if (properties == null) {
113             if (other.properties != null)
114                 return false;
115         } else if (!properties.equals(other.properties))
116             return false;
117         if (queueId != other.queueId)
118             return false;
119         return true;
120     }
121
122     @Override
123     public OFPacketQueue clone() {
124         try {
125             OFPacketQueue clone = (OFPacketQueue) super.clone();
126             if (this.properties != null) {
127                 List<OFQueueProperty> queueProps = new ArrayList<OFQueueProperty>();
128                 for (OFQueueProperty prop : this.properties) {
129                     queueProps.add(prop.clone());
130                 }
131                 clone.setProperties(queueProps);
132             }
133             return clone;
134         } catch (CloneNotSupportedException e) {
135             throw new RuntimeException(e);
136         }
137     }
138
139     @Override
140     public void setQueuePropertyFactory(
141             OFQueuePropertyFactory queuePropertyFactory) {
142         this.queuePropertyFactory = queuePropertyFactory;
143     }
144
145     /* (non-Javadoc)
146      * @see java.lang.Object#toString()
147      */
148     @Override
149     public String toString() {
150         return "OFPacketQueue [queueId=" + queueId + ", properties="
151                 + properties + "]";
152     }
153 }