blob: 81cc415a0a16aa10e2b103ac33c3ad9be553c4ec [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 Radoslavov295b2962014-10-23 01:12:41 -070072 //
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070073 // Event metrics:
74 // - Intent Submitted API operation
75 // - Intent Installed operation completion
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -080076 // - Intent Failed compilation or installation
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070077 // - Intent Withdraw Requested API operation
78 // - Intent Withdrawn 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 Radoslavov295b2962014-10-23 01:12:41 -070085
86 @Activate
87 protected void activate() {
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -080088 appId =
Brian O'Connorabafb502014-12-02 22:26:20 -080089 coreService.registerApplication("org.onosproject.metrics.intent");
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) {
108 return ImmutableList.<IntentEvent>copyOf(lastEvents);
109 }
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
138 public void event(IntentEvent event) {
139 synchronized (lastEvents) {
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700140 switch (event.type()) {
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800141 case INSTALL_REQ:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700142 intentSubmittedEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700143 break;
144 case INSTALLED:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700145 intentInstalledEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700146 break;
147 case FAILED:
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -0800148 intentFailedEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700149 break;
Pavlin Radoslavov4749f842014-12-02 13:11:22 -0800150 case WITHDRAW_REQ:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700151 intentWithdrawRequestedEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700152 break;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700153 case WITHDRAWN:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700154 intentWithdrawnEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700155 break;
156 default:
157 break;
158 }
159
160 //
161 // Keep only the last N events, where N = LAST_EVENTS_MAX_N
162 //
163 while (lastEvents.size() >= LAST_EVENTS_MAX_N) {
164 lastEvents.remove();
165 }
166 lastEvents.add(event);
167 }
168
169 log.debug("Intent Event: time = {} type = {} event = {}",
170 event.time(), event.type(), event);
171 }
172
173 /**
174 * Clears the internal state.
175 */
176 private void clear() {
177 synchronized (lastEvents) {
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700178 lastEvents.clear();
179 }
180 }
181
182 /**
183 * Registers the metrics.
184 */
185 private void registerMetrics() {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700186 intentSubmittedEventMetric =
187 new EventMetric(metricsService, COMPONENT_NAME,
188 FEATURE_SUBMITTED_NAME);
189 intentInstalledEventMetric =
190 new EventMetric(metricsService, COMPONENT_NAME,
191 FEATURE_INSTALLED_NAME);
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -0800192 intentFailedEventMetric =
193 new EventMetric(metricsService, COMPONENT_NAME,
194 FEATURE_FAILED_NAME);
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700195 intentWithdrawRequestedEventMetric =
196 new EventMetric(metricsService, COMPONENT_NAME,
197 FEATURE_WITHDRAW_REQUESTED_NAME);
198 intentWithdrawnEventMetric =
199 new EventMetric(metricsService, COMPONENT_NAME,
200 FEATURE_WITHDRAWN_NAME);
201
202 intentSubmittedEventMetric.registerMetrics();
203 intentInstalledEventMetric.registerMetrics();
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -0800204 intentFailedEventMetric.registerMetrics();
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700205 intentWithdrawRequestedEventMetric.registerMetrics();
206 intentWithdrawnEventMetric.registerMetrics();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700207 }
208
209 /**
210 * Removes the metrics.
211 */
212 private void removeMetrics() {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700213 intentSubmittedEventMetric.removeMetrics();
214 intentInstalledEventMetric.removeMetrics();
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -0800215 intentFailedEventMetric.removeMetrics();
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700216 intentWithdrawRequestedEventMetric.removeMetrics();
217 intentWithdrawnEventMetric.removeMetrics();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700218 }
219}