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; |
| 20 | |
| 21 | import static java.lang.String.format; |
| 22 | |
| 23 | /** |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 24 | * Codec utilities. |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 25 | */ |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 26 | final class Utils { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 27 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 28 | private Utils() { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 29 | // Hide default construction |
| 30 | } |
| 31 | |
| 32 | static void assertSize(String entityDescr, ByteString value, int bitWidth) |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 33 | throws CodecException { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 34 | |
| 35 | int byteWidth = (int) Math.ceil((float) bitWidth / 8); |
| 36 | if (value.size() != byteWidth) { |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 37 | throw new CodecException(format( |
| 38 | "Wrong size for %s, expected %d bytes, but found %d", |
| 39 | entityDescr, byteWidth, value.size())); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
| 43 | static void assertPrefixLen(String entityDescr, int prefixLength, int bitWidth) |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 44 | throws CodecException { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 45 | |
| 46 | if (prefixLength > bitWidth) { |
Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 47 | throw new CodecException(format( |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 48 | "wrong prefix length for %s, field size is %d bits, but found one is %d", |
| 49 | entityDescr, bitWidth, prefixLength)); |
| 50 | } |
| 51 | } |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 52 | } |