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