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;
14 import org.apache.commons.lang3.tuple.ImmutablePair;
15 import org.apache.commons.lang3.tuple.Pair;
16 import org.opendaylight.controller.sal.match.Match;
17 import org.opendaylight.controller.sal.match.MatchType;
20 * Class that represents the IEEE 802.1Q objects
22 public class IEEE8021Q extends Packet {
23 private static final String PCP = "PriorityCodePoint";
24 private static final String CFI = "CanonicalFormatIndicator";
25 private static final String VID = "VlanIdentifier";
26 private static final String ETHT = "EtherType";
28 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
29 private static final long serialVersionUID = 1L;
31 put(PCP, new ImmutablePair<Integer, Integer>(0, 3));
32 put(CFI, new ImmutablePair<Integer, Integer>(3, 1));
33 put(VID, new ImmutablePair<Integer, Integer>(4, 12));
34 put(ETHT, new ImmutablePair<Integer, Integer>(16, 16));
37 private final Map<String, byte[]> fieldValues;
40 * Default constructor that creates and sets the HashMap
44 fieldValues = new HashMap<String, byte[]>();
45 hdrFieldCoordMap = fieldCoordinates;
46 hdrFieldsMap = fieldValues;
50 * Constructor that sets the access level for the packet and creates and
53 public IEEE8021Q(boolean writeAccess) {
55 fieldValues = new HashMap<String, byte[]>();
56 hdrFieldCoordMap = fieldCoordinates;
57 hdrFieldsMap = fieldValues;
62 * Store the value read from data stream in hdrFieldMap
64 public void setHeaderField(String headerField, byte[] readValue) {
65 if (headerField.equals(ETHT)) {
66 payloadClass = Ethernet.etherTypeClassMap.get(BitBufferHelper.getShort(readValue));
68 hdrFieldsMap.put(headerField, readValue);
72 * Gets the priority code point(PCP) stored
74 * @return byte - the PCP
76 public byte getPcp() {
77 return BitBufferHelper.getByte(fieldValues.get(PCP));
81 * Gets the canonical format indicator(CFI) stored
83 * @return byte - the CFI
85 public byte getCfi() {
86 return BitBufferHelper.getByte(fieldValues.get(CFI));
90 * Gets the VLAN identifier(VID) stored
92 * @return short - the VID
94 public short getVid() {
95 return BitBufferHelper.getShort(fieldValues.get(VID));
99 * Gets the etherType stored
101 * @return short - the etherType
103 public short getEtherType() {
104 return BitBufferHelper.getShort(fieldValues.get(ETHT));
108 * Sets the priority code point(PCP) for the current IEEE 802.1Q object
111 * @param byte - the PCP to set
113 public IEEE8021Q setPcp(byte pcp) {
114 byte[] priorityCodePoint = BitBufferHelper.toByteArray(pcp);
115 fieldValues.put(PCP, priorityCodePoint);
120 * Sets the canonical format indicator(CFI) for the current IEEE 802.1Q
123 * @param byte - the CFI to set
125 public IEEE8021Q setCfi(byte cfi) {
126 byte[] canonicalFormatIndicator = BitBufferHelper.toByteArray(cfi);
127 fieldValues.put(CFI, canonicalFormatIndicator);
132 * Sets the VLAN identifier(VID) for the current IEEE 802.1Q instance
134 * @param short - the VID to set
136 public IEEE8021Q setVid(short vid) {
137 byte[] vlanIdentifier = BitBufferHelper.toByteArray(vid);
138 fieldValues.put(VID, vlanIdentifier);
143 * Sets the etherType for the current IEEE 802.1Q object instance
145 * @param short - the etherType to set
147 public IEEE8021Q setEtherType(short etherType) {
148 byte[] ethType = BitBufferHelper.toByteArray(etherType);
149 fieldValues.put(ETHT, ethType);
154 public void populateMatch(Match match) {
155 match.setField(MatchType.DL_VLAN, this.getVid());
156 match.setField(MatchType.DL_VLAN_PR, this.getPcp());
157 match.setField(MatchType.DL_TYPE, this.getEtherType());