f0af8a7635088c21bd889dc0632c481fb61350ab
[openflowjava.git] / third-party / openflowj_netty / src / main / java / org / openflow / protocol / vendor / OFVendorId.java
1 /**
2 *    Copyright 2011, Big Switch Networks, Inc. 
3 *    Originally created by David Erickson & Rob Sherwood, Stanford 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.vendor;
19
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.jboss.netty.buffer.ChannelBuffer;
24
25 /**
26  * Base class for the vendor ID corresponding to vendor extensions from a
27  * given vendor. It is responsible for knowing how to parse out some sort of
28  * data type value from the vendor data in an OFVendor message so that we can
29  * dispatch to the different subclasses of OFVendorData corresponding to the
30  * different formats of data for the vendor extensions.
31  * 
32  * @author Rob Vaterlaus (rob.vaterlaus@bigswitch.com)
33  */
34 public abstract class OFVendorId {
35     static Map<Integer, OFVendorId> mapping = new HashMap<Integer, OFVendorId>();
36
37     /**
38      * The vendor id value, typically the OUI of the vendor prefixed with 0.
39      */
40     protected int id;
41     
42     /**
43      * Register a new vendor id.
44      * @param vendorId the vendor id to register
45      */
46     public static void registerVendorId(OFVendorId vendorId) {
47         mapping.put(vendorId.getId(), vendorId);
48     }
49     
50     /**
51      * Lookup the OFVendorId instance corresponding to the given id value.
52      * @param id the integer vendor id value
53      * @return the corresponding OFVendorId that's been registered for the
54      *     given value, or null if there id has not been registered.
55      */
56     public static OFVendorId lookupVendorId(int id) {
57         return mapping.get(id);
58     }
59     
60     /**
61      * Create an OFVendorId with the give vendor id value
62      * @param id
63      */
64     public OFVendorId(int id) {
65         this.id = id;
66     }
67     
68     /**
69      * @return the vendor id value
70      */
71     public int getId() {
72         return id;
73     }
74     
75     /**
76      * This function parses enough of the data from the channel buffer to be
77      * able to determine the appropriate OFVendorDataType for the data.
78      * 
79      * @param data the channel buffer containing the vendor data.
80      * @param length the length to the end of the enclosing message
81      * @return the OFVendorDataType that can be used to instantiate the
82      *         appropriate subclass of OFVendorData.
83      */
84     public abstract OFVendorDataType parseVendorDataType(ChannelBuffer data, int length);
85 }