blob: 8fa618a48a8155d503cec06e227bfbaad5282fea [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
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070018import com.google.common.collect.ImmutableList;
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070019import org.onlab.metrics.EventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070020import org.onlab.metrics.MetricsService;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.core.ApplicationId;
22import org.onosproject.core.CoreService;
23import org.onosproject.net.intent.IntentEvent;
24import org.onosproject.net.intent.IntentListener;
25import org.onosproject.net.intent.IntentService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070026import org.osgi.service.component.annotations.Activate;
27import org.osgi.service.component.annotations.Component;
28import org.osgi.service.component.annotations.Deactivate;
29import org.osgi.service.component.annotations.Reference;
30import org.osgi.service.component.annotations.ReferenceCardinality;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070031import org.slf4j.Logger;
32
Ray Milkeyd84f89b2018-08-17 14:54:17 -070033import java.util.LinkedList;
34import java.util.List;
35
36import static org.slf4j.LoggerFactory.getLogger;
37
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070038/**
39 * ONOS Intent Metrics Application that collects intent-related metrics.
40 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041@Component(immediate = true, service = { IntentMetricsService.class, IntentListener.class })
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070042public class IntentMetrics implements IntentMetricsService,
43 IntentListener {
44 private static final Logger log = getLogger(IntentMetrics.class);
45
Ray Milkeyd84f89b2018-08-17 14:54:17 -070046 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -080047 protected CoreService coreService;
48
Ray Milkeyd84f89b2018-08-17 14:54:17 -070049 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070050 protected IntentService intentService;
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -080051
Ray Milkeyd84f89b2018-08-17 14:54:17 -070052 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070053 protected MetricsService metricsService;
54
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -080055 private ApplicationId appId;
56
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070057 private LinkedList<IntentEvent> lastEvents = new LinkedList<>();
58 private static final int LAST_EVENTS_MAX_N = 100;
59
60 //
61 // Metrics
62 //
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070063 private static final String COMPONENT_NAME = "Intent";
64 private static final String FEATURE_SUBMITTED_NAME = "Submitted";
65 private static final String FEATURE_INSTALLED_NAME = "Installed";
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -080066 private static final String FEATURE_FAILED_NAME = "Failed";
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070067 private static final String FEATURE_WITHDRAW_REQUESTED_NAME =
68 "WithdrawRequested";
69 private static final String FEATURE_WITHDRAWN_NAME = "Withdrawn";
Pavlin Radoslavov166c5472015-03-18 11:47:50 -070070 private static final String FEATURE_PURGED_NAME = "Purged";
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070071 //
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070072 // Event metrics:
73 // - Intent Submitted API operation
74 // - Intent Installed operation completion
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -080075 // - Intent Failed compilation or installation
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070076 // - Intent Withdraw Requested API operation
77 // - Intent Withdrawn operation completion
Pavlin Radoslavov166c5472015-03-18 11:47:50 -070078 // - Intent Purged operation completion
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070079 //
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070080 private EventMetric intentSubmittedEventMetric;
81 private EventMetric intentInstalledEventMetric;
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -080082 private EventMetric intentFailedEventMetric;
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070083 private EventMetric intentWithdrawRequestedEventMetric;
84 private EventMetric intentWithdrawnEventMetric;
Pavlin Radoslavov166c5472015-03-18 11:47:50 -070085 private EventMetric intentPurgedEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070086
87 @Activate
88 protected void activate() {
Thomas Vachuskafba28572015-03-25 18:48:59 -070089 appId = coreService.registerApplication("org.onosproject.metrics");
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -080090
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070091 clear();
92 registerMetrics();
93 intentService.addListener(this);
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -080094 log.info("Started with Application ID {}", appId.id());
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070095 }
96
97 @Deactivate
98 public void deactivate() {
99 intentService.removeListener(this);
100 removeMetrics();
101 clear();
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -0800102 log.info("Stopped");
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700103 }
104
105 @Override
106 public List<IntentEvent> getEvents() {
107 synchronized (lastEvents) {
Sho SHIMIZUfd0933b2015-09-11 15:17:48 -0700108 return ImmutableList.copyOf(lastEvents);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700109 }
110 }
111
112 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700113 public EventMetric intentSubmittedEventMetric() {
114 return intentSubmittedEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700115 }
116
117 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700118 public EventMetric intentInstalledEventMetric() {
119 return intentInstalledEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700120 }
121
122 @Override
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -0800123 public EventMetric intentFailedEventMetric() {
124 return intentFailedEventMetric;
125 }
126
127 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700128 public EventMetric intentWithdrawRequestedEventMetric() {
129 return intentWithdrawRequestedEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700130 }
131
132 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700133 public EventMetric intentWithdrawnEventMetric() {
134 return intentWithdrawnEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700135 }
136
137 @Override
Pavlin Radoslavov166c5472015-03-18 11:47:50 -0700138 public EventMetric intentPurgedEventMetric() {
139 return intentPurgedEventMetric;
140 }
141
142 @Override
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700143 public void event(IntentEvent event) {
144 synchronized (lastEvents) {
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700145 switch (event.type()) {
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800146 case INSTALL_REQ:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700147 intentSubmittedEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700148 break;
149 case INSTALLED:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700150 intentInstalledEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700151 break;
152 case FAILED:
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -0800153 intentFailedEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700154 break;
Pavlin Radoslavov4749f842014-12-02 13:11:22 -0800155 case WITHDRAW_REQ:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700156 intentWithdrawRequestedEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700157 break;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700158 case WITHDRAWN:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700159 intentWithdrawnEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700160 break;
Pavlin Radoslavov166c5472015-03-18 11:47:50 -0700161 case PURGED:
162 intentPurgedEventMetric.eventReceived();
163 break;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700164 default:
165 break;
166 }
167
168 //
169 // Keep only the last N events, where N = LAST_EVENTS_MAX_N
170 //
171 while (lastEvents.size() >= LAST_EVENTS_MAX_N) {
172 lastEvents.remove();
173 }
174 lastEvents.add(event);
175 }
176
177 log.debug("Intent Event: time = {} type = {} event = {}",
178 event.time(), event.type(), event);
179 }
180
181 /**
182 * Clears the internal state.
183 */
184 private void clear() {
185 synchronized (lastEvents) {
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700186 lastEvents.clear();
187 }
188 }
189
190 /**
191 * Registers the metrics.
192 */
193 private void registerMetrics() {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700194 intentSubmittedEventMetric =
195 new EventMetric(metricsService, COMPONENT_NAME,
196 FEATURE_SUBMITTED_NAME);
197 intentInstalledEventMetric =
198 new EventMetric(metricsService, COMPONENT_NAME,
199 FEATURE_INSTALLED_NAME);
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -0800200 intentFailedEventMetric =
201 new EventMetric(metricsService, COMPONENT_NAME,
202 FEATURE_FAILED_NAME);
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700203 intentWithdrawRequestedEventMetric =
204 new EventMetric(metricsService, COMPONENT_NAME,
205 FEATURE_WITHDRAW_REQUESTED_NAME);
206 intentWithdrawnEventMetric =
207 new EventMetric(metricsService, COMPONENT_NAME,
208 FEATURE_WITHDRAWN_NAME);
Pavlin Radoslavov166c5472015-03-18 11:47:50 -0700209 intentPurgedEventMetric =
210 new EventMetric(metricsService, COMPONENT_NAME,
211 FEATURE_PURGED_NAME);
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700212
213 intentSubmittedEventMetric.registerMetrics();
214 intentInstalledEventMetric.registerMetrics();
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -0800215 intentFailedEventMetric.registerMetrics();
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700216 intentWithdrawRequestedEventMetric.registerMetrics();
217 intentWithdrawnEventMetric.registerMetrics();
Pavlin Radoslavov166c5472015-03-18 11:47:50 -0700218 intentPurgedEventMetric.registerMetrics();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700219 }
220
221 /**
222 * Removes the metrics.
223 */
224 private void removeMetrics() {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700225 intentSubmittedEventMetric.removeMetrics();
226 intentInstalledEventMetric.removeMetrics();
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -0800227 intentFailedEventMetric.removeMetrics();
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700228 intentWithdrawRequestedEventMetric.removeMetrics();
229 intentWithdrawnEventMetric.removeMetrics();
Pavlin Radoslavov166c5472015-03-18 11:47:50 -0700230 intentPurgedEventMetric.removeMetrics();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700231 }
232}