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