Georgios Katsikas | 13ccba6 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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.drivers.server; |
| 18 | |
| 19 | import org.onosproject.net.DeviceId; |
| 20 | import org.onosproject.net.behaviour.TableStatisticsDiscovery; |
| 21 | import org.onosproject.net.flow.DefaultTableStatisticsEntry; |
| 22 | import org.onosproject.net.flow.IndexTableId; |
| 23 | import org.onosproject.net.flow.TableStatisticsEntry; |
| 24 | import org.onosproject.protocol.rest.RestSBDevice; |
| 25 | |
| 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | import com.fasterxml.jackson.databind.JsonNode; |
| 29 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 30 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 31 | import com.google.common.collect.Lists; |
| 32 | import com.google.common.collect.ImmutableList; |
| 33 | |
| 34 | import java.io.InputStream; |
| 35 | import java.io.IOException; |
| 36 | import java.util.Collections; |
| 37 | import java.util.List; |
| 38 | import java.util.Map; |
| 39 | import javax.ws.rs.ProcessingException; |
| 40 | |
| 41 | import static com.google.common.base.Preconditions.checkArgument; |
| 42 | import static com.google.common.base.Preconditions.checkNotNull; |
| 43 | import static org.onosproject.drivers.server.Constants.JSON; |
Georgios Katsikas | 13ccba6 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 44 | import static org.onosproject.drivers.server.Constants.MSG_DEVICE_ID_NULL; |
Georgios Katsikas | 1a64c3a | 2020-06-03 17:30:43 +0200 | [diff] [blame^] | 45 | import static org.onosproject.drivers.server.Constants.MSG_DEVICE_NULL; |
Georgios Katsikas | 13ccba6 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 46 | import static org.onosproject.drivers.server.Constants.MSG_NIC_TABLE_COUNTER_NEGATIVE; |
Georgios Katsikas | 1a64c3a | 2020-06-03 17:30:43 +0200 | [diff] [blame^] | 47 | import static org.onosproject.drivers.server.Constants.MSG_NIC_TABLE_INDEX_NEGATIVE; |
| 48 | import static org.onosproject.drivers.server.Constants.MSG_NIC_TABLE_SIZE_NEGATIVE; |
Georgios Katsikas | 13ccba6 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 49 | import static org.onosproject.drivers.server.Constants.PARAM_ID; |
| 50 | import static org.onosproject.drivers.server.Constants.PARAM_NICS; |
| 51 | import static org.onosproject.drivers.server.Constants.PARAM_NIC_TABLE; |
| 52 | import static org.onosproject.drivers.server.Constants.PARAM_NIC_TABLE_ACTIVE_ENTRIES; |
Georgios Katsikas | 1a64c3a | 2020-06-03 17:30:43 +0200 | [diff] [blame^] | 53 | import static org.onosproject.drivers.server.Constants.PARAM_NIC_TABLE_MAX_SIZE; |
Georgios Katsikas | 13ccba6 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 54 | import static org.onosproject.drivers.server.Constants.PARAM_NIC_TABLE_PKTS_LOOKED_UP; |
| 55 | import static org.onosproject.drivers.server.Constants.PARAM_NIC_TABLE_PKTS_MATCHED; |
Georgios Katsikas | 13ccba6 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 56 | import static org.onosproject.drivers.server.Constants.URL_RULE_TABLE_STATS; |
| 57 | import static org.slf4j.LoggerFactory.getLogger; |
| 58 | |
| 59 | /** |
| 60 | * Implementation of table statistics discovery for server devices. |
| 61 | */ |
| 62 | public class ServerTableStatisticsDiscovery |
| 63 | extends BasicServerDriver |
| 64 | implements TableStatisticsDiscovery { |
| 65 | |
| 66 | private final Logger log = getLogger(getClass()); |
| 67 | |
| 68 | public ServerTableStatisticsDiscovery() { |
| 69 | super(); |
| 70 | log.debug("Started"); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public List<TableStatisticsEntry> getTableStatistics() { |
| 75 | // Retrieve the device ID from the handler |
| 76 | DeviceId deviceId = super.getDeviceId(); |
| 77 | checkNotNull(deviceId, MSG_DEVICE_ID_NULL); |
| 78 | |
| 79 | // Get the device |
| 80 | RestSBDevice device = super.getDevice(deviceId); |
| 81 | checkNotNull(device, MSG_DEVICE_NULL); |
| 82 | |
| 83 | // Hit the path that provides the server's NIC table statistics |
| 84 | InputStream response = null; |
| 85 | try { |
| 86 | response = getController().get(deviceId, URL_RULE_TABLE_STATS, JSON); |
| 87 | } catch (ProcessingException pEx) { |
| 88 | log.error("Failed to get NIC table statistics from device: {}", deviceId); |
| 89 | return Collections.EMPTY_LIST; |
| 90 | } |
| 91 | |
| 92 | // Load the JSON into object |
| 93 | ObjectMapper mapper = new ObjectMapper(); |
| 94 | Map<String, Object> jsonMap = null; |
| 95 | JsonNode jsonNode = null; |
| 96 | ObjectNode objNode = null; |
| 97 | try { |
| 98 | jsonMap = mapper.readValue(response, Map.class); |
| 99 | jsonNode = mapper.convertValue(jsonMap, JsonNode.class); |
| 100 | objNode = (ObjectNode) jsonNode; |
| 101 | } catch (IOException ioEx) { |
| 102 | log.error("Failed to get NIC table statistics from device: {}", deviceId); |
| 103 | return Collections.EMPTY_LIST; |
| 104 | } |
| 105 | |
| 106 | if (jsonNode == null) { |
| 107 | log.error("Failed to get NIC table statistics from device: {}", deviceId); |
| 108 | return Collections.EMPTY_LIST; |
| 109 | } |
| 110 | |
| 111 | List<TableStatisticsEntry> tableStats = Lists.newArrayList(); |
| 112 | |
| 113 | JsonNode nicNode = objNode.path(PARAM_NICS); |
| 114 | |
| 115 | for (JsonNode nn : nicNode) { |
| 116 | ObjectNode nicObjNode = (ObjectNode) nn; |
| 117 | |
| 118 | // The index of the NIC that hosts rules table(s) |
| 119 | long nicIndex = nicObjNode.path(Constants.PARAM_ID).asLong(); |
| 120 | |
| 121 | JsonNode tableNode = nicObjNode.path(PARAM_NIC_TABLE); |
| 122 | if (tableNode == null) { |
| 123 | throw new IllegalArgumentException("No tables reported for NIC " + nicIndex); |
| 124 | } |
| 125 | |
| 126 | for (JsonNode tn : tableNode) { |
| 127 | ObjectNode tableObjNode = (ObjectNode) tn; |
| 128 | |
| 129 | // NIC table attributes |
| 130 | int tableIndex = tableObjNode.path(PARAM_ID).asInt(); |
| 131 | checkArgument(tableIndex >= 0, MSG_NIC_TABLE_INDEX_NEGATIVE); |
| 132 | |
| 133 | long tableActiveEntries = tableObjNode.path(PARAM_NIC_TABLE_ACTIVE_ENTRIES).asLong(); |
| 134 | checkArgument(tableActiveEntries >= 0, MSG_NIC_TABLE_COUNTER_NEGATIVE); |
| 135 | |
| 136 | long tablePktsLookedUp = tableObjNode.path(PARAM_NIC_TABLE_PKTS_LOOKED_UP).asLong(); |
| 137 | checkArgument(tablePktsLookedUp >= 0, MSG_NIC_TABLE_COUNTER_NEGATIVE); |
| 138 | |
| 139 | long tablePktsMatched = tableObjNode.path(PARAM_NIC_TABLE_PKTS_MATCHED).asLong(); |
| 140 | checkArgument(tablePktsMatched >= 0, MSG_NIC_TABLE_COUNTER_NEGATIVE); |
| 141 | |
| 142 | long tableMaxsize = tableObjNode.path(PARAM_NIC_TABLE_MAX_SIZE).asLong(); |
| 143 | checkArgument(tableMaxsize >= 0, MSG_NIC_TABLE_SIZE_NEGATIVE); |
| 144 | |
| 145 | // Server's device ID and NIC ID compose a NIC device ID |
| 146 | DeviceId nicDeviceId = DeviceId.deviceId( |
| 147 | deviceId.toString() + ":nic" + String.valueOf(nicIndex)); |
| 148 | |
| 149 | TableStatisticsEntry tableStat = DefaultTableStatisticsEntry.builder() |
| 150 | .withDeviceId(nicDeviceId) |
| 151 | .withTableId(IndexTableId.of(tableIndex)) |
| 152 | .withActiveFlowEntries(tableActiveEntries) |
| 153 | .withPacketsLookedUpCount(tablePktsLookedUp) |
| 154 | .withPacketsMatchedCount(tablePktsMatched) |
| 155 | .withMaxSize(tableMaxsize > 0 ? tableMaxsize : -1) |
| 156 | .build(); |
| 157 | |
| 158 | tableStats.add(tableStat); |
| 159 | |
| 160 | log.debug("[Device {}] NIC {} with table statistics: {}", |
| 161 | deviceId, nicIndex, tableStat); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return ImmutableList.copyOf(tableStats); |
| 166 | } |
| 167 | |
| 168 | } |