blob: 5dafe2bd46936c3455b4712f7109916f2c378ff1 [file] [log] [blame]
Yi Tseng82512da2017-08-16 19:46:36 -07001/*
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
17package org.onosproject.p4runtime.ctl;
18
19import com.google.protobuf.ByteString;
Carmelo Cascone6af4e172018-06-15 16:01:30 +020020import p4.v1.P4RuntimeOuterClass;
Yi Tseng82512da2017-08-16 19:46:36 -070021
22import static java.lang.String.format;
23
24/**
25 * Utilities for P4 runtime control.
26 */
Carmelo Cascone6af4e172018-06-15 16:01:30 +020027final class P4RuntimeUtils {
Yi Tseng82512da2017-08-16 19:46:36 -070028
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 Cascone81929aa2018-04-07 01:38:55 -070052
53 static P4RuntimeOuterClass.Index indexMsg(long index) {
54 return P4RuntimeOuterClass.Index.newBuilder().setIndex(index).build();
55 }
Yi Tseng82512da2017-08-16 19:46:36 -070056}