blob: 07986f9efa658a30fa423485e9fe272e7ab7177a [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
17package org.onosproject.pipelines.fabric;
18
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
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}