2 * Copyright (c) 2013 - 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.vpnservice.mdsalutil.packet;
11 import java.util.HashMap;
12 import java.util.LinkedHashMap;
15 import org.apache.commons.lang3.tuple.ImmutablePair;
16 import org.apache.commons.lang3.tuple.Pair;
17 import org.opendaylight.controller.liblldp.BitBufferHelper;
18 import org.opendaylight.controller.liblldp.Packet;
21 * Class that represents the UDP datagram objects
24 public class UDP extends Packet {
26 private static final String SRCPORT = "SourcePort";
27 private static final String DESTPORT = "DestinationPort";
28 private static final String LENGTH = "Length";
29 private static final String CHECKSUM = "Checksum";
31 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
32 private static final long serialVersionUID = 1L;
34 put(SRCPORT, new ImmutablePair<Integer, Integer>(0, 16));
35 put(DESTPORT, new ImmutablePair<Integer, Integer>(16, 16));
36 put(LENGTH, new ImmutablePair<Integer, Integer>(32, 16));
37 put(CHECKSUM, new ImmutablePair<Integer, Integer>(48, 16));
43 fieldValues = new HashMap<String, byte[]>();
44 hdrFieldCoordMap = fieldCoordinates;
45 hdrFieldsMap = fieldValues;
46 /* Setting all remaining header field values to
47 * default value of 0. These maybe changed as needed
49 setSourcePort((short) 0);
50 setDestinationPort((short) 0);
51 setChecksum((short) 0);
54 public UDP(boolean writeAccess) {
56 fieldValues = new HashMap<String, byte[]>();
57 hdrFieldCoordMap = fieldCoordinates;
58 hdrFieldsMap = fieldValues;
59 /* Setting all remaining header field values to
60 * default value of 0. These maybe changed as needed
62 setSourcePort((short) 0);
63 setDestinationPort((short) 0);
64 setChecksum((short) 0);
67 private final Map<String, byte[]> fieldValues;
69 /* public static Map<Short, Class<? extends Packet>> decodeMap;
72 decodeMap = new HashMap<Short, Class<? extends Packet>>();
73 UDP.decodeMap.put((short)67, DHCP.class);
74 UDP.decodeMap.put((short)68, DHCP.class);
77 * Get the stored source port
78 * @return short - the sourcePort
80 public short getSourcePort() {
81 return (BitBufferHelper.getShort(fieldValues.get(SRCPORT)));
85 * Get the stored destination port
86 * @return short - the destinationPort
88 public short getDestinationPort() {
89 return (BitBufferHelper.getShort(fieldValues.get(DESTPORT)));
93 * Gets the stored length of UDP header
94 * @return short - the length
96 public short getLength() {
97 return (BitBufferHelper.getShort(fieldValues.get(LENGTH)));
101 * Get the stored checksum value of the UDP header
102 * @return short - the checksum
104 public short getChecksum() {
105 return (BitBufferHelper.getShort(fieldValues.get(CHECKSUM)));
110 * Store the value read from data stream in hdrFieldMap
112 public void setHeaderField(String headerField, byte[] readValue) {
113 hdrFieldsMap.put(headerField, readValue);
117 * Sets the sourcePort value for the current UDP object instance
118 * @param udpSourcePort short source port to set
121 public UDP setSourcePort(short udpSourcePort) {
122 byte[] sourcePort = BitBufferHelper.toByteArray(udpSourcePort);
123 fieldValues.put(SRCPORT, sourcePort);
128 * Sets the destinationPort value for the current UDP object instance
129 * @param udpDestinationPort short destination port to set
132 public UDP setDestinationPort(short udpDestinationPort) {
133 byte[] destinationPort = BitBufferHelper
134 .toByteArray(udpDestinationPort);
135 fieldValues.put(DESTPORT, destinationPort);
140 * Set the UDP header length value for the current UDP object instance
141 * @param udpLength - short - the length to set
144 public UDP setLength(short udpLength) {
145 byte[] length = BitBufferHelper.toByteArray(udpLength);
146 fieldValues.put(LENGTH, length);
151 * Set the checksum for the current UDP object instance
152 * @param udpChecksum - short - the checksum to set
155 public UDP setChecksum(short udpChecksum) {
156 byte[] checksum = BitBufferHelper.toByteArray(udpChecksum);
157 fieldValues.put(CHECKSUM, checksum);