Imported vpnservice as a subtree
[netvirt.git] / vpnservice / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / vpnservice / mdsalutil / packet / TCP.java
1 /*
2  * Copyright (c) 2013 - 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.vpnservice.mdsalutil.packet;
10
11 import java.util.HashMap;
12 import java.util.LinkedHashMap;
13 import java.util.Map;
14
15 import org.apache.commons.lang3.tuple.ImmutablePair;
16 import org.apache.commons.lang3.tuple.Pair;
17 import org.opendaylight.controller.liblldp.BitBufferHelper;
18 import org.opendaylight.controller.liblldp.Packet;
19
20 /**
21  * Class that represents the TCP segment objects
22  */
23 public class TCP extends Packet {
24
25     public static final String SRCPORT = "SourcePort";
26     public static final String DESTPORT = "DestinationPort";
27     public static final String SEQNUMBER = "SequenceNumber";
28     public static final String ACKNUMBER = "AcknowledgementNumber";
29     public static final String DATAOFFSET = "DataOffset";
30     public static final String RESERVED = "Reserved";
31     public static final String HEADERLENFLAGS = "HeaderLenFlags";
32     public static final String WINDOWSIZE = "WindowSize";
33     public static final String CHECKSUM = "Checksum";
34     public static final String URGENTPOINTER = "UrgentPointer";
35
36     private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
37         private static final long serialVersionUID = 1L;
38         {
39             put(SRCPORT, new ImmutablePair<Integer, Integer>(0, 16));
40             put(DESTPORT, new ImmutablePair<Integer, Integer>(16, 16));
41             put(SEQNUMBER, new ImmutablePair<Integer, Integer>(32, 32));
42             put(ACKNUMBER, new ImmutablePair<Integer, Integer>(64, 32));
43             put(DATAOFFSET, new ImmutablePair<Integer, Integer>(96, 4));
44             put(RESERVED, new ImmutablePair<Integer, Integer>(100, 3));
45             put(HEADERLENFLAGS, new ImmutablePair<Integer, Integer>(103, 9));
46             put(WINDOWSIZE, new ImmutablePair<Integer, Integer>(112, 16));
47             put(CHECKSUM, new ImmutablePair<Integer, Integer>(128, 16));
48             put(URGENTPOINTER, new ImmutablePair<Integer, Integer>(144, 16));
49         }
50     };
51
52     private final Map<String, byte[]> fieldValues;
53
54     /**
55      * Default constructor that sets all the header fields to zero
56      */
57     public TCP() {
58         super();
59         fieldValues = new HashMap<String, byte[]>();
60         hdrFieldCoordMap = fieldCoordinates;
61         hdrFieldsMap = fieldValues;
62         /* Setting all remaining header field values to
63          * default value of 0.  These maybe changed as needed
64          */
65         setSourcePort((short) 0);
66         setDestinationPort((short) 0);
67         setSequenceNumber(0);
68         setAckNumber(0);
69         setDataOffset((byte) 0);
70         setReserved((byte) 0);
71         setWindowSize((short) 0);
72         setUrgentPointer((short) 0);
73         setChecksum((short) 0);
74     }
75
76     /**
77      * Constructor that sets the access level for the packet and
78      * sets all the header fields to zero.
79      * @param writeAccess - boolean
80      */
81     public TCP(boolean writeAccess) {
82         super(writeAccess);
83         fieldValues = new HashMap<String, byte[]>();
84         hdrFieldCoordMap = fieldCoordinates;
85         hdrFieldsMap = fieldValues;
86         /* Setting all remaining header field values to
87          * default value of 0.  These maybe changed as needed
88          */
89         setSourcePort((short) 0);
90         setDestinationPort((short) 0);
91         setSequenceNumber(0);
92         setAckNumber(0);
93         setDataOffset((byte) 0);
94         setReserved((byte) 0);
95         setWindowSize((short) 0);
96         setUrgentPointer((short) 0);
97         setChecksum((short) 0);
98     }
99
100     @Override
101     /**
102      * Stores the value read from data stream
103      * @param headerField - String
104      * @param readValue - byte[]
105      */
106     public void setHeaderField(String headerField, byte[] readValue) {
107         hdrFieldsMap.put(headerField, readValue);
108     }
109
110     /**
111      * Sets the TCP source port for the current TCP object instance
112      * @param tcpSourcePort short
113      * @return TCP
114      */
115     public TCP setSourcePort(short tcpSourcePort) {
116         byte[] sourcePort = BitBufferHelper.toByteArray(tcpSourcePort);
117         fieldValues.put(SRCPORT, sourcePort);
118         return this;
119     }
120
121     /**
122      * Sets the TCP destination port for the current TCP object instance
123      * @param tcpDestinationPort short
124      * @return TCP
125      */
126     public TCP setDestinationPort(short tcpDestinationPort) {
127         byte[] destinationPort = BitBufferHelper
128                 .toByteArray(tcpDestinationPort);
129         fieldValues.put(DESTPORT, destinationPort);
130         return this;
131     }
132
133     /**
134      * Sets the TCP sequence number for the current TCP object instance
135      * @param tcpSequenceNumber - int
136      * @return TCP
137      */
138     public TCP setSequenceNumber(int tcpSequenceNumber) {
139         byte[] sequenceNumber = BitBufferHelper.toByteArray(tcpSequenceNumber);
140         fieldValues.put(SEQNUMBER, sequenceNumber);
141         return this;
142     }
143
144     /**
145      * Sets the TCP data offset for the current TCP object instance
146      * @param tcpDataOffset - byte
147      * @return TCP
148      */
149     public TCP setDataOffset(byte tcpDataOffset) {
150         byte[] offset = BitBufferHelper.toByteArray(tcpDataOffset);
151         fieldValues.put("DataOffset", offset);
152         return this;
153     }
154
155     /**
156      * Sets the TCP reserved bits for the current TCP object instance
157      * @param tcpReserved byte
158      * @return TCP
159      */
160     public TCP setReserved(byte tcpReserved) {
161         byte[] reserved = BitBufferHelper.toByteArray(tcpReserved);
162         fieldValues.put("Reserved", reserved);
163         return this;
164     }
165
166     /**
167      * Sets the TCP Ack number for the current TCP object instance
168      * @param tcpAckNumber int
169      * @return TCP
170      */
171     public TCP setAckNumber(int tcpAckNumber) {
172         byte[] ackNumber = BitBufferHelper.toByteArray(tcpAckNumber);
173         fieldValues.put(ACKNUMBER, ackNumber);
174         return this;
175     }
176
177     /**
178      * Sets the TCP flags for the current TCP object instance
179      * @param tcpFlags short
180      * @return TCP
181      */
182     public TCP setHeaderLenFlags(short tcpFlags) {
183         byte[] headerLenFlags = BitBufferHelper.toByteArray(tcpFlags);
184         fieldValues.put(HEADERLENFLAGS, headerLenFlags);
185         return this;
186     }
187
188     /**
189      * Sets the TCP window size for the current TCP object instance
190      * @param tcpWsize short
191      * @return TCP
192      */
193     public TCP setWindowSize(short tcpWsize) {
194         byte[] wsize = BitBufferHelper.toByteArray(tcpWsize);
195         fieldValues.put(WINDOWSIZE, wsize);
196         return this;
197     }
198
199     /**
200      * Sets the TCP checksum for the current TCP object instance
201      * @param tcpChecksum short
202      * @return TCP
203      */
204     public TCP setChecksum(short tcpChecksum) {
205         byte[] checksum = BitBufferHelper.toByteArray(tcpChecksum);
206         fieldValues.put(CHECKSUM, checksum);
207         return this;
208     }
209
210     /**
211      * Sets the TCP Urgent Pointer for the current TCP object instance
212      * @param tcpUrgentPointer short
213      * @return TCP
214      */
215     public TCP setUrgentPointer(short tcpUrgentPointer) {
216         byte[] urgentPointer = BitBufferHelper.toByteArray(tcpUrgentPointer);
217         fieldValues.put(URGENTPOINTER, urgentPointer);
218         return this;
219     }
220
221     /**
222      * Gets the stored source port value of TCP header
223      * @return the sourcePort
224      */
225     public short getSourcePort() {
226         return (BitBufferHelper.getShort(fieldValues.get(SRCPORT)));
227     }
228
229     /**
230      * Gets the stored destination port value of TCP header
231      * @return the destinationPort
232      */
233     public short getDestinationPort() {
234         return (BitBufferHelper.getShort(fieldValues.get(DESTPORT)));
235     }
236
237     /**
238      * Get the stored checksum value of the TCP header
239      * @return short - the checksum
240      */
241     public short getChecksum() {
242         return (BitBufferHelper.getShort(fieldValues.get(CHECKSUM)));
243     }
244
245 }