Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 19 | package org.onlab.onos.metrics.topology; |
| 20 | |
| 21 | import static org.slf4j.LoggerFactory.getLogger; |
| 22 | |
| 23 | import java.util.LinkedList; |
| 24 | import java.util.List; |
| 25 | |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 26 | import com.google.common.collect.ImmutableList; |
| 27 | import org.apache.felix.scr.annotations.Activate; |
| 28 | import org.apache.felix.scr.annotations.Component; |
| 29 | import org.apache.felix.scr.annotations.Deactivate; |
| 30 | import org.apache.felix.scr.annotations.Reference; |
| 31 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 32 | import org.apache.felix.scr.annotations.Service; |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 33 | import org.onlab.metrics.EventMetric; |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 34 | import org.onlab.metrics.MetricsService; |
| 35 | import org.onlab.onos.event.Event; |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.device.DeviceEvent; |
| 37 | import org.onlab.onos.net.device.DeviceListener; |
| 38 | import org.onlab.onos.net.device.DeviceService; |
| 39 | import org.onlab.onos.net.host.HostEvent; |
| 40 | import org.onlab.onos.net.host.HostListener; |
| 41 | import org.onlab.onos.net.host.HostService; |
| 42 | import org.onlab.onos.net.link.LinkEvent; |
| 43 | import org.onlab.onos.net.link.LinkListener; |
| 44 | import org.onlab.onos.net.link.LinkService; |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 45 | import org.onlab.onos.net.topology.TopologyEvent; |
| 46 | import org.onlab.onos.net.topology.TopologyListener; |
| 47 | import org.onlab.onos.net.topology.TopologyService; |
| 48 | import org.slf4j.Logger; |
| 49 | |
| 50 | /** |
| 51 | * ONOS Topology Metrics Application that collects topology-related metrics. |
| 52 | */ |
| 53 | @Component(immediate = true) |
| 54 | @Service |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 55 | public class TopologyMetrics implements TopologyMetricsService { |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 56 | private static final Logger log = getLogger(TopologyMetrics.class); |
| 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 59 | protected DeviceService deviceService; |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 61 | protected HostService hostService; |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected LinkService linkService; |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 65 | protected TopologyService topologyService; |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | protected MetricsService metricsService; |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 68 | |
| 69 | private LinkedList<Event> lastEvents = new LinkedList<>(); |
| 70 | private static final int LAST_EVENTS_MAX_N = 100; |
| 71 | |
| 72 | private final DeviceListener deviceListener = new InnerDeviceListener(); |
| 73 | private final HostListener hostListener = new InnerHostListener(); |
| 74 | private final LinkListener linkListener = new InnerLinkListener(); |
| 75 | private final TopologyListener topologyListener = |
| 76 | new InnerTopologyListener(); |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 77 | |
| 78 | // |
| 79 | // Metrics |
| 80 | // |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 81 | private static final String COMPONENT_NAME = "Topology"; |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 82 | private static final String FEATURE_DEVICE_NAME = "DeviceEvent"; |
| 83 | private static final String FEATURE_HOST_NAME = "HostEvent"; |
| 84 | private static final String FEATURE_LINK_NAME = "LinkEvent"; |
| 85 | private static final String FEATURE_GRAPH_NAME = "GraphEvent"; |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 86 | // |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 87 | // Event metrics: |
| 88 | // - Device events |
| 89 | // - Host events |
| 90 | // - Link events |
| 91 | // - Topology Graph events |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 92 | // |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 93 | private EventMetric topologyDeviceEventMetric; |
| 94 | private EventMetric topologyHostEventMetric; |
| 95 | private EventMetric topologyLinkEventMetric; |
| 96 | private EventMetric topologyGraphEventMetric; |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 97 | |
| 98 | @Activate |
| 99 | protected void activate() { |
| 100 | clear(); |
| 101 | registerMetrics(); |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 102 | |
| 103 | // Register for all topology-related events |
| 104 | deviceService.addListener(deviceListener); |
| 105 | hostService.addListener(hostListener); |
| 106 | linkService.addListener(linkListener); |
| 107 | topologyService.addListener(topologyListener); |
| 108 | |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 109 | log.info("ONOS Topology Metrics started."); |
| 110 | } |
| 111 | |
| 112 | @Deactivate |
| 113 | public void deactivate() { |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 114 | // De-register from all topology-related events |
| 115 | deviceService.removeListener(deviceListener); |
| 116 | hostService.removeListener(hostListener); |
| 117 | linkService.removeListener(linkListener); |
| 118 | topologyService.removeListener(topologyListener); |
| 119 | |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 120 | removeMetrics(); |
| 121 | clear(); |
| 122 | log.info("ONOS Topology Metrics stopped."); |
| 123 | } |
| 124 | |
| 125 | @Override |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 126 | public List<Event> getEvents() { |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 127 | synchronized (lastEvents) { |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 128 | return ImmutableList.<Event>copyOf(lastEvents); |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | @Override |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 133 | public EventMetric topologyDeviceEventMetric() { |
| 134 | return topologyDeviceEventMetric; |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 138 | public EventMetric topologyHostEventMetric() { |
| 139 | return topologyHostEventMetric; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public EventMetric topologyLinkEventMetric() { |
| 144 | return topologyLinkEventMetric; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public EventMetric topologyGraphEventMetric() { |
| 149 | return topologyGraphEventMetric; |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 152 | /** |
| 153 | * Records an event. |
| 154 | * |
| 155 | * @param event the event to record |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 156 | * @param eventMetric the Event Metric to use |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 157 | */ |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 158 | private void recordEvent(Event event, EventMetric eventMetric) { |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 159 | synchronized (lastEvents) { |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 160 | eventMetric.eventReceived(); |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 161 | |
| 162 | // |
| 163 | // Keep only the last N events, where N = LAST_EVENTS_MAX_N |
| 164 | // |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 165 | while (lastEvents.size() >= LAST_EVENTS_MAX_N) { |
| 166 | lastEvents.remove(); |
| 167 | } |
| 168 | lastEvents.add(event); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 173 | * Inner Device Event Listener class. |
| 174 | */ |
| 175 | private class InnerDeviceListener implements DeviceListener { |
| 176 | @Override |
| 177 | public void event(DeviceEvent event) { |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 178 | recordEvent(event, topologyDeviceEventMetric); |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 179 | log.debug("Device Event: time = {} type = {} event = {}", |
| 180 | event.time(), event.type(), event); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Inner Host Event Listener class. |
| 186 | */ |
| 187 | private class InnerHostListener implements HostListener { |
| 188 | @Override |
| 189 | public void event(HostEvent event) { |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 190 | recordEvent(event, topologyHostEventMetric); |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 191 | log.debug("Host Event: time = {} type = {} event = {}", |
| 192 | event.time(), event.type(), event); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Inner Link Event Listener class. |
| 198 | */ |
| 199 | private class InnerLinkListener implements LinkListener { |
| 200 | @Override |
| 201 | public void event(LinkEvent event) { |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 202 | recordEvent(event, topologyLinkEventMetric); |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 203 | log.debug("Link Event: time = {} type = {} event = {}", |
| 204 | event.time(), event.type(), event); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Inner Topology Event Listener class. |
| 210 | */ |
| 211 | private class InnerTopologyListener implements TopologyListener { |
| 212 | @Override |
| 213 | public void event(TopologyEvent event) { |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 214 | recordEvent(event, topologyGraphEventMetric); |
Pavlin Radoslavov | 5ba8b28 | 2014-10-23 01:03:10 -0700 | [diff] [blame] | 215 | log.debug("Topology Event: time = {} type = {} event = {}", |
| 216 | event.time(), event.type(), event); |
| 217 | for (Event reason : event.reasons()) { |
| 218 | log.debug("Topology Event Reason: time = {} type = {} event = {}", |
| 219 | reason.time(), reason.type(), reason); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /** |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 225 | * Clears the internal state. |
| 226 | */ |
| 227 | private void clear() { |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 228 | synchronized (lastEvents) { |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 229 | lastEvents.clear(); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Registers the metrics. |
| 235 | */ |
| 236 | private void registerMetrics() { |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 237 | topologyDeviceEventMetric = |
| 238 | new EventMetric(metricsService, COMPONENT_NAME, |
| 239 | FEATURE_DEVICE_NAME); |
| 240 | topologyHostEventMetric = |
| 241 | new EventMetric(metricsService, COMPONENT_NAME, |
| 242 | FEATURE_HOST_NAME); |
| 243 | topologyLinkEventMetric = |
| 244 | new EventMetric(metricsService, COMPONENT_NAME, |
| 245 | FEATURE_LINK_NAME); |
| 246 | topologyGraphEventMetric = |
| 247 | new EventMetric(metricsService, COMPONENT_NAME, |
| 248 | FEATURE_GRAPH_NAME); |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 249 | |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 250 | topologyDeviceEventMetric.registerMetrics(); |
| 251 | topologyHostEventMetric.registerMetrics(); |
| 252 | topologyLinkEventMetric.registerMetrics(); |
| 253 | topologyGraphEventMetric.registerMetrics(); |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Removes the metrics. |
| 258 | */ |
| 259 | private void removeMetrics() { |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 260 | topologyDeviceEventMetric.removeMetrics(); |
| 261 | topologyHostEventMetric.removeMetrics(); |
| 262 | topologyLinkEventMetric.removeMetrics(); |
| 263 | topologyGraphEventMetric.removeMetrics(); |
Pavlin Radoslavov | 64d9e47 | 2014-10-21 22:01:08 -0700 | [diff] [blame] | 264 | } |
| 265 | } |