3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
10 package org.opendaylight.controller.sal.packet;
12 import java.util.HashMap;
13 import java.util.LinkedHashMap;
16 import org.apache.commons.lang3.tuple.ImmutablePair;
17 import org.apache.commons.lang3.tuple.Pair;
20 * Class that represents the UDP datagram objects
23 public class UDP extends Packet {
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";
30 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
31 private static final long serialVersionUID = 1L;
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));
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
48 setSourcePort((short) 0);
49 setDestinationPort((short) 0);
50 setChecksum((short) 0);
53 public UDP(boolean 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
61 setSourcePort((short) 0);
62 setDestinationPort((short) 0);
63 setChecksum((short) 0);
66 private final Map<String, byte[]> fieldValues;
68 /* public static Map<Short, Class<? extends Packet>> decodeMap;
71 decodeMap = new HashMap<Short, Class<? extends Packet>>();
72 UDP.decodeMap.put((short)67, DHCP.class);
73 UDP.decodeMap.put((short)68, DHCP.class);
76 * Get the stored source port
77 * @return short - the sourcePort
79 public short getSourcePort() {
80 return (BitBufferHelper.getShort(fieldValues.get(SRCPORT)));
84 * Get the stored destination port
85 * @return short - the destinationPort
87 public short getDestinationPort() {
88 return (BitBufferHelper.getShort(fieldValues.get(DESTPORT)));
92 * Gets the stored length of UDP header
93 * @return short - the length
95 public short getLength() {
96 return (BitBufferHelper.getShort(fieldValues.get(LENGTH)));
100 * Get the stored checksum value of the UDP header
101 * @return short - the checksum
103 public short getChecksum() {
104 return (BitBufferHelper.getShort(fieldValues.get(CHECKSUM)));
109 * Store the value read from data stream in hdrFieldMap
111 public void setHeaderField(String headerField, byte[] readValue) {
112 /*if (headerField.equals("Protocol")) {
113 payloadClass = decodeMap.get(readValue);
115 hdrFieldsMap.put(headerField, readValue);
119 * Sets the sourcePort value for the current UDP object instance
120 * @param short - the sourcePort to set
123 public UDP setSourcePort(short udpSourcePort) {
124 byte[] sourcePort = BitBufferHelper.toByteArray(udpSourcePort);
125 fieldValues.put(SRCPORT, sourcePort);
130 * Sets the destinationPort value for the current UDP object instance
131 * @param short - the destinationPort to set
134 public UDP setDestinationPort(short udpDestinationPort) {
135 byte[] destinationPort = BitBufferHelper
136 .toByteArray(udpDestinationPort);
137 fieldValues.put(DESTPORT, destinationPort);
142 * Set the UDP header length value for the current UDP object instance
143 * @param short - the length to set
146 public UDP setLength(short udpLength) {
147 byte[] length = BitBufferHelper.toByteArray(udpLength);
148 fieldValues.put(LENGTH, length);
153 * Set the checksum for the current UDP object instance
154 * @param short - the checksum to set
157 public UDP setChecksum(short udpChecksum) {
158 byte[] checksum = BitBufferHelper.toByteArray(udpChecksum);
159 fieldValues.put(CHECKSUM, checksum);