2 * Copyright (c) 2013 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.controller.sal.packet;
11 import java.util.HashMap;
12 import java.util.LinkedHashMap;
14 import org.apache.commons.lang3.tuple.ImmutablePair;
15 import org.apache.commons.lang3.tuple.Pair;
18 * Class that represents the IEEE 802.1Q objects
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";
26 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
27 private static final long serialVersionUID = 1L;
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));
35 private final Map<String, byte[]> fieldValues;
38 * Default constructor that creates and sets the HashMap
42 fieldValues = new HashMap<String, byte[]>();
43 hdrFieldCoordMap = fieldCoordinates;
44 hdrFieldsMap = fieldValues;
48 * Constructor that sets the access level for the packet and creates and
51 public IEEE8021Q(boolean writeAccess) {
53 fieldValues = new HashMap<String, byte[]>();
54 hdrFieldCoordMap = fieldCoordinates;
55 hdrFieldsMap = fieldValues;
60 * Store the value read from data stream in hdrFieldMap
62 public void setHeaderField(String headerField, byte[] readValue) {
63 if (headerField.equals(ETHT)) {
64 payloadClass = Ethernet.etherTypeClassMap.get(BitBufferHelper.getShort(readValue));
66 hdrFieldsMap.put(headerField, readValue);
70 * Gets the priority code point(PCP) stored
72 * @return byte - the PCP
74 public byte getPcp() {
75 return BitBufferHelper.getByte(fieldValues.get(PCP));
79 * Gets the canonical format indicator(CFI) stored
81 * @return byte - the CFI
83 public byte getCfi() {
84 return BitBufferHelper.getByte(fieldValues.get(CFI));
88 * Gets the VLAN identifier(VID) stored
90 * @return short - the VID
92 public short getVid() {
93 return BitBufferHelper.getShort(fieldValues.get(VID));
97 * Gets the etherType stored
99 * @return short - the etherType
101 public short getEtherType() {
102 return BitBufferHelper.getShort(fieldValues.get(ETHT));
106 * Sets the priority code point(PCP) for the current IEEE 802.1Q object
109 * @param byte - the PCP to set
111 public IEEE8021Q setPcp(byte pcp) {
112 byte[] priorityCodePoint = BitBufferHelper.toByteArray(pcp);
113 fieldValues.put(PCP, priorityCodePoint);
118 * Sets the canonical format indicator(CFI) for the current IEEE 802.1Q
121 * @param byte - the CFI to set
123 public IEEE8021Q setCfi(byte cfi) {
124 byte[] canonicalFormatIndicator = BitBufferHelper.toByteArray(cfi);
125 fieldValues.put(CFI, canonicalFormatIndicator);
130 * Sets the VLAN identifier(VID) for the current IEEE 802.1Q instance
132 * @param short - the VID to set
134 public IEEE8021Q setVid(short vid) {
135 byte[] vlanIdentifier = BitBufferHelper.toByteArray(vid);
136 fieldValues.put(VID, vlanIdentifier);
141 * Sets the etherType for the current IEEE 802.1Q object instance
143 * @param short - the etherType to set
145 public IEEE8021Q setEtherType(short etherType) {
146 byte[] ethType = BitBufferHelper.toByteArray(etherType);
147 fieldValues.put(ETHT, ethType);