| /* |
| * Copyright 2018-present Open Networking Foundation |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package org.onosproject.pipelines.fabric; |
| |
| import org.onosproject.net.pi.model.PiPipeconf; |
| import org.slf4j.Logger; |
| |
| import java.io.BufferedReader; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.io.InputStreamReader; |
| import java.util.Optional; |
| |
| import static com.google.common.base.Preconditions.checkNotNull; |
| import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.CPU_PORT_TXT; |
| import static org.slf4j.LoggerFactory.getLogger; |
| |
| /** |
| * Representation of the capabilities of a given fabric pipeconf. |
| */ |
| public class FabricCapabilities { |
| |
| private final Logger log = getLogger(getClass()); |
| |
| private final PiPipeconf pipeconf; |
| |
| FabricCapabilities(PiPipeconf pipeconf) { |
| this.pipeconf = checkNotNull(pipeconf); |
| } |
| |
| public boolean hasHashedTable() { |
| return pipeconf.pipelineModel() |
| .table(FabricConstants.FABRIC_INGRESS_NEXT_HASHED).isPresent(); |
| } |
| |
| public Optional<Integer> cpuPort() { |
| // This is probably brittle, but needed to dynamically get the CPU port |
| // for different platforms. |
| if (!pipeconf.extension(CPU_PORT_TXT).isPresent()) { |
| log.warn("Missing {} extension in pipeconf {}", CPU_PORT_TXT, pipeconf.id()); |
| return Optional.empty(); |
| } |
| try { |
| final InputStream stream = pipeconf.extension(CPU_PORT_TXT).get(); |
| final BufferedReader buff = new BufferedReader( |
| new InputStreamReader(stream)); |
| final String str = buff.readLine(); |
| buff.close(); |
| if (str == null) { |
| log.error("Empty CPU port file for {}", pipeconf.id()); |
| return Optional.empty(); |
| } |
| try { |
| return Optional.of(Integer.parseInt(str)); |
| } catch (NumberFormatException e) { |
| log.error("Invalid CPU port for {}: {}", pipeconf.id(), str); |
| return Optional.empty(); |
| } |
| } catch (IOException e) { |
| log.error("Unable to read CPU port file of {}: {}", |
| pipeconf.id(), e.getMessage()); |
| return Optional.empty(); |
| } |
| } |
| } |