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