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