Jian Li
Committed by Gerrit Code Review

[ONOS-3535] Implement control metrics aggregation logic

Change-Id: I9953146851d5f3fdf7bcee9561fa86ddb7c7b3fe
...@@ -33,7 +33,48 @@ ...@@ -33,7 +33,48 @@
33 <dependency> 33 <dependency>
34 <groupId>org.onosproject</groupId> 34 <groupId>org.onosproject</groupId>
35 <artifactId>onos-api</artifactId> 35 <artifactId>onos-api</artifactId>
36 - <version>1.5.0-SNAPSHOT</version> 36 + <version>${project.version}</version>
37 + </dependency>
38 + <dependency>
39 + <groupId>org.osgi</groupId>
40 + <artifactId>org.osgi.compendium</artifactId>
41 + </dependency>
42 + <dependency>
43 + <groupId>org.onosproject</groupId>
44 + <artifactId>onlab-junit</artifactId>
45 + <scope>test</scope>
46 + </dependency>
47 + <dependency>
48 + <groupId>org.onosproject</groupId>
49 + <artifactId>onos-api</artifactId>
50 + <version>${project.version}</version>
51 + <classifier>tests</classifier>
52 + <scope>test</scope>
53 + </dependency>
54 + <dependency>
55 + <groupId>org.onosproject</groupId>
56 + <artifactId>onlab-misc</artifactId>
57 + <version>${project.version}</version>
58 + <classifier>tests</classifier>
59 + <scope>test</scope>
37 </dependency> 60 </dependency>
38 </dependencies> 61 </dependencies>
39 -</project> 62 +
63 + <build>
64 + <plugins>
65 + <plugin>
66 + <groupId>org.apache.felix</groupId>
67 + <artifactId>maven-bundle-plugin</artifactId>
68 + </plugin>
69 +
70 + <plugin>
71 + <groupId>org.apache.felix</groupId>
72 + <artifactId>maven-scr-plugin</artifactId>
73 + </plugin>
74 + <plugin>
75 + <groupId>org.onosproject</groupId>
76 + <artifactId>onos-maven-plugin</artifactId>
77 + </plugin>
78 + </plugins>
79 + </build>
80 +</project>
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -22,10 +22,10 @@ import java.util.concurrent.TimeUnit; ...@@ -22,10 +22,10 @@ import java.util.concurrent.TimeUnit;
22 /** 22 /**
23 * Data repository for control plane load information. 23 * Data repository for control plane load information.
24 */ 24 */
25 -public interface ControlPlaneLoad extends Load { 25 +public interface ControlLoad extends Load {
26 26
27 /** 27 /**
28 - * Obtain the average of the specified time duration. 28 + * Obtains the average of the specified time duration.
29 * 29 *
30 * @param duration time duration 30 * @param duration time duration
31 * @param unit time unit 31 * @param unit time unit
...@@ -34,7 +34,7 @@ public interface ControlPlaneLoad extends Load { ...@@ -34,7 +34,7 @@ public interface ControlPlaneLoad extends Load {
34 long average(int duration, TimeUnit unit); 34 long average(int duration, TimeUnit unit);
35 35
36 /** 36 /**
37 - * Obtain the average of all time duration. 37 + * Obtains the average of all time duration.
38 * 38 *
39 * @return average control plane metric value 39 * @return average control plane metric value
40 */ 40 */
......
...@@ -18,24 +18,21 @@ package org.onosproject.cpman; ...@@ -18,24 +18,21 @@ package org.onosproject.cpman;
18 /** 18 /**
19 * Include various control plane metrics. 19 * Include various control plane metrics.
20 */ 20 */
21 -public class ControlPlaneMetric { 21 +public class ControlMetric {
22 22
23 - private ControlMetricType metricType; 23 + private final ControlMetricType metricType;
24 - private long metricValue; 24 + private final MetricValue metricValue;
25 25
26 - ControlMetricType metricType() { 26 + ControlMetric(ControlMetricType metricType, MetricValue metricValue) {
27 - return metricType;
28 - }
29 -
30 - void setMetricType(ControlMetricType metricType) {
31 this.metricType = metricType; 27 this.metricType = metricType;
28 + this.metricValue = metricValue;
32 } 29 }
33 30
34 - long metricValue() { 31 + ControlMetricType metricType() {
35 - return metricValue; 32 + return metricType;
36 } 33 }
37 34
38 - void setMetricValue(long metricValue) { 35 + MetricValue metricValue() {
39 - this.metricValue = metricValue; 36 + return metricValue;
40 } 37 }
41 } 38 }
......
...@@ -20,15 +20,27 @@ package org.onosproject.cpman; ...@@ -20,15 +20,27 @@ package org.onosproject.cpman;
20 */ 20 */
21 public enum ControlMetricType { 21 public enum ControlMetricType {
22 22
23 - /** Racket Rate of Control Message. */ 23 + /** Mapped to PACKET-IN message of OpenFlow. */
24 - PacketRate, 24 + INBOUND_PACKET,
25 25
26 - /** Byte Rate of Control Message. */ 26 + /** Mapped to PACKET-OUT message of OpenFlow. */
27 - ByteRate, 27 + OUTBOUND_PACKET,
28 +
29 + /** Mapped to FLOW-MOD message of OpenFlow. */
30 + FLOW_MOD_PACKET,
31 +
32 + /** Mapped to FLOW-REMOVED message of OpenFlow. */
33 + FLOW_REMOVED_PACKET,
34 +
35 + /** Mapped to STATS-REQUEST message of OpenFlow. */
36 + REQUEST_PACKET,
37 +
38 + /** Mapped to STATS-REPLY message of OpenFlow. */
39 + REPLY_PACKET,
28 40
29 /** Cpu Utilization. */ 41 /** Cpu Utilization. */
30 - CpuInfo, 42 + CPU_INFO,
31 43
32 /** Memory Utilization. */ 44 /** Memory Utilization. */
33 - MemoryInfo 45 + MEMORY_INFO
34 } 46 }
......
1 +/*
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 + */
16 +package org.onosproject.cpman;
17 +
18 +import org.onlab.metrics.MetricsService;
19 +import org.onosproject.net.DeviceId;
20 +import org.onosproject.net.device.DeviceService;
21 +
22 +import java.util.HashSet;
23 +import java.util.Map;
24 +import java.util.Optional;
25 +import java.util.Set;
26 +import java.util.concurrent.ConcurrentHashMap;
27 +
28 +/**
29 + * Singleton class to provide various control plane metrics to other components.
30 + */
31 +public final class ControlMetricsFactory {
32 + private static volatile ControlMetricsFactory uniqueInstance;
33 +
34 + private MetricsService metricsService;
35 + private boolean enableMonitor = false;
36 +
37 + // define a set of MetricsAggregators
38 + private MetricsAggregator cpuInfoMetric;
39 + private MetricsAggregator memoryInfoMetric;
40 + private Map<DeviceId, MetricsAggregator> inboundPacketMetrics;
41 + private Map<DeviceId, MetricsAggregator> outboundPacketMetrics;
42 + private Map<DeviceId, MetricsAggregator> flowmodPacketMetrics;
43 + private Map<DeviceId, MetricsAggregator> flowrmvPacketMetrics;
44 + private Map<DeviceId, MetricsAggregator> requestPacketMetrics;
45 + private Map<DeviceId, MetricsAggregator> replyPacketMetrics;
46 + private Set<DeviceId> deviceIds = new HashSet<>();
47 +
48 + private ControlMetricsFactory(MetricsService metricsService, DeviceService deviceService) {
49 + this.metricsService = metricsService;
50 + registerMetrics();
51 +
52 + deviceService.getDevices().forEach(d->deviceIds.add(d.id()));
53 +
54 + addAllDeviceMetrics(deviceIds);
55 + }
56 +
57 + public static ControlMetricsFactory getInstance(MetricsService metricsService,
58 + DeviceService deviceService) {
59 + if (uniqueInstance == null) {
60 + synchronized (ControlMetricsFactory.class) {
61 + if (uniqueInstance == null) {
62 + uniqueInstance = new ControlMetricsFactory(metricsService, deviceService);
63 + }
64 + }
65 + }
66 + return uniqueInstance;
67 + }
68 +
69 + /**
70 + * Adds control metrics of a new device.
71 + *
72 + * @param deviceId {@link org.onosproject.net.DeviceId}
73 + */
74 + public void addMetricsByDeviceId(DeviceId deviceId) {
75 + MetricsAggregator inbound = new MetricsAggregator(metricsService,
76 + ControlMetricType.INBOUND_PACKET, Optional.of(deviceId));
77 + MetricsAggregator outbound = new MetricsAggregator(metricsService,
78 + ControlMetricType.OUTBOUND_PACKET, Optional.of(deviceId));
79 + MetricsAggregator flowmod = new MetricsAggregator(metricsService,
80 + ControlMetricType.FLOW_MOD_PACKET, Optional.of(deviceId));
81 + MetricsAggregator flowrmv = new MetricsAggregator(metricsService,
82 + ControlMetricType.FLOW_REMOVED_PACKET, Optional.of(deviceId));
83 + MetricsAggregator request = new MetricsAggregator(metricsService,
84 + ControlMetricType.REQUEST_PACKET, Optional.of(deviceId));
85 + MetricsAggregator reply = new MetricsAggregator(metricsService,
86 + ControlMetricType.REPLY_PACKET, Optional.of(deviceId));
87 +
88 + inboundPacketMetrics.putIfAbsent(deviceId, inbound);
89 + outboundPacketMetrics.putIfAbsent(deviceId, outbound);
90 + flowmodPacketMetrics.putIfAbsent(deviceId, flowmod);
91 + flowrmvPacketMetrics.putIfAbsent(deviceId, flowrmv);
92 + requestPacketMetrics.putIfAbsent(deviceId, request);
93 + replyPacketMetrics.putIfAbsent(deviceId, reply);
94 +
95 + deviceIds.add(deviceId);
96 + }
97 +
98 + /**
99 + * Removes control metrics of an existing device.
100 + *
101 + * @param deviceId {@link org.onosproject.net.DeviceId}
102 + */
103 + public void removeMetricsByDeviceId(DeviceId deviceId) {
104 + inboundPacketMetrics.remove(deviceId);
105 + outboundPacketMetrics.remove(deviceId);
106 + flowmodPacketMetrics.remove(deviceId);
107 + flowrmvPacketMetrics.remove(deviceId);
108 + requestPacketMetrics.remove(deviceId);
109 + replyPacketMetrics.remove(deviceId);
110 +
111 + deviceIds.remove(deviceId);
112 + }
113 +
114 + public Set<DeviceId> getDeviceIds() {
115 + return this.deviceIds;
116 + }
117 +
118 + /**
119 + * Adds control metrics for all devices.
120 + *
121 + * @param deviceIds a set of deviceIds
122 + */
123 + public void addAllDeviceMetrics(Set<DeviceId> deviceIds) {
124 + deviceIds.forEach(v -> addMetricsByDeviceId(v));
125 + }
126 +
127 + /**
128 + * Returns monitoring status.
129 + *
130 + * @return monitoring status
131 + */
132 + public boolean isMonitor() {
133 + return this.enableMonitor;
134 + }
135 +
136 + /**
137 + * Enable control plane monitoring.
138 + */
139 + protected void startMonitor() {
140 + this.enableMonitor = true;
141 + }
142 +
143 + /**
144 + * Disable control plane monitoring.
145 + */
146 + protected void stopMonitor() {
147 + this.enableMonitor = false;
148 + }
149 +
150 + /**
151 + * Registers new control metrics.
152 + */
153 + protected void registerMetrics() {
154 + cpuInfoMetric = new MetricsAggregator(metricsService,
155 + ControlMetricType.CPU_INFO, Optional.ofNullable(null));
156 + memoryInfoMetric = new MetricsAggregator(metricsService,
157 + ControlMetricType.MEMORY_INFO, Optional.ofNullable(null));
158 +
159 + inboundPacketMetrics = new ConcurrentHashMap<>();
160 + outboundPacketMetrics = new ConcurrentHashMap<>();
161 + flowmodPacketMetrics = new ConcurrentHashMap<>();
162 + flowrmvPacketMetrics = new ConcurrentHashMap<>();
163 + requestPacketMetrics = new ConcurrentHashMap<>();
164 + replyPacketMetrics = new ConcurrentHashMap<>();
165 + }
166 +
167 + /**
168 + * Unregisters all control metrics.
169 + */
170 + protected void unregisterMetrics() {
171 + cpuInfoMetric.removeMetrics();
172 + memoryInfoMetric.removeMetrics();
173 +
174 + inboundPacketMetrics.clear();
175 + outboundPacketMetrics.clear();
176 + flowmodPacketMetrics.clear();
177 + flowrmvPacketMetrics.clear();
178 + requestPacketMetrics.clear();
179 + replyPacketMetrics.clear();
180 + }
181 +
182 + public MetricsAggregator cpuInfoMetric() {
183 + return cpuInfoMetric;
184 + }
185 +
186 + public MetricsAggregator memoryInfoMetric() {
187 + return memoryInfoMetric;
188 + }
189 +
190 + public Map<DeviceId, MetricsAggregator> inboundPacketMetrics() {
191 + return inboundPacketMetrics;
192 + }
193 +
194 + public Map<DeviceId, MetricsAggregator> outboundPacketMetrics() {
195 + return outboundPacketMetrics;
196 + }
197 +
198 + public Map<DeviceId, MetricsAggregator> flowmodPacketMetrics() {
199 + return flowmodPacketMetrics;
200 + }
201 +
202 + public Map<DeviceId, MetricsAggregator> flowrmvPacketMetrics() {
203 + return flowrmvPacketMetrics;
204 + }
205 +
206 + public Map<DeviceId, MetricsAggregator> requestPacketMetrics() {
207 + return requestPacketMetrics;
208 + }
209 +
210 + public Map<DeviceId, MetricsAggregator> replyPacketMetrics() {
211 + return replyPacketMetrics;
212 + }
213 +
214 + public MetricsAggregator inboundPacketMetrics(DeviceId deviceId) {
215 + return inboundPacketMetrics.get(deviceId);
216 + }
217 +
218 + public MetricsAggregator outboundPacketMetrics(DeviceId deviceId) {
219 + return outboundPacketMetrics.get(deviceId);
220 + }
221 +
222 + public MetricsAggregator flowmodPacketMetrics(DeviceId deviceId) {
223 + return flowmodPacketMetrics.get(deviceId);
224 + }
225 +
226 + public MetricsAggregator flowrmvPacketMetrics(DeviceId deviceId) {
227 + return flowrmvPacketMetrics.get(deviceId);
228 + }
229 +
230 + public MetricsAggregator requestPacketMetrics(DeviceId deviceId) {
231 + return requestPacketMetrics.get(deviceId);
232 + }
233 +
234 + public MetricsAggregator replyPacketMetrics(DeviceId deviceId) {
235 + return replyPacketMetrics.get(deviceId);
236 + }
237 +}
...\ No newline at end of file ...\ No newline at end of file
...@@ -15,29 +15,20 @@ ...@@ -15,29 +15,20 @@
15 */ 15 */
16 package org.onosproject.cpman; 16 package org.onosproject.cpman;
17 17
18 -/** 18 +import org.onosproject.net.DeviceId;
19 - * Abstracted Control Message Type.
20 - */
21 -public enum ControlMessageType {
22 -
23 - /** Mapped to PACKET-IN message of OpenFlow. */
24 - INBOUND_PACKET,
25 -
26 - /** Mapped to PACKET-OUT message of OpenFlow. */
27 - OUTBOUND_PACKET,
28 -
29 - /** Mapped to FLOW-MOD message of OpenFlow. */
30 - FLOW_MOD_PACKET,
31 19
32 - /** Mapped to FLOW-REMOVED message of OpenFlow. */ 20 +import java.util.Optional;
33 - FLOW_REMOVED_PACKET,
34 21
35 - /** Mapped to STATS-REQUEST message of OpenFlow. */ 22 +/**
36 - REQUEST_PACKET, 23 + * Control metrics observer interface.
37 - 24 + */
38 - /** Mapped to STATS-REPLY message of OpenFlow. */ 25 +public interface ControlMetricsObserver {
39 - REPLY_PACKET, 26 +
40 - 27 + /**
41 - /** All message types. */ 28 + * Feeds the extracted value from MetricAggregator to back-end storage.
42 - ALL 29 + *
30 + * @param metricsAggregator metric aggregator
31 + * @param deviceId device id {@link org.onosproject.net.DeviceId}
32 + */
33 + void feedMetrics(MetricsAggregator metricsAggregator, Optional<DeviceId> deviceId);
43 } 34 }
......
...@@ -15,33 +15,118 @@ ...@@ -15,33 +15,118 @@
15 */ 15 */
16 package org.onosproject.cpman; 16 package org.onosproject.cpman;
17 17
18 -import com.sun.jndi.toolkit.ctx.ComponentContext;
19 import org.apache.felix.scr.annotations.Activate; 18 import org.apache.felix.scr.annotations.Activate;
20 import org.apache.felix.scr.annotations.Deactivate; 19 import org.apache.felix.scr.annotations.Deactivate;
21 import org.apache.felix.scr.annotations.Modified; 20 import org.apache.felix.scr.annotations.Modified;
21 +import org.apache.felix.scr.annotations.Component;
22 +import org.apache.felix.scr.annotations.Reference;
23 +import org.apache.felix.scr.annotations.ReferenceCardinality;
24 +import org.onlab.metrics.MetricsService;
25 +import org.onosproject.core.ApplicationId;
26 +import org.onosproject.core.CoreService;
27 +import org.onosproject.net.device.DeviceService;
22 import org.slf4j.Logger; 28 import org.slf4j.Logger;
23 29
30 +import java.util.HashSet;
31 +import java.util.Optional;
32 +import java.util.Set;
33 +
24 import static org.slf4j.LoggerFactory.getLogger; 34 import static org.slf4j.LoggerFactory.getLogger;
25 35
26 /** 36 /**
27 * Control plane management application. 37 * Control plane management application.
28 */ 38 */
39 +@Component(immediate = true)
29 public class ControlPlaneManager { 40 public class ControlPlaneManager {
30 41
31 private final Logger log = getLogger(getClass()); 42 private final Logger log = getLogger(getClass());
43 + 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;
32 58
33 @Activate 59 @Activate
34 - public void activate(ComponentContext context) { 60 + public void activate() {
61 + appId = coreService.registerApplication("org.onosproject.cpman");
35 62
63 + cmf = ControlMetricsFactory.getInstance(metricsService, deviceService);
64 + // currently disable monitoring by default
65 + // cmf.startMonitor();
66 +
67 + registerObserver();
68 +
69 + log.info("Started");
36 } 70 }
37 71
38 @Deactivate 72 @Deactivate
39 public void deactivate() { 73 public void deactivate() {
40 - 74 + unregisterObserver();
75 + cmf.stopMonitor();
76 + log.info("Stopped");
41 } 77 }
42 78
43 @Modified 79 @Modified
44 - public void modified(ComponentContext context) { 80 + public void modified() {
81 + }
82 +
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 + }
45 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);
46 } 131 }
47 -}
...\ No newline at end of file ...\ No newline at end of file
132 +}
......
1 +/*
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 + */
16 +package org.onosproject.cpman;
17 +
18 +import org.apache.felix.scr.annotations.Activate;
19 +import org.apache.felix.scr.annotations.Deactivate;
20 +import org.apache.felix.scr.annotations.Modified;
21 +import org.apache.felix.scr.annotations.Component;
22 +import org.apache.felix.scr.annotations.Service;
23 +import org.onosproject.cluster.NodeId;
24 +import org.onosproject.net.DeviceId;
25 +import org.osgi.service.component.ComponentContext;
26 +import org.slf4j.Logger;
27 +
28 +import java.util.Optional;
29 +import java.util.concurrent.TimeUnit;
30 +
31 +import static org.slf4j.LoggerFactory.getLogger;
32 +
33 +/**
34 + * Control plane monitoring service class.
35 + */
36 +@Component(immediate = true)
37 +@Service
38 +public class ControlPlaneMonitor implements ControlPlaneMonitorService {
39 +
40 + private final Logger log = getLogger(getClass());
41 +
42 + @Activate
43 + public void activate() {
44 + }
45 +
46 + @Deactivate
47 + public void deactivate() {
48 + }
49 +
50 + @Modified
51 + public void modified(ComponentContext context) {
52 + }
53 +
54 + @Override
55 + public void updateMetric(ControlMetric cpm, int updateInterval,
56 + Optional<DeviceId> deviceId) {
57 + }
58 +
59 + @Override
60 + public ControlLoad getLoad(NodeId nodeId, ControlMetricType type,
61 + Optional<DeviceId> deviceId) {
62 + return null;
63 + }
64 +
65 + @Override
66 + public ControlLoad getLoad(NodeId nodeId, ControlMetricType type,
67 + Optional<DeviceId> deviceId, int duration, TimeUnit unit) {
68 + return null;
69 + }
70 +}
...\ No newline at end of file ...\ No newline at end of file
...@@ -24,28 +24,28 @@ import java.util.concurrent.TimeUnit; ...@@ -24,28 +24,28 @@ import java.util.concurrent.TimeUnit;
24 /** 24 /**
25 * Control Plane Statistics Service Interface. 25 * Control Plane Statistics Service Interface.
26 */ 26 */
27 -public interface ControlPlaneStatsService { 27 +public interface ControlPlaneMonitorService {
28 28
29 /** 29 /**
30 - * Add a new control plane metric value with a certain update interval. 30 + * Adds a new control metric value with a certain update interval.
31 * 31 *
32 - * @param cpm control plane metric (e.g., control message rate, cpu, memory, etc.) 32 + * @param controlMetric control plane metric (e.g., control message rate, cpu, memory, etc.)
33 * @param updateInterval value update interval (time unit will be in minute) 33 * @param updateInterval value update interval (time unit will be in minute)
34 */ 34 */
35 - void updateMetric(ControlPlaneMetric cpm, int updateInterval); 35 + void updateMetric(ControlMetric controlMetric, int updateInterval, Optional<DeviceId> deviceId);
36 36
37 /** 37 /**
38 - * Obtain the control plane load of a specific device. 38 + * Obtains the control plane load of a specific device.
39 * 39 *
40 * @param nodeId node id {@link org.onosproject.cluster.NodeId} 40 * @param nodeId node id {@link org.onosproject.cluster.NodeId}
41 * @param type control metric type 41 * @param type control metric type
42 * @param deviceId device id {@link org.onosproject.net.DeviceId} 42 * @param deviceId device id {@link org.onosproject.net.DeviceId}
43 * @return control plane load 43 * @return control plane load
44 */ 44 */
45 - ControlPlaneLoad getLoad(NodeId nodeId, ControlMetricType type, Optional<DeviceId> deviceId); 45 + ControlLoad getLoad(NodeId nodeId, ControlMetricType type, Optional<DeviceId> deviceId);
46 46
47 /** 47 /**
48 - * Obtain the control plane load of a specific device with a specific time duration. 48 + * Obtains the control plane load of a specific device with a specific time duration.
49 * 49 *
50 * @param nodeId node id {@link org.onosproject.cluster.NodeId} 50 * @param nodeId node id {@link org.onosproject.cluster.NodeId}
51 * @param type control metric type 51 * @param type control metric type
...@@ -54,6 +54,6 @@ public interface ControlPlaneStatsService { ...@@ -54,6 +54,6 @@ public interface ControlPlaneStatsService {
54 * @param deviceId device id {@link org.onosproject.net.Device} 54 * @param deviceId device id {@link org.onosproject.net.Device}
55 * @return control plane load 55 * @return control plane load
56 */ 56 */
57 - ControlPlaneLoad getLoad(NodeId nodeId, ControlMetricType type, Optional<DeviceId> deviceId, 57 + ControlLoad getLoad(NodeId nodeId, ControlMetricType type, Optional<DeviceId> deviceId,
58 - int duration, TimeUnit unit); 58 + int duration, TimeUnit unit);
59 } 59 }
...\ No newline at end of file ...\ No newline at end of file
......
1 +/*
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 + */
16 +package org.onosproject.cpman;
17 +
18 +import org.apache.felix.scr.annotations.Reference;
19 +import org.apache.felix.scr.annotations.ReferenceCardinality;
20 +import org.onosproject.net.DeviceId;
21 +
22 +import java.util.Optional;
23 +
24 +/**
25 + * Default ControlMetricsObserver.
26 + */
27 +public class DefaultControlMetricsObserver implements ControlMetricsObserver {
28 +
29 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
30 + protected ControlPlaneMonitorService controlPlaneMonitorService;
31 +
32 + @Override
33 + public void feedMetrics(MetricsAggregator ma, Optional<DeviceId> deviceId) {
34 + MetricValue mv = new MetricValue((long) ma.getRate(), (long) ma.getLoad(), (long) ma.getCount());
35 + ControlMetric cpm = new ControlMetric(ma.getMetricsType(), mv);
36 + controlPlaneMonitorService.updateMetric(cpm, 1, deviceId);
37 + }
38 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
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 + */
16 +package org.onosproject.cpman;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +
20 +/**
21 + * Primitive Metric Value.
22 + */
23 +public class MetricValue {
24 +
25 + private final long rate;
26 + private final long load;
27 + private final long count;
28 +
29 + /**
30 + * Constructor.
31 + *
32 + * @param rate rate
33 + * @param load load
34 + * @param count count
35 + */
36 + MetricValue(long rate, long load, long count) {
37 + this.rate = rate;
38 + this.load = load;
39 + this.count = count;
40 + }
41 +
42 + public long getRate() {
43 + return rate;
44 + }
45 +
46 + public long getLoad() {
47 + return load;
48 + }
49 +
50 + public long getCount() {
51 + return count;
52 + }
53 +
54 + @Override
55 + public boolean equals(Object obj) {
56 + if (this == obj) {
57 + return true;
58 + }
59 + if (obj instanceof MetricValue) {
60 + MetricValue other = (MetricValue) obj;
61 + if (this.rate == other.rate &&
62 + this.load == other.load &&
63 + this.count == other.count) {
64 + return true;
65 + }
66 + }
67 + return false;
68 + }
69 +
70 + @Override
71 + public int hashCode() {
72 + final int prime = 1004;
73 + int result = super.hashCode();
74 + result = prime * result + (int) this.rate;
75 + result = prime * result + (int) this.load;
76 + result = prime * result + (int) this.count;
77 + return result;
78 + }
79 +
80 + @Override
81 + public String toString() {
82 + return toStringHelper(getClass())
83 + .add("rate", Long.toHexString(rate))
84 + .add("load", Long.toHexString(load))
85 + .add("count", Long.toHexString(count)).toString();
86 + }
87 +}
1 +/*
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 + */
16 +package org.onosproject.cpman;
17 +
18 +import com.codahale.metrics.Meter;
19 +import org.onlab.metrics.MetricsComponent;
20 +import org.onlab.metrics.MetricsFeature;
21 +import org.onlab.metrics.MetricsService;
22 +import org.onosproject.net.DeviceId;
23 +
24 +import java.util.Optional;
25 +
26 +import static com.google.common.base.Preconditions.checkNotNull;
27 +
28 +/**
29 + * An aggregator that aggregates a specific network or performance metrics via the metrics service.
30 + */
31 +public class MetricsAggregator {
32 +
33 + private Meter rateMeter;
34 + private Meter countMeter;
35 + private MetricsService metricsService;
36 + private MetricsComponent metricsComponent;
37 + private MetricsFeature metricsFeature;
38 + private ControlMetricType metricsType;
39 + private static final int EXECUTE_PERIOD_IN_SECOND = 60;
40 + private static final String RATE_NAME = "rate";
41 + private static final String COUNT_NAME = "count";
42 +
43 + /**
44 + * Constructs a new MetricsAggregator for aggregating a metric.
45 + * Instantiates the metrics service
46 + * Initializes all the general metrics for that object
47 + *
48 + * @param type Control metric type
49 + * @param deviceId DeviceId
50 + */
51 + MetricsAggregator(MetricsService metricsService, ControlMetricType type, Optional<DeviceId> deviceId) {
52 + String primitiveName = type.toString();
53 + String objName = "all";
54 + if (deviceId.isPresent()) {
55 + objName = deviceId.toString();
56 + }
57 +
58 + checkNotNull(primitiveName, "Component name cannot be null");
59 + checkNotNull(objName, "Feature name cannot be null");
60 +
61 + this.metricsType = type;
62 +
63 + this.metricsService = metricsService;
64 + this.metricsComponent = metricsService.registerComponent(primitiveName);
65 + this.metricsFeature = metricsComponent.registerFeature(objName);
66 +
67 + this.rateMeter = metricsService.createMeter(metricsComponent, metricsFeature, RATE_NAME);
68 + this.countMeter = metricsService.createMeter(metricsComponent, metricsFeature, COUNT_NAME);
69 + }
70 +
71 + public ControlMetricType getMetricsType() {
72 + return metricsType;
73 + }
74 +
75 + /**
76 + * Removes both rate and count metrics.
77 + */
78 + protected void removeMetrics() {
79 + metricsService.removeMetric(metricsComponent, metricsFeature, RATE_NAME);
80 + metricsService.removeMetric(metricsComponent, metricsFeature, COUNT_NAME);
81 + }
82 +
83 + /**
84 + * Increments the meter rate by {@code n}, and the meter counter by 1.
85 + *
86 + * @param n Increment the meter rate by {@code n}.
87 + */
88 + public void increment(long n) {
89 + rateMeter.mark(n);
90 + countMeter.mark(1);
91 + }
92 +
93 + /**
94 + * Obtains the average load value.
95 + *
96 + * @return load value
97 + */
98 + public double getLoad() {
99 + return rateMeter.getOneMinuteRate() / countMeter.getOneMinuteRate();
100 + }
101 +
102 + /**
103 + * Obtains the average meter rate within recent 1 minute.
104 + *
105 + * @return rate value
106 + */
107 + public double getRate() {
108 + return rateMeter.getOneMinuteRate();
109 + }
110 +
111 + /**
112 + * Obtains the average meter count within recent 1 minute.
113 + *
114 + * @return count value
115 + */
116 + public double getCount() {
117 + return countMeter.getOneMinuteRate() * EXECUTE_PERIOD_IN_SECOND;
118 + }
119 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
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 + */
16 +package org.onosproject.cpman;
17 +
18 +import org.junit.After;
19 +import org.junit.Before;
20 +import org.junit.Test;
21 +import org.onlab.metrics.MetricsServiceAdapter;
22 +import org.onosproject.core.CoreServiceAdapter;
23 +import org.onosproject.net.device.DeviceServiceAdapter;
24 +
25 +/**
26 + * Set of tests of the ONOS application component.
27 + */
28 +public class ControlPlaneManagerTest {
29 +
30 + private ControlPlaneManager cpMan;
31 +
32 + /**
33 + * Sets up the services required by the CPMan application.
34 + */
35 + @Before
36 + public void setUp() {
37 + cpMan = new ControlPlaneManager();
38 + cpMan.coreService = new CoreServiceAdapter();
39 + cpMan.deviceService = new DeviceServiceAdapter();
40 + cpMan.metricsService = new MetricsServiceAdapter();
41 + cpMan.activate();
42 + }
43 +
44 + /**
45 + * Tears down the CPMan application.
46 + */
47 + @After
48 + public void tearDown() {
49 + cpMan.deactivate();
50 + }
51 +
52 + /**
53 + * Tests the control metric aggregating function.
54 + *
55 + * @throws Exception if metric collection fails.
56 + */
57 + @Test
58 + public void testMetricsAggregation() throws Exception {
59 + }
60 +
61 + /**
62 + * Tests the control metric collecting function.
63 + *
64 + * @throws Exception
65 + */
66 + @Test
67 + public void testMetricsCollection() throws Exception {
68 + }
69 +}
...@@ -107,4 +107,4 @@ public class MetricsServiceAdapter implements MetricsService { ...@@ -107,4 +107,4 @@ public class MetricsServiceAdapter implements MetricsService {
107 @Override 107 @Override
108 public void removeMatching(MetricFilter filter) { 108 public void removeMatching(MetricFilter filter) {
109 } 109 }
110 -} 110 +}
...\ No newline at end of file ...\ No newline at end of file
......