HONEYCOMB-58 - Routing Api
[honeycomb.git] / vpp-common / vpp-translate-utils / src / main / java / io / fd / honeycomb / translate / vpp / 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
17 package io.fd.honeycomb.translate.vpp.util;
18
19 import com.google.common.base.Preconditions;
20 import com.google.common.primitives.UnsignedBytes;
21 import javax.annotation.Nonnegative;
22 import javax.annotation.Nullable;
23
24 /**
25  * Defines vlan tag rewrite config options for VPP.
26  * <p/>
27  * TODO HONEYCOMB-184 corresponding enum (defined in l2_vtr.h) should be defined in vpe.api
28  * (does vpp's IDL support enum type definition?)
29  * which would allow to generate this class in jvpp
30  */
31 public enum TagRewriteOperation {
32     disabled(0),
33     push_1(0),
34     push_2(0),
35     pop_1(1),
36     pop_2(2),
37     translate_1_to_1(1),
38     translate_1_to_2(1),
39     translate_2_to_1(2),
40     translate_2_to_2(2);
41
42     private static final int MAX_INDEX = 3;
43     private static TagRewriteOperation[][] translation = new TagRewriteOperation[][]{
44             {disabled, push_1, push_2},
45             {pop_1, translate_1_to_1, translate_1_to_2},
46             {pop_2, translate_2_to_1, translate_2_to_2}
47     };
48     private final int code;
49     private final byte popTags;
50
51     TagRewriteOperation(final int popTags) {
52         this.code = this.ordinal();
53         this.popTags = UnsignedBytes.checkedCast(popTags);
54     }
55
56     /**
57      * Returns VPP tag rewrite operation for given number of tags to pop and tags to push.
58      *
59      * @param toPop  number of tags to pop (0..2)
60      * @param toPush number of tags to push (0..2)
61      * @return vpp tag rewrite operation for given input parameters
62      */
63     public static TagRewriteOperation get(@Nonnegative final int toPop, @Nonnegative final int toPush) {
64         Preconditions.checkElementIndex(toPop, MAX_INDEX, "Illegal number of tags to pop");
65         Preconditions.checkElementIndex(toPush, MAX_INDEX, "Illegal number of tags to push");
66         return translation[toPop][toPush];
67     }
68
69     /**
70      * Returns VPP tag rewrite operation for given operation code.
71      *
72      * @param code VPP tag rewrite operation code
73      * @return vpp tag rewrite operation for given input parameter
74      */
75     @Nullable
76     public static TagRewriteOperation get(@Nonnegative final int code) {
77         for (TagRewriteOperation operation : TagRewriteOperation.values()) {
78             if (code == operation.code) {
79                 return operation;
80             }
81         }
82         return null;
83     }
84
85     public byte getPopTags() {
86         return popTags;
87     }
88 }