Initial opendaylight infrastructure commit!!
[controller.git] / third-party / openflowj / src / main / java / org / openflow / protocol / OFStatisticsMessageBase.java
1 package org.openflow.protocol;
2
3 import java.nio.ByteBuffer;
4 import java.util.List;
5
6 import org.openflow.protocol.factory.OFStatisticsFactory;
7 import org.openflow.protocol.factory.OFStatisticsFactoryAware;
8 import org.openflow.protocol.statistics.OFStatistics;
9 import org.openflow.protocol.statistics.OFStatisticsType;
10
11
12 /**
13  * Base class for statistics requests/replies
14  *
15  * @author David Erickson (daviderickson@cs.stanford.edu) - Mar 27, 2010
16  */
17 public abstract class OFStatisticsMessageBase extends OFMessage implements
18         OFStatisticsFactoryAware {
19     public static int MINIMUM_LENGTH = 12;
20
21     protected OFStatisticsFactory statisticsFactory;
22     protected OFStatisticsType statisticType;
23     protected short flags;
24     protected List<OFStatistics> statistics;
25
26     /**
27      * @return the statisticType
28      */
29     public OFStatisticsType getStatisticType() {
30         return statisticType;
31     }
32
33     /**
34      * @param statisticType the statisticType to set
35      */
36     public void setStatisticType(OFStatisticsType statisticType) {
37         this.statisticType = statisticType;
38     }
39
40     /**
41      * @return the flags
42      */
43     public short getFlags() {
44         return flags;
45     }
46
47     /**
48      * @param flags the flags to set
49      */
50     public void setFlags(short flags) {
51         this.flags = flags;
52     }
53
54     /**
55      * @return the statistics
56      */
57     public List<OFStatistics> getStatistics() {
58         return statistics;
59     }
60
61     /**
62      * @param statistics the statistics to set
63      */
64     public void setStatistics(List<OFStatistics> statistics) {
65         this.statistics = statistics;
66     }
67
68     @Override
69     public void setStatisticsFactory(OFStatisticsFactory statisticsFactory) {
70         this.statisticsFactory = statisticsFactory;
71     }
72
73     @Override
74     public void readFrom(ByteBuffer data) {
75         super.readFrom(data);
76         this.statisticType = OFStatisticsType.valueOf(data.getShort(), this
77                 .getType());
78         this.flags = data.getShort();
79         if (this.statisticsFactory == null)
80             throw new RuntimeException("OFStatisticsFactory not set");
81         this.statistics = statisticsFactory.parseStatistics(this.getType(),
82                 this.statisticType, data, super.getLengthU() - MINIMUM_LENGTH);
83     }
84
85     @Override
86     public void writeTo(ByteBuffer data) {
87         super.writeTo(data);
88         data.putShort(this.statisticType.getTypeValue());
89         data.putShort(this.flags);
90         if (this.statistics != null) {
91             for (OFStatistics statistic : this.statistics) {
92                 statistic.writeTo(data);
93             }
94         }
95     }
96
97     @Override
98     public int hashCode() {
99         final int prime = 317;
100         int result = super.hashCode();
101         result = prime * result + flags;
102         result = prime * result
103                 + ((statisticType == null) ? 0 : statisticType.hashCode());
104         result = prime * result
105                 + ((statistics == null) ? 0 : statistics.hashCode());
106         return result;
107     }
108
109     @Override
110     public boolean equals(Object obj) {
111         if (this == obj) {
112             return true;
113         }
114         if (!super.equals(obj)) {
115             return false;
116         }
117         if (!(obj instanceof OFStatisticsMessageBase)) {
118             return false;
119         }
120         OFStatisticsMessageBase other = (OFStatisticsMessageBase) obj;
121         if (flags != other.flags) {
122             return false;
123         }
124         if (statisticType == null) {
125             if (other.statisticType != null) {
126                 return false;
127             }
128         } else if (!statisticType.equals(other.statisticType)) {
129             return false;
130         }
131         if (statistics == null) {
132             if (other.statistics != null) {
133                 return false;
134             }
135         } else if (!statistics.equals(other.statistics)) {
136             return false;
137         }
138         return true;
139     }
140 }