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