e5a9c01e0093e29055bb75943909860490347e43
[controller.git] / third-party / openflowj_netty / src / main / java / org / openflow / protocol / OFStatisticsMessageBase.java
1 /**
2 *    Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior
3 *    University
4 *
5 *    Licensed under the Apache License, Version 2.0 (the "License"); you may
6 *    not use this file except in compliance with the License. You may obtain
7 *    a copy of the License at
8 *
9 *         http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *    Unless required by applicable law or agreed to in writing, software
12 *    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 *    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 *    License for the specific language governing permissions and limitations
15 *    under the License.
16 **/
17
18 package org.openflow.protocol;
19
20 import java.util.List;
21
22 import org.jboss.netty.buffer.ChannelBuffer;
23 import org.openflow.protocol.factory.OFStatisticsFactory;
24 import org.openflow.protocol.factory.OFStatisticsFactoryAware;
25 import org.openflow.protocol.statistics.OFStatistics;
26 import org.openflow.protocol.statistics.OFStatisticsType;
27
28
29 /**
30  * Base class for statistics requests/replies
31  *
32  * @author David Erickson (daviderickson@cs.stanford.edu) - Mar 27, 2010
33  */
34 public abstract class OFStatisticsMessageBase extends OFMessage implements
35         OFStatisticsFactoryAware {
36     public static int MINIMUM_LENGTH = 12;
37
38     protected OFStatisticsFactory statisticsFactory;
39     protected OFStatisticsType statisticType;
40     protected short flags;
41
42     // TODO: this should be List<? extends OFStatistics>, to
43     // allow for type safe assignments of lists of specific message
44     protected List<? extends OFStatistics> statistics;
45
46     /**
47      * @return the statisticType
48      */
49     public OFStatisticsType getStatisticType() {
50         return statisticType;
51     }
52
53     /**
54      * @param statisticType the statisticType to set
55      */
56     public void setStatisticType(OFStatisticsType statisticType) {
57         this.statisticType = statisticType;
58     }
59
60     /**
61      * @return the flags
62      */
63     public short getFlags() {
64         return flags;
65     }
66
67     /**
68      * @param flags the flags to set
69      */
70     public void setFlags(short flags) {
71         this.flags = flags;
72     }
73
74     /**
75      * @return the statistics
76      */
77     public List<? extends OFStatistics> getStatistics() {
78         return statistics;
79     }
80
81     /**
82      * return the first statistics request in the list of statistics, for
83      * statistics messages that expect exactly one message in their body (e.g.,
84      * flow stats request, port statsrequest)
85      *
86      * @return the first and only element in the list of statistics
87      * @throw IllegalArgumentException if the list does not contain exactly one
88      *        element
89      */
90     public OFStatistics getFirstStatistics() {
91         if (statistics == null ) {
92             throw new IllegalArgumentException("Invariant violation: statistics message of type "+statisticType+" is null");
93         }
94         if (statistics.size() != 1) {
95             throw new IllegalArgumentException("Invariant violation: statistics message of type "+statisticType+" contains "+statistics.size() +" statreq/reply messages in its body (should be 1)");
96         }
97
98         return statistics.get(0);
99     }
100
101     /**
102      * @param statistics the statistics to set
103      */
104     public void setStatistics(List<? extends OFStatistics> statistics) {
105         this.statistics = statistics;
106     }
107
108     @Override
109     public void setStatisticsFactory(OFStatisticsFactory statisticsFactory) {
110         this.statisticsFactory = statisticsFactory;
111     }
112
113     @Override
114     public void readFrom(ChannelBuffer data) {
115         super.readFrom(data);
116         this.statisticType = OFStatisticsType.valueOf(data.readShort(), this
117                 .getType());
118         this.flags = data.readShort();
119         if (this.statisticsFactory == null)
120             throw new RuntimeException("OFStatisticsFactory not set");
121         this.statistics = statisticsFactory.parseStatistics(this.getType(),
122                 this.statisticType, data, super.getLengthU() - MINIMUM_LENGTH);
123     }
124
125     @Override
126     public void writeTo(ChannelBuffer data) {
127         super.writeTo(data);
128         data.writeShort(this.statisticType.getTypeValue());
129         data.writeShort(this.flags);
130         if (this.statistics != null) {
131             for (OFStatistics statistic : this.statistics) {
132                 statistic.writeTo(data);
133             }
134         }
135     }
136
137     @Override
138     public int hashCode() {
139         final int prime = 317;
140         int result = super.hashCode();
141         result = prime * result + flags;
142         result = prime * result
143                 + ((statisticType == null) ? 0 : statisticType.hashCode());
144         result = prime * result
145                 + ((statistics == null) ? 0 : statistics.hashCode());
146         return result;
147     }
148
149     @Override
150     public boolean equals(Object obj) {
151         if (this == obj) {
152             return true;
153         }
154         if (!super.equals(obj)) {
155             return false;
156         }
157         if (!(obj instanceof OFStatisticsMessageBase)) {
158             return false;
159         }
160         OFStatisticsMessageBase other = (OFStatisticsMessageBase) obj;
161         if (flags != other.flags) {
162             return false;
163         }
164         if (statisticType == null) {
165             if (other.statisticType != null) {
166                 return false;
167             }
168         } else if (!statisticType.equals(other.statisticType)) {
169             return false;
170         }
171         if (statistics == null) {
172             if (other.statistics != null) {
173                 return false;
174             }
175         } else if (!statistics.equals(other.statistics)) {
176             return false;
177         }
178         return true;
179     }
180 }