blob: 2460d6c169d09abf34085a02139d817da3d21eb8 [file] [log] [blame]
Jian Li60804322015-12-02 14:46:31 -08001/*
Jian Lie044d1a2016-01-25 09:01:20 -08002 * Copyright 2015-2016 Open Networking Laboratory
Jian Li60804322015-12-02 14:46:31 -08003 *
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 */
Jian Li6b86a762016-01-29 09:30:40 -080016package org.onosproject.cpman.impl;
Jian Li60804322015-12-02 14:46:31 -080017
Jian Lie044d1a2016-01-25 09:01:20 -080018import com.google.common.collect.ImmutableMap;
19import com.google.common.collect.ImmutableSet;
20import com.google.common.collect.Sets;
Jian Li60804322015-12-02 14:46:31 -080021import org.onlab.metrics.MetricsService;
Jian Li6b86a762016-01-29 09:30:40 -080022import org.onosproject.cpman.ControlMetricType;
Jian Li60804322015-12-02 14:46:31 -080023import org.onosproject.net.DeviceId;
24import org.onosproject.net.device.DeviceService;
25
Jian Li60804322015-12-02 14:46:31 -080026import java.util.Map;
27import java.util.Optional;
28import java.util.Set;
29import java.util.concurrent.ConcurrentHashMap;
30
31/**
32 * Singleton class to provide various control plane metrics to other components.
33 */
34public final class ControlMetricsFactory {
35 private static volatile ControlMetricsFactory uniqueInstance;
36
37 private MetricsService metricsService;
38 private boolean enableMonitor = false;
39
Jian Lie044d1a2016-01-25 09:01:20 -080040 // define a ControlMetricsSystemSpec
41 private ControlMetricsSystemSpec cmss;
42
Jian Li60804322015-12-02 14:46:31 -080043 // define a set of MetricsAggregators
Jian Lie044d1a2016-01-25 09:01:20 -080044 private MetricsAggregator cpuLoad;
45 private MetricsAggregator totalCpuTime;
46 private MetricsAggregator sysCpuTime;
47 private MetricsAggregator userCpuTime;
48 private MetricsAggregator cpuIdleTime;
49 private MetricsAggregator memoryUsed;
50 private MetricsAggregator memoryFree;
Jian Li7d180c52016-02-01 21:53:08 -080051 private MetricsAggregator memoryUsedRatio;
52 private MetricsAggregator memoryFreeRatio;
Jian Lie044d1a2016-01-25 09:01:20 -080053 private Map<String, MetricsAggregator> diskReadBytes;
54 private Map<String, MetricsAggregator> diskWriteBytes;
55 private Map<String, MetricsAggregator> nwIncomingBytes;
56 private Map<String, MetricsAggregator> nwOutgoingBytes;
57 private Map<String, MetricsAggregator> nwIncomingPackets;
58 private Map<String, MetricsAggregator> nwOutgoingPackets;
59
60 private Map<DeviceId, MetricsAggregator> inboundPacket;
61 private Map<DeviceId, MetricsAggregator> outboundPacket;
62 private Map<DeviceId, MetricsAggregator> flowmodPacket;
63 private Map<DeviceId, MetricsAggregator> flowrmvPacket;
64 private Map<DeviceId, MetricsAggregator> requestPacket;
65 private Map<DeviceId, MetricsAggregator> replyPacket;
66 private Set<DeviceId> deviceIds = Sets.newConcurrentHashSet();
67 private Set<String> diskPartitions = Sets.newConcurrentHashSet();
68 private Set<String> nwInterfaces = Sets.newConcurrentHashSet();
Jian Li60804322015-12-02 14:46:31 -080069
70 private ControlMetricsFactory(MetricsService metricsService, DeviceService deviceService) {
71 this.metricsService = metricsService;
72 registerMetrics();
73
74 deviceService.getDevices().forEach(d->deviceIds.add(d.id()));
75
Jian Lie044d1a2016-01-25 09:01:20 -080076 addAllControlMessageMetrics(deviceIds);
Jian Li60804322015-12-02 14:46:31 -080077 }
78
79 public static ControlMetricsFactory getInstance(MetricsService metricsService,
80 DeviceService deviceService) {
81 if (uniqueInstance == null) {
82 synchronized (ControlMetricsFactory.class) {
83 if (uniqueInstance == null) {
84 uniqueInstance = new ControlMetricsFactory(metricsService, deviceService);
85 }
86 }
87 }
88 return uniqueInstance;
89 }
90
91 /**
Jian Lie044d1a2016-01-25 09:01:20 -080092 * Sets system specification.
93 *
94 * @param cmss ControlMetricsSystemSpec object
95 */
96 public void setSystemSpec(ControlMetricsSystemSpec cmss) {
97 if (this.cmss == null) {
98 this.cmss = cmss;
99 }
100 }
101
102 /**
Jian Li60804322015-12-02 14:46:31 -0800103 * Adds control metrics of a new device.
104 *
105 * @param deviceId {@link org.onosproject.net.DeviceId}
106 */
Jian Lie044d1a2016-01-25 09:01:20 -0800107 public void addControlMessageMetricsByDeviceId(DeviceId deviceId) {
Jian Li60804322015-12-02 14:46:31 -0800108 MetricsAggregator inbound = new MetricsAggregator(metricsService,
109 ControlMetricType.INBOUND_PACKET, Optional.of(deviceId));
110 MetricsAggregator outbound = new MetricsAggregator(metricsService,
111 ControlMetricType.OUTBOUND_PACKET, Optional.of(deviceId));
112 MetricsAggregator flowmod = new MetricsAggregator(metricsService,
113 ControlMetricType.FLOW_MOD_PACKET, Optional.of(deviceId));
114 MetricsAggregator flowrmv = new MetricsAggregator(metricsService,
115 ControlMetricType.FLOW_REMOVED_PACKET, Optional.of(deviceId));
116 MetricsAggregator request = new MetricsAggregator(metricsService,
117 ControlMetricType.REQUEST_PACKET, Optional.of(deviceId));
118 MetricsAggregator reply = new MetricsAggregator(metricsService,
119 ControlMetricType.REPLY_PACKET, Optional.of(deviceId));
120
Jian Lie044d1a2016-01-25 09:01:20 -0800121 inboundPacket.putIfAbsent(deviceId, inbound);
122 outboundPacket.putIfAbsent(deviceId, outbound);
123 flowmodPacket.putIfAbsent(deviceId, flowmod);
124 flowrmvPacket.putIfAbsent(deviceId, flowrmv);
125 requestPacket.putIfAbsent(deviceId, request);
126 replyPacket.putIfAbsent(deviceId, reply);
Jian Li60804322015-12-02 14:46:31 -0800127
128 deviceIds.add(deviceId);
129 }
130
131 /**
Jian Lie044d1a2016-01-25 09:01:20 -0800132 * Adds control metrics of a disk.
133 *
134 * @param partitionName disk partition name
135 */
136 public void addDiskMetricsByPartition(String partitionName) {
137 MetricsAggregator readBytes = new MetricsAggregator(metricsService,
138 ControlMetricType.DISK_READ_BYTES, partitionName);
139 MetricsAggregator writeBytes = new MetricsAggregator(metricsService,
140 ControlMetricType.DISK_WRITE_BYTES, partitionName);
141
142 diskReadBytes.putIfAbsent(partitionName, readBytes);
143 diskWriteBytes.putIfAbsent(partitionName, writeBytes);
144
145 diskPartitions.add(partitionName);
146 }
147
148 /**
149 * Adds control metrics of a ethernet interface.
150 *
151 * @param interfaceName network interface name
152 */
153 public void addNetworkMetricsByInterface(String interfaceName) {
154 MetricsAggregator incomingBytes = new MetricsAggregator(metricsService,
155 ControlMetricType.NW_INCOMING_BYTES, interfaceName);
156 MetricsAggregator outgoingBytes = new MetricsAggregator(metricsService,
157 ControlMetricType.NW_OUTGOING_BYTES, interfaceName);
158 MetricsAggregator incomingPackets = new MetricsAggregator(metricsService,
159 ControlMetricType.NW_INCOMING_PACKETS, interfaceName);
160 MetricsAggregator outgoingPackets = new MetricsAggregator(metricsService,
161 ControlMetricType.NW_OUTGOING_PACKETS, interfaceName);
162
163 nwIncomingBytes.putIfAbsent(interfaceName, incomingBytes);
164 nwOutgoingBytes.putIfAbsent(interfaceName, outgoingBytes);
165 nwIncomingPackets.putIfAbsent(interfaceName, incomingPackets);
166 nwOutgoingPackets.putIfAbsent(interfaceName, outgoingPackets);
167
168 nwInterfaces.add(interfaceName);
169 }
170
171 /**
Jian Li60804322015-12-02 14:46:31 -0800172 * Removes control metrics of an existing device.
173 *
174 * @param deviceId {@link org.onosproject.net.DeviceId}
175 */
Jian Lie044d1a2016-01-25 09:01:20 -0800176 public void removeControlMessageMetricsByDeviceId(DeviceId deviceId) {
177 inboundPacket.remove(deviceId);
178 outboundPacket.remove(deviceId);
179 flowmodPacket.remove(deviceId);
180 flowrmvPacket.remove(deviceId);
181 requestPacket.remove(deviceId);
182 replyPacket.remove(deviceId);
Jian Li60804322015-12-02 14:46:31 -0800183
184 deviceIds.remove(deviceId);
185 }
186
Jian Lie044d1a2016-01-25 09:01:20 -0800187 /**
188 * Removes control metrics of a disk.
189 *
190 * @param partitionName disk partition name
191 */
192 public void removeDiskMetricsByResourceName(String partitionName) {
193 diskReadBytes.remove(partitionName);
194 diskWriteBytes.remove(partitionName);
195
196 diskPartitions.remove(partitionName);
197 }
198
199 /**
200 * Removes control metrics of a network interface.
201 *
202 * @param interfaceName network interface name
203 */
204 public void removeNetworkInterfacesByResourceName(String interfaceName) {
205 nwIncomingBytes.remove(interfaceName);
206 nwOutgoingBytes.remove(interfaceName);
207 nwIncomingPackets.remove(interfaceName);
208 nwOutgoingPackets.remove(interfaceName);
209
210 nwInterfaces.remove(interfaceName);
211 }
212
213 /**
214 * Returns all device ids.
215 *
216 * @return a collection of device id
217 */
Jian Li60804322015-12-02 14:46:31 -0800218 public Set<DeviceId> getDeviceIds() {
Jian Lie044d1a2016-01-25 09:01:20 -0800219 return ImmutableSet.copyOf(this.deviceIds);
220 }
221
222 /**
223 * Returns all disk partition names.
224 *
225 * @return a collection of disk partition.
226 */
227 public Set<String> getDiskPartitions() {
228 return ImmutableSet.copyOf(this.diskPartitions);
229 }
230
231 /**
232 * Returns all network interface names.
233 *
234 * @return a collection of network interface.
235 */
236 public Set<String> getNetworkInterfaces() {
237 return ImmutableSet.copyOf(this.nwInterfaces);
Jian Li60804322015-12-02 14:46:31 -0800238 }
239
240 /**
241 * Adds control metrics for all devices.
242 *
243 * @param deviceIds a set of deviceIds
244 */
Jian Lie044d1a2016-01-25 09:01:20 -0800245 public void addAllControlMessageMetrics(Set<DeviceId> deviceIds) {
246 deviceIds.forEach(v -> addControlMessageMetricsByDeviceId(v));
Jian Li60804322015-12-02 14:46:31 -0800247 }
248
249 /**
250 * Returns monitoring status.
251 *
252 * @return monitoring status
253 */
254 public boolean isMonitor() {
255 return this.enableMonitor;
256 }
257
258 /**
259 * Enable control plane monitoring.
260 */
261 protected void startMonitor() {
262 this.enableMonitor = true;
263 }
264
265 /**
266 * Disable control plane monitoring.
267 */
268 protected void stopMonitor() {
269 this.enableMonitor = false;
270 }
271
272 /**
273 * Registers new control metrics.
274 */
275 protected void registerMetrics() {
Jian Lie044d1a2016-01-25 09:01:20 -0800276 /* CPU */
277 cpuLoad = new MetricsAggregator(metricsService, ControlMetricType.CPU_LOAD);
278 totalCpuTime = new MetricsAggregator(metricsService, ControlMetricType.TOTAL_CPU_TIME);
279 sysCpuTime = new MetricsAggregator(metricsService, ControlMetricType.SYS_CPU_TIME);
280 userCpuTime = new MetricsAggregator(metricsService, ControlMetricType.USER_CPU_TIME);
281 cpuIdleTime = new MetricsAggregator(metricsService, ControlMetricType.CPU_IDLE_TIME);
Jian Li60804322015-12-02 14:46:31 -0800282
Jian Lie044d1a2016-01-25 09:01:20 -0800283 /* Memory */
284 memoryFree = new MetricsAggregator(metricsService, ControlMetricType.MEMORY_FREE);
285 memoryUsed = new MetricsAggregator(metricsService, ControlMetricType.MEMORY_USED);
Jian Li7d180c52016-02-01 21:53:08 -0800286 memoryFreeRatio = new MetricsAggregator(metricsService,
287 ControlMetricType.MEMORY_FREE_RATIO);
288 memoryUsedRatio = new MetricsAggregator(metricsService,
289 ControlMetricType.MEMORY_USED_RATIO);
Jian Lie044d1a2016-01-25 09:01:20 -0800290
291 /* Disk I/O */
292 diskReadBytes = new ConcurrentHashMap<>();
293 diskWriteBytes = new ConcurrentHashMap<>();
294
295 /* Network I/O */
296 nwIncomingBytes = new ConcurrentHashMap<>();
297 nwOutgoingBytes = new ConcurrentHashMap<>();
298 nwIncomingPackets = new ConcurrentHashMap<>();
299 nwOutgoingPackets = new ConcurrentHashMap<>();
300
301 /* OpenFlow Messages */
302 inboundPacket = new ConcurrentHashMap<>();
303 outboundPacket = new ConcurrentHashMap<>();
304 flowmodPacket = new ConcurrentHashMap<>();
305 flowrmvPacket = new ConcurrentHashMap<>();
306 requestPacket = new ConcurrentHashMap<>();
307 replyPacket = new ConcurrentHashMap<>();
Jian Li60804322015-12-02 14:46:31 -0800308 }
309
310 /**
311 * Unregisters all control metrics.
312 */
313 protected void unregisterMetrics() {
Jian Lie044d1a2016-01-25 09:01:20 -0800314 /* Disk I/O */
315 diskReadBytes.clear();
316 diskWriteBytes.clear();
Jian Li60804322015-12-02 14:46:31 -0800317
Jian Lie044d1a2016-01-25 09:01:20 -0800318 /* Network I/O */
319 nwIncomingBytes.clear();
320 nwOutgoingBytes.clear();
321 nwIncomingPackets.clear();
322 nwOutgoingPackets.clear();
323
324 /* OpenFlow Message */
325 inboundPacket.clear();
326 outboundPacket.clear();
327 flowmodPacket.clear();
328 flowrmvPacket.clear();
329 requestPacket.clear();
330 replyPacket.clear();
Jian Li60804322015-12-02 14:46:31 -0800331 }
332
Jian Lie044d1a2016-01-25 09:01:20 -0800333 public MetricsAggregator cpuLoadMetric() {
334 return cpuLoad;
Jian Li60804322015-12-02 14:46:31 -0800335 }
336
Jian Lie044d1a2016-01-25 09:01:20 -0800337 public MetricsAggregator totalCpuTimeMetric() {
338 return totalCpuTime;
Jian Li60804322015-12-02 14:46:31 -0800339 }
340
Jian Lie044d1a2016-01-25 09:01:20 -0800341 public MetricsAggregator sysCpuTimeMetric() {
342 return sysCpuTime;
Jian Li60804322015-12-02 14:46:31 -0800343 }
344
Jian Lie044d1a2016-01-25 09:01:20 -0800345 public MetricsAggregator userCpuTime() {
346 return userCpuTime;
Jian Li60804322015-12-02 14:46:31 -0800347 }
348
Jian Lie044d1a2016-01-25 09:01:20 -0800349 public MetricsAggregator cpuIdleTime() {
350 return cpuIdleTime;
Jian Li60804322015-12-02 14:46:31 -0800351 }
352
Jian Li7d180c52016-02-01 21:53:08 -0800353 public MetricsAggregator memoryFreeRatio() {
354 return memoryFreeRatio;
Jian Li60804322015-12-02 14:46:31 -0800355 }
356
Jian Li7d180c52016-02-01 21:53:08 -0800357 public MetricsAggregator memoryUsedRatio() {
358 return memoryUsedRatio;
Jian Li60804322015-12-02 14:46:31 -0800359 }
360
Jian Lie044d1a2016-01-25 09:01:20 -0800361 public MetricsAggregator diskReadBytes(String partitionName) {
362 return diskReadBytes.get(partitionName);
Jian Li60804322015-12-02 14:46:31 -0800363 }
364
Jian Lie044d1a2016-01-25 09:01:20 -0800365 public MetricsAggregator diskWriteBytes(String partitionName) {
366 return diskWriteBytes.get(partitionName);
Jian Li60804322015-12-02 14:46:31 -0800367 }
368
Jian Lie044d1a2016-01-25 09:01:20 -0800369 public MetricsAggregator nwIncomingBytes(String interfaceName) {
370 return nwIncomingBytes.get(interfaceName);
Jian Li60804322015-12-02 14:46:31 -0800371 }
372
Jian Lie044d1a2016-01-25 09:01:20 -0800373 public MetricsAggregator nwOutgoingBytes(String interfaceName) {
374 return nwOutgoingBytes.get(interfaceName);
Jian Li60804322015-12-02 14:46:31 -0800375 }
376
Jian Lie044d1a2016-01-25 09:01:20 -0800377 public MetricsAggregator nwIncomingPackets(String interfaceName) {
378 return nwIncomingPackets.get(interfaceName);
Jian Li60804322015-12-02 14:46:31 -0800379 }
380
Jian Lie044d1a2016-01-25 09:01:20 -0800381 public MetricsAggregator nwOutgoingPackets(String interfaceName) {
382 return nwOutgoingPackets.get(interfaceName);
Jian Li60804322015-12-02 14:46:31 -0800383 }
384
Jian Lie044d1a2016-01-25 09:01:20 -0800385 public Map<DeviceId, MetricsAggregator> inboundPacket() {
386 return ImmutableMap.copyOf(inboundPacket);
387 }
388
389 public Map<DeviceId, MetricsAggregator> outboundPacket() {
390 return ImmutableMap.copyOf(outboundPacket);
391 }
392
393 public Map<DeviceId, MetricsAggregator> flowmodPacket() {
394 return ImmutableMap.copyOf(flowmodPacket);
395 }
396
397 public Map<DeviceId, MetricsAggregator> flowrmvPacket() {
398 return ImmutableMap.copyOf(flowrmvPacket);
399 }
400
401 public Map<DeviceId, MetricsAggregator> requestPacket() {
402 return ImmutableMap.copyOf(requestPacket);
403 }
404
405 public Map<DeviceId, MetricsAggregator> replyPacket() {
406 return ImmutableMap.copyOf(replyPacket);
407 }
408
409 public MetricsAggregator inboundPacket(DeviceId deviceId) {
410 return inboundPacket.get(deviceId);
411 }
412
413 public MetricsAggregator outboundPacket(DeviceId deviceId) {
414 return outboundPacket.get(deviceId);
415 }
416
417 public MetricsAggregator flowmodPacket(DeviceId deviceId) {
418 return flowmodPacket.get(deviceId);
419 }
420
421 public MetricsAggregator flowrmvPacket(DeviceId deviceId) {
422 return flowrmvPacket.get(deviceId);
423 }
424
425 public MetricsAggregator requestPacket(DeviceId deviceId) {
426 return requestPacket.get(deviceId);
427 }
428
429 public MetricsAggregator replyPacket(DeviceId deviceId) {
430 return replyPacket.get(deviceId);
Jian Li60804322015-12-02 14:46:31 -0800431 }
432}