3 * Copyright (c) 2013-2014 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;
18 import org.opendaylight.controller.sal.match.Match;
19 import org.opendaylight.controller.sal.match.MatchType;
22 * Class that represents the UDP datagram objects
25 public class UDP extends Packet {
27 private static final String SRCPORT = "SourcePort";
28 private static final String DESTPORT = "DestinationPort";
29 private static final String LENGTH = "Length";
30 private static final String CHECKSUM = "Checksum";
32 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
33 private static final long serialVersionUID = 1L;
35 put(SRCPORT, new ImmutablePair<Integer, Integer>(0, 16));
36 put(DESTPORT, new ImmutablePair<Integer, Integer>(16, 16));
37 put(LENGTH, new ImmutablePair<Integer, Integer>(32, 16));
38 put(CHECKSUM, new ImmutablePair<Integer, Integer>(48, 16));
44 fieldValues = new HashMap<String, byte[]>();
45 hdrFieldCoordMap = fieldCoordinates;
46 hdrFieldsMap = fieldValues;
47 /* Setting all remaining header field values to
48 * default value of 0. These maybe changed as needed
50 setSourcePort((short) 0);
51 setDestinationPort((short) 0);
52 setChecksum((short) 0);
55 public UDP(boolean writeAccess) {
57 fieldValues = new HashMap<String, byte[]>();
58 hdrFieldCoordMap = fieldCoordinates;
59 hdrFieldsMap = fieldValues;
60 /* Setting all remaining header field values to
61 * default value of 0. These maybe changed as needed
63 setSourcePort((short) 0);
64 setDestinationPort((short) 0);
65 setChecksum((short) 0);
68 private final Map<String, byte[]> fieldValues;
70 /* public static Map<Short, Class<? extends Packet>> decodeMap;
73 decodeMap = new HashMap<Short, Class<? extends Packet>>();
74 UDP.decodeMap.put((short)67, DHCP.class);
75 UDP.decodeMap.put((short)68, DHCP.class);
78 * Get the stored source port
79 * @return short - the sourcePort
81 public short getSourcePort() {
82 return (BitBufferHelper.getShort(fieldValues.get(SRCPORT)));
86 * Get the stored destination port
87 * @return short - the destinationPort
89 public short getDestinationPort() {
90 return (BitBufferHelper.getShort(fieldValues.get(DESTPORT)));
94 * Gets the stored length of UDP header
95 * @return short - the length
97 public short getLength() {
98 return (BitBufferHelper.getShort(fieldValues.get(LENGTH)));
102 * Get the stored checksum value of the UDP header
103 * @return short - the checksum
105 public short getChecksum() {
106 return (BitBufferHelper.getShort(fieldValues.get(CHECKSUM)));
111 * Store the value read from data stream in hdrFieldMap
113 public void setHeaderField(String headerField, byte[] readValue) {
114 hdrFieldsMap.put(headerField, readValue);
118 * Sets the sourcePort value for the current UDP object instance
119 * @param short - the sourcePort to set
122 public UDP setSourcePort(short udpSourcePort) {
123 byte[] sourcePort = BitBufferHelper.toByteArray(udpSourcePort);
124 fieldValues.put(SRCPORT, sourcePort);
129 * Sets the destinationPort value for the current UDP object instance
130 * @param short - the destinationPort to set
133 public UDP setDestinationPort(short udpDestinationPort) {
134 byte[] destinationPort = BitBufferHelper
135 .toByteArray(udpDestinationPort);
136 fieldValues.put(DESTPORT, destinationPort);
141 * Set the UDP header length value for the current UDP object instance
142 * @param short - the length to set
145 public UDP setLength(short udpLength) {
146 byte[] length = BitBufferHelper.toByteArray(udpLength);
147 fieldValues.put(LENGTH, length);
152 * Set the checksum for the current UDP object instance
153 * @param short - the checksum to set
156 public UDP setChecksum(short udpChecksum) {
157 byte[] checksum = BitBufferHelper.toByteArray(udpChecksum);
158 fieldValues.put(CHECKSUM, checksum);
163 public void populateMatch(Match match) {
164 match.setField(MatchType.TP_SRC, this.getSourcePort());
165 match.setField(MatchType.TP_DST, this.getDestinationPort());