blob: b1d91887fc73f6d5bc66c2d23909c418f5325b49 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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() {
Thomas Vachuskafba28572015-03-25 18:48:59 -070091 appId = coreService.registerApplication("org.onosproject.metrics");
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -080092
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070093 clear();
94 registerMetrics();
95 intentService.addListener(this);
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -080096 log.info("Started with Application ID {}", appId.id());
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070097 }
98
99 @Deactivate
100 public void deactivate() {
101 intentService.removeListener(this);
102 removeMetrics();
103 clear();
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -0800104 log.info("Stopped");
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700105 }
106
107 @Override
108 public List<IntentEvent> getEvents() {
109 synchronized (lastEvents) {
Sho SHIMIZUfd0933b2015-09-11 15:17:48 -0700110 return ImmutableList.copyOf(lastEvents);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700111 }
112 }
113
114 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700115 public EventMetric intentSubmittedEventMetric() {
116 return intentSubmittedEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700117 }
118
119 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700120 public EventMetric intentInstalledEventMetric() {
121 return intentInstalledEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700122 }
123
124 @Override
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -0800125 public EventMetric intentFailedEventMetric() {
126 return intentFailedEventMetric;
127 }
128
129 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700130 public EventMetric intentWithdrawRequestedEventMetric() {
131 return intentWithdrawRequestedEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700132 }
133
134 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700135 public EventMetric intentWithdrawnEventMetric() {
136 return intentWithdrawnEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700137 }
138
139 @Override
Pavlin Radoslavov166c5472015-03-18 11:47:50 -0700140 public EventMetric intentPurgedEventMetric() {
141 return intentPurgedEventMetric;
142 }
143
144 @Override
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700145 public void event(IntentEvent event) {
146 synchronized (lastEvents) {
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700147 switch (event.type()) {
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800148 case INSTALL_REQ:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700149 intentSubmittedEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700150 break;
151 case INSTALLED:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700152 intentInstalledEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700153 break;
154 case FAILED:
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -0800155 intentFailedEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700156 break;
Pavlin Radoslavov4749f842014-12-02 13:11:22 -0800157 case WITHDRAW_REQ:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700158 intentWithdrawRequestedEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700159 break;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700160 case WITHDRAWN:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700161 intentWithdrawnEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700162 break;
Pavlin Radoslavov166c5472015-03-18 11:47:50 -0700163 case PURGED:
164 intentPurgedEventMetric.eventReceived();
165 break;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700166 default:
167 break;
168 }
169
170 //
171 // Keep only the last N events, where N = LAST_EVENTS_MAX_N
172 //
173 while (lastEvents.size() >= LAST_EVENTS_MAX_N) {
174 lastEvents.remove();
175 }
176 lastEvents.add(event);
177 }
178
179 log.debug("Intent Event: time = {} type = {} event = {}",
180 event.time(), event.type(), event);
181 }
182
183 /**
184 * Clears the internal state.
185 */
186 private void clear() {
187 synchronized (lastEvents) {
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700188 lastEvents.clear();
189 }
190 }
191
192 /**
193 * Registers the metrics.
194 */
195 private void registerMetrics() {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700196 intentSubmittedEventMetric =
197 new EventMetric(metricsService, COMPONENT_NAME,
198 FEATURE_SUBMITTED_NAME);
199 intentInstalledEventMetric =
200 new EventMetric(metricsService, COMPONENT_NAME,
201 FEATURE_INSTALLED_NAME);
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -0800202 intentFailedEventMetric =
203 new EventMetric(metricsService, COMPONENT_NAME,
204 FEATURE_FAILED_NAME);
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700205 intentWithdrawRequestedEventMetric =
206 new EventMetric(metricsService, COMPONENT_NAME,
207 FEATURE_WITHDRAW_REQUESTED_NAME);
208 intentWithdrawnEventMetric =
209 new EventMetric(metricsService, COMPONENT_NAME,
210 FEATURE_WITHDRAWN_NAME);
Pavlin Radoslavov166c5472015-03-18 11:47:50 -0700211 intentPurgedEventMetric =
212 new EventMetric(metricsService, COMPONENT_NAME,
213 FEATURE_PURGED_NAME);
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700214
215 intentSubmittedEventMetric.registerMetrics();
216 intentInstalledEventMetric.registerMetrics();
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -0800217 intentFailedEventMetric.registerMetrics();
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700218 intentWithdrawRequestedEventMetric.registerMetrics();
219 intentWithdrawnEventMetric.registerMetrics();
Pavlin Radoslavov166c5472015-03-18 11:47:50 -0700220 intentPurgedEventMetric.registerMetrics();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700221 }
222
223 /**
224 * Removes the metrics.
225 */
226 private void removeMetrics() {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700227 intentSubmittedEventMetric.removeMetrics();
228 intentInstalledEventMetric.removeMetrics();
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -0800229 intentFailedEventMetric.removeMetrics();
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700230 intentWithdrawRequestedEventMetric.removeMetrics();
231 intentWithdrawnEventMetric.removeMetrics();
Pavlin Radoslavov166c5472015-03-18 11:47:50 -0700232 intentPurgedEventMetric.removeMetrics();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700233 }
234}