Checkstyle enforcer
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / action / PushVlan.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.sal.action;
11
12 import javax.xml.bind.annotation.XmlAccessType;
13 import javax.xml.bind.annotation.XmlAccessorType;
14 import javax.xml.bind.annotation.XmlElement;
15 import javax.xml.bind.annotation.XmlRootElement;
16
17 import org.opendaylight.controller.sal.utils.EtherTypes;
18
19 /**
20  * Insert a 802.1q (outermost) header action
21  * Execute it multiple times to achieve QinQ
22  *
23  * 802.1q = [TPID(16) + TCI(16)]
24  *                      TCI = [PCP(3) + CFI(1) + VID(12)]
25  *
26  *
27  *
28  */
29 @XmlRootElement
30 @XmlAccessorType(XmlAccessType.NONE)
31
32 public class PushVlan extends Action {
33     private int tag; // TPID - 16 bits
34     private int pcp; // PCP - 3 bits
35     private int cfi; // CFI - 1 bit (drop eligible)
36     private int vlanId; // VID - 12 bits
37     private transient int tci; // TCI = [PCP + CFI + VID] - 16 bits
38     private transient int header; // full 802.1q header [TPID + TCI] - 32 bits
39
40     /* Dummy constructor for JAXB */
41     private PushVlan () {
42     }
43
44     public PushVlan(int tag, int pcp, int cfi, int vlanId) {
45         type = ActionType.PUSH_VLAN;
46         this.tag = tag;
47         this.cfi = cfi;
48         this.pcp = pcp;
49         this.vlanId = vlanId;
50         this.tci = createTci();
51         this.header = createHeader();
52         runChecks();
53     }
54
55     public PushVlan(EtherTypes tag, int pcp, int cfi, int vlanId) {
56         type = ActionType.PUSH_VLAN;
57         this.tag = tag.intValue();
58         this.cfi = cfi;
59         this.pcp = pcp;
60         this.vlanId = vlanId;
61         this.tci = createTci();
62         this.header = createHeader();
63         runChecks();
64     }
65
66     private int createTci() {
67         return (pcp & 0x7) << 13 | (cfi & 0x1) << 12 | (vlanId & 0xfff);
68     }
69
70     private int createHeader() {
71         return (tag & 0xffff) << 16 | (pcp & 0x7) << 13 | (cfi & 0x1) << 12
72                 | (vlanId & 0xfff);
73     }
74
75     private void runChecks() {
76         checkValue(ActionType.SET_DL_TYPE, tag);
77         checkValue(ActionType.SET_VLAN_PCP, pcp);
78         checkValue(ActionType.SET_VLAN_CFI, cfi);
79         checkValue(ActionType.SET_VLAN_ID, vlanId);
80         checkValue(tci);
81
82         // Run action specific check which cannot be run by parent
83         if (tag != EtherTypes.VLANTAGGED.intValue()
84                 && tag != EtherTypes.QINQ.intValue()
85                 && tag != EtherTypes.OLDQINQ.intValue()
86                 && tag != EtherTypes.CISCOQINQ.intValue()) {
87             // pass a value which will tell fail and tell something about the original wrong value
88             checkValue(ActionType.SET_DL_TYPE, 0xBAD << 16 | tag);
89         }
90     }
91
92     /**
93      * Returns the VID portion of the 802.1q header this action will insert
94      * VID - (12 bits)
95      * @return byte[]
96      */
97     public int getVlanId() {
98         return vlanId;
99     }
100
101     /**
102      * Returns the CFI portion of the 802.1q header this action will insert
103      * CFI - (1 bit)
104      * @return
105      */
106     public int getCfi() {
107         return cfi;
108     }
109
110     /**
111      * Returns the vlan PCP portion of the 802.1q header this action will insert
112      * PCP - (3 bits)
113      * @return byte[]
114      */
115     public int getPcp() {
116         return pcp;
117     }
118
119     /**
120      * Returns the TPID portion of the 802.1q header this action will insert
121      * TPID - (16 bits)
122      */
123     public int getTag() {
124         return tag;
125     }
126
127     /**
128      * Returns the TCI portion of the 802.1q header this action will insert
129      * TCI = [PCP + CFI + VID] - (16 bits)
130      * @return
131      */
132     public int getTci() {
133         return tci;
134     }
135
136     /**
137      * Returns the full 802.1q header this action will insert
138      * header = [TPID + TIC] (32 bits)
139      *
140      * @return int
141      */
142     @XmlElement(name="VlanHeader")
143     public int getHeader() {
144         return header;
145     }
146
147     @Override
148     public boolean equals(Object obj) {
149         if (this == obj)
150             return true;
151         if (!super.equals(obj))
152             return false;
153         if (getClass() != obj.getClass())
154             return false;
155         PushVlan other = (PushVlan) obj;
156         if (cfi != other.cfi)
157             return false;
158         if (pcp != other.pcp)
159             return false;
160         if (tag != other.tag)
161             return false;
162         if (vlanId != other.vlanId)
163             return false;
164         return true;
165     }
166
167     @Override
168     public int hashCode() {
169         final int prime = 31;
170         int result = super.hashCode();
171         result = prime * result + cfi;
172         result = prime * result + pcp;
173         result = prime * result + tag;
174         result = prime * result + vlanId;
175         return result;
176     }
177
178     @Override
179     public String toString() {
180         return type + "[tag = " + tag + ", pcp = " + pcp + ", cfi = " + cfi
181                 + ", vlanId = " + vlanId + "]";
182     }
183
184 }