Jian Li
Committed by Gerrit Code Review

Deprecate and clean up some classes on control metric aggregation

Since the metric aggregation logic has been moved to message
provider, now there is no caller which refers to those classes.
Due to this reason, following classes need to be purged to
maintain clean code repository.

Change-Id: Id5bcb7eda6ce3538c18007301da35df9255159ae
1 -/*
2 - * Copyright 2016-present Open Networking Laboratory
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 -package org.onosproject.cpman.impl;
17 -
18 -import com.google.common.collect.ImmutableMap;
19 -import com.google.common.collect.ImmutableSet;
20 -import com.google.common.collect.Sets;
21 -import org.onlab.metrics.MetricsService;
22 -import org.onosproject.cpman.ControlMetricType;
23 -import org.onosproject.net.DeviceId;
24 -import org.onosproject.net.device.DeviceService;
25 -
26 -import java.util.Map;
27 -import java.util.Optional;
28 -import java.util.Set;
29 -import java.util.concurrent.ConcurrentHashMap;
30 -
31 -/**
32 - * Singleton class to provide various control plane metrics to other components.
33 - */
34 -public final class ControlMetricsFactory {
35 - private MetricsService metricsService;
36 - private boolean enableMonitor = false;
37 - private Boolean isInitialized = false;
38 -
39 - // define a set of MetricsAggregators
40 - private MetricsAggregator cpuLoad;
41 - private MetricsAggregator totalCpuTime;
42 - private MetricsAggregator sysCpuTime;
43 - private MetricsAggregator userCpuTime;
44 - private MetricsAggregator cpuIdleTime;
45 - private MetricsAggregator memoryUsed;
46 - private MetricsAggregator memoryFree;
47 - private MetricsAggregator memoryUsedRatio;
48 - private MetricsAggregator memoryFreeRatio;
49 - private Map<String, MetricsAggregator> diskReadBytes;
50 - private Map<String, MetricsAggregator> diskWriteBytes;
51 - private Map<String, MetricsAggregator> nwIncomingBytes;
52 - private Map<String, MetricsAggregator> nwOutgoingBytes;
53 - private Map<String, MetricsAggregator> nwIncomingPackets;
54 - private Map<String, MetricsAggregator> nwOutgoingPackets;
55 -
56 - private Map<DeviceId, MetricsAggregator> inboundPacket;
57 - private Map<DeviceId, MetricsAggregator> outboundPacket;
58 - private Map<DeviceId, MetricsAggregator> flowmodPacket;
59 - private Map<DeviceId, MetricsAggregator> flowrmvPacket;
60 - private Map<DeviceId, MetricsAggregator> requestPacket;
61 - private Map<DeviceId, MetricsAggregator> replyPacket;
62 - private Set<DeviceId> deviceIds = Sets.newConcurrentHashSet();
63 - private Set<String> diskPartitions = Sets.newConcurrentHashSet();
64 - private Set<String> nwInterfaces = Sets.newConcurrentHashSet();
65 -
66 - /**
67 - * Initializes the control metrics factory instance using the given
68 - * metric service and device service. Makes sure that we only initialize
69 - * control metrics factory instance once.
70 - *
71 - * @param metricsService metric service
72 - * @param deviceService device service
73 - */
74 - public void initialization(MetricsService metricsService, DeviceService deviceService) {
75 - synchronized (isInitialized) {
76 - if (!isInitialized) {
77 - this.metricsService = metricsService;
78 - registerMetrics();
79 - deviceService.getDevices().forEach(d->deviceIds.add(d.id()));
80 - addAllControlMessageMetrics(deviceIds);
81 - isInitialized = true;
82 - }
83 - }
84 - }
85 -
86 - /**
87 - * Adds control metrics of a new device.
88 - *
89 - * @param deviceId device identifier
90 - */
91 - public void addControlMessageMetricsByDeviceId(DeviceId deviceId) {
92 - MetricsAggregator inbound = new MetricsAggregator(metricsService,
93 - ControlMetricType.INBOUND_PACKET, Optional.of(deviceId));
94 - MetricsAggregator outbound = new MetricsAggregator(metricsService,
95 - ControlMetricType.OUTBOUND_PACKET, Optional.of(deviceId));
96 - MetricsAggregator flowmod = new MetricsAggregator(metricsService,
97 - ControlMetricType.FLOW_MOD_PACKET, Optional.of(deviceId));
98 - MetricsAggregator flowrmv = new MetricsAggregator(metricsService,
99 - ControlMetricType.FLOW_REMOVED_PACKET, Optional.of(deviceId));
100 - MetricsAggregator request = new MetricsAggregator(metricsService,
101 - ControlMetricType.REQUEST_PACKET, Optional.of(deviceId));
102 - MetricsAggregator reply = new MetricsAggregator(metricsService,
103 - ControlMetricType.REPLY_PACKET, Optional.of(deviceId));
104 -
105 - inboundPacket.putIfAbsent(deviceId, inbound);
106 - outboundPacket.putIfAbsent(deviceId, outbound);
107 - flowmodPacket.putIfAbsent(deviceId, flowmod);
108 - flowrmvPacket.putIfAbsent(deviceId, flowrmv);
109 - requestPacket.putIfAbsent(deviceId, request);
110 - replyPacket.putIfAbsent(deviceId, reply);
111 -
112 - deviceIds.add(deviceId);
113 - }
114 -
115 - /**
116 - * Adds control metrics of a disk.
117 - *
118 - * @param partitionName disk partition name
119 - */
120 - public void addDiskMetricsByPartition(String partitionName) {
121 - MetricsAggregator readBytes = new MetricsAggregator(metricsService,
122 - ControlMetricType.DISK_READ_BYTES, partitionName);
123 - MetricsAggregator writeBytes = new MetricsAggregator(metricsService,
124 - ControlMetricType.DISK_WRITE_BYTES, partitionName);
125 -
126 - diskReadBytes.putIfAbsent(partitionName, readBytes);
127 - diskWriteBytes.putIfAbsent(partitionName, writeBytes);
128 -
129 - diskPartitions.add(partitionName);
130 - }
131 -
132 - /**
133 - * Adds control metrics of a ethernet interface.
134 - *
135 - * @param interfaceName network interface name
136 - */
137 - public void addNetworkMetricsByInterface(String interfaceName) {
138 - MetricsAggregator incomingBytes = new MetricsAggregator(metricsService,
139 - ControlMetricType.NW_INCOMING_BYTES, interfaceName);
140 - MetricsAggregator outgoingBytes = new MetricsAggregator(metricsService,
141 - ControlMetricType.NW_OUTGOING_BYTES, interfaceName);
142 - MetricsAggregator incomingPackets = new MetricsAggregator(metricsService,
143 - ControlMetricType.NW_INCOMING_PACKETS, interfaceName);
144 - MetricsAggregator outgoingPackets = new MetricsAggregator(metricsService,
145 - ControlMetricType.NW_OUTGOING_PACKETS, interfaceName);
146 -
147 - nwIncomingBytes.putIfAbsent(interfaceName, incomingBytes);
148 - nwOutgoingBytes.putIfAbsent(interfaceName, outgoingBytes);
149 - nwIncomingPackets.putIfAbsent(interfaceName, incomingPackets);
150 - nwOutgoingPackets.putIfAbsent(interfaceName, outgoingPackets);
151 -
152 - nwInterfaces.add(interfaceName);
153 - }
154 -
155 - /**
156 - * Removes control metrics of an existing device.
157 - *
158 - * @param deviceId device identifier
159 - */
160 - public void removeControlMessageMetricsByDeviceId(DeviceId deviceId) {
161 - inboundPacket.remove(deviceId);
162 - outboundPacket.remove(deviceId);
163 - flowmodPacket.remove(deviceId);
164 - flowrmvPacket.remove(deviceId);
165 - requestPacket.remove(deviceId);
166 - replyPacket.remove(deviceId);
167 -
168 - deviceIds.remove(deviceId);
169 - }
170 -
171 - /**
172 - * Removes control metrics of a disk.
173 - *
174 - * @param partitionName disk partition name
175 - */
176 - public void removeDiskMetricsByResourceName(String partitionName) {
177 - diskReadBytes.remove(partitionName);
178 - diskWriteBytes.remove(partitionName);
179 -
180 - diskPartitions.remove(partitionName);
181 - }
182 -
183 - /**
184 - * Removes control metrics of a network interface.
185 - *
186 - * @param interfaceName network interface name
187 - */
188 - public void removeNetworkInterfacesByResourceName(String interfaceName) {
189 - nwIncomingBytes.remove(interfaceName);
190 - nwOutgoingBytes.remove(interfaceName);
191 - nwIncomingPackets.remove(interfaceName);
192 - nwOutgoingPackets.remove(interfaceName);
193 -
194 - nwInterfaces.remove(interfaceName);
195 - }
196 -
197 - /**
198 - * Returns all device identifiers.
199 - *
200 - * @return a collection of device identifiers
201 - */
202 - public Set<DeviceId> getDeviceIds() {
203 - return ImmutableSet.copyOf(this.deviceIds);
204 - }
205 -
206 - /**
207 - * Returns all disk partition names.
208 - *
209 - * @return a collection of disk partitions.
210 - */
211 - public Set<String> getDiskPartitions() {
212 - return ImmutableSet.copyOf(this.diskPartitions);
213 - }
214 -
215 - /**
216 - * Returns all network interface names.
217 - *
218 - * @return a collection of network interfaces.
219 - */
220 - public Set<String> getNetworkInterfaces() {
221 - return ImmutableSet.copyOf(this.nwInterfaces);
222 - }
223 -
224 - /**
225 - * Adds control metrics for all devices.
226 - *
227 - * @param deviceIds a set of device identifiers
228 - */
229 - public void addAllControlMessageMetrics(Set<DeviceId> deviceIds) {
230 - deviceIds.forEach(v -> addControlMessageMetricsByDeviceId(v));
231 - }
232 -
233 - /**
234 - * Returns monitoring status.
235 - *
236 - * @return monitoring status
237 - */
238 - public boolean isMonitor() {
239 - return this.enableMonitor;
240 - }
241 -
242 - /**
243 - * Enable control plane monitoring.
244 - */
245 - protected void startMonitor() {
246 - this.enableMonitor = true;
247 - }
248 -
249 - /**
250 - * Disable control plane monitoring.
251 - */
252 - protected void stopMonitor() {
253 - this.enableMonitor = false;
254 - }
255 -
256 - /**
257 - * Registers new control metrics.
258 - */
259 - protected void registerMetrics() {
260 - /* CPU */
261 - cpuLoad = new MetricsAggregator(metricsService, ControlMetricType.CPU_LOAD);
262 - totalCpuTime = new MetricsAggregator(metricsService, ControlMetricType.TOTAL_CPU_TIME);
263 - sysCpuTime = new MetricsAggregator(metricsService, ControlMetricType.SYS_CPU_TIME);
264 - userCpuTime = new MetricsAggregator(metricsService, ControlMetricType.USER_CPU_TIME);
265 - cpuIdleTime = new MetricsAggregator(metricsService, ControlMetricType.CPU_IDLE_TIME);
266 -
267 - /* Memory */
268 - memoryFree = new MetricsAggregator(metricsService, ControlMetricType.MEMORY_FREE);
269 - memoryUsed = new MetricsAggregator(metricsService, ControlMetricType.MEMORY_USED);
270 - memoryFreeRatio = new MetricsAggregator(metricsService,
271 - ControlMetricType.MEMORY_FREE_RATIO);
272 - memoryUsedRatio = new MetricsAggregator(metricsService,
273 - ControlMetricType.MEMORY_USED_RATIO);
274 -
275 - /* Disk I/O */
276 - diskReadBytes = new ConcurrentHashMap<>();
277 - diskWriteBytes = new ConcurrentHashMap<>();
278 -
279 - /* Network I/O */
280 - nwIncomingBytes = new ConcurrentHashMap<>();
281 - nwOutgoingBytes = new ConcurrentHashMap<>();
282 - nwIncomingPackets = new ConcurrentHashMap<>();
283 - nwOutgoingPackets = new ConcurrentHashMap<>();
284 -
285 - /* OpenFlow Messages */
286 - inboundPacket = new ConcurrentHashMap<>();
287 - outboundPacket = new ConcurrentHashMap<>();
288 - flowmodPacket = new ConcurrentHashMap<>();
289 - flowrmvPacket = new ConcurrentHashMap<>();
290 - requestPacket = new ConcurrentHashMap<>();
291 - replyPacket = new ConcurrentHashMap<>();
292 - }
293 -
294 - /**
295 - * Unregisters all control metrics.
296 - */
297 - protected void unregisterMetrics() {
298 - /* Disk I/O */
299 - diskReadBytes.clear();
300 - diskWriteBytes.clear();
301 -
302 - /* Network I/O */
303 - nwIncomingBytes.clear();
304 - nwOutgoingBytes.clear();
305 - nwIncomingPackets.clear();
306 - nwOutgoingPackets.clear();
307 -
308 - /* OpenFlow Message */
309 - inboundPacket.clear();
310 - outboundPacket.clear();
311 - flowmodPacket.clear();
312 - flowrmvPacket.clear();
313 - requestPacket.clear();
314 - replyPacket.clear();
315 - }
316 -
317 - /**
318 - * Returns CPU load metric aggregator.
319 - *
320 - * @return metric aggregator
321 - */
322 - public MetricsAggregator cpuLoadMetric() {
323 - return cpuLoad;
324 - }
325 -
326 - /**
327 - * Returns total CPU time metric aggregator.
328 - *
329 - * @return metric aggregator
330 - */
331 - public MetricsAggregator totalCpuTimeMetric() {
332 - return totalCpuTime;
333 - }
334 -
335 - /**
336 - * Returns system CPU time metric aggregator.
337 - *
338 - * @return metric aggregator
339 - */
340 - public MetricsAggregator sysCpuTimeMetric() {
341 - return sysCpuTime;
342 - }
343 -
344 - /**
345 - * Returns user CPU time metric aggregator.
346 - *
347 - * @return metric aggregator
348 - */
349 - public MetricsAggregator userCpuTime() {
350 - return userCpuTime;
351 - }
352 -
353 - /**
354 - * Returns CPU idle time metric aggregator.
355 - *
356 - * @return metric aggregator
357 - */
358 - public MetricsAggregator cpuIdleTime() {
359 - return cpuIdleTime;
360 - }
361 -
362 - /**
363 - * Returns free memory ratio metric aggregator.
364 - *
365 - * @return metric aggregator
366 - */
367 - public MetricsAggregator memoryFreeRatio() {
368 - return memoryFreeRatio;
369 - }
370 -
371 - /**
372 - * Returns used memory ratio metric aggregator.
373 - *
374 - * @return metric aggregator
375 - */
376 - public MetricsAggregator memoryUsedRatio() {
377 - return memoryUsedRatio;
378 - }
379 -
380 - /**
381 - * Returns disk read bytes metric aggregator.
382 - *
383 - * @param resourceName name of disk resource
384 - * @return metric aggregator
385 - */
386 - public MetricsAggregator diskReadBytes(String resourceName) {
387 - return diskReadBytes.get(resourceName);
388 - }
389 -
390 - /**
391 - * Returns disk write bytes metric aggregator.
392 - *
393 - * @param resourceName name of disk resource
394 - * @return metric aggregator
395 - */
396 - public MetricsAggregator diskWriteBytes(String resourceName) {
397 - return diskWriteBytes.get(resourceName);
398 - }
399 -
400 - /**
401 - * Returns incoming bytes metric aggregator.
402 - *
403 - * @param interfaceName name of network interface
404 - * @return metric aggregator
405 - */
406 - public MetricsAggregator nwIncomingBytes(String interfaceName) {
407 - return nwIncomingBytes.get(interfaceName);
408 - }
409 -
410 - /**
411 - * Returns outgoing bytes metric aggregator.
412 - *
413 - * @param interfaceName name of network interface
414 - * @return metric aggregator
415 - */
416 - public MetricsAggregator nwOutgoingBytes(String interfaceName) {
417 - return nwOutgoingBytes.get(interfaceName);
418 - }
419 -
420 - /**
421 - * Returns incoming packets metric aggregator.
422 - *
423 - * @param interfaceName name of network interface
424 - * @return metric aggregator
425 - */
426 - public MetricsAggregator nwIncomingPackets(String interfaceName) {
427 - return nwIncomingPackets.get(interfaceName);
428 - }
429 -
430 - /**
431 - * Returns outgoing packets metric aggregator.
432 - *
433 - * @param interfaceName name of network interface
434 - * @return metric aggregator
435 - */
436 - public MetricsAggregator nwOutgoingPackets(String interfaceName) {
437 - return nwOutgoingPackets.get(interfaceName);
438 - }
439 -
440 - /**
441 - * Returns inbound packet metric aggregator of all devices.
442 - *
443 - * @return metric aggregator
444 - */
445 - public Map<DeviceId, MetricsAggregator> inboundPacket() {
446 - return ImmutableMap.copyOf(inboundPacket);
447 - }
448 -
449 - /**
450 - * Returns outgoing packet metric aggregator of all devices.
451 - *
452 - * @return metric aggregator
453 - */
454 - public Map<DeviceId, MetricsAggregator> outboundPacket() {
455 - return ImmutableMap.copyOf(outboundPacket);
456 - }
457 -
458 - /**
459 - * Returns flow-mod packet metric aggregator of all devices.
460 - *
461 - * @return metric aggregator
462 - */
463 - public Map<DeviceId, MetricsAggregator> flowmodPacket() {
464 - return ImmutableMap.copyOf(flowmodPacket);
465 - }
466 -
467 - /**
468 - * Returns flow-removed packet metric aggregator of all devices.
469 - *
470 - * @return metric aggregator
471 - */
472 - public Map<DeviceId, MetricsAggregator> flowrmvPacket() {
473 - return ImmutableMap.copyOf(flowrmvPacket);
474 - }
475 -
476 - /**
477 - * Returns request packet metric aggregator of all devices.
478 - *
479 - * @return metric aggregator
480 - */
481 - public Map<DeviceId, MetricsAggregator> requestPacket() {
482 - return ImmutableMap.copyOf(requestPacket);
483 - }
484 -
485 - /**
486 - * Returns reply packet metric aggregator of all devices.
487 - *
488 - * @return metric aggregator
489 - */
490 - public Map<DeviceId, MetricsAggregator> replyPacket() {
491 - return ImmutableMap.copyOf(replyPacket);
492 - }
493 -
494 - /**
495 - * Returns inbound packet metric aggregator of a specified device.
496 - *
497 - * @param deviceId device identifier
498 - * @return metric aggregator
499 - */
500 - public MetricsAggregator inboundPacket(DeviceId deviceId) {
501 - return inboundPacket.get(deviceId);
502 - }
503 -
504 - /**
505 - * Returns outbound packet metric aggregator of a specified device.
506 - *
507 - * @param deviceId device identifier
508 - * @return metric aggregator
509 - */
510 - public MetricsAggregator outboundPacket(DeviceId deviceId) {
511 - return outboundPacket.get(deviceId);
512 - }
513 -
514 - /**
515 - * Returns flow-mod packet metric aggregator of a specified device.
516 - *
517 - * @param deviceId device identifier
518 - * @return metric aggregator
519 - */
520 - public MetricsAggregator flowmodPacket(DeviceId deviceId) {
521 - return flowmodPacket.get(deviceId);
522 - }
523 -
524 - /**
525 - * Returns flow-removed packet metric aggregator of a specified device.
526 - *
527 - * @param deviceId device identifier
528 - * @return metric aggregator
529 - */
530 - public MetricsAggregator flowrmvPacket(DeviceId deviceId) {
531 - return flowrmvPacket.get(deviceId);
532 - }
533 -
534 - /**
535 - * Returns request packet metric aggregator of a specified device.
536 - *
537 - * @param deviceId device identifier
538 - * @return metric aggregator
539 - */
540 - public MetricsAggregator requestPacket(DeviceId deviceId) {
541 - return requestPacket.get(deviceId);
542 - }
543 -
544 - /**
545 - * Returns reply packet metric aggregator of a specified device.
546 - *
547 - * @param deviceId device identifier
548 - * @return metric aggregator
549 - */
550 - public MetricsAggregator replyPacket(DeviceId deviceId) {
551 - return replyPacket.get(deviceId);
552 - }
553 -
554 - /**
555 - * Returns an instance of control metrics factory.
556 - *
557 - * @return instance of control metrics factory
558 - */
559 - public static ControlMetricsFactory getInstance() {
560 - return SingletonHelper.INSTANCE;
561 - }
562 -
563 - private static class SingletonHelper {
564 - private static final ControlMetricsFactory INSTANCE = new ControlMetricsFactory();
565 - }
566 -}
...\ No newline at end of file ...\ No newline at end of file
1 -/*
2 - * Copyright 2016-present Open Networking Laboratory
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 -package org.onosproject.cpman.impl;
17 -
18 -import org.onosproject.net.DeviceId;
19 -
20 -import java.util.Optional;
21 -
22 -/**
23 - * Control metrics observer interface.
24 - */
25 -public interface ControlMetricsObserver {
26 -
27 - /**
28 - * Feeds the extracted value from MetricAggregator to back-end storage.
29 - *
30 - * @param metricsAggregator metric aggregator
31 - * @param deviceId device identification
32 - */
33 - void feedMetrics(MetricsAggregator metricsAggregator, Optional<DeviceId> deviceId);
34 -
35 - /**
36 - * Feeds the extracted value from MetricAggregator to back-end storage.
37 - *
38 - * @param metricsAggregator metric aggregator
39 - * @param resourceName resource name
40 - */
41 - void feedMetrics(MetricsAggregator metricsAggregator, String resourceName);
42 -}
1 -/*
2 - * Copyright 2016-present Open Networking Laboratory
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 -package org.onosproject.cpman.impl;
17 -
18 -import org.apache.felix.scr.annotations.Reference;
19 -import org.apache.felix.scr.annotations.ReferenceCardinality;
20 -import org.onosproject.cpman.ControlMetric;
21 -import org.onosproject.cpman.ControlPlaneMonitorService;
22 -import org.onosproject.cpman.MetricValue;
23 -import org.onosproject.net.DeviceId;
24 -
25 -import java.util.Optional;
26 -
27 -/**
28 - * Default ControlMetricsObserver.
29 - */
30 -public class DefaultControlMetricsObserver implements ControlMetricsObserver {
31 -
32 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
33 - protected ControlPlaneMonitorService controlPlaneMonitorService;
34 -
35 - @Override
36 - public void feedMetrics(MetricsAggregator ma, Optional<DeviceId> deviceId) {
37 - MetricValue mv = new MetricValue.Builder()
38 - .rate(ma.getRate())
39 - .count(ma.getCount())
40 - .load(ma.getLoad())
41 - .add();
42 - ControlMetric cm = new ControlMetric(ma.getMetricsType(), mv);
43 - controlPlaneMonitorService.updateMetric(cm, 1, deviceId);
44 - }
45 -
46 - @Override
47 - public void feedMetrics(MetricsAggregator ma, String resourceName) {
48 - MetricValue mv = new MetricValue.Builder()
49 - .rate(ma.getRate())
50 - .count(ma.getCount())
51 - .load(ma.getLoad())
52 - .add();
53 - ControlMetric cm = new ControlMetric(ma.getMetricsType(), mv);
54 - controlPlaneMonitorService.updateMetric(cm, 1, resourceName);
55 - }
56 -}
...\ No newline at end of file ...\ No newline at end of file
1 -/*
2 - * Copyright 2016-present Open Networking Laboratory
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 -package org.onosproject.cpman.impl;
17 -
18 -import com.codahale.metrics.Meter;
19 -import org.apache.commons.lang3.StringUtils;
20 -import org.onlab.metrics.MetricsComponent;
21 -import org.onlab.metrics.MetricsFeature;
22 -import org.onlab.metrics.MetricsService;
23 -import org.onosproject.cpman.ControlMetricType;
24 -import org.onosproject.net.DeviceId;
25 -
26 -import java.util.Optional;
27 -
28 -import static com.google.common.base.Preconditions.checkNotNull;
29 -
30 -/**
31 - * An aggregator that aggregates a specific network or performance metrics via the metrics service.
32 - */
33 -public class MetricsAggregator {
34 -
35 - private Meter rateMeter;
36 - private Meter countMeter;
37 - private MetricsService metricsService;
38 - private MetricsComponent metricsComponent;
39 - private MetricsFeature metricsFeature;
40 - private ControlMetricType metricsType;
41 - private static final int EXECUTE_PERIOD_IN_SECOND = 60;
42 - private static final String RATE_NAME = "rate";
43 - private static final String COUNT_NAME = "count";
44 -
45 - /**
46 - * Constructs a new metrics aggregator for aggregating a metric.
47 - * Instantiates the metrics service
48 - * Initializes all the general metrics for that object
49 - *
50 - * @param metricsService metric service reference
51 - * @param type control metric type
52 - * @param deviceId device identification
53 - */
54 - MetricsAggregator(MetricsService metricsService, ControlMetricType type,
55 - Optional<DeviceId> deviceId) {
56 - init(metricsService, type, deviceId, null);
57 - }
58 -
59 - /**
60 - * Constructs a new metrics aggregator for aggregating a metric.
61 - * Instantiates the metrics service
62 - * Initializes all the general metrics for that object
63 - *
64 - * @param metricsService metric service reference
65 - * @param type control metric type
66 - * @param resourceName resource name (e.g., ethernet interface name)
67 - */
68 - MetricsAggregator(MetricsService metricsService, ControlMetricType type,
69 - String resourceName) {
70 - init(metricsService, type, Optional.ofNullable(null), resourceName);
71 -
72 - }
73 -
74 - /**
75 - * Constructs a new metrics aggregator for aggregating a metric.
76 - * Instantiates the metrics service
77 - * Initializes all the general metrics for that object
78 - *
79 - * @param metricsService metrics service reference
80 - * @param type control metric type
81 - */
82 - MetricsAggregator(MetricsService metricsService, ControlMetricType type) {
83 - init(metricsService, type, Optional.ofNullable(null), null);
84 - }
85 -
86 - /**
87 - * Base method of the constructor of this class.
88 - *
89 - * @param metricsService metrics service reference
90 - * @param type control metric type
91 - * @param deviceId device identification
92 - * @param resourceName resource name
93 - */
94 - private void init(MetricsService metricsService, ControlMetricType type,
95 - Optional<DeviceId> deviceId, String resourceName) {
96 - String primitiveName = type.toString();
97 - String objName = "all";
98 - if (deviceId.isPresent()) {
99 - objName = deviceId.toString();
100 - }
101 -
102 - if (StringUtils.isNotEmpty(resourceName)) {
103 - objName = resourceName;
104 - }
105 -
106 - checkNotNull(primitiveName, "Component name cannot be null");
107 - checkNotNull(objName, "Feature name cannot be null");
108 -
109 - this.metricsType = type;
110 -
111 - this.metricsService = metricsService;
112 - this.metricsComponent = metricsService.registerComponent(primitiveName);
113 - this.metricsFeature = metricsComponent.registerFeature(objName);
114 -
115 - this.rateMeter = metricsService.createMeter(metricsComponent, metricsFeature, RATE_NAME);
116 - this.countMeter = metricsService.createMeter(metricsComponent, metricsFeature, COUNT_NAME);
117 - }
118 -
119 - /**
120 - * Returns control metrics type.
121 - *
122 - * @return control metrics type
123 - */
124 - public ControlMetricType getMetricsType() {
125 - return metricsType;
126 - }
127 -
128 - /**
129 - * Increments the meter rate by n, and the meter counter by 1.
130 - *
131 - * @param n increment rate.
132 - */
133 - public void increment(long n) {
134 - rateMeter.mark(n);
135 - countMeter.mark(1);
136 - }
137 -
138 - /**
139 - * Returns the average load value.
140 - *
141 - * @return load value
142 - */
143 - public long getLoad() {
144 - return (long) rateMeter.getOneMinuteRate() / (long) countMeter.getOneMinuteRate();
145 - }
146 -
147 - /**
148 - * Returns the average meter rate within recent 1 minute.
149 - *
150 - * @return rate value
151 - */
152 - public long getRate() {
153 - return (long) rateMeter.getOneMinuteRate();
154 - }
155 -
156 - /**
157 - * Returns the average meter count within recent 1 minute.
158 - *
159 - * @return count value
160 - */
161 - public long getCount() {
162 - return (long) countMeter.getOneMinuteRate() * EXECUTE_PERIOD_IN_SECOND;
163 - }
164 -}
...\ No newline at end of file ...\ No newline at end of file