blob: c01fabd0c6783c70a402f08cc9f7da459c368e4d [file] [log] [blame]
Jian Li0967cd72015-11-25 17:38:48 -08001/*
2 * Copyright 2015 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 */
16package org.onosproject.cpman;
17
Jian Li0967cd72015-11-25 17:38:48 -080018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Deactivate;
20import org.apache.felix.scr.annotations.Modified;
Jian Li60804322015-12-02 14:46:31 -080021import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.onlab.metrics.MetricsService;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
27import org.onosproject.net.device.DeviceService;
Jian Li0967cd72015-11-25 17:38:48 -080028import org.slf4j.Logger;
29
Jian Li60804322015-12-02 14:46:31 -080030import java.util.HashSet;
31import java.util.Optional;
32import java.util.Set;
33
Jian Li0967cd72015-11-25 17:38:48 -080034import static org.slf4j.LoggerFactory.getLogger;
35
36/**
37 * Control plane management application.
38 */
Jian Li60804322015-12-02 14:46:31 -080039@Component(immediate = true)
Jian Li0967cd72015-11-25 17:38:48 -080040public class ControlPlaneManager {
41
42 private final Logger log = getLogger(getClass());
Jian Li60804322015-12-02 14:46:31 -080043 private Set<ControlMetricsObserver> controlMetricsObservers = new HashSet<>();
44 private ControlMetricsObserver cpObserver;
45
46 private ApplicationId appId;
47
48 private ControlMetricsFactory cmf;
49
50 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 protected DeviceService deviceService;
52
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected CoreService coreService;
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected MetricsService metricsService;
Jian Li0967cd72015-11-25 17:38:48 -080058
59 @Activate
Jian Li60804322015-12-02 14:46:31 -080060 public void activate() {
61 appId = coreService.registerApplication("org.onosproject.cpman");
Jian Li0967cd72015-11-25 17:38:48 -080062
Jian Li60804322015-12-02 14:46:31 -080063 cmf = ControlMetricsFactory.getInstance(metricsService, deviceService);
64 // currently disable monitoring by default
65 // cmf.startMonitor();
66
67 registerObserver();
68
69 log.info("Started");
Jian Li0967cd72015-11-25 17:38:48 -080070 }
71
72 @Deactivate
73 public void deactivate() {
Jian Li60804322015-12-02 14:46:31 -080074 unregisterObserver();
75 cmf.stopMonitor();
76 log.info("Stopped");
Jian Li0967cd72015-11-25 17:38:48 -080077 }
78
79 @Modified
Jian Li60804322015-12-02 14:46:31 -080080 public void modified() {
Jian Li0967cd72015-11-25 17:38:48 -080081 }
Jian Li60804322015-12-02 14:46:31 -080082
83 private void registerObserver() {
84 cpObserver = new DefaultControlMetricsObserver();
85 this.addControlMetricsObserver(cpObserver);
86 }
87
88 private void unregisterObserver() {
89 this.removeControlMetricsObserver(cpObserver);
90 }
91
92 private void executeMonitorTask() {
93
94 // TODO: execute monitoring task with 1 minute period
95 if (cmf.isMonitor()) {
96 controlMetricsObservers.forEach(observer -> {
97
98 // try to feed the CPU and memory stats
99 observer.feedMetrics(cmf.cpuInfoMetric(), Optional.ofNullable(null));
100 observer.feedMetrics(cmf.memoryInfoMetric(), Optional.ofNullable(null));
101
102 // try to feed the control message stats
103 cmf.getDeviceIds().forEach(v -> {
104 observer.feedMetrics(cmf.inboundPacketMetrics(v), Optional.of(v));
105 observer.feedMetrics(cmf.outboundPacketMetrics(v), Optional.of(v));
106 observer.feedMetrics(cmf.flowmodPacketMetrics(v), Optional.of(v));
107 observer.feedMetrics(cmf.flowrmvPacketMetrics(v), Optional.of(v));
108 observer.feedMetrics(cmf.requestPacketMetrics(v), Optional.of(v));
109 observer.feedMetrics(cmf.replyPacketMetrics(v), Optional.of(v));
110 });
111 });
112 }
113 }
114
115 /**
116 * Adds a new control metrics observer.
117 *
118 * @param cmObserver control metric observer instance
119 */
120 public void addControlMetricsObserver(ControlMetricsObserver cmObserver) {
121 controlMetricsObservers.add(cmObserver);
122 }
123
124 /**
125 * Removes an existing control metrics observer.
126 *
127 * @param cmObserver control metric observer instance
128 */
129 public void removeControlMetricsObserver(ControlMetricsObserver cmObserver) {
130 controlMetricsObservers.remove(cmObserver);
131 }
132}