Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.protobuf.ExtensionRegistry; |
| 21 | import com.google.protobuf.TextFormat; |
| 22 | import org.onosproject.net.pi.model.PiPipeconf; |
| 23 | import org.onosproject.net.pi.model.PiPipeconfId; |
| 24 | import org.slf4j.Logger; |
| 25 | import p4.config.P4InfoOuterClass.P4Info; |
| 26 | |
| 27 | import java.io.IOException; |
| 28 | import java.io.InputStream; |
| 29 | import java.io.InputStreamReader; |
| 30 | import java.util.Map; |
| 31 | |
| 32 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT; |
| 33 | import static org.slf4j.LoggerFactory.getLogger; |
| 34 | |
| 35 | /** |
| 36 | * Utility class to deal with pipeconfs in the context of P4runtime. |
| 37 | */ |
| 38 | final class PipeconfHelper { |
| 39 | |
| 40 | private static final Logger log = getLogger(PipeconfHelper.class); |
| 41 | |
| 42 | // TODO: consider implementing this via a cache that expires unused browsers. |
| 43 | private static final Map<PiPipeconfId, P4InfoBrowser> BROWSERS = Maps.newConcurrentMap(); |
| 44 | private static final Map<PiPipeconfId, P4Info> P4INFOS = Maps.newConcurrentMap(); |
| 45 | |
| 46 | private PipeconfHelper() { |
| 47 | // hide. |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Extracts and returns a P4Info protobuf message from the given pipeconf. If the pipeconf does not define any |
| 52 | * extension of type {@link PiPipeconf.ExtensionType#P4_INFO_TEXT}, returns null; |
| 53 | * |
| 54 | * @param pipeconf pipeconf |
| 55 | * @return P4Info or null |
| 56 | */ |
| 57 | static P4Info getP4Info(PiPipeconf pipeconf) { |
| 58 | return P4INFOS.computeIfAbsent(pipeconf.id(), piPipeconfId -> { |
| 59 | if (!pipeconf.extension(P4_INFO_TEXT).isPresent()) { |
| 60 | log.warn("Missing P4Info extension in pipeconf {}", pipeconf.id()); |
| 61 | return null; |
| 62 | } |
| 63 | |
| 64 | InputStream p4InfoStream = pipeconf.extension(P4_INFO_TEXT).get(); |
| 65 | P4Info.Builder p4iInfoBuilder = P4Info.newBuilder(); |
| 66 | try { |
| 67 | TextFormat.getParser().merge(new InputStreamReader(p4InfoStream), ExtensionRegistry.getEmptyRegistry(), |
| 68 | p4iInfoBuilder); |
| 69 | } catch (IOException ex) { |
| 70 | log.warn("Unable to parse P4Info of pipeconf {}: {}", pipeconf.id(), ex.getMessage()); |
| 71 | return null; |
| 72 | } |
| 73 | |
| 74 | return p4iInfoBuilder.build(); |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns a P4Info browser for the given pipeconf. If the pipeconf does not define any extension of type |
| 80 | * {@link PiPipeconf.ExtensionType#P4_INFO_TEXT}, returns null; |
| 81 | * |
| 82 | * @param pipeconf pipeconf |
| 83 | * @return P4Info browser or null |
| 84 | */ |
| 85 | static P4InfoBrowser getP4InfoBrowser(PiPipeconf pipeconf) { |
| 86 | return BROWSERS.computeIfAbsent(pipeconf.id(), (pipeconfId) -> { |
| 87 | P4Info p4info = PipeconfHelper.getP4Info(pipeconf); |
| 88 | if (p4info == null) { |
| 89 | return null; |
| 90 | } else { |
| 91 | return new P4InfoBrowser(p4info); |
| 92 | } |
| 93 | }); |
| 94 | } |
| 95 | } |