Fix NPE in ICMP.computeChecksum()
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / packet / IEEE8021Q.java
1 /*
2  * Copyright (c) 2013 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.controller.sal.packet;
10
11 import java.util.HashMap;
12 import java.util.LinkedHashMap;
13 import java.util.Map;
14 import org.apache.commons.lang3.tuple.ImmutablePair;
15 import org.apache.commons.lang3.tuple.Pair;
16
17 /**
18  * Class that represents the IEEE 802.1Q objects
19  */
20 public class IEEE8021Q extends Packet {
21     private static final String PCP = "PriorityCodePoint";
22     private static final String CFI = "CanonicalFormatIndicator";
23     private static final String VID = "VlanIdentifier";
24     private static final String ETHT = "EtherType";
25
26     private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
27         private static final long serialVersionUID = 1L;
28         {
29             put(PCP, new ImmutablePair<Integer, Integer>(0, 3));
30             put(CFI, new ImmutablePair<Integer, Integer>(3, 1));
31             put(VID, new ImmutablePair<Integer, Integer>(4, 12));
32             put(ETHT, new ImmutablePair<Integer, Integer>(16, 16));
33         }
34     };
35     private final Map<String, byte[]> fieldValues;
36
37     /**
38      * Default constructor that creates and sets the HashMap
39      */
40     public IEEE8021Q() {
41         super();
42         fieldValues = new HashMap<String, byte[]>();
43         hdrFieldCoordMap = fieldCoordinates;
44         hdrFieldsMap = fieldValues;
45     }
46
47     /**
48      * Constructor that sets the access level for the packet and creates and
49      * sets the HashMap
50      */
51     public IEEE8021Q(boolean writeAccess) {
52         super(writeAccess);
53         fieldValues = new HashMap<String, byte[]>();
54         hdrFieldCoordMap = fieldCoordinates;
55         hdrFieldsMap = fieldValues;
56     }
57
58     @Override
59     /**
60      * Store the value read from data stream in hdrFieldMap
61      */
62     public void setHeaderField(String headerField, byte[] readValue) {
63         if (headerField.equals(ETHT)) {
64             payloadClass = Ethernet.etherTypeClassMap.get(BitBufferHelper.getShort(readValue));
65         }
66         hdrFieldsMap.put(headerField, readValue);
67     }
68
69     /**
70      * Gets the priority code point(PCP) stored
71      *
72      * @return byte - the PCP
73      */
74     public byte getPcp() {
75         return BitBufferHelper.getByte(fieldValues.get(PCP));
76     }
77
78     /**
79      * Gets the canonical format indicator(CFI) stored
80      *
81      * @return byte - the CFI
82      */
83     public byte getCfi() {
84         return BitBufferHelper.getByte(fieldValues.get(CFI));
85     }
86
87     /**
88      * Gets the VLAN identifier(VID) stored
89      *
90      * @return short - the VID
91      */
92     public short getVid() {
93         return BitBufferHelper.getShort(fieldValues.get(VID));
94     }
95
96     /**
97      * Gets the etherType stored
98      *
99      * @return short - the etherType
100      */
101     public short getEtherType() {
102         return BitBufferHelper.getShort(fieldValues.get(ETHT));
103     }
104
105     /**
106      * Sets the priority code point(PCP) for the current IEEE 802.1Q object
107      * instance
108      *
109      * @param byte - the PCP to set
110      */
111     public IEEE8021Q setPcp(byte pcp) {
112         byte[] priorityCodePoint = BitBufferHelper.toByteArray(pcp);
113         fieldValues.put(PCP, priorityCodePoint);
114         return this;
115     }
116
117     /**
118      * Sets the canonical format indicator(CFI) for the current IEEE 802.1Q
119      * object instance
120      *
121      * @param byte - the CFI to set
122      */
123     public IEEE8021Q setCfi(byte cfi) {
124         byte[] canonicalFormatIndicator = BitBufferHelper.toByteArray(cfi);
125         fieldValues.put(CFI, canonicalFormatIndicator);
126         return this;
127     }
128
129     /**
130      * Sets the VLAN identifier(VID) for the current IEEE 802.1Q instance
131      *
132      * @param short - the VID to set
133      */
134     public IEEE8021Q setVid(short vid) {
135         byte[] vlanIdentifier = BitBufferHelper.toByteArray(vid);
136         fieldValues.put(VID, vlanIdentifier);
137         return this;
138     }
139
140     /**
141      * Sets the etherType for the current IEEE 802.1Q object instance
142      *
143      * @param short - the etherType to set
144      */
145     public IEEE8021Q setEtherType(short etherType) {
146         byte[] ethType = BitBufferHelper.toByteArray(etherType);
147         fieldValues.put(ETHT, ethType);
148         return this;
149     }
150
151 }