Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 1 | package org.onlab.onos.metrics.intent; |
| 2 | |
| 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
| 5 | import java.util.LinkedList; |
| 6 | import java.util.List; |
| 7 | |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 8 | import com.google.common.collect.ImmutableList; |
| 9 | import org.apache.felix.scr.annotations.Activate; |
| 10 | import org.apache.felix.scr.annotations.Component; |
| 11 | import org.apache.felix.scr.annotations.Deactivate; |
| 12 | import org.apache.felix.scr.annotations.Reference; |
| 13 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 14 | import org.apache.felix.scr.annotations.Service; |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame^] | 15 | import org.onlab.metrics.EventMetric; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 16 | import org.onlab.metrics.MetricsService; |
| 17 | import org.onlab.onos.net.intent.IntentEvent; |
| 18 | import org.onlab.onos.net.intent.IntentListener; |
| 19 | import org.onlab.onos.net.intent.IntentService; |
| 20 | import org.slf4j.Logger; |
| 21 | |
| 22 | /** |
| 23 | * ONOS Intent Metrics Application that collects intent-related metrics. |
| 24 | */ |
| 25 | @Component(immediate = true) |
| 26 | @Service |
| 27 | public class IntentMetrics implements IntentMetricsService, |
| 28 | IntentListener { |
| 29 | private static final Logger log = getLogger(IntentMetrics.class); |
| 30 | |
| 31 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 32 | protected IntentService intentService; |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame^] | 33 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 34 | protected MetricsService metricsService; |
| 35 | |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 36 | private LinkedList<IntentEvent> lastEvents = new LinkedList<>(); |
| 37 | private static final int LAST_EVENTS_MAX_N = 100; |
| 38 | |
| 39 | // |
| 40 | // Metrics |
| 41 | // |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 42 | private static final String COMPONENT_NAME = "Intent"; |
| 43 | private static final String FEATURE_SUBMITTED_NAME = "Submitted"; |
| 44 | private static final String FEATURE_INSTALLED_NAME = "Installed"; |
| 45 | private static final String FEATURE_WITHDRAW_REQUESTED_NAME = |
| 46 | "WithdrawRequested"; |
| 47 | private static final String FEATURE_WITHDRAWN_NAME = "Withdrawn"; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 48 | // |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame^] | 49 | // Event metrics: |
| 50 | // - Intent Submitted API operation |
| 51 | // - Intent Installed operation completion |
| 52 | // - Intent Withdraw Requested API operation |
| 53 | // - Intent Withdrawn operation completion |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 54 | // |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame^] | 55 | private EventMetric intentSubmittedEventMetric; |
| 56 | private EventMetric intentInstalledEventMetric; |
| 57 | private EventMetric intentWithdrawRequestedEventMetric; |
| 58 | private EventMetric intentWithdrawnEventMetric; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 59 | |
| 60 | @Activate |
| 61 | protected void activate() { |
| 62 | clear(); |
| 63 | registerMetrics(); |
| 64 | intentService.addListener(this); |
| 65 | log.info("ONOS Intent Metrics started."); |
| 66 | } |
| 67 | |
| 68 | @Deactivate |
| 69 | public void deactivate() { |
| 70 | intentService.removeListener(this); |
| 71 | removeMetrics(); |
| 72 | clear(); |
| 73 | log.info("ONOS Intent Metrics stopped."); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public List<IntentEvent> getEvents() { |
| 78 | synchronized (lastEvents) { |
| 79 | return ImmutableList.<IntentEvent>copyOf(lastEvents); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | @Override |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame^] | 84 | public EventMetric intentSubmittedEventMetric() { |
| 85 | return intentSubmittedEventMetric; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame^] | 89 | public EventMetric intentInstalledEventMetric() { |
| 90 | return intentInstalledEventMetric; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame^] | 94 | public EventMetric intentWithdrawRequestedEventMetric() { |
| 95 | return intentWithdrawRequestedEventMetric; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame^] | 99 | public EventMetric intentWithdrawnEventMetric() { |
| 100 | return intentWithdrawnEventMetric; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void event(IntentEvent event) { |
| 105 | synchronized (lastEvents) { |
| 106 | // |
| 107 | // TODO: The processing below is incomplete: we don't have |
| 108 | // an event equivalent of "Withdraw Requested" |
| 109 | // |
| 110 | switch (event.type()) { |
| 111 | case SUBMITTED: |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame^] | 112 | intentSubmittedEventMetric.eventReceived(); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 113 | break; |
| 114 | case INSTALLED: |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame^] | 115 | intentInstalledEventMetric.eventReceived(); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 116 | break; |
| 117 | case FAILED: |
| 118 | // TODO: Just ignore? |
| 119 | break; |
| 120 | /* |
| 121 | case WITHDRAW_REQUESTED: |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame^] | 122 | intentWithdrawRequestedEventMetric.eventReceived(); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 123 | break; |
| 124 | */ |
| 125 | case WITHDRAWN: |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame^] | 126 | intentWithdrawnEventMetric.eventReceived(); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 127 | break; |
| 128 | default: |
| 129 | break; |
| 130 | } |
| 131 | |
| 132 | // |
| 133 | // Keep only the last N events, where N = LAST_EVENTS_MAX_N |
| 134 | // |
| 135 | while (lastEvents.size() >= LAST_EVENTS_MAX_N) { |
| 136 | lastEvents.remove(); |
| 137 | } |
| 138 | lastEvents.add(event); |
| 139 | } |
| 140 | |
| 141 | log.debug("Intent Event: time = {} type = {} event = {}", |
| 142 | event.time(), event.type(), event); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Clears the internal state. |
| 147 | */ |
| 148 | private void clear() { |
| 149 | synchronized (lastEvents) { |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 150 | lastEvents.clear(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Registers the metrics. |
| 156 | */ |
| 157 | private void registerMetrics() { |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame^] | 158 | intentSubmittedEventMetric = |
| 159 | new EventMetric(metricsService, COMPONENT_NAME, |
| 160 | FEATURE_SUBMITTED_NAME); |
| 161 | intentInstalledEventMetric = |
| 162 | new EventMetric(metricsService, COMPONENT_NAME, |
| 163 | FEATURE_INSTALLED_NAME); |
| 164 | intentWithdrawRequestedEventMetric = |
| 165 | new EventMetric(metricsService, COMPONENT_NAME, |
| 166 | FEATURE_WITHDRAW_REQUESTED_NAME); |
| 167 | intentWithdrawnEventMetric = |
| 168 | new EventMetric(metricsService, COMPONENT_NAME, |
| 169 | FEATURE_WITHDRAWN_NAME); |
| 170 | |
| 171 | intentSubmittedEventMetric.registerMetrics(); |
| 172 | intentInstalledEventMetric.registerMetrics(); |
| 173 | intentWithdrawRequestedEventMetric.registerMetrics(); |
| 174 | intentWithdrawnEventMetric.registerMetrics(); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Removes the metrics. |
| 179 | */ |
| 180 | private void removeMetrics() { |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame^] | 181 | intentSubmittedEventMetric.removeMetrics(); |
| 182 | intentInstalledEventMetric.removeMetrics(); |
| 183 | intentWithdrawRequestedEventMetric.removeMetrics(); |
| 184 | intentWithdrawnEventMetric.removeMetrics(); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 185 | } |
| 186 | } |