blob: ce6d2ed827c7f96129e7daa81d8a261b0519fa5c [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070019package org.onlab.onos.metrics.intent;
20
21import static org.slf4j.LoggerFactory.getLogger;
22
23import java.util.LinkedList;
24import java.util.List;
25
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070026import com.google.common.collect.ImmutableList;
27import org.apache.felix.scr.annotations.Activate;
28import org.apache.felix.scr.annotations.Component;
29import org.apache.felix.scr.annotations.Deactivate;
30import org.apache.felix.scr.annotations.Reference;
31import org.apache.felix.scr.annotations.ReferenceCardinality;
32import org.apache.felix.scr.annotations.Service;
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070033import org.onlab.metrics.EventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070034import org.onlab.metrics.MetricsService;
35import org.onlab.onos.net.intent.IntentEvent;
36import org.onlab.onos.net.intent.IntentListener;
37import org.onlab.onos.net.intent.IntentService;
38import org.slf4j.Logger;
39
40/**
41 * ONOS Intent Metrics Application that collects intent-related metrics.
42 */
43@Component(immediate = true)
44@Service
45public class IntentMetrics implements IntentMetricsService,
46 IntentListener {
47 private static final Logger log = getLogger(IntentMetrics.class);
48
49 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
50 protected IntentService intentService;
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070051 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected MetricsService metricsService;
53
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070054 private LinkedList<IntentEvent> lastEvents = new LinkedList<>();
55 private static final int LAST_EVENTS_MAX_N = 100;
56
57 //
58 // Metrics
59 //
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070060 private static final String COMPONENT_NAME = "Intent";
61 private static final String FEATURE_SUBMITTED_NAME = "Submitted";
62 private static final String FEATURE_INSTALLED_NAME = "Installed";
63 private static final String FEATURE_WITHDRAW_REQUESTED_NAME =
64 "WithdrawRequested";
65 private static final String FEATURE_WITHDRAWN_NAME = "Withdrawn";
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070066 //
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070067 // Event metrics:
68 // - Intent Submitted API operation
69 // - Intent Installed operation completion
70 // - Intent Withdraw Requested API operation
71 // - Intent Withdrawn operation completion
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070072 //
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070073 private EventMetric intentSubmittedEventMetric;
74 private EventMetric intentInstalledEventMetric;
75 private EventMetric intentWithdrawRequestedEventMetric;
76 private EventMetric intentWithdrawnEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070077
78 @Activate
79 protected void activate() {
80 clear();
81 registerMetrics();
82 intentService.addListener(this);
83 log.info("ONOS Intent Metrics started.");
84 }
85
86 @Deactivate
87 public void deactivate() {
88 intentService.removeListener(this);
89 removeMetrics();
90 clear();
91 log.info("ONOS Intent Metrics stopped.");
92 }
93
94 @Override
95 public List<IntentEvent> getEvents() {
96 synchronized (lastEvents) {
97 return ImmutableList.<IntentEvent>copyOf(lastEvents);
98 }
99 }
100
101 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700102 public EventMetric intentSubmittedEventMetric() {
103 return intentSubmittedEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700104 }
105
106 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700107 public EventMetric intentInstalledEventMetric() {
108 return intentInstalledEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700109 }
110
111 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700112 public EventMetric intentWithdrawRequestedEventMetric() {
113 return intentWithdrawRequestedEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700114 }
115
116 @Override
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700117 public EventMetric intentWithdrawnEventMetric() {
118 return intentWithdrawnEventMetric;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700119 }
120
121 @Override
122 public void event(IntentEvent event) {
123 synchronized (lastEvents) {
124 //
125 // TODO: The processing below is incomplete: we don't have
126 // an event equivalent of "Withdraw Requested"
127 //
128 switch (event.type()) {
129 case SUBMITTED:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700130 intentSubmittedEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700131 break;
132 case INSTALLED:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700133 intentInstalledEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700134 break;
135 case FAILED:
136 // TODO: Just ignore?
137 break;
138 /*
139 case WITHDRAW_REQUESTED:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700140 intentWithdrawRequestedEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700141 break;
142 */
143 case WITHDRAWN:
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700144 intentWithdrawnEventMetric.eventReceived();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700145 break;
146 default:
147 break;
148 }
149
150 //
151 // Keep only the last N events, where N = LAST_EVENTS_MAX_N
152 //
153 while (lastEvents.size() >= LAST_EVENTS_MAX_N) {
154 lastEvents.remove();
155 }
156 lastEvents.add(event);
157 }
158
159 log.debug("Intent Event: time = {} type = {} event = {}",
160 event.time(), event.type(), event);
161 }
162
163 /**
164 * Clears the internal state.
165 */
166 private void clear() {
167 synchronized (lastEvents) {
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700168 lastEvents.clear();
169 }
170 }
171
172 /**
173 * Registers the metrics.
174 */
175 private void registerMetrics() {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700176 intentSubmittedEventMetric =
177 new EventMetric(metricsService, COMPONENT_NAME,
178 FEATURE_SUBMITTED_NAME);
179 intentInstalledEventMetric =
180 new EventMetric(metricsService, COMPONENT_NAME,
181 FEATURE_INSTALLED_NAME);
182 intentWithdrawRequestedEventMetric =
183 new EventMetric(metricsService, COMPONENT_NAME,
184 FEATURE_WITHDRAW_REQUESTED_NAME);
185 intentWithdrawnEventMetric =
186 new EventMetric(metricsService, COMPONENT_NAME,
187 FEATURE_WITHDRAWN_NAME);
188
189 intentSubmittedEventMetric.registerMetrics();
190 intentInstalledEventMetric.registerMetrics();
191 intentWithdrawRequestedEventMetric.registerMetrics();
192 intentWithdrawnEventMetric.registerMetrics();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700193 }
194
195 /**
196 * Removes the metrics.
197 */
198 private void removeMetrics() {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700199 intentSubmittedEventMetric.removeMetrics();
200 intentInstalledEventMetric.removeMetrics();
201 intentWithdrawRequestedEventMetric.removeMetrics();
202 intentWithdrawnEventMetric.removeMetrics();
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700203 }
204}