blob: 85fa2a89baab670cbf251ff40733152053ce3961 [file] [log] [blame]
Carmelo Cascone8d99b172017-07-18 17:26:31 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-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
17package org.onosproject.p4runtime.ctl;
18
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;
25import org.onosproject.net.pi.model.PiPipeconfId;
26import org.slf4j.Logger;
Carmelo Cascone6af4e172018-06-15 16:01:30 +020027import p4.config.v1.P4InfoOuterClass.P4Info;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040028
29import java.io.IOException;
30import java.io.InputStream;
31import java.io.InputStreamReader;
32import java.util.Map;
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +053033import java.util.concurrent.ExecutionException;
34import java.util.concurrent.TimeUnit;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040035
36import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT;
37import static org.slf4j.LoggerFactory.getLogger;
38
39/**
40 * Utility class to deal with pipeconfs in the context of P4runtime.
41 */
42final class PipeconfHelper {
43
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +053044 private static final int P4INFO_BROWSER_EXPIRE_TIME_IN_MIN = 10;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040045 private static final Logger log = getLogger(PipeconfHelper.class);
46
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +053047 private static final Cache<PiPipeconfId, P4InfoBrowser> BROWSERS = CacheBuilder.newBuilder()
48 .expireAfterAccess(P4INFO_BROWSER_EXPIRE_TIME_IN_MIN, TimeUnit.MINUTES)
49 .build();
Carmelo Cascone8d99b172017-07-18 17:26:31 -040050 private static final Map<PiPipeconfId, P4Info> P4INFOS = Maps.newConcurrentMap();
51
52 private PipeconfHelper() {
53 // hide.
54 }
55
56 /**
57 * Extracts and returns a P4Info protobuf message from the given pipeconf. If the pipeconf does not define any
58 * extension of type {@link PiPipeconf.ExtensionType#P4_INFO_TEXT}, returns null;
59 *
60 * @param pipeconf pipeconf
61 * @return P4Info or null
62 */
63 static P4Info getP4Info(PiPipeconf pipeconf) {
64 return P4INFOS.computeIfAbsent(pipeconf.id(), piPipeconfId -> {
65 if (!pipeconf.extension(P4_INFO_TEXT).isPresent()) {
66 log.warn("Missing P4Info extension in pipeconf {}", pipeconf.id());
67 return null;
68 }
69
70 InputStream p4InfoStream = pipeconf.extension(P4_INFO_TEXT).get();
71 P4Info.Builder p4iInfoBuilder = P4Info.newBuilder();
72 try {
73 TextFormat.getParser().merge(new InputStreamReader(p4InfoStream), ExtensionRegistry.getEmptyRegistry(),
74 p4iInfoBuilder);
75 } catch (IOException ex) {
76 log.warn("Unable to parse P4Info of pipeconf {}: {}", pipeconf.id(), ex.getMessage());
77 return null;
78 }
79
80 return p4iInfoBuilder.build();
81 });
82 }
83
84 /**
85 * Returns a P4Info browser for the given pipeconf. If the pipeconf does not define any extension of type
86 * {@link PiPipeconf.ExtensionType#P4_INFO_TEXT}, returns null;
87 *
88 * @param pipeconf pipeconf
89 * @return P4Info browser or null
90 */
91 static P4InfoBrowser getP4InfoBrowser(PiPipeconf pipeconf) {
Manjunath Vanaraj59ad6572017-12-26 11:10:57 +053092 try {
93 return BROWSERS.get(pipeconf.id(), () -> {
94 P4Info p4info = PipeconfHelper.getP4Info(pipeconf);
95 if (p4info == null) {
96 return null;
97 } else {
98 return new P4InfoBrowser(p4info);
99 }
100 });
101 } catch (ExecutionException e) {
102 log.error("Exception while accessing the P4InfoBrowser cache", e);
103 return null;
104 }
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400105 }
106}