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.Arrays;
22 import junit.framework.TestCase;
24 import org.jboss.netty.buffer.ChannelBuffer;
25 import org.jboss.netty.buffer.ChannelBuffers;
26 import org.openflow.protocol.factory.BasicFactory;
27 import org.openflow.protocol.vendor.OFByteArrayVendorData;
28 import org.openflow.protocol.vendor.OFBasicVendorDataType;
29 import org.openflow.protocol.vendor.OFBasicVendorId;
30 import org.openflow.protocol.vendor.OFVendorData;
31 import org.openflow.protocol.vendor.OFVendorId;
32 import org.openflow.util.OFTestCase;
34 public class OFVendorTest extends OFTestCase {
36 public static int ACME_VENDOR_ID = 0x00112233;
38 static class AcmeVendorData implements OFVendorData {
39 protected int dataType;
41 public int getLength() {
45 public void readFrom(ChannelBuffer data, int length) {
46 dataType = data.readInt();
49 public void writeTo(ChannelBuffer data) {
50 data.writeInt(dataType);
54 static class AcmeVendorData1 extends AcmeVendorData {
58 public static int DATA_TYPE = 1;
60 public AcmeVendorData1() {
63 public AcmeVendorData1(short flags, short value) {
64 this.dataType = DATA_TYPE;
69 public short getFlags() {
73 public short getValue() {
77 public int getLength() {
81 public void readFrom(ChannelBuffer data, int length) {
82 super.readFrom(data, length);
83 flags = data.readShort();
84 value = data.readShort();
87 public void writeTo(ChannelBuffer data) {
89 data.writeShort(flags);
90 data.writeShort(value);
93 public static Instantiable<OFVendorData> getInstantiable() {
94 return new Instantiable<OFVendorData>() {
95 public OFVendorData instantiate() {
96 return new AcmeVendorData1();
102 static class AcmeVendorData2 extends AcmeVendorData {
106 public static int DATA_TYPE = 2;
108 public AcmeVendorData2() {
111 public AcmeVendorData2(int type, int subtype) {
112 this.dataType = DATA_TYPE;
114 this.subtype = subtype;
117 public int getType() {
121 public int getSubtype() {
125 public int getLength() {
129 public void readFrom(ChannelBuffer data, int length) {
130 super.readFrom(data, length);
131 type = data.readShort();
132 subtype = data.readShort();
135 public void writeTo(ChannelBuffer data) {
137 data.writeShort(type);
138 data.writeShort(subtype);
141 public static Instantiable<OFVendorData> getInstantiable() {
142 return new Instantiable<OFVendorData>() {
143 public OFVendorData instantiate() {
144 return new AcmeVendorData2();
151 OFBasicVendorId acmeVendorId = new OFBasicVendorId(ACME_VENDOR_ID, 4);
152 OFVendorId.registerVendorId(acmeVendorId);
153 OFBasicVendorDataType acmeVendorData1 = new OFBasicVendorDataType(
154 AcmeVendorData1.DATA_TYPE, AcmeVendorData1.getInstantiable());
155 acmeVendorId.registerVendorDataType(acmeVendorData1);
156 OFBasicVendorDataType acmeVendorData2 = new OFBasicVendorDataType(
157 AcmeVendorData2.DATA_TYPE, AcmeVendorData2.getInstantiable());
158 acmeVendorId.registerVendorDataType(acmeVendorData2);
161 private OFVendor makeVendorMessage(int vendor) {
162 OFVendor msg = (OFVendor) messageFactory.getMessage(OFType.VENDOR);
163 msg.setVendorDataFactory(new BasicFactory());
164 msg.setVendor(vendor);
168 public void testWriteRead() throws Exception {
169 OFVendor msg = makeVendorMessage(1);
170 ChannelBuffer bb = ChannelBuffers.dynamicBuffer();
174 TestCase.assertEquals(1, msg.getVendor());
177 public void testVendorData() throws Exception {
178 OFVendor msg = makeVendorMessage(ACME_VENDOR_ID);
179 OFVendorData vendorData = new AcmeVendorData1((short)11, (short)22);
180 msg.setVendorData(vendorData);
181 msg.setLengthU(OFVendor.MINIMUM_LENGTH + vendorData.getLength());
182 ChannelBuffer bb = ChannelBuffers.dynamicBuffer();
186 assertEquals(ACME_VENDOR_ID, msg.getVendor());
187 AcmeVendorData1 vendorData1 = (AcmeVendorData1) msg.getVendorData();
188 assertEquals(11, vendorData1.getFlags());
189 assertEquals(22, vendorData1.getValue());
191 vendorData = new AcmeVendorData2(33, 44);
192 msg.setVendorData(vendorData);
193 msg.setLengthU(OFVendor.MINIMUM_LENGTH + vendorData.getLength());
197 assertEquals(ACME_VENDOR_ID, msg.getVendor());
198 AcmeVendorData2 vendorData2 = (AcmeVendorData2) msg.getVendorData();
199 assertEquals(33, vendorData2.getType());
200 assertEquals(44, vendorData2.getSubtype());
202 final int DUMMY_VENDOR_ID = 55;
203 msg.setVendor(DUMMY_VENDOR_ID);
204 byte[] genericVendorDataBytes = new byte[] {0x55, 0x66};
205 vendorData = new OFByteArrayVendorData(genericVendorDataBytes);
206 msg.setVendorData(vendorData);
207 msg.setLengthU(OFVendor.MINIMUM_LENGTH + vendorData.getLength());
211 assertEquals(DUMMY_VENDOR_ID, msg.getVendor());
212 OFByteArrayVendorData genericVendorData = (OFByteArrayVendorData) msg.getVendorData();
213 assertTrue(Arrays.equals(genericVendorDataBytes, genericVendorData.getBytes()));