blob: 1516a02eaa023f1438c2e2832029ea27ff690184 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
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
18package org.openflow.protocol;
19
20import java.util.Arrays;
21
22import junit.framework.TestCase;
23
24import org.jboss.netty.buffer.ChannelBuffer;
25import org.jboss.netty.buffer.ChannelBuffers;
26import org.openflow.protocol.factory.BasicFactory;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080027import org.openflow.protocol.vendor.OFBasicVendorDataType;
28import org.openflow.protocol.vendor.OFBasicVendorId;
Jonathan Harta88fd242014-04-03 11:24:54 -070029import org.openflow.protocol.vendor.OFByteArrayVendorData;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080030import org.openflow.protocol.vendor.OFVendorData;
31import org.openflow.protocol.vendor.OFVendorId;
32import org.openflow.util.OFTestCase;
33
34public class OFVendorTest extends OFTestCase {
35
36 public static int ACME_VENDOR_ID = 0x00112233;
37
38 static class AcmeVendorData implements OFVendorData {
39 protected int dataType;
40
41 public int getLength() {
42 return 4;
43 }
44
45 public void readFrom(ChannelBuffer data, int length) {
46 dataType = data.readInt();
47 }
48
49 public void writeTo(ChannelBuffer data) {
50 data.writeInt(dataType);
51 }
52 }
53
54 static class AcmeVendorData1 extends AcmeVendorData {
55 public short flags;
56 public short value;
57
58 public static int DATA_TYPE = 1;
59
60 public AcmeVendorData1() {
61 }
62
63 public AcmeVendorData1(short flags, short value) {
64 this.dataType = DATA_TYPE;
65 this.flags = flags;
66 this.value = value;
67 }
68
69 public short getFlags() {
70 return flags;
71 }
72
73 public short getValue() {
74 return value;
75 }
76
77 public int getLength() {
78 return 8;
79 }
80
81 public void readFrom(ChannelBuffer data, int length) {
82 super.readFrom(data, length);
83 flags = data.readShort();
84 value = data.readShort();
85
86 }
87 public void writeTo(ChannelBuffer data) {
88 super.writeTo(data);
89 data.writeShort(flags);
90 data.writeShort(value);
91 }
92
93 public static Instantiable<OFVendorData> getInstantiable() {
94 return new Instantiable<OFVendorData>() {
95 public OFVendorData instantiate() {
96 return new AcmeVendorData1();
97 }
98 };
99 }
100 }
101
102 static class AcmeVendorData2 extends AcmeVendorData {
103 public int type;
104 public int subtype;
105
106 public static int DATA_TYPE = 2;
107
108 public AcmeVendorData2() {
109 }
110
111 public AcmeVendorData2(int type, int subtype) {
112 this.dataType = DATA_TYPE;
113 this.type = type;
114 this.subtype = subtype;
115 }
116
117 public int getType() {
118 return type;
119 }
120
121 public int getSubtype() {
122 return subtype;
123 }
124
125 public int getLength() {
126 return 12;
127 }
128
129 public void readFrom(ChannelBuffer data, int length) {
130 super.readFrom(data, length);
131 type = data.readShort();
132 subtype = data.readShort();
133
134 }
135 public void writeTo(ChannelBuffer data) {
136 super.writeTo(data);
137 data.writeShort(type);
138 data.writeShort(subtype);
139 }
140
141 public static Instantiable<OFVendorData> getInstantiable() {
142 return new Instantiable<OFVendorData>() {
143 public OFVendorData instantiate() {
144 return new AcmeVendorData2();
145 }
146 };
147 }
148 }
149
150 {
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);
159 }
160
161 private OFVendor makeVendorMessage(int vendor) {
162 OFVendor msg = (OFVendor) messageFactory.getMessage(OFType.VENDOR);
163 msg.setVendorDataFactory(new BasicFactory());
164 msg.setVendor(vendor);
165 return msg;
166 }
167
168 public void testWriteRead() throws Exception {
169 OFVendor msg = makeVendorMessage(1);
170 ChannelBuffer bb = ChannelBuffers.dynamicBuffer();
171 bb.clear();
172 msg.writeTo(bb);
173 msg.readFrom(bb);
174 TestCase.assertEquals(1, msg.getVendor());
175 }
176
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();
183 bb.clear();
184 msg.writeTo(bb);
185 msg.readFrom(bb);
186 assertEquals(ACME_VENDOR_ID, msg.getVendor());
187 AcmeVendorData1 vendorData1 = (AcmeVendorData1) msg.getVendorData();
188 assertEquals(11, vendorData1.getFlags());
189 assertEquals(22, vendorData1.getValue());
190
191 vendorData = new AcmeVendorData2(33, 44);
192 msg.setVendorData(vendorData);
193 msg.setLengthU(OFVendor.MINIMUM_LENGTH + vendorData.getLength());
194 bb.clear();
195 msg.writeTo(bb);
196 msg.readFrom(bb);
197 assertEquals(ACME_VENDOR_ID, msg.getVendor());
198 AcmeVendorData2 vendorData2 = (AcmeVendorData2) msg.getVendorData();
199 assertEquals(33, vendorData2.getType());
200 assertEquals(44, vendorData2.getSubtype());
201
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());
208 bb.clear();
209 msg.writeTo(bb);
210 msg.readFrom(bb);
211 assertEquals(DUMMY_VENDOR_ID, msg.getVendor());
212 OFByteArrayVendorData genericVendorData = (OFByteArrayVendorData) msg.getVendorData();
213 assertTrue(Arrays.equals(genericVendorDataBytes, genericVendorData.getBytes()));
214 }
215}