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