blob: 237fd170eed785874104cdaee9e53d9f4cfdf6b9 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.metrics.intent;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070017
18import static org.slf4j.LoggerFactory.getLogger;
19
20import java.util.LinkedList;
21import java.util.List;
22
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070023import com.google.common.collect.ImmutableList;
24import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
27import org.apache.felix.scr.annotations.Reference;
28import org.apache.felix.scr.annotations.ReferenceCardinality;
29import org.apache.felix.scr.annotations.Service;
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070030import org.onlab.metrics.EventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070031import org.onlab.metrics.MetricsService;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.core.ApplicationId;
33import org.onosproject.core.CoreService;
34import org.onosproject.net.intent.IntentEvent;
35import org.onosproject.net.intent.IntentListener;
36import org.onosproject.net.intent.IntentService;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070037import org.slf4j.Logger;
38
39/**
40 * ONOS Intent Metrics Application that collects intent-related metrics.
41 */
42@Component(immediate = true)
43@Service
44public class IntentMetrics implements IntentMetricsService,
45 IntentListener {
46 private static final Logger log = getLogger(IntentMetrics.class);
47
48 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -080049 protected CoreService coreService;
50
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070052 protected IntentService intentService;
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -080053
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070054 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected MetricsService metricsService;
56
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -080057 private ApplicationId appId;
58
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070059 private LinkedList<IntentEvent> lastEvents = new LinkedList<>();
60 private static final int LAST_EVENTS_MAX_N = 100;
61
62 //
63 // Metrics
64 //
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070065 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";
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -080068 private static final String FEATURE_FAILED_NAME = "Failed";
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070069 private static final String FEATURE_WITHDRAW_REQUESTED_NAME =
70 "WithdrawRequested";
71 private static final String FEATURE_WITHDRAWN_NAME = "Withdrawn";
Pavlin Radoslavov166c5472015-03-18 11:47:50 -070072 private static final String FEATURE_PURGED_NAME = "Purged";
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070073 //
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070074 // Event metrics:
75 // - Intent Submitted API operation
76 // - Intent Installed operation completion
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -080077 // - Intent Failed compilation or installation
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070078 // - Intent Withdraw Requested API operation
79 // - Intent Withdrawn operation completion
Pavlin Radoslavov166c5472015-03-18 11:47:50 -070080 // - Intent Purged operation completion
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070081 //
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070082 private EventMetric intentSubmittedEventMetric;
83 private EventMetric intentInstalledEventMetric;
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -080084 private EventMetric intentFailedEventMetric;
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070085 private EventMetric intentWithdrawRequestedEventMetric;
86 private EventMetric intentWithdrawnEventMetric;
Pavlin Radoslavov166c5472015-03-18 11:47:50 -070087 private EventMetric intentPurgedEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070088
89 @Activate
90 protected void activate() {
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -080091 appId =
Brian O'Connorabafb502014-12-02 22:26:20 -080092 coreService.registerApplication("org.onosproject.metrics.intent");
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -080093
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070094 clear();
95 registerMetrics();
96 intentService.addListener(this);
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -080097 log.info("Started with Application ID {}", appId.id());
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070098 }
99
100 @Deactivate
101 public void deactivate() {
102 intentService.removeListener(this);
103 removeMetrics();
104 clear();
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -0800105 log.info("Stopped");
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700106 }
107
108 @Override
109 public List<IntentEvent> getEvents() {
110 synchronized (lastEvents) {
111 return ImmutableList.<IntentEvent>copyOf(lastEvents);
112 }
113 }
114
115 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700116 public EventMetric intentSubmittedEventMetric() {
117 return intentSubmittedEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700118 }
119
120 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700121 public EventMetric intentInstalledEventMetric() {
122 return intentInstalledEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700123 }
124
125 @Override
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -0800126 public EventMetric intentFailedEventMetric() {
127 return intentFailedEventMetric;
128 }
129
130 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700131 public EventMetric intentWithdrawRequestedEventMetric() {
132 return intentWithdrawRequestedEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700133 }
134
135 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700136 public EventMetric intentWithdrawnEventMetric() {
137 return intentWithdrawnEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700138 }
139
140 @Override
Pavlin Radoslavov166c5472015-03-18 11:47:50 -0700141 public EventMetric intentPurgedEventMetric() {
142 return intentPurgedEventMetric;
143 }
144
145 @Override
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700146 public void event(IntentEvent event) {
147 synchronized (lastEvents) {
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700148 switch (event.type()) {
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800149 case INSTALL_REQ:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700150 intentSubmittedEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700151 break;
152 case INSTALLED:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700153 intentInstalledEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700154 break;
155 case FAILED:
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -0800156 intentFailedEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700157 break;
Pavlin Radoslavov4749f842014-12-02 13:11:22 -0800158 case WITHDRAW_REQ:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700159 intentWithdrawRequestedEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700160 break;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700161 case WITHDRAWN:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700162 intentWithdrawnEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700163 break;
Pavlin Radoslavov166c5472015-03-18 11:47:50 -0700164 case PURGED:
165 intentPurgedEventMetric.eventReceived();
166 break;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700167 default:
168 break;
169 }
170
171 //
172 // Keep only the last N events, where N = LAST_EVENTS_MAX_N
173 //
174 while (lastEvents.size() >= LAST_EVENTS_MAX_N) {
175 lastEvents.remove();
176 }
177 lastEvents.add(event);
178 }
179
180 log.debug("Intent Event: time = {} type = {} event = {}",
181 event.time(), event.type(), event);
182 }
183
184 /**
185 * Clears the internal state.
186 */
187 private void clear() {
188 synchronized (lastEvents) {
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700189 lastEvents.clear();
190 }
191 }
192
193 /**
194 * Registers the metrics.
195 */
196 private void registerMetrics() {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700197 intentSubmittedEventMetric =
198 new EventMetric(metricsService, COMPONENT_NAME,
199 FEATURE_SUBMITTED_NAME);
200 intentInstalledEventMetric =
201 new EventMetric(metricsService, COMPONENT_NAME,
202 FEATURE_INSTALLED_NAME);
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -0800203 intentFailedEventMetric =
204 new EventMetric(metricsService, COMPONENT_NAME,
205 FEATURE_FAILED_NAME);
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700206 intentWithdrawRequestedEventMetric =
207 new EventMetric(metricsService, COMPONENT_NAME,
208 FEATURE_WITHDRAW_REQUESTED_NAME);
209 intentWithdrawnEventMetric =
210 new EventMetric(metricsService, COMPONENT_NAME,
211 FEATURE_WITHDRAWN_NAME);
Pavlin Radoslavov166c5472015-03-18 11:47:50 -0700212 intentPurgedEventMetric =
213 new EventMetric(metricsService, COMPONENT_NAME,
214 FEATURE_PURGED_NAME);
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700215
216 intentSubmittedEventMetric.registerMetrics();
217 intentInstalledEventMetric.registerMetrics();
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -0800218 intentFailedEventMetric.registerMetrics();
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700219 intentWithdrawRequestedEventMetric.registerMetrics();
220 intentWithdrawnEventMetric.registerMetrics();
Pavlin Radoslavov166c5472015-03-18 11:47:50 -0700221 intentPurgedEventMetric.registerMetrics();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700222 }
223
224 /**
225 * Removes the metrics.
226 */
227 private void removeMetrics() {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700228 intentSubmittedEventMetric.removeMetrics();
229 intentInstalledEventMetric.removeMetrics();
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -0800230 intentFailedEventMetric.removeMetrics();
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700231 intentWithdrawRequestedEventMetric.removeMetrics();
232 intentWithdrawnEventMetric.removeMetrics();
Pavlin Radoslavov166c5472015-03-18 11:47:50 -0700233 intentPurgedEventMetric.removeMetrics();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700234 }
235}