Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 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 | |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 19 | import com.google.common.cache.Cache; |
| 20 | import com.google.common.cache.CacheBuilder; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 21 | import com.google.common.collect.Maps; |
| 22 | import com.google.protobuf.ExtensionRegistry; |
| 23 | import com.google.protobuf.TextFormat; |
| 24 | import org.onosproject.net.pi.model.PiPipeconf; |
| 25 | import org.onosproject.net.pi.model.PiPipeconfId; |
| 26 | import org.slf4j.Logger; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 27 | import p4.config.v1.P4InfoOuterClass.P4Info; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 28 | |
| 29 | import java.io.IOException; |
| 30 | import java.io.InputStream; |
| 31 | import java.io.InputStreamReader; |
| 32 | import java.util.Map; |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 33 | import java.util.concurrent.ExecutionException; |
| 34 | import java.util.concurrent.TimeUnit; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 35 | |
| 36 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT; |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | /** |
| 40 | * Utility class to deal with pipeconfs in the context of P4runtime. |
| 41 | */ |
| 42 | final class PipeconfHelper { |
| 43 | |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 44 | private static final int P4INFO_BROWSER_EXPIRE_TIME_IN_MIN = 10; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 45 | private static final Logger log = getLogger(PipeconfHelper.class); |
| 46 | |
Manjunath Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 47 | private static final Cache<PiPipeconfId, P4InfoBrowser> BROWSERS = CacheBuilder.newBuilder() |
| 48 | .expireAfterAccess(P4INFO_BROWSER_EXPIRE_TIME_IN_MIN, TimeUnit.MINUTES) |
| 49 | .build(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 50 | 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 Vanaraj | 59ad657 | 2017-12-26 11:10:57 +0530 | [diff] [blame] | 92 | 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 Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 105 | } |
| 106 | } |