yangshuo | a179c34 | 2017-02-14 22:16:55 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 org.onosproject.nemo.model.object.match.item.definitions; |
| 17 | |
| 18 | import org.onosproject.nemo.model.common.MatchItemName; |
| 19 | |
| 20 | import static com.google.common.collect.ImmutableMap.builder; |
| 21 | |
| 22 | import java.util.Map; |
| 23 | |
| 24 | import com.google.common.collect.ImmutableMap.Builder; |
| 25 | |
| 26 | public interface MatchItemDefinition { |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * Returns the name of an match item. |
| 31 | * |
| 32 | * @return the match item name |
| 33 | */ |
| 34 | MatchItemName getMatchItemName(); |
| 35 | |
| 36 | /** |
| 37 | * Returns the type of a parameter value. |
| 38 | * |
| 39 | * @return the type |
| 40 | */ |
| 41 | MatchItemValueType getMatchItemValueType(); |
| 42 | |
| 43 | /** |
| 44 | * Returns the primary key of YANG type list. |
| 45 | * |
| 46 | * @return the match item definition key |
| 47 | */ |
| 48 | MatchItemDefinitionKey getKey(); |
| 49 | |
| 50 | /** |
| 51 | * Types of match item value. |
| 52 | */ |
| 53 | enum MatchItemValueType { |
| 54 | /** A string-valued match item. */ |
| 55 | STRING(0), |
| 56 | |
| 57 | /** An integer-valued match item. */ |
| 58 | INT(1), |
| 59 | |
| 60 | /** An integer-range match item. */ |
| 61 | RANGE(2); |
| 62 | |
| 63 | private static final Map<Integer, MatchItemValueType> VALUE_MAP; |
| 64 | |
| 65 | int code; |
| 66 | |
| 67 | MatchItemValueType(int value) { |
| 68 | this.code = value; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns the code of this action definition. |
| 73 | * |
| 74 | * @return the code |
| 75 | */ |
| 76 | public int codeValue() { |
| 77 | return code; |
| 78 | } |
| 79 | |
| 80 | static { |
| 81 | Builder<Integer, MatchItemValueType> b = builder(); |
| 82 | for (MatchItemValueType enumItem : MatchItemValueType.values()) { |
| 83 | b.put(enumItem.code, enumItem); |
| 84 | } |
| 85 | VALUE_MAP = b.build(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns the type from the given match item code. |
| 90 | * |
| 91 | * @param code the given code |
| 92 | * @return corresponding type constant |
| 93 | * @throws IllegalArgumentException if there is no mapping |
| 94 | */ |
| 95 | public static MatchItemValueType forValue(int code) { |
| 96 | MatchItemValueType type = VALUE_MAP.get(code); |
| 97 | if (type == null) { |
| 98 | throw new IllegalArgumentException( |
| 99 | "No MatchItemType for value: " + code); |
| 100 | } |
| 101 | return type; |
| 102 | } |
| 103 | } |
| 104 | } |