blob: f5afd417d3864eb33bf32d3cf53618bcd9ee16ff [file] [log] [blame]
Carmelo Cascone8d99b172017-07-18 17:26:31 -04001/*
Carmelo Cascone4c289b72019-01-22 15:30:45 -08002 * Copyright 2019-present Open Networking Foundation
Carmelo Cascone8d99b172017-07-18 17:26:31 -04003 *
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.utils;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040018
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +053019import com.google.common.cache.Cache;
20import com.google.common.cache.CacheBuilder;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040021import com.google.common.collect.Maps;
22import com.google.protobuf.ExtensionRegistry;
23import com.google.protobuf.TextFormat;
24import org.onosproject.net.pi.model.PiPipeconf;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040025import org.slf4j.Logger;
Carmelo Cascone6af4e172018-06-15 16:01:30 +020026import p4.config.v1.P4InfoOuterClass.P4Info;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040027
28import java.io.IOException;
29import java.io.InputStream;
30import java.io.InputStreamReader;
31import java.util.Map;
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +053032import java.util.concurrent.ExecutionException;
33import java.util.concurrent.TimeUnit;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040034
35import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT;
36import static org.slf4j.LoggerFactory.getLogger;
37
38/**
Carmelo Cascone4c289b72019-01-22 15:30:45 -080039 * Utility class to deal with pipeconfs in the context of P4Runtime.
Carmelo Cascone8d99b172017-07-18 17:26:31 -040040 */
Carmelo Cascone4c289b72019-01-22 15:30:45 -080041public final class PipeconfHelper {
Carmelo Cascone8d99b172017-07-18 17:26:31 -040042
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +053043 private static final int P4INFO_BROWSER_EXPIRE_TIME_IN_MIN = 10;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040044 private static final Logger log = getLogger(PipeconfHelper.class);
45
Carmelo Casconed51a5552019-04-13 01:22:25 -070046 private static final Cache<Long, P4InfoBrowser> BROWSERS = CacheBuilder.newBuilder()
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +053047 .expireAfterAccess(P4INFO_BROWSER_EXPIRE_TIME_IN_MIN, TimeUnit.MINUTES)
48 .build();
Carmelo Casconed51a5552019-04-13 01:22:25 -070049 private static final Map<Long, P4Info> P4INFOS = Maps.newConcurrentMap();
Carmelo Cascone8d99b172017-07-18 17:26:31 -040050
51 private PipeconfHelper() {
52 // hide.
53 }
54
55 /**
56 * Extracts and returns a P4Info protobuf message from the given pipeconf. If the pipeconf does not define any
57 * extension of type {@link PiPipeconf.ExtensionType#P4_INFO_TEXT}, returns null;
58 *
59 * @param pipeconf pipeconf
60 * @return P4Info or null
61 */
Carmelo Cascone4c289b72019-01-22 15:30:45 -080062 public static P4Info getP4Info(PiPipeconf pipeconf) {
Carmelo Casconed51a5552019-04-13 01:22:25 -070063 return P4INFOS.computeIfAbsent(pipeconf.fingerprint(), piPipeconfId -> {
Carmelo Cascone8d99b172017-07-18 17:26:31 -040064 if (!pipeconf.extension(P4_INFO_TEXT).isPresent()) {
65 log.warn("Missing P4Info extension in pipeconf {}", pipeconf.id());
66 return null;
67 }
68
69 InputStream p4InfoStream = pipeconf.extension(P4_INFO_TEXT).get();
70 P4Info.Builder p4iInfoBuilder = P4Info.newBuilder();
71 try {
72 TextFormat.getParser().merge(new InputStreamReader(p4InfoStream), ExtensionRegistry.getEmptyRegistry(),
73 p4iInfoBuilder);
74 } catch (IOException ex) {
75 log.warn("Unable to parse P4Info of pipeconf {}: {}", pipeconf.id(), ex.getMessage());
76 return null;
77 }
78
79 return p4iInfoBuilder.build();
80 });
81 }
82
83 /**
84 * Returns a P4Info browser for the given pipeconf. If the pipeconf does not define any extension of type
85 * {@link PiPipeconf.ExtensionType#P4_INFO_TEXT}, returns null;
86 *
87 * @param pipeconf pipeconf
88 * @return P4Info browser or null
89 */
Carmelo Cascone4c289b72019-01-22 15:30:45 -080090 public static P4InfoBrowser getP4InfoBrowser(PiPipeconf pipeconf) {
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +053091 try {
Carmelo Casconed51a5552019-04-13 01:22:25 -070092 return BROWSERS.get(pipeconf.fingerprint(), () -> {
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +053093 P4Info p4info = PipeconfHelper.getP4Info(pipeconf);
94 if (p4info == null) {
95 return null;
96 } else {
97 return new P4InfoBrowser(p4info);
98 }
99 });
100 } catch (ExecutionException e) {
101 log.error("Exception while accessing the P4InfoBrowser cache", e);
102 return null;
103 }
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400104 }
105}