ab2cd7388aa2e921ead1689e754a394aa83562e3
[openflowjava.git] / third-party / openflow-codec / src / main / java / org / openflow / codec / protocol / statistics / table / OFPTableFeaturePropExperimenter.java
1 package org.openflow.codec.protocol.statistics.table;
2
3 import java.util.Arrays;
4
5 import org.openflow.codec.io.IDataBuffer;
6
7 /**
8  * Represents struct ofp_table_feature_prop_experimenter
9  *
10  * @author AnilGujele
11  *
12  */
13 public class OFPTableFeaturePropExperimenter extends OFPTableFeaturePropHeader {
14     private static final short MINIMUM_LENGTH = 12;
15     private int expId;
16     private int expType;
17     private int[] expData = new int[0];
18
19     /**
20      * constructor
21      */
22     public OFPTableFeaturePropExperimenter() {
23         super.setOFTableFeaturePropType(OFPTableFeaturePropType.EXPERIMENTER);
24         super.setLength(OFPTableFeaturePropExperimenter.MINIMUM_LENGTH);
25     }
26
27     public int getExpId() {
28         return expId;
29     }
30
31     public void setExpId(int expId) {
32         this.expId = expId;
33     }
34
35     public int getExpType() {
36         return expType;
37     }
38
39     public void setExpType(int expType) {
40         this.expType = expType;
41     }
42
43     public int[] getExpData() {
44         return expData;
45     }
46
47     public void setExpData(int[] expData) {
48         this.expData = expData;
49         updateLength();
50     }
51
52     /**
53      * update the length
54      *
55      */
56     private void updateLength() {
57         int length = this.getLength() + (expData.length * 4);
58         this.setLength((short) length);
59
60     }
61
62     /**
63      * read OFPTableFeaturePropExperimenter from buffer
64      *
65      * @param data
66      */
67     public void readFrom(IDataBuffer data) {
68         super.readFrom(data);
69         this.expId = data.getInt();
70         this.expType = data.getInt();
71         int dataLength = this.getLengthU() - OFPTableFeaturePropExperimenter.MINIMUM_LENGTH;
72         int intDataLength = dataLength / 4;
73         expData = new int[intDataLength];
74         for (int i = 0; i < intDataLength; i++) {
75             expData[i] = data.getInt();
76         }
77
78         /* Read the padding, if any */
79         int paddingLength = ((this.getLengthU() % MULTIPLE_OF_EIGHT) == 0) ? 0 : (MULTIPLE_OF_EIGHT - (this
80                 .getLengthU() % MULTIPLE_OF_EIGHT));
81         data.position(data.position() + paddingLength);
82
83     }
84
85     /**
86      * write OFPTableFeaturePropExperimenter to buffer
87      *
88      * @param data
89      */
90     public void writeTo(IDataBuffer data) {
91         super.writeTo(data);
92         data.putInt(this.expId);
93         data.putInt(this.expType);
94         for (int value : expData) {
95             data.putInt(value);
96         }
97
98         /* Add padding if structure is not 8 byte aligned */
99         int paddingLength = ((this.getLengthU() % MULTIPLE_OF_EIGHT) == 0) ? 0 : (MULTIPLE_OF_EIGHT - (this
100                 .getLengthU() % MULTIPLE_OF_EIGHT));
101         byte[] padding = new byte[paddingLength];
102         data.put(padding);
103
104     }
105
106     @Override
107     public int hashCode() {
108         final int prime = 746;
109         int result = super.hashCode();
110         result = prime * result + expId;
111         result = prime * result + expType;
112         result = prime * result + Arrays.hashCode(expData);
113         return result;
114     }
115
116     @Override
117     public boolean equals(Object obj) {
118         if (this == obj) {
119             return true;
120         }
121         if (!super.equals(obj)) {
122             return false;
123         }
124         if (!(obj instanceof OFPTableFeaturePropExperimenter)) {
125             return false;
126         }
127         OFPTableFeaturePropExperimenter other = (OFPTableFeaturePropExperimenter) obj;
128         if (this.expId != other.expId) {
129             return false;
130         }
131         if (this.expType != other.expType) {
132             return false;
133         }
134         if (!Arrays.equals(expData, other.expData)) {
135             return false;
136         }
137         return true;
138     }
139
140 }