Initial opendaylight infrastructure commit!!
[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.builder.EqualsBuilder;
17 import org.apache.commons.lang3.builder.HashCodeBuilder;
18 import org.apache.commons.lang3.tuple.ImmutablePair;
19 import org.apache.commons.lang3.tuple.Pair;
20
21 /**
22  * Class that represents the UDP datagram objects
23  *
24  *
25  */
26
27 public class UDP extends Packet {
28
29     private static final String SRCPORT = "SourcePort";
30     private static final String DESTPORT = "DestinationPort";
31     private static final String LENGTH = "Length";
32     private static final String CHECKSUM = "Checksum";
33
34     private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
35         private static final long serialVersionUID = 1L;
36         {
37             put(SRCPORT, new ImmutablePair<Integer, Integer>(0, 16));
38             put(DESTPORT, new ImmutablePair<Integer, Integer>(16, 16));
39             put(LENGTH, new ImmutablePair<Integer, Integer>(32, 16));
40             put(CHECKSUM, new ImmutablePair<Integer, Integer>(48, 16));
41         }
42     };
43
44     public UDP() {
45         super();
46         fieldValues = new HashMap<String, byte[]>();
47         hdrFieldCoordMap = fieldCoordinates;
48         hdrFieldsMap = fieldValues;
49         /* Setting all remaining header field values to
50          * default value of 0.  These maybe changed as needed
51          */
52         setSourcePort((short) 0);
53         setDestinationPort((short) 0);
54         setChecksum((short) 0);
55     }
56
57     public UDP(boolean writeAccess) {
58         super(writeAccess);
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         setChecksum((short) 0);
68     }
69
70     private Map<String, byte[]> fieldValues;
71
72     /* public static Map<Short, Class<? extends Packet>> decodeMap;
73
74       static {
75           decodeMap = new HashMap<Short, Class<? extends Packet>>();
76           UDP.decodeMap.put((short)67, DHCP.class);
77           UDP.decodeMap.put((short)68, DHCP.class);
78       }*/
79     /**
80      * Get the stored source port
81      * @return short - the sourcePort
82      */
83     public short getSourcePort() {
84         return (BitBufferHelper.getShort(fieldValues.get(SRCPORT)));
85     }
86
87     /**
88      * Get the stored destination port
89      * @return short - the destinationPort
90      */
91     public short getDestinationPort() {
92         return (BitBufferHelper.getShort(fieldValues.get(DESTPORT)));
93     }
94
95     /**
96      * Gets the stored length of UDP header
97      * @return short - the length
98      */
99     public short getLength() {
100         return (BitBufferHelper.getShort(fieldValues.get(LENGTH)));
101     }
102
103     /**
104      * Get the stored checksum value of the UDP header
105      * @return short - the checksum
106      */
107     public short getChecksum() {
108         return (BitBufferHelper.getShort(fieldValues.get(CHECKSUM)));
109     }
110
111     @Override
112     /**
113      * Store the value read from data stream in hdrFieldMap
114      */
115     public void setHeaderField(String headerField, byte[] readValue) {
116         /*if (headerField.equals("Protocol")) {
117                 payloadClass = decodeMap.get(readValue);
118         }*/
119         hdrFieldsMap.put(headerField, readValue);
120     }
121
122     /**
123      * Sets the sourcePort value for the current UDP object instance
124      * @param short - the sourcePort to set
125      * @return UDP
126      */
127     public UDP setSourcePort(short udpSourcePort) {
128         byte[] sourcePort = BitBufferHelper.toByteArray(udpSourcePort);
129         fieldValues.put(SRCPORT, sourcePort);
130         return this;
131     }
132
133     /**
134      * Sets the destinationPort value for the current UDP object instance
135      * @param short - the destinationPort to set
136      * @return UDP
137      */
138     public UDP setDestinationPort(short udpDestinationPort) {
139         byte[] destinationPort = BitBufferHelper
140                 .toByteArray(udpDestinationPort);
141         fieldValues.put(DESTPORT, destinationPort);
142         return this;
143     }
144
145     /**
146      * Set the UDP header length value for the current UDP object instance
147      * @param short - the length to set
148      * @return UDP
149      */
150     public UDP setLength(short udpLength) {
151         byte[] length = BitBufferHelper.toByteArray(udpLength);
152         fieldValues.put(LENGTH, length);
153         return this;
154     }
155
156     /**
157      * Set the checksum for the current UDP object instance
158      * @param short - the checksum to set
159      * @return UDP
160      */
161     public UDP setChecksum(short udpChecksum) {
162         byte[] checksum = BitBufferHelper.toByteArray(udpChecksum);
163         fieldValues.put(CHECKSUM, checksum);
164         return this;
165     }
166
167     @Override
168     public int hashCode() {
169         return HashCodeBuilder.reflectionHashCode(this);
170     }
171
172     @Override
173     public boolean equals(Object obj) {
174         return EqualsBuilder.reflectionEquals(this, obj);
175     }
176 }