blob: 2895b83565ae3316f7ed862bde9ca04fdf896be0 [file] [log] [blame]
Yi Tseng82512da2017-08-16 19:46:36 -07001/*
Carmelo Cascone4c289b72019-01-22 15:30:45 -08002 * Copyright 2019-present Open Networking Foundation
Yi Tseng82512da2017-08-16 19:46:36 -07003 *
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 Cascone4c289b72019-01-22 15:30:45 -080017package org.onosproject.p4runtime.ctl.codec;
Yi Tseng82512da2017-08-16 19:46:36 -070018
19import com.google.protobuf.ByteString;
Daniele Moroc6f2f7f2020-12-18 10:55:57 +010020import org.onosproject.net.pi.model.PiMatchType;
Yi Tseng82512da2017-08-16 19:46:36 -070021
22import static java.lang.String.format;
23
24/**
Carmelo Cascone4c289b72019-01-22 15:30:45 -080025 * Codec utilities.
Yi Tseng82512da2017-08-16 19:46:36 -070026 */
Carmelo Cascone4c289b72019-01-22 15:30:45 -080027final class Utils {
Yi Tseng82512da2017-08-16 19:46:36 -070028
Carmelo Cascone4c289b72019-01-22 15:30:45 -080029 private Utils() {
Yi Tseng82512da2017-08-16 19:46:36 -070030 // Hide default construction
31 }
32
33 static void assertSize(String entityDescr, ByteString value, int bitWidth)
Carmelo Cascone99c59db2019-01-17 15:39:35 -080034 throws CodecException {
Yi Tseng82512da2017-08-16 19:46:36 -070035
36 int byteWidth = (int) Math.ceil((float) bitWidth / 8);
37 if (value.size() != byteWidth) {
Carmelo Cascone99c59db2019-01-17 15:39:35 -080038 throw new CodecException(format(
39 "Wrong size for %s, expected %d bytes, but found %d",
40 entityDescr, byteWidth, value.size()));
Yi Tseng82512da2017-08-16 19:46:36 -070041 }
42 }
43
44 static void assertPrefixLen(String entityDescr, int prefixLength, int bitWidth)
Carmelo Cascone99c59db2019-01-17 15:39:35 -080045 throws CodecException {
Yi Tseng82512da2017-08-16 19:46:36 -070046
47 if (prefixLength > bitWidth) {
Carmelo Cascone99c59db2019-01-17 15:39:35 -080048 throw new CodecException(format(
Yi Tseng82512da2017-08-16 19:46:36 -070049 "wrong prefix length for %s, field size is %d bits, but found one is %d",
50 entityDescr, bitWidth, prefixLength));
51 }
52 }
Daniele Moroc6f2f7f2020-12-18 10:55:57 +010053
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 Tseng82512da2017-08-16 19:46:36 -070060}