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 | |
| 17 | package org.onosproject.pipelines.fabric; |
| 18 | |
| 19 | import org.onosproject.net.pi.model.PiPipeconf; |
| 20 | import org.slf4j.Logger; |
| 21 | |
| 22 | import java.io.BufferedReader; |
| 23 | import java.io.IOException; |
| 24 | import java.io.InputStream; |
| 25 | import java.io.InputStreamReader; |
| 26 | import java.util.Optional; |
| 27 | |
| 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.CPU_PORT_TXT; |
| 30 | import static org.slf4j.LoggerFactory.getLogger; |
| 31 | |
| 32 | /** |
| 33 | * Representation of the capabilities of a given fabric pipeconf. |
| 34 | */ |
| 35 | public class FabricCapabilities { |
| 36 | |
| 37 | private final Logger log = getLogger(getClass()); |
| 38 | |
| 39 | private final PiPipeconf pipeconf; |
| 40 | |
| 41 | FabricCapabilities(PiPipeconf pipeconf) { |
| 42 | 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 {}: {}", |
| 75 | pipeconf.id(), e.getMessage()); |
| 76 | return Optional.empty(); |
| 77 | } |
| 78 | } |
| 79 | } |