blob: 9aa65d9e99a8941afaeb25040289eee95ac36816 [file] [log] [blame]
Georgios Katsikas13ccba62020-03-18 12:05:03 +01001/*
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
17package org.onosproject.drivers.server;
18
19import org.onosproject.net.DeviceId;
20import org.onosproject.net.behaviour.TableStatisticsDiscovery;
21import org.onosproject.net.flow.DefaultTableStatisticsEntry;
22import org.onosproject.net.flow.IndexTableId;
23import org.onosproject.net.flow.TableStatisticsEntry;
24import org.onosproject.protocol.rest.RestSBDevice;
25
26import org.slf4j.Logger;
27
28import com.fasterxml.jackson.databind.JsonNode;
29import com.fasterxml.jackson.databind.ObjectMapper;
30import com.fasterxml.jackson.databind.node.ObjectNode;
31import com.google.common.collect.Lists;
32import com.google.common.collect.ImmutableList;
33
34import java.io.InputStream;
35import java.io.IOException;
36import java.util.Collections;
37import java.util.List;
38import java.util.Map;
39import javax.ws.rs.ProcessingException;
40
41import static com.google.common.base.Preconditions.checkArgument;
42import static com.google.common.base.Preconditions.checkNotNull;
43import static org.onosproject.drivers.server.Constants.JSON;
Georgios Katsikas13ccba62020-03-18 12:05:03 +010044import static org.onosproject.drivers.server.Constants.MSG_DEVICE_ID_NULL;
Georgios Katsikas1a64c3a2020-06-03 17:30:43 +020045import static org.onosproject.drivers.server.Constants.MSG_DEVICE_NULL;
Georgios Katsikas13ccba62020-03-18 12:05:03 +010046import static org.onosproject.drivers.server.Constants.MSG_NIC_TABLE_COUNTER_NEGATIVE;
Georgios Katsikas1a64c3a2020-06-03 17:30:43 +020047import static org.onosproject.drivers.server.Constants.MSG_NIC_TABLE_INDEX_NEGATIVE;
48import static org.onosproject.drivers.server.Constants.MSG_NIC_TABLE_SIZE_NEGATIVE;
Georgios Katsikas13ccba62020-03-18 12:05:03 +010049import static org.onosproject.drivers.server.Constants.PARAM_ID;
50import static org.onosproject.drivers.server.Constants.PARAM_NICS;
51import static org.onosproject.drivers.server.Constants.PARAM_NIC_TABLE;
52import static org.onosproject.drivers.server.Constants.PARAM_NIC_TABLE_ACTIVE_ENTRIES;
Georgios Katsikas1a64c3a2020-06-03 17:30:43 +020053import static org.onosproject.drivers.server.Constants.PARAM_NIC_TABLE_MAX_SIZE;
Georgios Katsikas13ccba62020-03-18 12:05:03 +010054import static org.onosproject.drivers.server.Constants.PARAM_NIC_TABLE_PKTS_LOOKED_UP;
55import static org.onosproject.drivers.server.Constants.PARAM_NIC_TABLE_PKTS_MATCHED;
Georgios Katsikas13ccba62020-03-18 12:05:03 +010056import static org.onosproject.drivers.server.Constants.URL_RULE_TABLE_STATS;
57import static org.slf4j.LoggerFactory.getLogger;
58
59/**
60 * Implementation of table statistics discovery for server devices.
61 */
62public 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}