Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.p4runtime.ctl; |
| 18 | |
| 19 | import com.google.protobuf.ByteString; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame^] | 20 | import p4.v1.P4RuntimeOuterClass; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 21 | |
| 22 | import static java.lang.String.format; |
| 23 | |
| 24 | /** |
| 25 | * Utilities for P4 runtime control. |
| 26 | */ |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame^] | 27 | final class P4RuntimeUtils { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 28 | |
| 29 | private P4RuntimeUtils() { |
| 30 | // Hide default construction |
| 31 | } |
| 32 | |
| 33 | static void assertSize(String entityDescr, ByteString value, int bitWidth) |
| 34 | throws EncodeException { |
| 35 | |
| 36 | int byteWidth = (int) Math.ceil((float) bitWidth / 8); |
| 37 | if (value.size() != byteWidth) { |
| 38 | throw new EncodeException(format("Wrong size for %s, expected %d bytes, but found %d", |
| 39 | entityDescr, byteWidth, value.size())); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | static void assertPrefixLen(String entityDescr, int prefixLength, int bitWidth) |
| 44 | throws EncodeException { |
| 45 | |
| 46 | if (prefixLength > bitWidth) { |
| 47 | throw new EncodeException(format( |
| 48 | "wrong prefix length for %s, field size is %d bits, but found one is %d", |
| 49 | entityDescr, bitWidth, prefixLength)); |
| 50 | } |
| 51 | } |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame] | 52 | |
| 53 | static P4RuntimeOuterClass.Index indexMsg(long index) { |
| 54 | return P4RuntimeOuterClass.Index.newBuilder().setIndex(index).build(); |
| 55 | } |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 56 | } |