blob: 93605e2d87c22418beda4c1f51aace884c76853e [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;
Daniele Moro8d630f12021-06-15 20:53:22 +020031import static org.onosproject.pipelines.fabric.FabricConstants.FABRIC_INGRESS_SPGW_DOWNLINK_PDRS;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080032import static org.slf4j.LoggerFactory.getLogger;
33
34/**
35 * Representation of the capabilities of a given fabric pipeconf.
36 */
37public class FabricCapabilities {
38
39 private final Logger log = getLogger(getClass());
40
41 private final PiPipeconf pipeconf;
42
Carmelo Cascone356ab8b2019-09-25 01:02:53 -070043 public FabricCapabilities(PiPipeconf pipeconf) {
Carmelo Casconeb5324e72018-11-25 02:26:32 -080044 this.pipeconf = checkNotNull(pipeconf);
45 }
46
47 public boolean hasHashedTable() {
48 return pipeconf.pipelineModel()
49 .table(FabricConstants.FABRIC_INGRESS_NEXT_HASHED).isPresent();
50 }
51
52 public Optional<Integer> cpuPort() {
53 // This is probably brittle, but needed to dynamically get the CPU port
54 // for different platforms.
55 if (!pipeconf.extension(CPU_PORT_TXT).isPresent()) {
56 log.warn("Missing {} extension in pipeconf {}", CPU_PORT_TXT, pipeconf.id());
57 return Optional.empty();
58 }
59 try {
60 final InputStream stream = pipeconf.extension(CPU_PORT_TXT).get();
61 final BufferedReader buff = new BufferedReader(
62 new InputStreamReader(stream));
63 final String str = buff.readLine();
64 buff.close();
65 if (str == null) {
66 log.error("Empty CPU port file for {}", pipeconf.id());
67 return Optional.empty();
68 }
69 try {
70 return Optional.of(Integer.parseInt(str));
71 } catch (NumberFormatException e) {
72 log.error("Invalid CPU port for {}: {}", pipeconf.id(), str);
73 return Optional.empty();
74 }
75 } catch (IOException e) {
76 log.error("Unable to read CPU port file of {}: {}",
Carmelo Casconeda15af82019-12-09 22:36:48 -080077 pipeconf.id(), e.getMessage());
Carmelo Casconeb5324e72018-11-25 02:26:32 -080078 return Optional.empty();
79 }
80 }
Daniele Morof51d0c12019-07-30 10:43:10 -070081
82 public boolean supportDoubleVlanTerm() {
83 if (pipeconf.pipelineModel()
Wailok Shumfb7e7872021-06-18 17:30:08 +080084 .table(FabricConstants.FABRIC_INGRESS_PRE_NEXT_NEXT_VLAN).isPresent()) {
85 return pipeconf.pipelineModel().table(FabricConstants.FABRIC_INGRESS_PRE_NEXT_NEXT_VLAN)
86 .get().action(FabricConstants.FABRIC_INGRESS_PRE_NEXT_SET_DOUBLE_VLAN)
Daniele Morof51d0c12019-07-30 10:43:10 -070087 .isPresent();
88 }
89 return false;
90 }
Carmelo Casconeda15af82019-12-09 22:36:48 -080091
92 /**
Daniele Moro8d630f12021-06-15 20:53:22 +020093 * Returns true if the pipeconf supports UPF capabilities, false otherwise.
94 *
95 * @return boolean
96 */
97 public boolean supportUpf() {
98 return pipeconf.pipelineModel()
99 .table(FABRIC_INGRESS_SPGW_DOWNLINK_PDRS)
100 .isPresent();
101 }
102
103 /**
Carmelo Casconeda15af82019-12-09 22:36:48 -0800104 * Returns true if the pipeconf supports BNG user plane capabilities, false
105 * otherwise.
106 *
107 * @return boolean
108 */
109 public boolean supportBng() {
110 return pipeconf.pipelineModel()
111 .counter(FabricConstants.FABRIC_INGRESS_BNG_INGRESS_DOWNSTREAM_C_LINE_RX)
112 .isPresent();
113 }
114
115 /**
116 * Returns the maximum number of BNG lines supported, or 0 if this pipeconf
117 * does not support BNG capabilities.
118 *
119 * @return maximum number of lines supported
120 */
121 public long bngMaxLineCount() {
122 if (!supportBng()) {
123 return 0;
124 }
125 return pipeconf.pipelineModel()
126 .counter(FabricConstants.FABRIC_INGRESS_BNG_INGRESS_DOWNSTREAM_C_LINE_RX)
127 .orElseThrow().size();
128 }
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800129}