BUG 2302 : odl-clustering-test-app should not be part of the odl-restconf-all feature set
[controller.git] / opendaylight / adsal / sal / api / src / main / java / org / opendaylight / controller / sal / packet / IEEE8021Q.java
1 /*
2  * Copyright (c) 2013-2014 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
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;
19
20 /**
21  * Class that represents the IEEE 802.1Q objects
22  */
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";
28
29     private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
30         private static final long serialVersionUID = 1L;
31         {
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));
36         }
37     };
38     private final Map<String, byte[]> fieldValues;
39
40     /**
41      * Default constructor that creates and sets the HashMap
42      */
43     public IEEE8021Q() {
44         super();
45         fieldValues = new HashMap<String, byte[]>();
46         hdrFieldCoordMap = fieldCoordinates;
47         hdrFieldsMap = fieldValues;
48     }
49
50     /**
51      * Constructor that sets the access level for the packet and creates and
52      * sets the HashMap
53      */
54     public IEEE8021Q(boolean writeAccess) {
55         super(writeAccess);
56         fieldValues = new HashMap<String, byte[]>();
57         hdrFieldCoordMap = fieldCoordinates;
58         hdrFieldsMap = fieldValues;
59     }
60
61     @Override
62     /**
63      * Store the value read from data stream in hdrFieldMap
64      */
65     public void setHeaderField(String headerField, byte[] readValue) {
66         if (headerField.equals(ETHT)) {
67             payloadClass = Ethernet.etherTypeClassMap.get(BitBufferHelper.getShort(readValue));
68         }
69         hdrFieldsMap.put(headerField, readValue);
70     }
71
72     /**
73      * Gets the priority code point(PCP) stored
74      *
75      * @return byte - the PCP
76      */
77     public byte getPcp() {
78         return BitBufferHelper.getByte(fieldValues.get(PCP));
79     }
80
81     /**
82      * Gets the canonical format indicator(CFI) stored
83      *
84      * @return byte - the CFI
85      */
86     public byte getCfi() {
87         return BitBufferHelper.getByte(fieldValues.get(CFI));
88     }
89
90     /**
91      * Gets the VLAN identifier(VID) stored
92      *
93      * @return short - the VID
94      */
95     public short getVid() {
96         return BitBufferHelper.getShort(fieldValues.get(VID));
97     }
98
99     /**
100      * Gets the etherType stored
101      *
102      * @return short - the etherType
103      */
104     public short getEtherType() {
105         return BitBufferHelper.getShort(fieldValues.get(ETHT));
106     }
107
108     /**
109      * Sets the priority code point(PCP) for the current IEEE 802.1Q object
110      * instance
111      *
112      * @param byte - the PCP to set
113      */
114     public IEEE8021Q setPcp(byte pcp) {
115         byte[] priorityCodePoint = BitBufferHelper.toByteArray(pcp);
116         fieldValues.put(PCP, priorityCodePoint);
117         return this;
118     }
119
120     /**
121      * Sets the canonical format indicator(CFI) for the current IEEE 802.1Q
122      * object instance
123      *
124      * @param byte - the CFI to set
125      */
126     public IEEE8021Q setCfi(byte cfi) {
127         byte[] canonicalFormatIndicator = BitBufferHelper.toByteArray(cfi);
128         fieldValues.put(CFI, canonicalFormatIndicator);
129         return this;
130     }
131
132     /**
133      * Sets the VLAN identifier(VID) for the current IEEE 802.1Q instance
134      *
135      * @param short - the VID to set
136      */
137     public IEEE8021Q setVid(short vid) {
138         byte[] vlanIdentifier = BitBufferHelper.toByteArray(vid);
139         fieldValues.put(VID, vlanIdentifier);
140         return this;
141     }
142
143     /**
144      * Sets the etherType for the current IEEE 802.1Q object instance
145      *
146      * @param short - the etherType to set
147      */
148     public IEEE8021Q setEtherType(short etherType) {
149         byte[] ethType = BitBufferHelper.toByteArray(etherType);
150         fieldValues.put(ETHT, ethType);
151         return this;
152     }
153
154     @Override
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());
159     }
160
161     /**
162      * Gets the header size in bits
163      * @return The .1Q header size in bits
164      */
165     @Override
166     public int getHeaderSize() {
167         return 32;
168     }
169
170 }