blob: 11ab99b9123ac9a7aaa6ad414bbfb279648cfc22 [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;
32import org.onlab.onos.net.intent.IntentEvent;
33import org.onlab.onos.net.intent.IntentListener;
34import org.onlab.onos.net.intent.IntentService;
35import org.slf4j.Logger;
36
37/**
38 * ONOS Intent Metrics Application that collects intent-related metrics.
39 */
40@Component(immediate = true)
41@Service
42public class IntentMetrics implements IntentMetricsService,
43 IntentListener {
44 private static final Logger log = getLogger(IntentMetrics.class);
45
46 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
47 protected IntentService intentService;
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070048 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected MetricsService metricsService;
50
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070051 private LinkedList<IntentEvent> lastEvents = new LinkedList<>();
52 private static final int LAST_EVENTS_MAX_N = 100;
53
54 //
55 // Metrics
56 //
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070057 private static final String COMPONENT_NAME = "Intent";
58 private static final String FEATURE_SUBMITTED_NAME = "Submitted";
59 private static final String FEATURE_INSTALLED_NAME = "Installed";
60 private static final String FEATURE_WITHDRAW_REQUESTED_NAME =
61 "WithdrawRequested";
62 private static final String FEATURE_WITHDRAWN_NAME = "Withdrawn";
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070063 //
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070064 // Event metrics:
65 // - Intent Submitted API operation
66 // - Intent Installed operation completion
67 // - Intent Withdraw Requested API operation
68 // - Intent Withdrawn operation completion
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070069 //
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070070 private EventMetric intentSubmittedEventMetric;
71 private EventMetric intentInstalledEventMetric;
72 private EventMetric intentWithdrawRequestedEventMetric;
73 private EventMetric intentWithdrawnEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070074
75 @Activate
76 protected void activate() {
77 clear();
78 registerMetrics();
79 intentService.addListener(this);
80 log.info("ONOS Intent Metrics started.");
81 }
82
83 @Deactivate
84 public void deactivate() {
85 intentService.removeListener(this);
86 removeMetrics();
87 clear();
88 log.info("ONOS Intent Metrics stopped.");
89 }
90
91 @Override
92 public List<IntentEvent> getEvents() {
93 synchronized (lastEvents) {
94 return ImmutableList.<IntentEvent>copyOf(lastEvents);
95 }
96 }
97
98 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070099 public EventMetric intentSubmittedEventMetric() {
100 return intentSubmittedEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700101 }
102
103 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700104 public EventMetric intentInstalledEventMetric() {
105 return intentInstalledEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700106 }
107
108 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700109 public EventMetric intentWithdrawRequestedEventMetric() {
110 return intentWithdrawRequestedEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700111 }
112
113 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700114 public EventMetric intentWithdrawnEventMetric() {
115 return intentWithdrawnEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700116 }
117
118 @Override
119 public void event(IntentEvent event) {
120 synchronized (lastEvents) {
121 //
122 // TODO: The processing below is incomplete: we don't have
123 // an event equivalent of "Withdraw Requested"
124 //
125 switch (event.type()) {
126 case SUBMITTED:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700127 intentSubmittedEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700128 break;
129 case INSTALLED:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700130 intentInstalledEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700131 break;
132 case FAILED:
133 // TODO: Just ignore?
134 break;
135 /*
136 case WITHDRAW_REQUESTED:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700137 intentWithdrawRequestedEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700138 break;
139 */
140 case WITHDRAWN:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700141 intentWithdrawnEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700142 break;
143 default:
144 break;
145 }
146
147 //
148 // Keep only the last N events, where N = LAST_EVENTS_MAX_N
149 //
150 while (lastEvents.size() >= LAST_EVENTS_MAX_N) {
151 lastEvents.remove();
152 }
153 lastEvents.add(event);
154 }
155
156 log.debug("Intent Event: time = {} type = {} event = {}",
157 event.time(), event.type(), event);
158 }
159
160 /**
161 * Clears the internal state.
162 */
163 private void clear() {
164 synchronized (lastEvents) {
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700165 lastEvents.clear();
166 }
167 }
168
169 /**
170 * Registers the metrics.
171 */
172 private void registerMetrics() {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700173 intentSubmittedEventMetric =
174 new EventMetric(metricsService, COMPONENT_NAME,
175 FEATURE_SUBMITTED_NAME);
176 intentInstalledEventMetric =
177 new EventMetric(metricsService, COMPONENT_NAME,
178 FEATURE_INSTALLED_NAME);
179 intentWithdrawRequestedEventMetric =
180 new EventMetric(metricsService, COMPONENT_NAME,
181 FEATURE_WITHDRAW_REQUESTED_NAME);
182 intentWithdrawnEventMetric =
183 new EventMetric(metricsService, COMPONENT_NAME,
184 FEATURE_WITHDRAWN_NAME);
185
186 intentSubmittedEventMetric.registerMetrics();
187 intentInstalledEventMetric.registerMetrics();
188 intentWithdrawRequestedEventMetric.registerMetrics();
189 intentWithdrawnEventMetric.registerMetrics();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700190 }
191
192 /**
193 * Removes the metrics.
194 */
195 private void removeMetrics() {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700196 intentSubmittedEventMetric.removeMetrics();
197 intentInstalledEventMetric.removeMetrics();
198 intentWithdrawRequestedEventMetric.removeMetrics();
199 intentWithdrawnEventMetric.removeMetrics();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700200 }
201}