blob: 3540819553488b79fb211fcd824a46f4e65de10c [file] [log] [blame]
Daniele Morod900fe42021-02-11 16:12:57 +01001/*
2 * Copyright 2021-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.model;
18
19import p4.config.v1.P4InfoOuterClass;
20
21/**
22 * Provides utility methods for P4Info annotations.
23 */
24public final class P4InfoAnnotationUtils {
25
26 public static final String ONE_SHOT_ONLY_ANNOTATION = "oneshot";
27 public static final String MAX_GROUP_SIZE_ANNOTATION = "max_group_size";
28
29 private P4InfoAnnotationUtils() {
30 }
31
32 /**
33 * Gets the annotation value if available in the given P4Info preamble.
34 * Supports annotation in the form @my_annotation(value).
35 *
36 * @param name Annotation name
37 * @param preamble preamble of the P4Info object
38 * @return The annotation value if present, null otherwise
39 */
40 public static String getAnnotationValue(String name, P4InfoOuterClass.Preamble preamble) {
41 return preamble.getAnnotationsList().stream()
42 .filter(a -> a.startsWith("@" + name))
43 // e.g. @my_annotation(value)
44 .map(a -> a.substring(name.length() + 2, a.length() - 1))
45 .findFirst()
46 .orElse(null);
47 }
48
49 /**
50 * Checks if the given annotation name is present in the given P4Info preamble.
51 * Supports annotation in the form @my_annotation* (i.e., @oneshot, @max_group_size(10)).
52 *
53 * @param name Annotation name
54 * @param preamble preamble of the P4Info object
55 * @return True if the annotation is available, False otherwise.
56 */
57 public static boolean isAnnotationPresent(String name, P4InfoOuterClass.Preamble preamble) {
58 return preamble.getAnnotationsList().stream()
59 .anyMatch(a -> a.startsWith("@" + name));
60 }
61}