Bug 1245: Dropped Binding prefix from Binding Data APIs.
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / packet / UDP.java
1
2 /*
3  * Copyright (c) 2013 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
19 /**
20  * Class that represents the UDP datagram objects
21  */
22
23 public class UDP extends Packet {
24
25     private static final String SRCPORT = "SourcePort";
26     private static final String DESTPORT = "DestinationPort";
27     private static final String LENGTH = "Length";
28     private static final String CHECKSUM = "Checksum";
29
30     private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
31         private static final long serialVersionUID = 1L;
32         {
33             put(SRCPORT, new ImmutablePair<Integer, Integer>(0, 16));
34             put(DESTPORT, new ImmutablePair<Integer, Integer>(16, 16));
35             put(LENGTH, new ImmutablePair<Integer, Integer>(32, 16));
36             put(CHECKSUM, new ImmutablePair<Integer, Integer>(48, 16));
37         }
38     };
39
40     public UDP() {
41         super();
42         fieldValues = new HashMap<String, byte[]>();
43         hdrFieldCoordMap = fieldCoordinates;
44         hdrFieldsMap = fieldValues;
45         /* Setting all remaining header field values to
46          * default value of 0.  These maybe changed as needed
47          */
48         setSourcePort((short) 0);
49         setDestinationPort((short) 0);
50         setChecksum((short) 0);
51     }
52
53     public UDP(boolean writeAccess) {
54         super(writeAccess);
55         fieldValues = new HashMap<String, byte[]>();
56         hdrFieldCoordMap = fieldCoordinates;
57         hdrFieldsMap = fieldValues;
58         /* Setting all remaining header field values to
59          * default value of 0.  These maybe changed as needed
60          */
61         setSourcePort((short) 0);
62         setDestinationPort((short) 0);
63         setChecksum((short) 0);
64     }
65
66     private final Map<String, byte[]> fieldValues;
67
68     /* public static Map<Short, Class<? extends Packet>> decodeMap;
69
70       static {
71           decodeMap = new HashMap<Short, Class<? extends Packet>>();
72           UDP.decodeMap.put((short)67, DHCP.class);
73           UDP.decodeMap.put((short)68, DHCP.class);
74       }*/
75     /**
76      * Get the stored source port
77      * @return short - the sourcePort
78      */
79     public short getSourcePort() {
80         return (BitBufferHelper.getShort(fieldValues.get(SRCPORT)));
81     }
82
83     /**
84      * Get the stored destination port
85      * @return short - the destinationPort
86      */
87     public short getDestinationPort() {
88         return (BitBufferHelper.getShort(fieldValues.get(DESTPORT)));
89     }
90
91     /**
92      * Gets the stored length of UDP header
93      * @return short - the length
94      */
95     public short getLength() {
96         return (BitBufferHelper.getShort(fieldValues.get(LENGTH)));
97     }
98
99     /**
100      * Get the stored checksum value of the UDP header
101      * @return short - the checksum
102      */
103     public short getChecksum() {
104         return (BitBufferHelper.getShort(fieldValues.get(CHECKSUM)));
105     }
106
107     @Override
108     /**
109      * Store the value read from data stream in hdrFieldMap
110      */
111     public void setHeaderField(String headerField, byte[] readValue) {
112         hdrFieldsMap.put(headerField, readValue);
113     }
114
115     /**
116      * Sets the sourcePort value for the current UDP object instance
117      * @param short - the sourcePort to set
118      * @return UDP
119      */
120     public UDP setSourcePort(short udpSourcePort) {
121         byte[] sourcePort = BitBufferHelper.toByteArray(udpSourcePort);
122         fieldValues.put(SRCPORT, sourcePort);
123         return this;
124     }
125
126     /**
127      * Sets the destinationPort value for the current UDP object instance
128      * @param short - the destinationPort to set
129      * @return UDP
130      */
131     public UDP setDestinationPort(short udpDestinationPort) {
132         byte[] destinationPort = BitBufferHelper
133                 .toByteArray(udpDestinationPort);
134         fieldValues.put(DESTPORT, destinationPort);
135         return this;
136     }
137
138     /**
139      * Set the UDP header length value for the current UDP object instance
140      * @param short - the length to set
141      * @return UDP
142      */
143     public UDP setLength(short udpLength) {
144         byte[] length = BitBufferHelper.toByteArray(udpLength);
145         fieldValues.put(LENGTH, length);
146         return this;
147     }
148
149     /**
150      * Set the checksum for the current UDP object instance
151      * @param short - the checksum to set
152      * @return UDP
153      */
154     public UDP setChecksum(short udpChecksum) {
155         byte[] checksum = BitBufferHelper.toByteArray(udpChecksum);
156         fieldValues.put(CHECKSUM, checksum);
157         return this;
158     }
159 }