Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 1 | /* |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 2 | * Copyright 2019-present Open Networking Foundation |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 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 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 17 | package org.onosproject.p4runtime.ctl.codec; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 18 | |
| 19 | import com.google.protobuf.ByteString; |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame^] | 20 | import org.onosproject.net.pi.model.PiMatchType; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 21 | |
| 22 | import static java.lang.String.format; |
| 23 | |
| 24 | /** |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 25 | * Codec utilities. |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 26 | */ |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 27 | final class Utils { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 28 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 29 | private Utils() { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 30 | // Hide default construction |
| 31 | } |
| 32 | |
| 33 | static void assertSize(String entityDescr, ByteString value, int bitWidth) |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 34 | throws CodecException { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 35 | |
| 36 | int byteWidth = (int) Math.ceil((float) bitWidth / 8); |
| 37 | if (value.size() != byteWidth) { |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 38 | throw new CodecException(format( |
| 39 | "Wrong size for %s, expected %d bytes, but found %d", |
| 40 | entityDescr, byteWidth, value.size())); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
| 44 | static void assertPrefixLen(String entityDescr, int prefixLength, int bitWidth) |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 45 | throws CodecException { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 46 | |
| 47 | if (prefixLength > bitWidth) { |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 48 | throw new CodecException(format( |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 49 | "wrong prefix length for %s, field size is %d bits, but found one is %d", |
| 50 | entityDescr, bitWidth, prefixLength)); |
| 51 | } |
| 52 | } |
Daniele Moro | c6f2f7f | 2020-12-18 10:55:57 +0100 | [diff] [blame^] | 53 | |
| 54 | static void sdnStringUnsupported(String entityDescr, PiMatchType matchType) |
| 55 | throws CodecException { |
| 56 | throw new CodecException(format( |
| 57 | "%s is expected to be a sdn_string, but it is unsupported for %s match type", |
| 58 | entityDescr, matchType.name())); |
| 59 | } |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 60 | } |