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.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;
22 * Class that represents the UDP datagram objects
27 public class UDP extends Packet {
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";
34 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
35 private static final long serialVersionUID = 1L;
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));
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
52 setSourcePort((short) 0);
53 setDestinationPort((short) 0);
54 setChecksum((short) 0);
57 public UDP(boolean 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
65 setSourcePort((short) 0);
66 setDestinationPort((short) 0);
67 setChecksum((short) 0);
70 private Map<String, byte[]> fieldValues;
72 /* public static Map<Short, Class<? extends Packet>> decodeMap;
75 decodeMap = new HashMap<Short, Class<? extends Packet>>();
76 UDP.decodeMap.put((short)67, DHCP.class);
77 UDP.decodeMap.put((short)68, DHCP.class);
80 * Get the stored source port
81 * @return short - the sourcePort
83 public short getSourcePort() {
84 return (BitBufferHelper.getShort(fieldValues.get(SRCPORT)));
88 * Get the stored destination port
89 * @return short - the destinationPort
91 public short getDestinationPort() {
92 return (BitBufferHelper.getShort(fieldValues.get(DESTPORT)));
96 * Gets the stored length of UDP header
97 * @return short - the length
99 public short getLength() {
100 return (BitBufferHelper.getShort(fieldValues.get(LENGTH)));
104 * Get the stored checksum value of the UDP header
105 * @return short - the checksum
107 public short getChecksum() {
108 return (BitBufferHelper.getShort(fieldValues.get(CHECKSUM)));
113 * Store the value read from data stream in hdrFieldMap
115 public void setHeaderField(String headerField, byte[] readValue) {
116 /*if (headerField.equals("Protocol")) {
117 payloadClass = decodeMap.get(readValue);
119 hdrFieldsMap.put(headerField, readValue);
123 * Sets the sourcePort value for the current UDP object instance
124 * @param short - the sourcePort to set
127 public UDP setSourcePort(short udpSourcePort) {
128 byte[] sourcePort = BitBufferHelper.toByteArray(udpSourcePort);
129 fieldValues.put(SRCPORT, sourcePort);
134 * Sets the destinationPort value for the current UDP object instance
135 * @param short - the destinationPort to set
138 public UDP setDestinationPort(short udpDestinationPort) {
139 byte[] destinationPort = BitBufferHelper
140 .toByteArray(udpDestinationPort);
141 fieldValues.put(DESTPORT, destinationPort);
146 * Set the UDP header length value for the current UDP object instance
147 * @param short - the length to set
150 public UDP setLength(short udpLength) {
151 byte[] length = BitBufferHelper.toByteArray(udpLength);
152 fieldValues.put(LENGTH, length);
157 * Set the checksum for the current UDP object instance
158 * @param short - the checksum to set
161 public UDP setChecksum(short udpChecksum) {
162 byte[] checksum = BitBufferHelper.toByteArray(udpChecksum);
163 fieldValues.put(CHECKSUM, checksum);
168 public int hashCode() {
169 return HashCodeBuilder.reflectionHashCode(this);
173 public boolean equals(Object obj) {
174 return EqualsBuilder.reflectionEquals(this, obj);