Fix checkstyle warnings in netconf-cli
[controller.git] / third-party / openflowj / src / main / java / org / openflow / protocol / action / ActionVendorOutputNextHop.java
1 package org.openflow.protocol.action;
2
3         import java.net.Inet4Address;
4         import java.net.InetAddress;
5         import java.net.UnknownHostException;
6 import java.nio.ByteBuffer;
7
8 import org.openflow.util.HexString;
9
10
11         public class ActionVendorOutputNextHop extends OFActionVendor {
12                 private static final long serialVersionUID = 1L;
13                 private static int VENDOR_CISCO = 0xC;
14                 private enum ONHLength {
15                         ONH_LEN_MIN(16),
16                         ONH_LEN_P2P(16),
17                         ONH_LEN_IPV4(24),
18                         ONH_LEN_MAC(32),
19                         ONH_LEN_IPV6(40);
20                 private int value;
21                 private ONHLength(int value) {
22                         this.value = value;
23                 }
24                 public int getValue() {
25                         return this.value;
26                 }
27                 }       
28                 private enum ONHActionType {
29                         ONH_ACTION_NONE(0),
30                         ONH_ACTION_OUTPUT_NH(1),
31                         ONH_ACTION_NETFLOW(2);
32                 private int value;
33                 private ONHActionType(int value) {
34                         this.value = value;
35                 }
36                 public int getValue() {
37                         return this.value;
38                 }
39                 }
40                 private enum ONHAddressType {
41                 ONH_ADDRTYPE_NONE(0),
42                 ONH_ADDRTYPE_P2P(1),
43                 ONH_ADDRTYPE_IPV4(2),
44                 ONH_ADDRTYPE_IPV6(3),
45                 ONH_ADDRTYPE_MAC48(4);
46                 private int value;
47                 private ONHAddressType(int value) {
48                         this.value = value;
49                 }
50                 public int getValue() {
51                         return this.value;
52                 }
53             }
54             private enum ONHXAddressType {
55                 ONH_XADDRTYPE_NONE(0),
56                 ONH_XADDRTYPE_PORT(1),
57                 ONH_XADDRTYPE_VPNID(2);
58                 private int value;
59                 private ONHXAddressType(int value) {
60                         this.value = value;
61                 }
62                 public int getValue() {
63                         return this.value;
64                 }
65            }
66                 protected InetAddress address; 
67                         
68                 public ActionVendorOutputNextHop() {
69                 super();
70                 super.setLength((short)ONHLength.ONH_LEN_MIN.getValue());
71                         super.setVendor(VENDOR_CISCO);
72                         this.address = null;
73                 }
74                 
75                 public void setNextHop(InetAddress address) {
76                         short actionLen;
77                         if (address instanceof Inet4Address) {
78                 actionLen = (short)ONHLength.ONH_LEN_IPV4.getValue();
79             } else {
80                 actionLen = (short)ONHLength.ONH_LEN_IPV6.getValue();
81             }
82             super.setLength(actionLen);
83                         this.address = address;
84                 }
85                 public InetAddress getNextHop() {
86                         return this.address;
87                 }
88             @Override
89             public void readFrom(ByteBuffer data) {
90                 /*
91                  * For now, only contains the next hop address
92                  */
93                 //super.readFrom(data); don't need this
94                 
95                 if (data.remaining() < super.getLength()-8) {
96                         /*
97                          * malformed element, skip over 
98                          */
99                         data.position(data.remaining());
100                         return;
101                 }
102                 if ((super.getLength() !=  (short)ONHLength.ONH_LEN_IPV4.getValue()) &&
103                         (super.getLength() !=  (short)ONHLength.ONH_LEN_IPV6.getValue())) {
104                         /*
105                          * mal-formed element, skip over 
106                          */
107                         data.position(super.getLength());
108                         return;
109                 }
110                 data.getShort();  // skip the ONH_ACTION_OUTPUT_NH
111                 data.getShort(); // skip address and xtraaddress types
112                 data.getInt();     // skip the extra address (8 bytes)
113                 data.getInt();
114                 byte[] a;
115                 if (super.getLength() == (short)ONHLength.ONH_LEN_IPV4.getValue()) {
116                         a = new byte[4];
117                         data.get(a);
118                 } else {
119                         a = new byte[16];
120                         data.get(a);
121                         data.getInt(); //4 bytes pad
122                 }
123                 try {
124                         this.address = InetAddress.getByAddress(a);
125                 } catch (UnknownHostException e) {
126                         e.printStackTrace();
127                 }
128             }
129
130             @Override
131             public void writeTo(ByteBuffer data) {
132                 byte atype = (byte)(ONHAddressType.ONH_ADDRTYPE_NONE.getValue());
133                 byte xatype = (byte)(ONHXAddressType.ONH_XADDRTYPE_NONE.getValue());
134                 if (address instanceof Inet4Address) 
135                         atype =  (byte)(ONHAddressType.ONH_ADDRTYPE_IPV4.getValue());
136                 else
137                         atype = (byte)(ONHAddressType.ONH_ADDRTYPE_IPV6.getValue());
138                 super.writeTo(data); // this writes the standard  8byte ofp_action_vendor_header
139                 data.putShort((short)(ONHActionType.ONH_ACTION_OUTPUT_NH.getValue()));
140                 data.put(atype);
141                 data.put(xatype);
142                 /*
143                  * write the xtra address. For now it is all 0
144                  */
145                 data.putInt(0); // 8-byte pad
146                 data.putInt(0);
147                 /* 
148                  * write the address only when address type is not P2P
149                  */
150                 if (atype == (byte)(ONHAddressType.ONH_ADDRTYPE_IPV4.getValue())) {
151                         data.put(address.getAddress()); // no need to pad
152                         //avnh.put(address.getAddress()); 
153                 } else if (atype == (byte)(ONHAddressType.ONH_ADDRTYPE_IPV6.getValue())) {
154                         data.put(address.getAddress());
155                         //avnh.put(address.getAddress());
156                         data.putInt(0); // 4-byte pad
157                         //avnh.putInt(0);
158                 }
159                 ActionVendorOutputNextHop a = new ActionVendorOutputNextHop();
160                 a.setLength((short)24);
161             }
162
163             @Override
164             public int hashCode() {
165                 final int prime = 347;
166                 int result = super.hashCode();
167                 result = prime * result + address.hashCode();
168                 return result;
169             }
170
171             @Override
172             public boolean equals(Object obj) {
173                 if (this == obj) {
174                     return true;
175                 }
176                 if (!super.equals(obj)) {
177                     return false;
178                 }
179                 if (!(obj instanceof ActionVendorOutputNextHop)) {
180                     return false;
181                 }
182                 ActionVendorOutputNextHop other = (ActionVendorOutputNextHop) obj;
183                 if (!other.address.equals(this.address))
184                         return false;
185                 return true;
186             }
187
188                 public String toString() {
189                         return ("OutputNextHop: " + address.getHostAddress());
190                 }
191
192         }
193