Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.metrics.intent; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 17 | |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 19 | import org.onlab.metrics.EventMetric; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 20 | import org.onlab.metrics.MetricsService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.core.CoreService; |
| 23 | import org.onosproject.net.intent.IntentEvent; |
| 24 | import org.onosproject.net.intent.IntentListener; |
| 25 | import org.onosproject.net.intent.IntentService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 26 | import org.osgi.service.component.annotations.Activate; |
| 27 | import org.osgi.service.component.annotations.Component; |
| 28 | import org.osgi.service.component.annotations.Deactivate; |
| 29 | import org.osgi.service.component.annotations.Reference; |
| 30 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 33 | import java.util.LinkedList; |
| 34 | import java.util.List; |
| 35 | |
| 36 | import static org.slf4j.LoggerFactory.getLogger; |
| 37 | |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 38 | /** |
| 39 | * ONOS Intent Metrics Application that collects intent-related metrics. |
| 40 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 41 | @Component(immediate = true, service = { IntentMetricsService.class, IntentListener.class }) |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 42 | public class IntentMetrics implements IntentMetricsService, |
| 43 | IntentListener { |
| 44 | private static final Logger log = getLogger(IntentMetrics.class); |
| 45 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 46 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Pavlin Radoslavov | 3a46e48 | 2014-11-06 15:57:06 -0800 | [diff] [blame] | 47 | protected CoreService coreService; |
| 48 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 49 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 50 | protected IntentService intentService; |
Pavlin Radoslavov | 3a46e48 | 2014-11-06 15:57:06 -0800 | [diff] [blame] | 51 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 53 | protected MetricsService metricsService; |
| 54 | |
Pavlin Radoslavov | 3a46e48 | 2014-11-06 15:57:06 -0800 | [diff] [blame] | 55 | private ApplicationId appId; |
| 56 | |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 57 | private LinkedList<IntentEvent> lastEvents = new LinkedList<>(); |
| 58 | private static final int LAST_EVENTS_MAX_N = 100; |
| 59 | |
| 60 | // |
| 61 | // Metrics |
| 62 | // |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 63 | private static final String COMPONENT_NAME = "Intent"; |
| 64 | private static final String FEATURE_SUBMITTED_NAME = "Submitted"; |
| 65 | private static final String FEATURE_INSTALLED_NAME = "Installed"; |
Pavlin Radoslavov | 0f8a1e4 | 2015-01-13 11:52:52 -0800 | [diff] [blame] | 66 | private static final String FEATURE_FAILED_NAME = "Failed"; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 67 | private static final String FEATURE_WITHDRAW_REQUESTED_NAME = |
| 68 | "WithdrawRequested"; |
| 69 | private static final String FEATURE_WITHDRAWN_NAME = "Withdrawn"; |
Pavlin Radoslavov | 166c547 | 2015-03-18 11:47:50 -0700 | [diff] [blame] | 70 | private static final String FEATURE_PURGED_NAME = "Purged"; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 71 | // |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 72 | // Event metrics: |
| 73 | // - Intent Submitted API operation |
| 74 | // - Intent Installed operation completion |
Pavlin Radoslavov | 0f8a1e4 | 2015-01-13 11:52:52 -0800 | [diff] [blame] | 75 | // - Intent Failed compilation or installation |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 76 | // - Intent Withdraw Requested API operation |
| 77 | // - Intent Withdrawn operation completion |
Pavlin Radoslavov | 166c547 | 2015-03-18 11:47:50 -0700 | [diff] [blame] | 78 | // - Intent Purged operation completion |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 79 | // |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 80 | private EventMetric intentSubmittedEventMetric; |
| 81 | private EventMetric intentInstalledEventMetric; |
Pavlin Radoslavov | 0f8a1e4 | 2015-01-13 11:52:52 -0800 | [diff] [blame] | 82 | private EventMetric intentFailedEventMetric; |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 83 | private EventMetric intentWithdrawRequestedEventMetric; |
| 84 | private EventMetric intentWithdrawnEventMetric; |
Pavlin Radoslavov | 166c547 | 2015-03-18 11:47:50 -0700 | [diff] [blame] | 85 | private EventMetric intentPurgedEventMetric; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 86 | |
| 87 | @Activate |
| 88 | protected void activate() { |
Thomas Vachuska | fba2857 | 2015-03-25 18:48:59 -0700 | [diff] [blame] | 89 | appId = coreService.registerApplication("org.onosproject.metrics"); |
Pavlin Radoslavov | 3a46e48 | 2014-11-06 15:57:06 -0800 | [diff] [blame] | 90 | |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 91 | clear(); |
| 92 | registerMetrics(); |
| 93 | intentService.addListener(this); |
Pavlin Radoslavov | 3a46e48 | 2014-11-06 15:57:06 -0800 | [diff] [blame] | 94 | log.info("Started with Application ID {}", appId.id()); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Deactivate |
| 98 | public void deactivate() { |
| 99 | intentService.removeListener(this); |
| 100 | removeMetrics(); |
| 101 | clear(); |
Pavlin Radoslavov | 3a46e48 | 2014-11-06 15:57:06 -0800 | [diff] [blame] | 102 | log.info("Stopped"); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public List<IntentEvent> getEvents() { |
| 107 | synchronized (lastEvents) { |
Sho SHIMIZU | fd0933b | 2015-09-11 15:17:48 -0700 | [diff] [blame] | 108 | return ImmutableList.copyOf(lastEvents); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | @Override |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 113 | public EventMetric intentSubmittedEventMetric() { |
| 114 | return intentSubmittedEventMetric; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 118 | public EventMetric intentInstalledEventMetric() { |
| 119 | return intentInstalledEventMetric; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
Pavlin Radoslavov | 0f8a1e4 | 2015-01-13 11:52:52 -0800 | [diff] [blame] | 123 | public EventMetric intentFailedEventMetric() { |
| 124 | return intentFailedEventMetric; |
| 125 | } |
| 126 | |
| 127 | @Override |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 128 | public EventMetric intentWithdrawRequestedEventMetric() { |
| 129 | return intentWithdrawRequestedEventMetric; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 133 | public EventMetric intentWithdrawnEventMetric() { |
| 134 | return intentWithdrawnEventMetric; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
Pavlin Radoslavov | 166c547 | 2015-03-18 11:47:50 -0700 | [diff] [blame] | 138 | public EventMetric intentPurgedEventMetric() { |
| 139 | return intentPurgedEventMetric; |
| 140 | } |
| 141 | |
| 142 | @Override |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 143 | public void event(IntentEvent event) { |
| 144 | synchronized (lastEvents) { |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 145 | switch (event.type()) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 146 | case INSTALL_REQ: |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 147 | intentSubmittedEventMetric.eventReceived(); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 148 | break; |
| 149 | case INSTALLED: |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 150 | intentInstalledEventMetric.eventReceived(); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 151 | break; |
| 152 | case FAILED: |
Pavlin Radoslavov | 0f8a1e4 | 2015-01-13 11:52:52 -0800 | [diff] [blame] | 153 | intentFailedEventMetric.eventReceived(); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 154 | break; |
Pavlin Radoslavov | 4749f84 | 2014-12-02 13:11:22 -0800 | [diff] [blame] | 155 | case WITHDRAW_REQ: |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 156 | intentWithdrawRequestedEventMetric.eventReceived(); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 157 | break; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 158 | case WITHDRAWN: |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 159 | intentWithdrawnEventMetric.eventReceived(); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 160 | break; |
Pavlin Radoslavov | 166c547 | 2015-03-18 11:47:50 -0700 | [diff] [blame] | 161 | case PURGED: |
| 162 | intentPurgedEventMetric.eventReceived(); |
| 163 | break; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 164 | default: |
| 165 | break; |
| 166 | } |
| 167 | |
| 168 | // |
| 169 | // Keep only the last N events, where N = LAST_EVENTS_MAX_N |
| 170 | // |
| 171 | while (lastEvents.size() >= LAST_EVENTS_MAX_N) { |
| 172 | lastEvents.remove(); |
| 173 | } |
| 174 | lastEvents.add(event); |
| 175 | } |
| 176 | |
| 177 | log.debug("Intent Event: time = {} type = {} event = {}", |
| 178 | event.time(), event.type(), event); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Clears the internal state. |
| 183 | */ |
| 184 | private void clear() { |
| 185 | synchronized (lastEvents) { |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 186 | lastEvents.clear(); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Registers the metrics. |
| 192 | */ |
| 193 | private void registerMetrics() { |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 194 | intentSubmittedEventMetric = |
| 195 | new EventMetric(metricsService, COMPONENT_NAME, |
| 196 | FEATURE_SUBMITTED_NAME); |
| 197 | intentInstalledEventMetric = |
| 198 | new EventMetric(metricsService, COMPONENT_NAME, |
| 199 | FEATURE_INSTALLED_NAME); |
Pavlin Radoslavov | 0f8a1e4 | 2015-01-13 11:52:52 -0800 | [diff] [blame] | 200 | intentFailedEventMetric = |
| 201 | new EventMetric(metricsService, COMPONENT_NAME, |
| 202 | FEATURE_FAILED_NAME); |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 203 | intentWithdrawRequestedEventMetric = |
| 204 | new EventMetric(metricsService, COMPONENT_NAME, |
| 205 | FEATURE_WITHDRAW_REQUESTED_NAME); |
| 206 | intentWithdrawnEventMetric = |
| 207 | new EventMetric(metricsService, COMPONENT_NAME, |
| 208 | FEATURE_WITHDRAWN_NAME); |
Pavlin Radoslavov | 166c547 | 2015-03-18 11:47:50 -0700 | [diff] [blame] | 209 | intentPurgedEventMetric = |
| 210 | new EventMetric(metricsService, COMPONENT_NAME, |
| 211 | FEATURE_PURGED_NAME); |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 212 | |
| 213 | intentSubmittedEventMetric.registerMetrics(); |
| 214 | intentInstalledEventMetric.registerMetrics(); |
Pavlin Radoslavov | 0f8a1e4 | 2015-01-13 11:52:52 -0800 | [diff] [blame] | 215 | intentFailedEventMetric.registerMetrics(); |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 216 | intentWithdrawRequestedEventMetric.registerMetrics(); |
| 217 | intentWithdrawnEventMetric.registerMetrics(); |
Pavlin Radoslavov | 166c547 | 2015-03-18 11:47:50 -0700 | [diff] [blame] | 218 | intentPurgedEventMetric.registerMetrics(); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Removes the metrics. |
| 223 | */ |
| 224 | private void removeMetrics() { |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 225 | intentSubmittedEventMetric.removeMetrics(); |
| 226 | intentInstalledEventMetric.removeMetrics(); |
Pavlin Radoslavov | 0f8a1e4 | 2015-01-13 11:52:52 -0800 | [diff] [blame] | 227 | intentFailedEventMetric.removeMetrics(); |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 228 | intentWithdrawRequestedEventMetric.removeMetrics(); |
| 229 | intentWithdrawnEventMetric.removeMetrics(); |
Pavlin Radoslavov | 166c547 | 2015-03-18 11:47:50 -0700 | [diff] [blame] | 230 | intentPurgedEventMetric.removeMetrics(); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 231 | } |
| 232 | } |