8ecb862becf1a2ba52d4c0ddabeb8474a5c58ef5
[controller.git] / third-party / openflowj_netty / src / main / java / org / openflow / protocol / OFVendor.java
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 org.jboss.netty.buffer.ChannelBuffer;
21 import org.openflow.util.U16;
22 import org.openflow.protocol.factory.OFVendorDataFactory;
23 import org.openflow.protocol.factory.OFVendorDataFactoryAware;
24 import org.openflow.protocol.vendor.OFVendorData;
25
26 /**
27  * Represents ofp_vendor_header
28  * @author David Erickson (daviderickson@cs.stanford.edu)
29  */
30 public class OFVendor extends OFMessage implements OFVendorDataFactoryAware {
31     public static int MINIMUM_LENGTH = 12;
32
33     protected int vendor;
34     protected OFVendorData vendorData;
35     protected OFVendorDataFactory vendorDataFactory;
36
37     public OFVendor() {
38         super();
39         this.type = OFType.VENDOR;
40         this.length = U16.t(MINIMUM_LENGTH);
41     }
42
43     /**
44      * @return the vendor
45      */
46     public int getVendor() {
47         return vendor;
48     }
49
50     /**
51      * @param vendor the vendor to set
52      */
53     public void setVendor(int vendor) {
54         this.vendor = vendor;
55     }
56
57     /**
58      * @return the data
59      */
60     public OFVendorData getVendorData() {
61         return vendorData;
62     }
63
64     /**
65      * @param data the data to set
66      */
67     public void setVendorData(OFVendorData vendorData) {
68         this.vendorData = vendorData;
69     }
70
71     @Override
72     public void setVendorDataFactory(OFVendorDataFactory vendorDataFactory) {
73         this.vendorDataFactory = vendorDataFactory;
74     }
75       
76     @Override
77     public void readFrom(ChannelBuffer data) {
78         super.readFrom(data);
79         this.vendor = data.readInt();
80         if (vendorDataFactory == null)
81             throw new RuntimeException("OFVendorDataFactory not set");
82             
83         this.vendorData = vendorDataFactory.parseVendorData(vendor,
84                 data, super.getLengthU() - MINIMUM_LENGTH);
85     }
86
87     @Override
88     public void writeTo(ChannelBuffer data) {
89         super.writeTo(data);
90         data.writeInt(this.vendor);
91         if (vendorData != null)
92             vendorData.writeTo(data);
93     }
94
95     /* (non-Javadoc)
96      * @see java.lang.Object#hashCode()
97      */
98     @Override
99     public int hashCode() {
100         final int prime = 337;
101         int result = super.hashCode();
102         result = prime * result + vendor;
103         if (vendorData != null)
104             result = prime * result + vendorData.hashCode();
105         return result;
106     }
107
108     /* (non-Javadoc)
109      * @see java.lang.Object#equals(java.lang.Object)
110      */
111     @Override
112     public boolean equals(Object obj) {
113         if (this == obj)
114             return true;
115         if (!super.equals(obj))
116             return false;
117         if (getClass() != obj.getClass())
118             return false;
119         OFVendor other = (OFVendor) obj;
120         if (vendor != other.vendor)
121             return false;
122         if (vendorData == null) {
123             if (other.vendorData != null) {
124                 return false;
125             }
126         } else if (!vendorData.equals(other.vendorData)) {
127             return false;
128         }
129         return true;
130     }
131 }