Imported vpnservice as a subtree
[netvirt.git] / vpnservice / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / vpnservice / mdsalutil / packet / UDP.java
1 /*
2  * Copyright (c) 2013 - 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.vpnservice.mdsalutil.packet;
10
11 import java.util.HashMap;
12 import java.util.LinkedHashMap;
13 import java.util.Map;
14
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;
19
20 /**
21  * Class that represents the UDP datagram objects
22  */
23
24 public class UDP extends Packet {
25
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";
30
31     private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
32         private static final long serialVersionUID = 1L;
33         {
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));
38         }
39     };
40
41     public UDP() {
42         super();
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
48          */
49         setSourcePort((short) 0);
50         setDestinationPort((short) 0);
51         setChecksum((short) 0);
52     }
53
54     public UDP(boolean writeAccess) {
55         super(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
61          */
62         setSourcePort((short) 0);
63         setDestinationPort((short) 0);
64         setChecksum((short) 0);
65     }
66
67     private final Map<String, byte[]> fieldValues;
68
69     /* public static Map<Short, Class<? extends Packet>> decodeMap;
70
71       static {
72           decodeMap = new HashMap<Short, Class<? extends Packet>>();
73           UDP.decodeMap.put((short)67, DHCP.class);
74           UDP.decodeMap.put((short)68, DHCP.class);
75       }*/
76     /**
77      * Get the stored source port
78      * @return short - the sourcePort
79      */
80     public short getSourcePort() {
81         return (BitBufferHelper.getShort(fieldValues.get(SRCPORT)));
82     }
83
84     /**
85      * Get the stored destination port
86      * @return short - the destinationPort
87      */
88     public short getDestinationPort() {
89         return (BitBufferHelper.getShort(fieldValues.get(DESTPORT)));
90     }
91
92     /**
93      * Gets the stored length of UDP header
94      * @return short - the length
95      */
96     public short getLength() {
97         return (BitBufferHelper.getShort(fieldValues.get(LENGTH)));
98     }
99
100     /**
101      * Get the stored checksum value of the UDP header
102      * @return short - the checksum
103      */
104     public short getChecksum() {
105         return (BitBufferHelper.getShort(fieldValues.get(CHECKSUM)));
106     }
107
108     @Override
109     /**
110      * Store the value read from data stream in hdrFieldMap
111      */
112     public void setHeaderField(String headerField, byte[] readValue) {
113         hdrFieldsMap.put(headerField, readValue);
114     }
115
116     /**
117      * Sets the sourcePort value for the current UDP object instance
118      * @param udpSourcePort short source port to set
119      * @return UDP
120      */
121     public UDP setSourcePort(short udpSourcePort) {
122         byte[] sourcePort = BitBufferHelper.toByteArray(udpSourcePort);
123         fieldValues.put(SRCPORT, sourcePort);
124         return this;
125     }
126
127     /**
128      * Sets the destinationPort value for the current UDP object instance
129      * @param udpDestinationPort short destination port to set
130      * @return UDP
131      */
132     public UDP setDestinationPort(short udpDestinationPort) {
133         byte[] destinationPort = BitBufferHelper
134                 .toByteArray(udpDestinationPort);
135         fieldValues.put(DESTPORT, destinationPort);
136         return this;
137     }
138
139     /**
140      * Set the UDP header length value for the current UDP object instance
141      * @param udpLength - short - the length to set
142      * @return UDP
143      */
144     public UDP setLength(short udpLength) {
145         byte[] length = BitBufferHelper.toByteArray(udpLength);
146         fieldValues.put(LENGTH, length);
147         return this;
148     }
149
150     /**
151      * Set the checksum for the current UDP object instance
152      * @param udpChecksum - short - the checksum to set
153      * @return UDP
154      */
155     public UDP setChecksum(short udpChecksum) {
156         byte[] checksum = BitBufferHelper.toByteArray(udpChecksum);
157         fieldValues.put(CHECKSUM, checksum);
158         return this;
159     }
160
161 }