c71e9e3b1c1860024819f36f3210f46ffbf5f386
[openflowjava.git] / third-party / openflow-codec / src / main / java / org / openflow / codec / protocol / statistics / OFPGroupDescription.java
1 package org.openflow.codec.protocol.statistics;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.LinkedList;
6 import java.util.List;
7
8 import org.openflow.codec.io.IDataBuffer;
9 import org.openflow.codec.protocol.action.OFPBucket;
10 import org.openflow.codec.protocol.factory.OFPActionFactory;
11 import org.openflow.codec.protocol.factory.OFPActionFactoryAware;
12
13 /**
14  * Represents an ofp_group_desc_stats structure
15  *
16  * @author Yugandhar Sarraju
17  */
18 public class OFPGroupDescription implements OFPStatistics, OFPActionFactoryAware, Serializable {
19
20     private static final int MINIMUM_LENGTH = 8;
21
22     protected OFPActionFactory actionFactory;
23     private short length = MINIMUM_LENGTH;
24     private byte groupType;
25     private int group_id;
26     private List<OFPBucket> buckets;
27
28     public byte getGroupType() {
29         return groupType;
30     }
31
32     public OFPGroupDescription setGroupType(byte groupType) {
33         this.groupType = groupType;
34         return this;
35     }
36
37     public int getGroup_id() {
38         return group_id;
39     }
40
41     public OFPGroupDescription setGroup_id(int group_id) {
42         this.group_id = group_id;
43         return this;
44     }
45
46     public List<OFPBucket> getBuckets() {
47         return buckets;
48     }
49
50     @Override
51     public int getLength() {
52         return length;
53     }
54
55     public OFPGroupDescription setLength(int length) {
56         this.length = (short) length;
57         return this;
58     }
59
60     /**
61      * @param buckets
62      *            the buckets to set
63      */
64     public OFPGroupDescription setBuckets(List<OFPBucket> buckets) {
65         this.buckets = buckets;
66         updateLength();
67
68         return this;
69     }
70
71     private void updateLength() {
72         int totalLength = MINIMUM_LENGTH;
73         if (buckets != null) {
74             for (OFPBucket bucket : buckets) {
75                 totalLength += bucket.getLengthU();
76             }
77         }
78         this.setLength(totalLength);
79     }
80
81     @Override
82     public void readFrom(IDataBuffer data) {
83         this.length = data.getShort();
84         this.groupType = data.get();
85         data.get();
86         this.group_id = data.getInt();
87         if (this.buckets == null) {
88             this.buckets = new ArrayList<OFPBucket>();
89         } else {
90             this.buckets.clear();
91         }
92
93         OFPBucket bucket;
94         while (data.remaining() > 0) {
95             bucket = new OFPBucket();
96             bucket.setActionFactory(actionFactory);
97             bucket.readFrom(data);
98             this.buckets.add(bucket);
99         }
100
101     }
102
103     @Override
104     public void writeTo(IDataBuffer data) {
105         data.putShort(length);
106         data.put(groupType);
107         data.put((byte) 0);
108         data.putInt(group_id);
109         if (buckets != null) {
110             for (OFPBucket bucket : buckets) {
111                 bucket.writeTo(data);
112             }
113         }
114     }
115
116     @Override
117     public int hashCode() {
118         final int prime = 461;
119         int result = 1;
120         result = prime * result + length;
121         result = prime * result + ((buckets == null) ? 0 : buckets.hashCode());
122         result = prime * result + groupType;
123         result = prime * result + group_id;
124         return result;
125     }
126
127     @Override
128     public boolean equals(Object obj) {
129         if (this == obj) {
130             return true;
131         }
132         if (obj == null) {
133             return false;
134         }
135         if (!(obj instanceof OFPGroupDescription)) {
136             return false;
137         }
138         OFPGroupDescription other = (OFPGroupDescription) obj;
139         if (length != other.length) {
140             return false;
141         }
142         if (buckets == null) {
143             if (other.buckets != null) {
144                 return false;
145             }
146         } else if (!buckets.equals(other.buckets)) {
147             return false;
148         }
149         if (groupType != other.groupType) {
150             return false;
151         }
152         if (group_id != other.group_id) {
153             return false;
154         }
155         return true;
156     }
157
158     /*
159      * (non-Javadoc)
160      *
161      * @see java.lang.Object#clone()
162      */
163     @Override
164     public OFPGroupDescription clone() {
165         try {
166             OFPGroupDescription groupDes = (OFPGroupDescription) super.clone();
167             List<OFPBucket> neoBuckets = new LinkedList<OFPBucket>();
168             for (OFPBucket bucket : this.buckets)
169                 neoBuckets.add((OFPBucket) bucket.clone());
170             groupDes.setBuckets(neoBuckets);
171             return groupDes;
172         } catch (CloneNotSupportedException e) {
173             // Won't happen
174             throw new RuntimeException(e);
175         }
176     }
177
178     /*
179      * (non-Javadoc)
180      *
181      * @see java.lang.Object#toString()
182      */
183     @Override
184     public String toString() {
185         return "OFPGroupDes [ buckets=" + buckets + ", length=" + length + ", groupType=" + groupType + ", group_id="
186                 + group_id + "]";
187     }
188
189     @Override
190     public void setActionFactory(OFPActionFactory actionFactory) {
191         this.actionFactory = actionFactory;
192
193     }
194
195 }