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