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
24 public class IEEE8021Q extends Packet {
25 private static final String PCP = "PriorityCodePoint";
26 private static final String CFI = "CanonicalFormatIndicator";
27 private static final String VID = "VlanIdentifier";
28 private static final String ETHT = "EtherType";
30 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
31 private static final long serialVersionUID = 1L;
33 put(PCP, new ImmutablePair<Integer, Integer>(0, 3));
34 put(CFI, new ImmutablePair<Integer, Integer>(3, 1));
35 put(VID, new ImmutablePair<Integer, Integer>(4, 12));
36 put(ETHT, new ImmutablePair<Integer, Integer>(16, 16));
39 private final Map<String, byte[]> fieldValues;
42 * Default constructor that creates and sets the HashMap
46 fieldValues = new HashMap<String, byte[]>();
47 hdrFieldCoordMap = fieldCoordinates;
48 hdrFieldsMap = fieldValues;
52 * Constructor that sets the access level for the packet and creates and
55 public IEEE8021Q(boolean writeAccess) {
57 fieldValues = new HashMap<String, byte[]>();
58 hdrFieldCoordMap = fieldCoordinates;
59 hdrFieldsMap = fieldValues;
64 * Store the value read from data stream in hdrFieldMap
66 public void setHeaderField(String headerField, byte[] readValue) {
67 if (headerField.equals(ETHT)) {
68 payloadClass = Ethernet.etherTypeClassMap.get(BitBufferHelper.getShort(readValue));
70 hdrFieldsMap.put(headerField, readValue);
74 * Gets the priority code point(PCP) stored
76 * @return byte - the PCP
78 public byte getPcp() {
79 return BitBufferHelper.getByte(fieldValues.get(PCP));
83 * Gets the canonical format indicator(CFI) stored
85 * @return byte - the CFI
87 public byte getCfi() {
88 return BitBufferHelper.getByte(fieldValues.get(CFI));
92 * Gets the VLAN identifier(VID) stored
94 * @return short - the VID
96 public short getVid() {
97 return BitBufferHelper.getShort(fieldValues.get(VID));
101 * Gets the etherType stored
103 * @return short - the etherType
105 public short getEtherType() {
106 return BitBufferHelper.getShort(fieldValues.get(ETHT));
110 * Sets the priority code point(PCP) for the current IEEE 802.1Q object
113 * @param byte - the PCP to set
115 public IEEE8021Q setPcp(byte pcp) {
116 byte[] priorityCodePoint = BitBufferHelper.toByteArray(pcp);
117 fieldValues.put(PCP, priorityCodePoint);
122 * Sets the canonical format indicator(CFI) for the current IEEE 802.1Q
125 * @param byte - the CFI to set
127 public IEEE8021Q setCfi(byte cfi) {
128 byte[] canonicalFormatIndicator = BitBufferHelper.toByteArray(cfi);
129 fieldValues.put(CFI, canonicalFormatIndicator);
134 * Sets the VLAN identifier(VID) for the current IEEE 802.1Q instance
136 * @param short - the VID to set
138 public IEEE8021Q setVid(short vid) {
139 byte[] vlanIdentifier = BitBufferHelper.toByteArray(vid);
140 fieldValues.put(VID, vlanIdentifier);
145 * Sets the etherType for the current IEEE 802.1Q object instance
147 * @param short - the etherType to set
149 public IEEE8021Q setEtherType(short etherType) {
150 byte[] ethType = BitBufferHelper.toByteArray(etherType);
151 fieldValues.put(ETHT, ethType);
156 public void populateMatch(Match match) {
157 match.setField(MatchType.DL_VLAN, this.getVid());
158 match.setField(MatchType.DL_VLAN_PR, this.getPcp());
159 match.setField(MatchType.DL_TYPE, this.getEtherType());
163 * Gets the header size in bits
164 * @return The .1Q header size in bits
167 public int getHeaderSize() {