blob: ef436022216448b009b92db411098aef80273963 [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";
68 private static final String FEATURE_WITHDRAW_REQUESTED_NAME =
69 "WithdrawRequested";
70 private static final String FEATURE_WITHDRAWN_NAME = "Withdrawn";
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
75 // - Intent Withdraw Requested API operation
76 // - Intent Withdrawn operation completion
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070077 //
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070078 private EventMetric intentSubmittedEventMetric;
79 private EventMetric intentInstalledEventMetric;
80 private EventMetric intentWithdrawRequestedEventMetric;
81 private EventMetric intentWithdrawnEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070082
83 @Activate
84 protected void activate() {
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -080085 appId =
Brian O'Connorabafb502014-12-02 22:26:20 -080086 coreService.registerApplication("org.onosproject.metrics.intent");
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -080087
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070088 clear();
89 registerMetrics();
90 intentService.addListener(this);
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -080091 log.info("Started with Application ID {}", appId.id());
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070092 }
93
94 @Deactivate
95 public void deactivate() {
96 intentService.removeListener(this);
97 removeMetrics();
98 clear();
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -080099 log.info("Stopped");
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700100 }
101
102 @Override
103 public List<IntentEvent> getEvents() {
104 synchronized (lastEvents) {
105 return ImmutableList.<IntentEvent>copyOf(lastEvents);
106 }
107 }
108
109 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700110 public EventMetric intentSubmittedEventMetric() {
111 return intentSubmittedEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700112 }
113
114 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700115 public EventMetric intentInstalledEventMetric() {
116 return intentInstalledEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700117 }
118
119 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700120 public EventMetric intentWithdrawRequestedEventMetric() {
121 return intentWithdrawRequestedEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700122 }
123
124 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700125 public EventMetric intentWithdrawnEventMetric() {
126 return intentWithdrawnEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700127 }
128
129 @Override
130 public void event(IntentEvent event) {
131 synchronized (lastEvents) {
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700132 switch (event.type()) {
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800133 case INSTALL_REQ:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700134 intentSubmittedEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700135 break;
136 case INSTALLED:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700137 intentInstalledEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700138 break;
139 case FAILED:
alshabib4785eec2014-12-04 16:45:45 -0800140 // ignore
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700141 break;
Pavlin Radoslavov4749f842014-12-02 13:11:22 -0800142 case WITHDRAW_REQ:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700143 intentWithdrawRequestedEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700144 break;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700145 case WITHDRAWN:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700146 intentWithdrawnEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700147 break;
148 default:
149 break;
150 }
151
152 //
153 // Keep only the last N events, where N = LAST_EVENTS_MAX_N
154 //
155 while (lastEvents.size() >= LAST_EVENTS_MAX_N) {
156 lastEvents.remove();
157 }
158 lastEvents.add(event);
159 }
160
161 log.debug("Intent Event: time = {} type = {} event = {}",
162 event.time(), event.type(), event);
163 }
164
165 /**
166 * Clears the internal state.
167 */
168 private void clear() {
169 synchronized (lastEvents) {
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700170 lastEvents.clear();
171 }
172 }
173
174 /**
175 * Registers the metrics.
176 */
177 private void registerMetrics() {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700178 intentSubmittedEventMetric =
179 new EventMetric(metricsService, COMPONENT_NAME,
180 FEATURE_SUBMITTED_NAME);
181 intentInstalledEventMetric =
182 new EventMetric(metricsService, COMPONENT_NAME,
183 FEATURE_INSTALLED_NAME);
184 intentWithdrawRequestedEventMetric =
185 new EventMetric(metricsService, COMPONENT_NAME,
186 FEATURE_WITHDRAW_REQUESTED_NAME);
187 intentWithdrawnEventMetric =
188 new EventMetric(metricsService, COMPONENT_NAME,
189 FEATURE_WITHDRAWN_NAME);
190
191 intentSubmittedEventMetric.registerMetrics();
192 intentInstalledEventMetric.registerMetrics();
193 intentWithdrawRequestedEventMetric.registerMetrics();
194 intentWithdrawnEventMetric.registerMetrics();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700195 }
196
197 /**
198 * Removes the metrics.
199 */
200 private void removeMetrics() {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700201 intentSubmittedEventMetric.removeMetrics();
202 intentInstalledEventMetric.removeMetrics();
203 intentWithdrawRequestedEventMetric.removeMetrics();
204 intentWithdrawnEventMetric.removeMetrics();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700205 }
206}