HONEYCOMB-106 - Support for generic cache management
[honeycomb.git] / v3po / vpp-translate-utils / src / main / java / io / fd / honeycomb / v3po / translate / v3po / util / TagRewriteOperation.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package io.fd.honeycomb.v3po.translate.v3po.util;
17
18 import com.google.common.base.Preconditions;
19 import com.google.common.primitives.UnsignedBytes;
20 import javax.annotation.Nonnegative;
21 import javax.annotation.Nullable;
22
23 /**
24  * Defines vlan tag rewrite config options for VPP
25  *
26  * TODO corresponding enum (defined in l2_vtr.h) should be defined in vpe.api
27  * (does vpp's IDL support enum type definition?)
28  * which would allow to generate this class in jvpp
29  */
30 public enum TagRewriteOperation {
31     disabled(0),
32     push_1(0),
33     push_2(0),
34     pop_1(1),
35     pop_2(2),
36     translate_1_to_1(1),
37     translate_1_to_2(1),
38     translate_2_to_1(2),
39     translate_2_to_2(2);
40
41     private final static int MAX_INDEX = 3;
42     private final int code;
43     private final byte popTags;
44
45     TagRewriteOperation(final int popTags) {
46         this.code = this.ordinal();
47         this.popTags = UnsignedBytes.checkedCast(popTags);
48     }
49
50     private static TagRewriteOperation[][] translation = new TagRewriteOperation[][] {
51         {disabled, push_1, push_2},
52         {pop_1, translate_1_to_1, translate_1_to_2},
53         {pop_2, translate_2_to_1, translate_2_to_2}
54     };
55
56     /**
57      * Returns VPP tag rewrite operation for given number of tags to pop and tags to push.
58      * @param toPop number of tags to pop (0..2)
59      * @param toPush number of tags to push (0..2)
60      * @return vpp tag rewrite operation for given input parameters
61      */
62     public static TagRewriteOperation get(@Nonnegative final int toPop, @Nonnegative final int toPush) {
63         Preconditions.checkElementIndex(toPop, MAX_INDEX, "Illegal number of tags to pop");
64         Preconditions.checkElementIndex(toPush, MAX_INDEX, "Illegal number of tags to push");
65         return translation[toPop][toPush];
66     }
67
68     /**
69      * Returns VPP tag rewrite operation for given operation code.
70      * @param code VPP tag rewrite operation code
71      * @return vpp tag rewrite operation for given input parameter
72      */
73     @Nullable
74     public static TagRewriteOperation get(@Nonnegative final int code) {
75         for (TagRewriteOperation operation : TagRewriteOperation.values()) {
76             if (code == operation.code){
77                 return operation;
78             }
79         }
80         return null;
81     }
82
83     public byte getPopTags() {
84         return popTags;
85     }
86 }