Initial opendaylight infrastructure commit!!
[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.apache.commons.lang3.builder.EqualsBuilder;
18 import org.apache.commons.lang3.builder.HashCodeBuilder;
19 import org.opendaylight.controller.sal.utils.EtherTypes;
20
21 /**
22  * Insert a 802.1q (outermost) header action
23  * Execute it multiple times to achieve QinQ
24  *
25  * 802.1q = [TPID(16) + TCI(16)]
26  *                      TCI = [PCP(3) + CFI(1) + VID(12)]
27  *
28  *
29  *
30  */
31 @XmlRootElement
32 @XmlAccessorType(XmlAccessType.NONE)
33
34 public class PushVlan extends Action {
35     private int tag; // TPID - 16 bits
36     private int pcp; // PCP - 3 bits
37     private int cfi; // CFI - 1 bit (drop eligible)
38     private int vlanId; // VID - 12 bits
39     private transient int tci; // TCI = [PCP + CFI + VID] - 16 bits
40     private transient int header; // full 802.1q header [TPID + TCI] - 32 bits
41
42     /* Dummy constructor for JAXB */
43     private PushVlan () {
44     }
45
46     public PushVlan(int tag, int pcp, int cfi, int vlanId) {
47         type = ActionType.PUSH_VLAN;
48         this.tag = tag;
49         this.cfi = cfi;
50         this.pcp = pcp;
51         this.vlanId = vlanId;
52         this.tci = createTci();
53         this.header = createHeader();
54         runChecks();
55     }
56
57     public PushVlan(EtherTypes tag, int pcp, int cfi, int vlanId) {
58         type = ActionType.PUSH_VLAN;
59         this.tag = tag.intValue();
60         this.cfi = cfi;
61         this.pcp = pcp;
62         this.vlanId = vlanId;
63         this.tci = createTci();
64         this.header = createHeader();
65         runChecks();
66     }
67
68     private int createTci() {
69         return (pcp & 0x7) << 13 | (cfi & 0x1) << 12 | (vlanId & 0xfff);
70     }
71
72     private int createHeader() {
73         return (tag & 0xffff) << 16 | (pcp & 0x7) << 13 | (cfi & 0x1) << 12
74                 | (vlanId & 0xfff);
75     }
76
77     private void runChecks() {
78         checkValue(ActionType.SET_DL_TYPE, tag);
79         checkValue(ActionType.SET_VLAN_PCP, pcp);
80         checkValue(ActionType.SET_VLAN_CFI, cfi);
81         checkValue(ActionType.SET_VLAN_ID, vlanId);
82         checkValue(tci);
83
84         // Run action specific check which cannot be run by parent
85         if (tag != EtherTypes.VLANTAGGED.intValue()
86                 && tag != EtherTypes.QINQ.intValue()
87                 && tag != EtherTypes.OLDQINQ.intValue()
88                 && tag != EtherTypes.CISCOQINQ.intValue()) {
89             // pass a value which will tell fail and tell something about the original wrong value
90             checkValue(ActionType.SET_DL_TYPE, 0xBAD << 16 | tag);
91         }
92     }
93
94     /**
95      * Returns the VID portion of the 802.1q header this action will insert
96      * VID - (12 bits)
97      * @return byte[]
98      */
99     public int getVlanId() {
100         return vlanId;
101     }
102
103     /**
104      * Returns the CFI portion of the 802.1q header this action will insert
105      * CFI - (1 bit)
106      * @return
107      */
108     public int getCfi() {
109         return cfi;
110     }
111
112     /**
113      * Returns the vlan PCP portion of the 802.1q header this action will insert
114      * PCP - (3 bits)
115      * @return byte[]
116      */
117     public int getPcp() {
118         return pcp;
119     }
120
121     /**
122      * Returns the TPID portion of the 802.1q header this action will insert
123      * TPID - (16 bits)
124      */
125     public int getTag() {
126         return tag;
127     }
128
129     /**
130      * Returns the TCI portion of the 802.1q header this action will insert
131      * TCI = [PCP + CFI + VID] - (16 bits)
132      * @return
133      */
134     public int getTci() {
135         return tci;
136     }
137
138     /**
139      * Returns the full 802.1q header this action will insert
140      * header = [TPID + TIC] (32 bits)
141      *
142      * @return int
143      */
144     @XmlElement(name="VlanHeader")
145     public int getHeader() {
146         return header;
147     }
148
149     @Override
150     public boolean equals(Object other) {
151         return EqualsBuilder.reflectionEquals(this, other);
152     }
153
154     @Override
155     public int hashCode() {
156         return HashCodeBuilder.reflectionHashCode(this);
157     }
158
159     @Override
160     public String toString() {
161         return type + "[tag = " + tag + ", pcp = " + pcp + ", cfi = " + cfi
162                 + ", vlanId = " + vlanId + "]";
163     }
164
165 }