8609207b6b5fcd06b0a471ca9b189832ba99d352
[hc2vpp.git] /
1 package io.fd.honeycomb.v3po.translate.v3po.util;
2
3 import com.google.common.base.Preconditions;
4 import com.google.common.primitives.UnsignedBytes;
5 import javax.annotation.Nonnegative;
6 import javax.annotation.Nullable;
7
8 /**
9  * Defines vlan tag rewrite config options for VPP
10  *
11  * TODO corresponding enum (defined in l2_vtr.h) should be defined in vpe.api
12  * (does vpp's IDL support enum type definition?)
13  * which would allow to generate this class in jvpp
14  */
15 public enum TagRewriteOperation {
16     disabled(0),
17     push_1(0),
18     push_2(0),
19     pop_1(1),
20     pop_2(2),
21     translate_1_to_1(1),
22     translate_1_to_2(1),
23     translate_2_to_1(2),
24     translate_2_to_2(2);
25
26     private final static int MAX_INDEX = 3;
27     private final int code;
28     private final byte popTags;
29
30     TagRewriteOperation(final int popTags) {
31         this.code = this.ordinal();
32         this.popTags = UnsignedBytes.checkedCast(popTags);
33     }
34
35     private static TagRewriteOperation[][] translation = new TagRewriteOperation[][] {
36         {disabled, push_1, push_2},
37         {pop_1, translate_1_to_1, translate_1_to_2},
38         {pop_2, translate_2_to_1, translate_2_to_2}
39     };
40
41     /**
42      * Returns VPP tag rewrite operation for given number of tags to pop and tags to push.
43      * @param toPop number of tags to pop (0..2)
44      * @param toPush number of tags to push (0..2)
45      * @return vpp tag rewrite operation for given input parameters
46      */
47     public static TagRewriteOperation get(@Nonnegative final int toPop, @Nonnegative final int toPush) {
48         Preconditions.checkElementIndex(toPop, MAX_INDEX, "Illegal number of tags to pop");
49         Preconditions.checkElementIndex(toPush, MAX_INDEX, "Illegal number of tags to push");
50         return translation[toPop][toPush];
51     }
52
53     /**
54      * Returns VPP tag rewrite operation for given operation code.
55      * @param code VPP tag rewrite operation code
56      * @return vpp tag rewrite operation for given input parameter
57      */
58     @Nullable
59     public static TagRewriteOperation get(@Nonnegative final int code) {
60         for (TagRewriteOperation operation : TagRewriteOperation.values()) {
61             if (code == operation.code){
62                 return operation;
63             }
64         }
65         return null;
66     }
67
68     public byte getPopTags() {
69         return popTags;
70     }
71 }