Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 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.ArrayList; |
| 21 | import java.util.List; |
| 22 | |
| 23 | |
| 24 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
| 25 | import org.jboss.netty.buffer.ChannelBuffer; |
| 26 | import org.openflow.protocol.serializers.OFFeaturesReplyJSONSerializer; |
| 27 | import org.openflow.util.U16; |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Represents a features reply message |
| 32 | * @author David Erickson (daviderickson@cs.stanford.edu) |
| 33 | * |
| 34 | */ |
| 35 | @JsonSerialize(using=OFFeaturesReplyJSONSerializer.class) |
| 36 | public class OFFeaturesReply extends OFMessage { |
| 37 | public static int MINIMUM_LENGTH = 32; |
| 38 | |
| 39 | /** |
| 40 | * Corresponds to bits on the capabilities field |
| 41 | */ |
| 42 | public enum OFCapabilities { |
| 43 | OFPC_FLOW_STATS (1 << 0), |
| 44 | OFPC_TABLE_STATS (1 << 1), |
| 45 | OFPC_PORT_STATS (1 << 2), |
| 46 | OFPC_STP (1 << 3), |
| 47 | OFPC_RESERVED (1 << 4), |
| 48 | OFPC_IP_REASM (1 << 5), |
| 49 | OFPC_QUEUE_STATS (1 << 6), |
| 50 | OFPC_ARP_MATCH_IP (1 << 7); |
| 51 | |
| 52 | protected int value; |
| 53 | |
| 54 | private OFCapabilities(int value) { |
| 55 | this.value = value; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @return the value |
| 60 | */ |
| 61 | public int getValue() { |
| 62 | return value; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | protected long datapathId; |
| 67 | protected int buffers; |
| 68 | protected byte tables; |
| 69 | protected int capabilities; |
| 70 | protected int actions; |
| 71 | protected List<OFPhysicalPort> ports; |
| 72 | |
| 73 | public OFFeaturesReply() { |
| 74 | super(); |
| 75 | this.type = OFType.FEATURES_REPLY; |
| 76 | this.length = U16.t(MINIMUM_LENGTH); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @return the datapathId |
| 81 | */ |
| 82 | public long getDatapathId() { |
| 83 | return datapathId; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @param datapathId the datapathId to set |
| 88 | */ |
| 89 | public void setDatapathId(long datapathId) { |
| 90 | this.datapathId = datapathId; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @return the buffers |
| 95 | */ |
| 96 | public int getBuffers() { |
| 97 | return buffers; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @param buffers the buffers to set |
| 102 | */ |
| 103 | public void setBuffers(int buffers) { |
| 104 | this.buffers = buffers; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @return the tables |
| 109 | */ |
| 110 | public byte getTables() { |
| 111 | return tables; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @param tables the tables to set |
| 116 | */ |
| 117 | public void setTables(byte tables) { |
| 118 | this.tables = tables; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @return the capabilities |
| 123 | */ |
| 124 | public int getCapabilities() { |
| 125 | return capabilities; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @param capabilities the capabilities to set |
| 130 | */ |
| 131 | public void setCapabilities(int capabilities) { |
| 132 | this.capabilities = capabilities; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @return the actions |
| 137 | */ |
| 138 | public int getActions() { |
| 139 | return actions; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @param actions the actions to set |
| 144 | */ |
| 145 | public void setActions(int actions) { |
| 146 | this.actions = actions; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @return the ports |
| 151 | */ |
| 152 | public List<OFPhysicalPort> getPorts() { |
| 153 | return ports; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @param ports the ports to set |
| 158 | */ |
| 159 | public void setPorts(List<OFPhysicalPort> ports) { |
| 160 | this.ports = ports; |
| 161 | if (ports == null) { |
| 162 | this.setLengthU(MINIMUM_LENGTH); |
| 163 | } else { |
| 164 | this.setLengthU(MINIMUM_LENGTH + ports.size() |
| 165 | * OFPhysicalPort.MINIMUM_LENGTH); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void readFrom(ChannelBuffer data) { |
| 171 | super.readFrom(data); |
| 172 | this.datapathId = data.readLong(); |
| 173 | this.buffers = data.readInt(); |
| 174 | this.tables = data.readByte(); |
| 175 | data.readerIndex(data.readerIndex() + 3); // pad |
| 176 | this.capabilities = data.readInt(); |
| 177 | this.actions = data.readInt(); |
| 178 | if (this.ports == null) { |
| 179 | this.ports = new ArrayList<OFPhysicalPort>(); |
| 180 | } else { |
| 181 | this.ports.clear(); |
| 182 | } |
| 183 | int portCount = (super.getLengthU() - 32) |
| 184 | / OFPhysicalPort.MINIMUM_LENGTH; |
| 185 | OFPhysicalPort port; |
| 186 | for (int i = 0; i < portCount; ++i) { |
| 187 | port = new OFPhysicalPort(); |
| 188 | port.readFrom(data); |
| 189 | this.ports.add(port); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void writeTo(ChannelBuffer data) { |
| 195 | super.writeTo(data); |
| 196 | data.writeLong(this.datapathId); |
| 197 | data.writeInt(this.buffers); |
| 198 | data.writeByte(this.tables); |
| 199 | data.writeShort((short) 0); // pad |
| 200 | data.writeByte((byte) 0); // pad |
| 201 | data.writeInt(this.capabilities); |
| 202 | data.writeInt(this.actions); |
| 203 | if (this.ports != null) |
| 204 | for (OFPhysicalPort port : this.ports) { |
| 205 | port.writeTo(data); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public int hashCode() { |
| 211 | final int prime = 139; |
| 212 | int result = super.hashCode(); |
| 213 | result = prime * result + actions; |
| 214 | result = prime * result + buffers; |
| 215 | result = prime * result + capabilities; |
| 216 | result = prime * result + (int) (datapathId ^ (datapathId >>> 32)); |
| 217 | result = prime * result + ((ports == null) ? 0 : ports.hashCode()); |
| 218 | result = prime * result + tables; |
| 219 | return result; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public boolean equals(Object obj) { |
| 224 | if (this == obj) { |
| 225 | return true; |
| 226 | } |
| 227 | if (!super.equals(obj)) { |
| 228 | return false; |
| 229 | } |
| 230 | if (!(obj instanceof OFFeaturesReply)) { |
| 231 | return false; |
| 232 | } |
| 233 | OFFeaturesReply other = (OFFeaturesReply) obj; |
| 234 | if (actions != other.actions) { |
| 235 | return false; |
| 236 | } |
| 237 | if (buffers != other.buffers) { |
| 238 | return false; |
| 239 | } |
| 240 | if (capabilities != other.capabilities) { |
| 241 | return false; |
| 242 | } |
| 243 | if (datapathId != other.datapathId) { |
| 244 | return false; |
| 245 | } |
| 246 | if (ports == null) { |
| 247 | if (other.ports != null) { |
| 248 | return false; |
| 249 | } |
| 250 | } else if (!ports.equals(other.ports)) { |
| 251 | return false; |
| 252 | } |
| 253 | if (tables != other.tables) { |
| 254 | return false; |
| 255 | } |
| 256 | return true; |
| 257 | } |
| 258 | } |