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