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