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