2 * Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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
18 package org.openflow.protocol;
20 import java.util.List;
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;
30 * Base class for statistics requests/replies
32 * @author David Erickson (daviderickson@cs.stanford.edu) - Mar 27, 2010
34 public abstract class OFStatisticsMessageBase extends OFMessage implements
35 OFStatisticsFactoryAware {
36 public static int MINIMUM_LENGTH = 12;
38 protected OFStatisticsFactory statisticsFactory;
39 protected OFStatisticsType statisticType;
40 protected short flags;
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;
47 * @return the statisticType
49 public OFStatisticsType getStatisticType() {
54 * @param statisticType the statisticType to set
56 public void setStatisticType(OFStatisticsType statisticType) {
57 this.statisticType = statisticType;
63 public short getFlags() {
68 * @param flags the flags to set
70 public void setFlags(short flags) {
75 * @return the statistics
77 public List<? extends OFStatistics> getStatistics() {
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)
86 * @return the first and only element in the list of statistics
87 * @throw IllegalArgumentException if the list does not contain exactly one
90 public OFStatistics getFirstStatistics() {
91 if (statistics == null ) {
92 throw new IllegalArgumentException("Invariant violation: statistics message of type "+statisticType+" is null");
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)");
98 return statistics.get(0);
102 * @param statistics the statistics to set
104 public void setStatistics(List<? extends OFStatistics> statistics) {
105 this.statistics = statistics;
109 public void setStatisticsFactory(OFStatisticsFactory statisticsFactory) {
110 this.statisticsFactory = statisticsFactory;
114 public void readFrom(ChannelBuffer data) {
115 super.readFrom(data);
116 this.statisticType = OFStatisticsType.valueOf(data.readShort(), this
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);
126 public void writeTo(ChannelBuffer 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);
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());
150 public boolean equals(Object obj) {
154 if (!super.equals(obj)) {
157 if (!(obj instanceof OFStatisticsMessageBase)) {
160 OFStatisticsMessageBase other = (OFStatisticsMessageBase) obj;
161 if (flags != other.flags) {
164 if (statisticType == null) {
165 if (other.statisticType != null) {
168 } else if (!statisticType.equals(other.statisticType)) {
171 if (statistics == null) {
172 if (other.statistics != null) {
175 } else if (!statistics.equals(other.statistics)) {