Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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.drivers.server.behavior.CpuStatisticsDiscovery; |
| 20 | import org.onosproject.drivers.server.behavior.MonitoringStatisticsDiscovery; |
| 21 | import org.onosproject.drivers.server.devices.CpuDevice; |
| 22 | import org.onosproject.drivers.server.devices.CpuVendor; |
Georgios Katsikas | 2ebd8a0 | 2018-06-27 18:32:50 +0200 | [diff] [blame] | 23 | import org.onosproject.drivers.server.devices.nic.NicDevice; |
| 24 | import org.onosproject.drivers.server.devices.nic.NicRxFilter; |
| 25 | import org.onosproject.drivers.server.devices.nic.NicRxFilter.RxFilter; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 26 | import org.onosproject.drivers.server.devices.ServerDeviceDescription; |
| 27 | import org.onosproject.drivers.server.devices.RestServerSBDevice; |
| 28 | import org.onosproject.drivers.server.stats.CpuStatistics; |
| 29 | import org.onosproject.drivers.server.stats.MonitoringStatistics; |
| 30 | import org.onosproject.drivers.server.stats.TimingStatistics; |
| 31 | |
| 32 | import org.onosproject.drivers.server.impl.devices.DefaultCpuDevice; |
| 33 | import org.onosproject.drivers.server.impl.devices.DefaultNicDevice; |
| 34 | import org.onosproject.drivers.server.impl.devices.DefaultRestServerSBDevice; |
| 35 | import org.onosproject.drivers.server.impl.devices.DefaultServerDeviceDescription; |
| 36 | import org.onosproject.drivers.server.impl.stats.DefaultCpuStatistics; |
| 37 | import org.onosproject.drivers.server.impl.stats.DefaultMonitoringStatistics; |
| 38 | import org.onosproject.drivers.server.impl.stats.DefaultTimingStatistics; |
| 39 | |
| 40 | import org.onlab.packet.ChassisId; |
| 41 | import org.onosproject.net.AnnotationKeys; |
| 42 | import org.onosproject.net.Device; |
| 43 | import org.onosproject.net.DeviceId; |
| 44 | import org.onosproject.net.DefaultAnnotations; |
| 45 | import org.onosproject.net.behaviour.DevicesDiscovery; |
| 46 | import org.onosproject.net.device.DeviceDescription; |
| 47 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 48 | import org.onosproject.net.device.DefaultPortStatistics; |
| 49 | import org.onosproject.net.device.DefaultPortDescription; |
| 50 | import org.onosproject.net.device.PortDescription; |
| 51 | import org.onosproject.net.device.PortStatistics; |
| 52 | import org.onosproject.net.device.PortStatisticsDiscovery; |
| 53 | import org.onosproject.net.Port; |
| 54 | import org.onosproject.net.PortNumber; |
| 55 | import org.onosproject.protocol.rest.RestSBDevice; |
| 56 | import org.onosproject.protocol.rest.RestSBDevice.AuthenticationScheme; |
| 57 | |
| 58 | import org.slf4j.Logger; |
| 59 | |
| 60 | import com.fasterxml.jackson.core.type.TypeReference; |
| 61 | import com.fasterxml.jackson.databind.JsonNode; |
| 62 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 63 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 64 | |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 65 | import com.google.common.base.Strings; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 66 | import com.google.common.collect.Lists; |
| 67 | import com.google.common.collect.ImmutableList; |
| 68 | |
| 69 | import javax.ws.rs.ProcessingException; |
| 70 | import java.io.InputStream; |
| 71 | import java.io.IOException; |
| 72 | import java.net.URI; |
| 73 | import java.net.URISyntaxException; |
| 74 | import java.util.ArrayList; |
| 75 | import java.util.Collection; |
| 76 | import java.util.Collections; |
| 77 | import java.util.HashSet; |
| 78 | import java.util.HashMap; |
| 79 | import java.util.List; |
| 80 | import java.util.Map; |
| 81 | import java.util.Set; |
| 82 | import java.util.TreeSet; |
| 83 | |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 84 | import static com.google.common.base.Preconditions.checkArgument; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 85 | import static com.google.common.base.Preconditions.checkNotNull; |
| 86 | import static org.slf4j.LoggerFactory.getLogger; |
| 87 | |
| 88 | /** |
| 89 | * Discovers the device details of |
| 90 | * REST-based commodity server devices. |
| 91 | */ |
| 92 | public class ServerDevicesDiscovery extends BasicServerDriver |
| 93 | implements DevicesDiscovery, DeviceDescriptionDiscovery, |
| 94 | PortStatisticsDiscovery, CpuStatisticsDiscovery, |
| 95 | MonitoringStatisticsDiscovery { |
| 96 | |
| 97 | private final Logger log = getLogger(getClass()); |
| 98 | |
| 99 | /** |
| 100 | * Resource endpoints of the server agent (REST server-side). |
| 101 | */ |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 102 | private static final String RESOURCE_DISCOVERY_URL = BASE_URL + SLASH + "resources"; |
| 103 | private static final String GLOBAL_STATS_URL = BASE_URL + SLASH + "stats"; |
| 104 | private static final String SERVICE_CHAINS_STATS_URL = BASE_URL + SLASH + "chains_stats"; // + /ID |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * Parameters to be exchanged with the server's agent. |
| 108 | */ |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 109 | private static final String PARAM_MANUFACTURER = "manufacturer"; |
| 110 | private static final String PARAM_HW_VENDOR = "hwVersion"; |
| 111 | private static final String PARAM_SW_VENDOR = "swVersion"; |
| 112 | private static final String PARAM_SERIAL = "serial"; |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 113 | private static final String PARAM_TIMING_STATS = "timingStats"; |
| 114 | private static final String PARAM_TIMING_AUTOSCALE = "autoScaleTimingStats"; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 115 | |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 116 | private static final String NIC_PARAM_NAME = "name"; |
| 117 | private static final String NIC_PARAM_PORT_INDEX = "index"; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 118 | private static final String NIC_PARAM_PORT_TYPE = "portType"; |
| 119 | private static final String NIC_PARAM_PORT_TYPE_FIBER = "fiber"; |
| 120 | private static final String NIC_PARAM_PORT_TYPE_COPPER = "copper"; |
| 121 | private static final String NIC_PARAM_SPEED = "speed"; |
| 122 | private static final String NIC_PARAM_STATUS = "status"; |
| 123 | private static final String NIC_PARAM_HW_ADDR = "hwAddr"; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * NIC statistics. |
| 127 | */ |
| 128 | private static final String NIC_STATS_TX_COUNT = "txCount"; |
| 129 | private static final String NIC_STATS_TX_BYTES = "txBytes"; |
| 130 | private static final String NIC_STATS_TX_DROPS = "txDropped"; |
| 131 | private static final String NIC_STATS_TX_ERRORS = "txErrors"; |
| 132 | private static final String NIC_STATS_RX_COUNT = "rxCount"; |
| 133 | private static final String NIC_STATS_RX_BYTES = "rxBytes"; |
| 134 | private static final String NIC_STATS_RX_DROPS = "rxDropped"; |
| 135 | private static final String NIC_STATS_RX_ERRORS = "rxErrors"; |
| 136 | |
| 137 | /** |
| 138 | * CPU statistics. |
| 139 | */ |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 140 | private static final String CPU_PARAM_ID = "id"; |
| 141 | private static final String CPU_PARAM_VENDOR = "vendor"; |
| 142 | private static final String CPU_PARAM_FREQUENCY = "frequency"; |
| 143 | private static final String CPU_PARAM_LOAD = "load"; |
| 144 | private static final String CPU_PARAM_QUEUE = "queue"; |
| 145 | private static final String CPU_PARAM_STATUS = "busy"; |
| 146 | private static final String CPU_PARAM_THROUGHPUT = "throughput"; |
| 147 | private static final String CPU_PARAM_LATENCY = "latency"; |
| 148 | private static final String MON_PARAM_UNIT = "unit"; |
| 149 | private static final String MON_PARAM_BUSY_CPUS = "busyCpus"; |
| 150 | private static final String MON_PARAM_FREE_CPUS = "freeCpus"; |
| 151 | private static final String MON_PARAM_MIN = "min"; |
| 152 | private static final String MON_PARAM_AVERAGE = "average"; |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 153 | private static final String MON_PARAM_MAX = "max"; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * Timing statistics. |
| 157 | */ |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 158 | private static final String TIMING_PARAM_PARSE = "parseTime"; |
| 159 | private static final String TIMING_PARAM_LAUNCH = "launchTime"; |
| 160 | private static final String TIMING_PARAM_DEPLOY = "deployTime"; |
| 161 | private static final String TIMING_PARAM_AUTOSCALE = "autoScaleTime"; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 162 | |
| 163 | /** |
| 164 | * Auxiliary constants. |
| 165 | */ |
| 166 | private static final short DISCOVERY_RETRIES = 3; |
| 167 | private static final String CPU_VENDOR_NULL = "Unsupported CPU vendor" + |
| 168 | " Choose one in: " + BasicServerDriver.enumTypesToString(CpuVendor.class); |
| 169 | private static final String NIC_RX_FILTER_NULL = "Unsupported NIC Rx filter" + |
| 170 | " Choose one in: " + BasicServerDriver.enumTypesToString(RxFilter.class); |
| 171 | |
| 172 | /** |
| 173 | * Port types that usually appear in commodity servers. |
| 174 | */ |
| 175 | public static final Map<String, Port.Type> PORT_TYPE_MAP = |
| 176 | Collections.unmodifiableMap( |
| 177 | new HashMap<String, Port.Type>() { |
| 178 | { |
| 179 | put(NIC_PARAM_PORT_TYPE_FIBER, Port.Type.FIBER); |
| 180 | put(NIC_PARAM_PORT_TYPE_COPPER, Port.Type.COPPER); |
| 181 | } |
| 182 | } |
| 183 | ); |
| 184 | |
| 185 | /** |
| 186 | * Constructs server device discovery. |
| 187 | */ |
| 188 | public ServerDevicesDiscovery() { |
| 189 | super(); |
| 190 | log.debug("Started"); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public Set<DeviceId> deviceIds() { |
| 195 | // Set of devices to return |
| 196 | Set<DeviceId> devices = new HashSet<DeviceId>(); |
| 197 | |
| 198 | DeviceId deviceId = getHandler().data().deviceId(); |
| 199 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 200 | devices.add(deviceId); |
| 201 | |
| 202 | return devices; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public DeviceDescription deviceDetails(DeviceId deviceId) { |
| 207 | return getDeviceDetails(deviceId); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public DeviceDescription discoverDeviceDetails() { |
| 212 | return getDeviceDetails(null); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Query a server to retrieve its features. |
| 217 | * |
| 218 | * @param deviceId the device ID to be queried |
| 219 | * @return a DeviceDescription with the device's features |
| 220 | */ |
| 221 | private DeviceDescription getDeviceDetails(DeviceId deviceId) { |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 222 | // Retrieve the device ID, if null given |
| 223 | if (deviceId == null) { |
| 224 | deviceId = getHandler().data().deviceId(); |
| 225 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 226 | } |
| 227 | |
| 228 | // Get the device |
| 229 | RestSBDevice device = getController().getDevice(deviceId); |
| 230 | checkNotNull(device, DEVICE_NULL); |
| 231 | |
| 232 | // Hit the path that provides the server's resources |
| 233 | InputStream response = null; |
| 234 | try { |
Georgios Katsikas | d472a32 | 2018-07-08 19:58:25 +0200 | [diff] [blame] | 235 | response = getController().get(deviceId, RESOURCE_DISCOVERY_URL, JSON); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 236 | } catch (ProcessingException pEx) { |
| 237 | log.error("Failed to discover the device details of: {}", deviceId); |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 238 | return null; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Load the JSON into objects |
| 242 | ObjectMapper mapper = new ObjectMapper(); |
| 243 | Map<String, Object> jsonMap = null; |
| 244 | JsonNode jsonNode = null; |
| 245 | ObjectNode objNode = null; |
| 246 | try { |
| 247 | jsonMap = mapper.readValue(response, Map.class); |
| 248 | jsonNode = mapper.convertValue(jsonMap, JsonNode.class); |
| 249 | objNode = (ObjectNode) jsonNode; |
| 250 | } catch (IOException ioEx) { |
| 251 | log.error("Failed to discover the device details of: {}", deviceId); |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 252 | return null; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | if (jsonMap == null) { |
| 256 | log.error("Failed to discover the device details of: {}", deviceId); |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 257 | return null; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // Get all the attributes |
Georgios Katsikas | 2ebd8a0 | 2018-06-27 18:32:50 +0200 | [diff] [blame] | 261 | String id = get(jsonNode, BasicServerDriver.PARAM_ID); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 262 | String vendor = get(jsonNode, PARAM_MANUFACTURER); |
| 263 | String hw = get(jsonNode, PARAM_HW_VENDOR); |
| 264 | String sw = get(jsonNode, PARAM_SW_VENDOR); |
| 265 | String serial = get(jsonNode, PARAM_SERIAL); |
| 266 | |
| 267 | // CPUs are composite attributes |
| 268 | Set<CpuDevice> cpuSet = new HashSet<CpuDevice>(); |
Georgios Katsikas | 2ebd8a0 | 2018-06-27 18:32:50 +0200 | [diff] [blame] | 269 | JsonNode cpuNode = objNode.path(BasicServerDriver.PARAM_CPUS); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 270 | |
| 271 | // Construct CPU objects |
| 272 | for (JsonNode cn : cpuNode) { |
| 273 | ObjectNode cpuObjNode = (ObjectNode) cn; |
| 274 | |
| 275 | // All the CPU attributes |
| 276 | int cpuId = cpuObjNode.path(CPU_PARAM_ID).asInt(); |
| 277 | String cpuVendorStr = get(cn, CPU_PARAM_VENDOR); |
| 278 | long cpuFrequency = cpuObjNode.path(CPU_PARAM_FREQUENCY).asLong(); |
| 279 | |
| 280 | // Verify that this is a valid vendor |
| 281 | CpuVendor cpuVendor = CpuVendor.getByName(cpuVendorStr); |
| 282 | checkNotNull(cpuVendor, CPU_VENDOR_NULL); |
| 283 | |
| 284 | // Construct a CPU device |
| 285 | CpuDevice cpu = new DefaultCpuDevice(cpuId, cpuVendor, cpuFrequency); |
| 286 | |
| 287 | // Add it to the set |
| 288 | cpuSet.add(cpu); |
| 289 | } |
| 290 | |
| 291 | // NICs are composite attributes too |
| 292 | Set<NicDevice> nicSet = new HashSet<NicDevice>(); |
| 293 | JsonNode nicNode = objNode.path(PARAM_NICS); |
| 294 | |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 295 | DefaultAnnotations.Builder annotations = DefaultAnnotations.builder(); |
| 296 | |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 297 | // Construct NIC objects |
| 298 | for (JsonNode nn : nicNode) { |
| 299 | ObjectNode nicObjNode = (ObjectNode) nn; |
| 300 | |
| 301 | // All the NIC attributes |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 302 | String nicName = get(nn, NIC_PARAM_NAME); |
| 303 | long nicIndex = nicObjNode.path(NIC_PARAM_PORT_INDEX).asLong(); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 304 | long speed = nicObjNode.path(NIC_PARAM_SPEED).asLong(); |
| 305 | String portTypeStr = get(nn, NIC_PARAM_PORT_TYPE); |
| 306 | Port.Type portType = PORT_TYPE_MAP.get(portTypeStr); |
| 307 | if (portType == null) { |
Ray Milkey | 067c44b | 2018-02-26 12:48:23 -0800 | [diff] [blame] | 308 | throw new IllegalArgumentException( |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 309 | portTypeStr + " is not a valid port type for NIC " + nicName); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 310 | } |
| 311 | boolean status = nicObjNode.path(NIC_PARAM_STATUS).asInt() > 0; |
| 312 | String hwAddr = get(nn, NIC_PARAM_HW_ADDR); |
Georgios Katsikas | 2ebd8a0 | 2018-06-27 18:32:50 +0200 | [diff] [blame] | 313 | JsonNode tagNode = nicObjNode.path(BasicServerDriver.NIC_PARAM_RX_FILTER); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 314 | if (tagNode == null) { |
Ray Milkey | 067c44b | 2018-02-26 12:48:23 -0800 | [diff] [blame] | 315 | throw new IllegalArgumentException( |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 316 | "The Rx filters of NIC " + nicName + " are not reported"); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | // Convert the JSON list into an array of strings |
| 320 | List<String> rxFilters = null; |
| 321 | try { |
Georgios Katsikas | d472a32 | 2018-07-08 19:58:25 +0200 | [diff] [blame] | 322 | rxFilters = mapper.readValue(tagNode.traverse(), |
| 323 | new TypeReference<ArrayList<String>>() { }); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 324 | } catch (IOException ioEx) { |
| 325 | continue; |
| 326 | } |
| 327 | |
| 328 | // Parse the array of strings and create an RxFilter object |
| 329 | NicRxFilter rxFilterMechanism = new NicRxFilter(); |
| 330 | for (String s : rxFilters) { |
| 331 | // Verify that this is a valid Rx filter |
| 332 | RxFilter rf = RxFilter.getByName(s); |
| 333 | checkNotNull(rf, NIC_RX_FILTER_NULL); |
| 334 | |
| 335 | rxFilterMechanism.addRxFilter(rf); |
| 336 | } |
| 337 | |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 338 | // Store NIC name to number mapping as an annotation |
| 339 | annotations.set(nicName, Long.toString(nicIndex)); |
| 340 | |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 341 | // Construct a NIC device for this server |
| 342 | NicDevice nic = new DefaultNicDevice( |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 343 | nicName, nicIndex, portType, speed, status, hwAddr, rxFilterMechanism); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 344 | |
| 345 | // Add it to the set |
| 346 | nicSet.add(nic); |
| 347 | } |
| 348 | |
Georgios Katsikas | 2ebd8a0 | 2018-06-27 18:32:50 +0200 | [diff] [blame] | 349 | // Construct a complete server device object. |
| 350 | // Lists of NICs and CPUs extend the information |
| 351 | // already in RestSBDevice (parent class). |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 352 | RestServerSBDevice dev = new DefaultRestServerSBDevice( |
| 353 | device.ip(), device.port(), device.username(), |
| 354 | device.password(), device.protocol(), device.url(), |
Georgios Katsikas | db14847 | 2018-10-27 20:48:13 +0200 | [diff] [blame] | 355 | device.isActive(), device.testUrl().orElse(""), |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 356 | vendor, hw, sw, AuthenticationScheme.BASIC, "", |
| 357 | cpuSet, nicSet |
| 358 | ); |
| 359 | checkNotNull(dev, DEVICE_NULL); |
| 360 | |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 361 | // Set alive |
| 362 | raiseDeviceReconnect(dev); |
| 363 | |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 364 | // Updates the controller with the complete device information |
| 365 | getController().removeDevice(deviceId); |
| 366 | getController().addDevice((RestSBDevice) dev); |
| 367 | |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 368 | // Create a description for this server device |
| 369 | ServerDeviceDescription desc = null; |
| 370 | |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 371 | try { |
| 372 | desc = new DefaultServerDeviceDescription( |
Georgios Katsikas | 40ecef3 | 2018-07-03 11:17:44 +0200 | [diff] [blame] | 373 | new URI(id), Device.Type.SERVER, vendor, |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 374 | hw, sw, serial, new ChassisId(), |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 375 | cpuSet, nicSet, annotations.build() |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 376 | ); |
| 377 | } catch (URISyntaxException uEx) { |
Georgios Katsikas | d472a32 | 2018-07-08 19:58:25 +0200 | [diff] [blame] | 378 | log.error("Failed to create a server device description for: {}", |
| 379 | deviceId); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 380 | return null; |
| 381 | } |
| 382 | |
| 383 | log.info("Device's {} details sent to the controller", deviceId); |
| 384 | |
| 385 | return desc; |
| 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public List<PortDescription> discoverPortDetails() { |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 390 | // Retrieve the device ID |
| 391 | DeviceId deviceId = getHandler().data().deviceId(); |
| 392 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 393 | |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 394 | // .. and object |
| 395 | RestServerSBDevice device = null; |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 396 | try { |
| 397 | device = (RestServerSBDevice) getController().getDevice(deviceId); |
| 398 | } catch (ClassCastException ccEx) { |
| 399 | log.error("Failed to discover ports for device {}", deviceId); |
| 400 | return Collections.EMPTY_LIST; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 401 | } |
| 402 | |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 403 | if (device == null) { |
| 404 | log.error("No device with ID {} is available for port discovery", deviceId); |
| 405 | return Collections.EMPTY_LIST; |
| 406 | } |
| 407 | if ((device.nics() == null) || (device.nics().size() == 0)) { |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 408 | log.error("No ports available on {}", deviceId); |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 409 | return Collections.EMPTY_LIST; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 410 | } |
| 411 | |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 412 | // List of port descriptions to return |
| 413 | List<PortDescription> portDescriptions = Lists.newArrayList(); |
| 414 | |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 415 | // Sorted list of NIC ports |
| 416 | Set<NicDevice> nics = new TreeSet(device.nics()); |
| 417 | |
| 418 | // Iterate through the NICs of this device to populate the list |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 419 | for (NicDevice nic : nics) { |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 420 | // Include the name of this device as an annotation |
| 421 | DefaultAnnotations.Builder annotations = DefaultAnnotations.builder() |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 422 | .set(AnnotationKeys.PORT_NAME, nic.name()); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 423 | |
| 424 | // Create a port description and add it to the list |
| 425 | portDescriptions.add( |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 426 | DefaultPortDescription.builder() |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 427 | .withPortNumber(PortNumber.portNumber(nic.portNumber(), nic.name())) |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 428 | .isEnabled(nic.status()) |
| 429 | .type(nic.portType()) |
| 430 | .portSpeed(nic.speed()) |
| 431 | .annotations(annotations.build()) |
| 432 | .build()); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 433 | |
Georgios Katsikas | d472a32 | 2018-07-08 19:58:25 +0200 | [diff] [blame] | 434 | log.info("Port discovery on device {}: NIC {} is {} at {} Mbps", |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 435 | deviceId, nic.portNumber(), nic.status() ? "up" : "down", |
Georgios Katsikas | d472a32 | 2018-07-08 19:58:25 +0200 | [diff] [blame] | 436 | nic.speed()); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | return ImmutableList.copyOf(portDescriptions); |
| 440 | } |
| 441 | |
| 442 | @Override |
| 443 | public Collection<PortStatistics> discoverPortStatistics() { |
| 444 | // Retrieve the device ID |
| 445 | DeviceId deviceId = getHandler().data().deviceId(); |
| 446 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 447 | |
| 448 | // Get port statistics for this device |
| 449 | return getPortStatistics(deviceId); |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Query a server to retrieve its port statistics. |
| 454 | * |
| 455 | * @param deviceId the device ID to be queried |
| 456 | * @return list of (per port) PortStatistics |
| 457 | */ |
| 458 | private Collection<PortStatistics> getPortStatistics(DeviceId deviceId) { |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 459 | // Get global monitoring statistics |
| 460 | MonitoringStatistics monStats = getGlobalMonitoringStatistics(deviceId); |
| 461 | if (monStats == null) { |
Georgios Katsikas | d472a32 | 2018-07-08 19:58:25 +0200 | [diff] [blame] | 462 | return Collections.EMPTY_LIST; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | // Filter out the NIC statistics |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 466 | Collection<PortStatistics> portStats = monStats.nicStatisticsAll(); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 467 | if (portStats == null) { |
Georgios Katsikas | d472a32 | 2018-07-08 19:58:25 +0200 | [diff] [blame] | 468 | return Collections.EMPTY_LIST; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | log.debug("Port statistics: {}", portStats.toString()); |
| 472 | |
| 473 | return portStats; |
| 474 | } |
| 475 | |
| 476 | @Override |
| 477 | public Collection<CpuStatistics> discoverCpuStatistics() { |
| 478 | // Retrieve the device ID |
| 479 | DeviceId deviceId = getHandler().data().deviceId(); |
| 480 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 481 | |
| 482 | // Get CPU statistics for this device |
| 483 | return getCpuStatistics(deviceId); |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Query a server to retrieve its CPU statistics. |
| 488 | * |
| 489 | * @param deviceId the device ID to be queried |
| 490 | * @return list of (per core) CpuStatistics |
| 491 | */ |
Georgios Katsikas | 2ebd8a0 | 2018-06-27 18:32:50 +0200 | [diff] [blame] | 492 | public Collection<CpuStatistics> getCpuStatistics(DeviceId deviceId) { |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 493 | // Get global monitoring statistics |
| 494 | MonitoringStatistics monStats = getGlobalMonitoringStatistics(deviceId); |
| 495 | if (monStats == null) { |
Georgios Katsikas | d472a32 | 2018-07-08 19:58:25 +0200 | [diff] [blame] | 496 | return Collections.EMPTY_LIST; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | // Filter out the CPU statistics |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 500 | Collection<CpuStatistics> cpuStats = monStats.cpuStatisticsAll(); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 501 | if (cpuStats == null) { |
Georgios Katsikas | d472a32 | 2018-07-08 19:58:25 +0200 | [diff] [blame] | 502 | return Collections.EMPTY_LIST; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | log.debug("CPU statistics: {}", cpuStats.toString()); |
| 506 | |
| 507 | return cpuStats; |
| 508 | } |
| 509 | |
| 510 | @Override |
| 511 | public MonitoringStatistics discoverGlobalMonitoringStatistics() { |
| 512 | // Retrieve the device ID |
| 513 | DeviceId deviceId = getHandler().data().deviceId(); |
| 514 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 515 | |
| 516 | // Get global monitoring statistics for this device |
| 517 | return getGlobalMonitoringStatistics(deviceId); |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * Query a server to retrieve its global monitoring statistics. |
| 522 | * |
| 523 | * @param deviceId the device ID to be queried |
| 524 | * @return global monitoring statistics |
| 525 | */ |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 526 | public MonitoringStatistics getGlobalMonitoringStatistics(DeviceId deviceId) { |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 527 | // Monitoring statistics to return |
| 528 | MonitoringStatistics monStats = null; |
| 529 | |
| 530 | RestServerSBDevice device = null; |
| 531 | try { |
| 532 | device = (RestServerSBDevice) getController().getDevice(deviceId); |
| 533 | } catch (ClassCastException ccEx) { |
Georgios Katsikas | d472a32 | 2018-07-08 19:58:25 +0200 | [diff] [blame] | 534 | log.error("Failed to retrieve global monitoring statistics from device {}", |
| 535 | deviceId); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 536 | return monStats; |
| 537 | } |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 538 | if ((device == null) || (!device.isActive())) { |
| 539 | return monStats; |
| 540 | } |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 541 | |
| 542 | // Hit the path that provides the server's global resources |
| 543 | InputStream response = null; |
| 544 | try { |
Georgios Katsikas | d472a32 | 2018-07-08 19:58:25 +0200 | [diff] [blame] | 545 | response = getController().get(deviceId, GLOBAL_STATS_URL, JSON); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 546 | } catch (ProcessingException pEx) { |
Georgios Katsikas | d472a32 | 2018-07-08 19:58:25 +0200 | [diff] [blame] | 547 | log.error("Failed to retrieve global monitoring statistics from device {}", |
| 548 | deviceId); |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 549 | raiseDeviceDisconnect(device); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 550 | return monStats; |
| 551 | } |
| 552 | |
| 553 | // Load the JSON into objects |
| 554 | ObjectMapper mapper = new ObjectMapper(); |
| 555 | Map<String, Object> jsonMap = null; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 556 | ObjectNode objNode = null; |
| 557 | try { |
| 558 | jsonMap = mapper.readValue(response, Map.class); |
Georgios Katsikas | 2ebd8a0 | 2018-06-27 18:32:50 +0200 | [diff] [blame] | 559 | JsonNode jsonNode = mapper.convertValue(jsonMap, JsonNode.class); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 560 | objNode = (ObjectNode) jsonNode; |
| 561 | } catch (IOException ioEx) { |
Georgios Katsikas | d472a32 | 2018-07-08 19:58:25 +0200 | [diff] [blame] | 562 | log.error("Failed to retrieve global monitoring statistics from device {}", |
| 563 | deviceId); |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 564 | raiseDeviceDisconnect(device); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 565 | return monStats; |
| 566 | } |
| 567 | |
| 568 | if (jsonMap == null) { |
Georgios Katsikas | d472a32 | 2018-07-08 19:58:25 +0200 | [diff] [blame] | 569 | log.error("Failed to retrieve global monitoring statistics from device {}", |
| 570 | deviceId); |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 571 | raiseDeviceDisconnect(device); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 572 | return monStats; |
| 573 | } |
| 574 | |
| 575 | // Get high-level CPU statistics |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 576 | int busyCpus = objNode.path(MON_PARAM_BUSY_CPUS).asInt(); |
| 577 | int freeCpus = objNode.path(MON_PARAM_FREE_CPUS).asInt(); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 578 | |
| 579 | // Get a list of CPU statistics per core |
| 580 | Collection<CpuStatistics> cpuStats = parseCpuStatistics(deviceId, objNode); |
| 581 | |
| 582 | // Get a list of port statistics |
| 583 | Collection<PortStatistics> nicStats = parseNicStatistics(deviceId, objNode); |
| 584 | |
| 585 | // Get zero timing statistics |
| 586 | TimingStatistics timinsgStats = getZeroTimingStatistics(); |
| 587 | |
| 588 | // Ready to construct the grand object |
| 589 | DefaultMonitoringStatistics.Builder statsBuilder = |
| 590 | DefaultMonitoringStatistics.builder(); |
| 591 | |
| 592 | statsBuilder.setDeviceId(deviceId) |
| 593 | .setTimingStatistics(timinsgStats) |
| 594 | .setCpuStatistics(cpuStats) |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 595 | .setNicStatistics(nicStats); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 596 | |
| 597 | monStats = statsBuilder.build(); |
| 598 | |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 599 | // When a device reports monitoring data, it means it is alive |
| 600 | raiseDeviceReconnect(device); |
| 601 | |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 602 | log.debug("Global monitoring statistics: {}", monStats.toString()); |
| 603 | |
| 604 | return monStats; |
| 605 | } |
| 606 | |
| 607 | @Override |
| 608 | public MonitoringStatistics discoverMonitoringStatistics(URI tcId) { |
| 609 | // Retrieve the device ID |
| 610 | DeviceId deviceId = getHandler().data().deviceId(); |
| 611 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 612 | |
| 613 | // Get resource-specific monitoring statistics for this device |
| 614 | return getMonitoringStatistics(deviceId, tcId); |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * Query a server to retrieve monitoring statistics for a |
| 619 | * specific resource (i.e., traffic class). |
| 620 | * |
| 621 | * @param deviceId the device ID to be queried |
| 622 | * @param tcId the ID of the traffic class to be monitored |
| 623 | * @return resource-specific monitoring statistics |
| 624 | */ |
| 625 | private MonitoringStatistics getMonitoringStatistics(DeviceId deviceId, URI tcId) { |
| 626 | // Monitoring statistics to return |
| 627 | MonitoringStatistics monStats = null; |
| 628 | |
| 629 | RestServerSBDevice device = null; |
| 630 | try { |
| 631 | device = (RestServerSBDevice) getController().getDevice(deviceId); |
| 632 | } catch (ClassCastException ccEx) { |
Georgios Katsikas | d472a32 | 2018-07-08 19:58:25 +0200 | [diff] [blame] | 633 | log.error("Failed to retrieve monitoring statistics from device {}", |
| 634 | deviceId); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 635 | return monStats; |
| 636 | } |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 637 | if (device == null) { |
| 638 | return monStats; |
| 639 | } |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 640 | |
| 641 | // Create a resource-specific URL |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 642 | String scUrl = SERVICE_CHAINS_STATS_URL + SLASH + tcId.toString(); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 643 | |
| 644 | // Hit the path that provides the server's specific resources |
| 645 | InputStream response = null; |
| 646 | try { |
Georgios Katsikas | d472a32 | 2018-07-08 19:58:25 +0200 | [diff] [blame] | 647 | response = getController().get(deviceId, scUrl, JSON); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 648 | } catch (ProcessingException pEx) { |
Georgios Katsikas | d472a32 | 2018-07-08 19:58:25 +0200 | [diff] [blame] | 649 | log.error("Failed to retrieve monitoring statistics from device {}", |
| 650 | deviceId); |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 651 | raiseDeviceDisconnect(device); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 652 | return monStats; |
| 653 | } |
| 654 | |
| 655 | // Load the JSON into objects |
| 656 | ObjectMapper mapper = new ObjectMapper(); |
| 657 | Map<String, Object> jsonMap = null; |
| 658 | JsonNode jsonNode = null; |
| 659 | ObjectNode objNode = null; |
| 660 | try { |
| 661 | jsonMap = mapper.readValue(response, Map.class); |
| 662 | jsonNode = mapper.convertValue(jsonMap, JsonNode.class); |
| 663 | objNode = (ObjectNode) jsonNode; |
| 664 | } catch (IOException ioEx) { |
Georgios Katsikas | d472a32 | 2018-07-08 19:58:25 +0200 | [diff] [blame] | 665 | log.error("Failed to retrieve monitoring statistics from device {}", |
| 666 | deviceId); |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 667 | raiseDeviceDisconnect(device); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 668 | return monStats; |
| 669 | } |
| 670 | |
| 671 | if (jsonMap == null) { |
Georgios Katsikas | d472a32 | 2018-07-08 19:58:25 +0200 | [diff] [blame] | 672 | log.error("Failed to retrieve monitoring statistics from device {}", |
| 673 | deviceId); |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 674 | raiseDeviceDisconnect(device); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 675 | return monStats; |
| 676 | } |
| 677 | |
| 678 | // Get the ID of the traffic class |
| 679 | String id = get(jsonNode, PARAM_ID); |
| 680 | |
| 681 | // And verify that this is the traffic class we want to monitor |
| 682 | if (!id.equals(tcId.toString())) { |
Ray Milkey | 067c44b | 2018-02-26 12:48:23 -0800 | [diff] [blame] | 683 | throw new IllegalStateException( |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 684 | "Failed to retrieve monitoring data for traffic class " + tcId + |
| 685 | ". Traffic class ID does not agree." |
| 686 | ); |
| 687 | } |
| 688 | |
| 689 | // Get a list of CPU statistics per core |
| 690 | Collection<CpuStatistics> cpuStats = parseCpuStatistics(deviceId, objNode); |
| 691 | |
| 692 | // Get a list of port statistics |
| 693 | Collection<PortStatistics> nicStats = parseNicStatistics(deviceId, objNode); |
| 694 | |
| 695 | // Get timing statistics |
| 696 | TimingStatistics timinsgStats = parseTimingStatistics(objNode); |
| 697 | |
| 698 | // Ready to construct the grand object |
| 699 | DefaultMonitoringStatistics.Builder statsBuilder = |
| 700 | DefaultMonitoringStatistics.builder(); |
| 701 | |
| 702 | statsBuilder.setDeviceId(deviceId) |
| 703 | .setTimingStatistics(timinsgStats) |
| 704 | .setCpuStatistics(cpuStats) |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 705 | .setNicStatistics(nicStats); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 706 | |
| 707 | monStats = statsBuilder.build(); |
| 708 | |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 709 | // When a device reports monitoring data, it means it is alive |
| 710 | raiseDeviceReconnect(device); |
| 711 | |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 712 | log.debug("Monitoring statistics: {}", monStats.toString()); |
| 713 | |
| 714 | return monStats; |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * Parse the input JSON object, looking for CPU-related |
| 719 | * statistics. Upon success, construct and return a list |
| 720 | * of CPU statistics objects. |
| 721 | * |
| 722 | * @param deviceId the device ID that sent the JSON object |
| 723 | * @param objNode input JSON node with CPU statistics information |
| 724 | * @return list of (per core) CpuStatistics |
| 725 | */ |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 726 | private Collection<CpuStatistics> parseCpuStatistics(DeviceId deviceId, JsonNode objNode) { |
| 727 | if ((deviceId == null) || (objNode == null)) { |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 728 | return Collections.EMPTY_LIST; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 729 | } |
| 730 | |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 731 | Collection<CpuStatistics> cpuStats = Lists.newArrayList(); |
| 732 | |
Georgios Katsikas | 2ebd8a0 | 2018-06-27 18:32:50 +0200 | [diff] [blame] | 733 | JsonNode cpuNode = objNode.path(BasicServerDriver.PARAM_CPUS); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 734 | |
| 735 | for (JsonNode cn : cpuNode) { |
| 736 | ObjectNode cpuObjNode = (ObjectNode) cn; |
| 737 | |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 738 | // CPU statistics builder |
| 739 | DefaultCpuStatistics.Builder cpuBuilder = DefaultCpuStatistics.builder(); |
| 740 | |
| 741 | // Throughput statistics are optional |
| 742 | JsonNode throughputNode = cpuObjNode.get(CPU_PARAM_THROUGHPUT); |
| 743 | if (throughputNode != null) { |
| 744 | String throughputUnit = get(throughputNode, MON_PARAM_UNIT); |
| 745 | if (!Strings.isNullOrEmpty(throughputUnit)) { |
| 746 | cpuBuilder.setThroughputUnit(throughputUnit); |
| 747 | } |
| 748 | float averageThroughput = (float) 0; |
| 749 | if (throughputNode.get(MON_PARAM_AVERAGE) != null) { |
| 750 | averageThroughput = throughputNode.path(MON_PARAM_AVERAGE).floatValue(); |
| 751 | } |
| 752 | cpuBuilder.setAverageThroughput(averageThroughput); |
| 753 | } |
| 754 | |
| 755 | // Latency statistics are optional |
| 756 | JsonNode latencyNode = cpuObjNode.get(CPU_PARAM_LATENCY); |
| 757 | if (latencyNode != null) { |
| 758 | String latencyUnit = get(latencyNode, MON_PARAM_UNIT); |
| 759 | if (!Strings.isNullOrEmpty(latencyUnit)) { |
| 760 | cpuBuilder.setLatencyUnit(latencyUnit); |
| 761 | } |
| 762 | float minLatency = (float) 0; |
| 763 | if (latencyNode.get(MON_PARAM_MIN) != null) { |
| 764 | minLatency = latencyNode.path(MON_PARAM_MIN).floatValue(); |
| 765 | } |
Georgios Katsikas | 973a265 | 2018-06-28 08:45:47 +0200 | [diff] [blame] | 766 | float averageLatency = (float) 0; |
| 767 | if (latencyNode.get(MON_PARAM_AVERAGE) != null) { |
| 768 | averageLatency = latencyNode.path(MON_PARAM_AVERAGE).floatValue(); |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 769 | } |
| 770 | float maxLatency = (float) 0; |
| 771 | if (latencyNode.get(MON_PARAM_MAX) != null) { |
| 772 | maxLatency = latencyNode.path(MON_PARAM_MAX).floatValue(); |
| 773 | } |
| 774 | |
| 775 | cpuBuilder.setMinLatency(minLatency) |
Georgios Katsikas | 973a265 | 2018-06-28 08:45:47 +0200 | [diff] [blame] | 776 | .setAverageLatency(averageLatency) |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 777 | .setMaxLatency(maxLatency); |
| 778 | } |
| 779 | |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 780 | // CPU ID with its load and status |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 781 | int cpuId = cpuObjNode.path(CPU_PARAM_ID).asInt(); |
| 782 | float cpuLoad = cpuObjNode.path(CPU_PARAM_LOAD).floatValue(); |
| 783 | int queueId = cpuObjNode.path(CPU_PARAM_QUEUE).asInt(); |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 784 | int busySince = cpuObjNode.path(CPU_PARAM_STATUS).asInt(); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 785 | |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 786 | // This is mandatory information |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 787 | cpuBuilder.setDeviceId(deviceId) |
| 788 | .setId(cpuId) |
| 789 | .setLoad(cpuLoad) |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 790 | .setQueue(queueId) |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame] | 791 | .setBusySince(busySince); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 792 | |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 793 | // We have all the statistics for this CPU core |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 794 | cpuStats.add(cpuBuilder.build()); |
| 795 | } |
| 796 | |
| 797 | return cpuStats; |
| 798 | } |
| 799 | |
| 800 | /** |
| 801 | * Parse the input JSON object, looking for NIC-related |
| 802 | * statistics. Upon success, construct and return a list |
| 803 | * of NIC statistics objects. |
| 804 | * |
| 805 | * @param deviceId the device ID that sent the JSON object |
| 806 | * @param objNode input JSON node with NIC statistics information |
| 807 | * @return list of (per port) PortStatistics |
| 808 | */ |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 809 | private Collection<PortStatistics> parseNicStatistics(DeviceId deviceId, JsonNode objNode) { |
| 810 | if ((deviceId == null) || (objNode == null)) { |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 811 | return Collections.EMPTY_LIST; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 812 | } |
| 813 | |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 814 | RestServerSBDevice device = null; |
| 815 | try { |
| 816 | device = (RestServerSBDevice) getController().getDevice(deviceId); |
| 817 | } catch (ClassCastException ccEx) { |
| 818 | return Collections.EMPTY_LIST; |
| 819 | } |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame] | 820 | if (device == null) { |
| 821 | return Collections.EMPTY_LIST; |
| 822 | } |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 823 | |
| 824 | Collection<PortStatistics> nicStats = Lists.newArrayList(); |
| 825 | |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 826 | JsonNode nicNode = objNode.path(PARAM_NICS); |
| 827 | |
| 828 | for (JsonNode nn : nicNode) { |
| 829 | ObjectNode nicObjNode = (ObjectNode) nn; |
| 830 | |
| 831 | // All the NIC attributes |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 832 | String nicName = get(nn, NIC_PARAM_NAME); |
| 833 | checkArgument(!Strings.isNullOrEmpty(nicName), "NIC name is empty or NULL"); |
| 834 | |
| 835 | long portNumber = device.portNumberFromName(nicName); |
| 836 | checkArgument(portNumber >= 0, "Unknown port ID " + portNumber + " for NIC " + nicName); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 837 | |
| 838 | long rxCount = nicObjNode.path(NIC_STATS_RX_COUNT).asLong(); |
| 839 | long rxBytes = nicObjNode.path(NIC_STATS_RX_BYTES).asLong(); |
| 840 | long rxDropped = nicObjNode.path(NIC_STATS_RX_DROPS).asLong(); |
| 841 | long rxErrors = nicObjNode.path(NIC_STATS_RX_ERRORS).asLong(); |
| 842 | long txCount = nicObjNode.path(NIC_STATS_TX_COUNT).asLong(); |
| 843 | long txBytes = nicObjNode.path(NIC_STATS_TX_BYTES).asLong(); |
| 844 | long txDropped = nicObjNode.path(NIC_STATS_TX_DROPS).asLong(); |
| 845 | long txErrors = nicObjNode.path(NIC_STATS_TX_ERRORS).asLong(); |
| 846 | |
| 847 | // Incorporate these statistics into an object |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 848 | DefaultPortStatistics.Builder nicBuilder = DefaultPortStatistics.builder(); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 849 | |
| 850 | nicBuilder.setDeviceId(deviceId) |
Ray Milkey | 5ec4208 | 2019-02-13 09:56:07 -0800 | [diff] [blame^] | 851 | .setPort(PortNumber.portNumber(portNumber)) |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 852 | .setPacketsReceived(rxCount) |
| 853 | .setPacketsSent(txCount) |
| 854 | .setBytesReceived(rxBytes) |
| 855 | .setBytesSent(txBytes) |
| 856 | .setPacketsRxDropped(rxDropped) |
| 857 | .setPacketsRxErrors(rxErrors) |
| 858 | .setPacketsTxDropped(txDropped) |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 859 | .setPacketsTxErrors(txErrors); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 860 | |
| 861 | // We have statistics for this NIC |
| 862 | nicStats.add(nicBuilder.build()); |
| 863 | } |
| 864 | |
| 865 | return nicStats; |
| 866 | } |
| 867 | |
| 868 | /** |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 869 | * Parse the input JSON object, looking for timing-related statistics. |
| 870 | * Upon success, return a timing statistics object with the advertized values. |
| 871 | * Upon failure, return a timing statistics object with zero-initialized values. |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 872 | * |
| 873 | * @param objNode input JSON node with timing statistics information |
| 874 | * @return TimingStatistics object or null |
| 875 | */ |
| 876 | private TimingStatistics parseTimingStatistics(JsonNode objNode) { |
| 877 | TimingStatistics timinsgStats = null; |
| 878 | |
| 879 | if (objNode == null) { |
| 880 | return timinsgStats; |
| 881 | } |
| 882 | |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 883 | // If no timing statistics are present, then send zeros |
| 884 | if (objNode.get(PARAM_TIMING_STATS) == null) { |
| 885 | return getZeroTimingStatistics(); |
| 886 | } |
| 887 | |
| 888 | DefaultTimingStatistics.Builder timingBuilder = DefaultTimingStatistics.builder(); |
| 889 | |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 890 | // Get timing statistics |
| 891 | JsonNode timingNode = objNode.path(PARAM_TIMING_STATS); |
| 892 | ObjectNode timingObjNode = (ObjectNode) timingNode; |
| 893 | |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 894 | // The unit of timing statistics |
| 895 | String timingStatsUnit = get(timingNode, MON_PARAM_UNIT); |
| 896 | if (!Strings.isNullOrEmpty(timingStatsUnit)) { |
| 897 | timingBuilder.setUnit(timingStatsUnit); |
| 898 | } |
| 899 | |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 900 | // Time (ns) to parse the controller's deployment instruction |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 901 | long parsingTime = 0; |
| 902 | if (timingObjNode.get(TIMING_PARAM_PARSE) != null) { |
| 903 | parsingTime = timingObjNode.path(TIMING_PARAM_PARSE).asLong(); |
| 904 | } |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 905 | // Time (ns) to do the deployment |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 906 | long launchingTime = 0; |
| 907 | if (timingObjNode.get(TIMING_PARAM_LAUNCH) != null) { |
| 908 | launchingTime = timingObjNode.path(TIMING_PARAM_LAUNCH).asLong(); |
| 909 | } |
| 910 | // Deployment time (ns) equals to time to parse + time to launch |
| 911 | long deployTime = 0; |
| 912 | if (timingObjNode.get(TIMING_PARAM_DEPLOY) != null) { |
| 913 | deployTime = timingObjNode.path(TIMING_PARAM_DEPLOY).asLong(); |
| 914 | } |
| 915 | checkArgument(deployTime == parsingTime + launchingTime, "Inconsistent timing statistics"); |
| 916 | |
| 917 | timingBuilder.setParsingTime(parsingTime) |
| 918 | .setLaunchingTime(launchingTime); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 919 | |
| 920 | // Get autoscale timing statistics |
| 921 | JsonNode autoscaleTimingNode = objNode.path(PARAM_TIMING_AUTOSCALE); |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 922 | if (autoscaleTimingNode == null) { |
| 923 | return timingBuilder.build(); |
| 924 | } |
| 925 | |
Georgios Katsikas | 973a265 | 2018-06-28 08:45:47 +0200 | [diff] [blame] | 926 | ObjectNode autoScaleTimingObjNode = (ObjectNode) autoscaleTimingNode; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 927 | // Time (ns) to autoscale a server's load |
Georgios Katsikas | 973a265 | 2018-06-28 08:45:47 +0200 | [diff] [blame] | 928 | long autoScaleTime = 0; |
| 929 | if (autoScaleTimingObjNode.get(TIMING_PARAM_AUTOSCALE) != null) { |
| 930 | autoScaleTime = autoScaleTimingObjNode.path(TIMING_PARAM_AUTOSCALE).asLong(); |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 931 | } |
Georgios Katsikas | 973a265 | 2018-06-28 08:45:47 +0200 | [diff] [blame] | 932 | timingBuilder.setAutoScaleTime(autoScaleTime); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 933 | |
| 934 | return timingBuilder.build(); |
| 935 | } |
| 936 | |
| 937 | /** |
| 938 | * Return a timing statistics object with zero counters. |
| 939 | * This is useful when constructing MonitoringStatistics |
| 940 | * objects that do not require timers. |
| 941 | * |
| 942 | * @return TimingStatistics object |
| 943 | */ |
| 944 | private TimingStatistics getZeroTimingStatistics() { |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 945 | DefaultTimingStatistics.Builder zeroTimingBuilder = DefaultTimingStatistics.builder(); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 946 | |
| 947 | zeroTimingBuilder.setParsingTime(0) |
| 948 | .setLaunchingTime(0) |
Georgios Katsikas | 973a265 | 2018-06-28 08:45:47 +0200 | [diff] [blame] | 949 | .setAutoScaleTime(0); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 950 | |
| 951 | return zeroTimingBuilder.build(); |
| 952 | } |
| 953 | |
| 954 | } |