2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.vpp.util;
19 import com.google.common.base.Preconditions;
20 import com.google.common.primitives.UnsignedBytes;
21 import javax.annotation.Nonnegative;
22 import javax.annotation.Nullable;
25 * Defines vlan tag rewrite config options for VPP.
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
31 public enum TagRewriteOperation {
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}
48 private final int code;
49 private final byte popTags;
51 TagRewriteOperation(final int popTags) {
52 this.code = this.ordinal();
53 this.popTags = UnsignedBytes.checkedCast(popTags);
57 * Returns VPP tag rewrite operation for given number of tags to pop and tags to push.
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
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];
70 * Returns VPP tag rewrite operation for given operation code.
72 * @param code VPP tag rewrite operation code
73 * @return vpp tag rewrite operation for given input parameter
76 public static TagRewriteOperation get(@Nonnegative final int code) {
77 for (TagRewriteOperation operation : TagRewriteOperation.values()) {
78 if (code == operation.code) {
85 public byte getPopTags() {