2 * Copyright (c) 2013-2014 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;
15 import org.apache.commons.lang3.tuple.ImmutablePair;
16 import org.apache.commons.lang3.tuple.Pair;
17 import org.opendaylight.controller.sal.match.Match;
18 import org.opendaylight.controller.sal.match.MatchType;
21 * Class that represents the IEEE 802.1Q objects
23 public class IEEE8021Q extends Packet {
24 private static final String PCP = "PriorityCodePoint";
25 private static final String CFI = "CanonicalFormatIndicator";
26 private static final String VID = "VlanIdentifier";
27 private static final String ETHT = "EtherType";
29 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
30 private static final long serialVersionUID = 1L;
32 put(PCP, new ImmutablePair<Integer, Integer>(0, 3));
33 put(CFI, new ImmutablePair<Integer, Integer>(3, 1));
34 put(VID, new ImmutablePair<Integer, Integer>(4, 12));
35 put(ETHT, new ImmutablePair<Integer, Integer>(16, 16));
38 private final Map<String, byte[]> fieldValues;
41 * Default constructor that creates and sets the HashMap
45 fieldValues = new HashMap<String, byte[]>();
46 hdrFieldCoordMap = fieldCoordinates;
47 hdrFieldsMap = fieldValues;
51 * Constructor that sets the access level for the packet and creates and
54 public IEEE8021Q(boolean writeAccess) {
56 fieldValues = new HashMap<String, byte[]>();
57 hdrFieldCoordMap = fieldCoordinates;
58 hdrFieldsMap = fieldValues;
63 * Store the value read from data stream in hdrFieldMap
65 public void setHeaderField(String headerField, byte[] readValue) {
66 if (headerField.equals(ETHT)) {
67 payloadClass = Ethernet.etherTypeClassMap.get(BitBufferHelper.getShort(readValue));
69 hdrFieldsMap.put(headerField, readValue);
73 * Gets the priority code point(PCP) stored
75 * @return byte - the PCP
77 public byte getPcp() {
78 return BitBufferHelper.getByte(fieldValues.get(PCP));
82 * Gets the canonical format indicator(CFI) stored
84 * @return byte - the CFI
86 public byte getCfi() {
87 return BitBufferHelper.getByte(fieldValues.get(CFI));
91 * Gets the VLAN identifier(VID) stored
93 * @return short - the VID
95 public short getVid() {
96 return BitBufferHelper.getShort(fieldValues.get(VID));
100 * Gets the etherType stored
102 * @return short - the etherType
104 public short getEtherType() {
105 return BitBufferHelper.getShort(fieldValues.get(ETHT));
109 * Sets the priority code point(PCP) for the current IEEE 802.1Q object
112 * @param byte - the PCP to set
114 public IEEE8021Q setPcp(byte pcp) {
115 byte[] priorityCodePoint = BitBufferHelper.toByteArray(pcp);
116 fieldValues.put(PCP, priorityCodePoint);
121 * Sets the canonical format indicator(CFI) for the current IEEE 802.1Q
124 * @param byte - the CFI to set
126 public IEEE8021Q setCfi(byte cfi) {
127 byte[] canonicalFormatIndicator = BitBufferHelper.toByteArray(cfi);
128 fieldValues.put(CFI, canonicalFormatIndicator);
133 * Sets the VLAN identifier(VID) for the current IEEE 802.1Q instance
135 * @param short - the VID to set
137 public IEEE8021Q setVid(short vid) {
138 byte[] vlanIdentifier = BitBufferHelper.toByteArray(vid);
139 fieldValues.put(VID, vlanIdentifier);
144 * Sets the etherType for the current IEEE 802.1Q object instance
146 * @param short - the etherType to set
148 public IEEE8021Q setEtherType(short etherType) {
149 byte[] ethType = BitBufferHelper.toByteArray(etherType);
150 fieldValues.put(ETHT, ethType);
155 public void populateMatch(Match match) {
156 match.setField(MatchType.DL_VLAN, this.getVid());
157 match.setField(MatchType.DL_VLAN_PR, this.getPcp());
158 match.setField(MatchType.DL_TYPE, this.getEtherType());
162 * Gets the header size in bits
163 * @return The .1Q header size in bits
166 public int getHeaderSize() {