Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 17 | package org.onosproject.pipelines.fabric.impl.behaviour; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 18 | |
| 19 | import org.onosproject.net.pi.model.PiPipeconf; |
Carmelo Cascone | 2102bfb | 2020-12-04 16:54:24 -0800 | [diff] [blame] | 20 | import org.onosproject.pipelines.fabric.FabricConstants; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 21 | import org.slf4j.Logger; |
| 22 | |
| 23 | import java.io.BufferedReader; |
| 24 | import java.io.IOException; |
| 25 | import java.io.InputStream; |
| 26 | import java.io.InputStreamReader; |
| 27 | import java.util.Optional; |
| 28 | |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.CPU_PORT_TXT; |
| 31 | import static org.slf4j.LoggerFactory.getLogger; |
| 32 | |
| 33 | /** |
| 34 | * Representation of the capabilities of a given fabric pipeconf. |
| 35 | */ |
| 36 | public class FabricCapabilities { |
| 37 | |
| 38 | private final Logger log = getLogger(getClass()); |
| 39 | |
| 40 | private final PiPipeconf pipeconf; |
| 41 | |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 42 | public FabricCapabilities(PiPipeconf pipeconf) { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 43 | this.pipeconf = checkNotNull(pipeconf); |
| 44 | } |
| 45 | |
| 46 | public boolean hasHashedTable() { |
| 47 | return pipeconf.pipelineModel() |
| 48 | .table(FabricConstants.FABRIC_INGRESS_NEXT_HASHED).isPresent(); |
| 49 | } |
| 50 | |
| 51 | public Optional<Integer> cpuPort() { |
| 52 | // This is probably brittle, but needed to dynamically get the CPU port |
| 53 | // for different platforms. |
| 54 | if (!pipeconf.extension(CPU_PORT_TXT).isPresent()) { |
| 55 | log.warn("Missing {} extension in pipeconf {}", CPU_PORT_TXT, pipeconf.id()); |
| 56 | return Optional.empty(); |
| 57 | } |
| 58 | try { |
| 59 | final InputStream stream = pipeconf.extension(CPU_PORT_TXT).get(); |
| 60 | final BufferedReader buff = new BufferedReader( |
| 61 | new InputStreamReader(stream)); |
| 62 | final String str = buff.readLine(); |
| 63 | buff.close(); |
| 64 | if (str == null) { |
| 65 | log.error("Empty CPU port file for {}", pipeconf.id()); |
| 66 | return Optional.empty(); |
| 67 | } |
| 68 | try { |
| 69 | return Optional.of(Integer.parseInt(str)); |
| 70 | } catch (NumberFormatException e) { |
| 71 | log.error("Invalid CPU port for {}: {}", pipeconf.id(), str); |
| 72 | return Optional.empty(); |
| 73 | } |
| 74 | } catch (IOException e) { |
| 75 | log.error("Unable to read CPU port file of {}: {}", |
Carmelo Cascone | da15af8 | 2019-12-09 22:36:48 -0800 | [diff] [blame] | 76 | pipeconf.id(), e.getMessage()); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 77 | return Optional.empty(); |
| 78 | } |
| 79 | } |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 80 | |
| 81 | public boolean supportDoubleVlanTerm() { |
| 82 | if (pipeconf.pipelineModel() |
| 83 | .table(FabricConstants.FABRIC_INGRESS_NEXT_NEXT_VLAN).isPresent()) { |
| 84 | return pipeconf.pipelineModel().table(FabricConstants.FABRIC_INGRESS_NEXT_NEXT_VLAN) |
| 85 | .get().action(FabricConstants.FABRIC_INGRESS_NEXT_SET_DOUBLE_VLAN) |
| 86 | .isPresent(); |
| 87 | } |
| 88 | return false; |
| 89 | } |
Carmelo Cascone | da15af8 | 2019-12-09 22:36:48 -0800 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * Returns true if the pipeconf supports BNG user plane capabilities, false |
| 93 | * otherwise. |
| 94 | * |
| 95 | * @return boolean |
| 96 | */ |
| 97 | public boolean supportBng() { |
| 98 | return pipeconf.pipelineModel() |
| 99 | .counter(FabricConstants.FABRIC_INGRESS_BNG_INGRESS_DOWNSTREAM_C_LINE_RX) |
| 100 | .isPresent(); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns the maximum number of BNG lines supported, or 0 if this pipeconf |
| 105 | * does not support BNG capabilities. |
| 106 | * |
| 107 | * @return maximum number of lines supported |
| 108 | */ |
| 109 | public long bngMaxLineCount() { |
| 110 | if (!supportBng()) { |
| 111 | return 0; |
| 112 | } |
| 113 | return pipeconf.pipelineModel() |
| 114 | .counter(FabricConstants.FABRIC_INGRESS_BNG_INGRESS_DOWNSTREAM_C_LINE_RX) |
| 115 | .orElseThrow().size(); |
| 116 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 117 | } |