blob: 2eb947d0a53f1c85b4f8676508d66ad000ecd67d [file] [log] [blame]
Carmelo Casconeb5324e72018-11-25 02:26:32 -08001/*
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 Cascone356ab8b2019-09-25 01:02:53 -070017package org.onosproject.pipelines.fabric.impl.behaviour;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080018
19import org.onosproject.net.pi.model.PiPipeconf;
Carmelo Cascone2102bfb2020-12-04 16:54:24 -080020import org.onosproject.pipelines.fabric.FabricConstants;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080021import org.slf4j.Logger;
22
23import java.io.BufferedReader;
24import java.io.IOException;
25import java.io.InputStream;
26import java.io.InputStreamReader;
27import java.util.Optional;
28
29import static com.google.common.base.Preconditions.checkNotNull;
30import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.CPU_PORT_TXT;
31import static org.slf4j.LoggerFactory.getLogger;
32
33/**
34 * Representation of the capabilities of a given fabric pipeconf.
35 */
36public class FabricCapabilities {
37
38 private final Logger log = getLogger(getClass());
39
40 private final PiPipeconf pipeconf;
41
Carmelo Cascone356ab8b2019-09-25 01:02:53 -070042 public FabricCapabilities(PiPipeconf pipeconf) {
Carmelo Casconeb5324e72018-11-25 02:26:32 -080043 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 Casconeda15af82019-12-09 22:36:48 -080076 pipeconf.id(), e.getMessage());
Carmelo Casconeb5324e72018-11-25 02:26:32 -080077 return Optional.empty();
78 }
79 }
Daniele Morof51d0c12019-07-30 10:43:10 -070080
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 Casconeda15af82019-12-09 22:36:48 -080090
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 Casconeb5324e72018-11-25 02:26:32 -0800117}