blob: bffed2e56fc4884179a0d6fb64d2ac534d5bafba [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 */
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070016package org.onlab.onos.metrics.intent;
17
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;
Pavlin Radoslavov3a46e482014-11-06 15:57:06 -080032import org.onlab.onos.core.ApplicationId;
33import org.onlab.onos.core.CoreService;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070034import org.onlab.onos.net.intent.IntentEvent;
35import org.onlab.onos.net.intent.IntentListener;
36import org.onlab.onos.net.intent.IntentService;
37import 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 =
86 coreService.registerApplication("org.onlab.onos.metrics.intent");
87
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) {
132 //
133 // TODO: The processing below is incomplete: we don't have
134 // an event equivalent of "Withdraw Requested"
135 //
136 switch (event.type()) {
137 case SUBMITTED:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700138 intentSubmittedEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700139 break;
140 case INSTALLED:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700141 intentInstalledEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700142 break;
143 case FAILED:
144 // TODO: Just ignore?
145 break;
146 /*
147 case WITHDRAW_REQUESTED:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700148 intentWithdrawRequestedEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700149 break;
150 */
151 case WITHDRAWN:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700152 intentWithdrawnEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700153 break;
154 default:
155 break;
156 }
157
158 //
159 // Keep only the last N events, where N = LAST_EVENTS_MAX_N
160 //
161 while (lastEvents.size() >= LAST_EVENTS_MAX_N) {
162 lastEvents.remove();
163 }
164 lastEvents.add(event);
165 }
166
167 log.debug("Intent Event: time = {} type = {} event = {}",
168 event.time(), event.type(), event);
169 }
170
171 /**
172 * Clears the internal state.
173 */
174 private void clear() {
175 synchronized (lastEvents) {
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700176 lastEvents.clear();
177 }
178 }
179
180 /**
181 * Registers the metrics.
182 */
183 private void registerMetrics() {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700184 intentSubmittedEventMetric =
185 new EventMetric(metricsService, COMPONENT_NAME,
186 FEATURE_SUBMITTED_NAME);
187 intentInstalledEventMetric =
188 new EventMetric(metricsService, COMPONENT_NAME,
189 FEATURE_INSTALLED_NAME);
190 intentWithdrawRequestedEventMetric =
191 new EventMetric(metricsService, COMPONENT_NAME,
192 FEATURE_WITHDRAW_REQUESTED_NAME);
193 intentWithdrawnEventMetric =
194 new EventMetric(metricsService, COMPONENT_NAME,
195 FEATURE_WITHDRAWN_NAME);
196
197 intentSubmittedEventMetric.registerMetrics();
198 intentInstalledEventMetric.registerMetrics();
199 intentWithdrawRequestedEventMetric.registerMetrics();
200 intentWithdrawnEventMetric.registerMetrics();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700201 }
202
203 /**
204 * Removes the metrics.
205 */
206 private void removeMetrics() {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700207 intentSubmittedEventMetric.removeMetrics();
208 intentInstalledEventMetric.removeMetrics();
209 intentWithdrawRequestedEventMetric.removeMetrics();
210 intentWithdrawnEventMetric.removeMetrics();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700211 }
212}