blob: 08db495cb45f0db83b86c17250ad3c412fb17524 [file] [log] [blame]
HIGUCHI Yuta9092db82016-01-03 18:45:01 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
HIGUCHI Yuta9092db82016-01-03 18:45:01 -08003 *
4 * 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
7 *
8 * 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.
15 */
16package org.onosproject.events;
17
18import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
19import static org.onlab.util.Tools.groupedThreads;
20import static org.onlab.util.Tools.minPriority;
21
22import java.util.Deque;
23import java.util.concurrent.ConcurrentLinkedDeque;
24import java.util.concurrent.ScheduledExecutorService;
25import java.util.concurrent.TimeUnit;
26
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.Property;
31import org.apache.felix.scr.annotations.Reference;
32import org.apache.felix.scr.annotations.ReferenceCardinality;
33import org.apache.felix.scr.annotations.Service;
34import org.onlab.util.UnmodifiableDeque;
35import org.onosproject.cluster.ClusterEvent;
36import org.onosproject.cluster.ClusterEventListener;
37import org.onosproject.cluster.ClusterService;
38import org.onosproject.core.ApplicationId;
39import org.onosproject.core.CoreService;
40import org.onosproject.event.Event;
41import org.onosproject.event.ListenerTracker;
42import org.onosproject.mastership.MastershipEvent;
43import org.onosproject.mastership.MastershipListener;
44import org.onosproject.mastership.MastershipService;
45import org.onosproject.net.device.DeviceEvent;
46import org.onosproject.net.device.DeviceListener;
47import org.onosproject.net.device.DeviceService;
Yuta HIGUCHI4e3af862016-07-21 16:03:34 -070048import org.onosproject.net.edge.EdgePortService;
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080049import org.onosproject.net.host.HostEvent;
50import org.onosproject.net.host.HostListener;
51import org.onosproject.net.host.HostService;
52import org.onosproject.net.link.LinkEvent;
53import org.onosproject.net.link.LinkListener;
54import org.onosproject.net.link.LinkService;
55import org.onosproject.net.topology.TopologyEvent;
56import org.onosproject.net.topology.TopologyListener;
57import org.onosproject.net.topology.TopologyService;
58import org.slf4j.Logger;
59import org.slf4j.LoggerFactory;
60
61/**
62 * Application to store history of instance local ONOS Events.
63 */
64@Component(immediate = true)
65@Service
66public class EventHistoryManager
67 implements EventHistoryService {
68
69 private final Logger log = LoggerFactory.getLogger(getClass());
70
71 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected CoreService coreService;
73
74 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
75 protected MastershipService mastershipService;
76
77 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected DeviceService deviceService;
79
80 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected LinkService linkService;
82
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected TopologyService topologyService;
85
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected HostService hostService;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected ClusterService clusterService;
91
Yuta HIGUCHI4e3af862016-07-21 16:03:34 -070092 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected EdgePortService edgeService;
94
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080095 @Property(name = "excludeStatsEvent", boolValue = true,
96 label = "Exclude stats related events")
97 private boolean excludeStatsEvent = true;
98
99 @Property(name = "sizeLimit", intValue = 10_000,
100 label = "Number of event history to store")
101 private int sizeLimit = 10_000;
102
103 private ApplicationId appId;
104
105 private ListenerTracker listeners;
106
107 // Using Deque so that it'll be possible to iterate from both ends
108 // (Tail-end is the most recent event)
109 private final Deque<Event<?, ?>> history = new ConcurrentLinkedDeque<>();
110
111 private ScheduledExecutorService pruner;
112
113 // pruneEventHistoryTask() execution interval in seconds
114 private long pruneInterval = 5;
115
116
117 @Activate
118 protected void activate() {
119 appId = coreService.registerApplication("org.onosproject.events");
120 log.debug("Registered as {}", appId);
121
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800122 pruner = newSingleThreadScheduledExecutor(
123 minPriority(groupedThreads("onos/events", "history-pruner", log)));
HIGUCHI Yuta9092db82016-01-03 18:45:01 -0800124
125 pruner.scheduleWithFixedDelay(this::pruneEventHistoryTask,
Jian Li68c4fc42016-01-11 16:07:03 -0800126 pruneInterval, pruneInterval, TimeUnit.SECONDS);
HIGUCHI Yuta9092db82016-01-03 18:45:01 -0800127
128 listeners = new ListenerTracker();
129 listeners.addListener(mastershipService, new InternalMastershipListener())
130 .addListener(deviceService, new InternalDeviceListener())
131 .addListener(linkService, new InternalLinkListener())
132 .addListener(topologyService, new InternalTopologyListener())
133 .addListener(hostService, new InternalHostListener())
Yuta HIGUCHI4e3af862016-07-21 16:03:34 -0700134 .addListener(clusterService, new InternalClusterListener())
135 .addListener(edgeService, this::addEvent);
HIGUCHI Yuta9092db82016-01-03 18:45:01 -0800136
137 log.info("Started");
138 }
139
140 @Deactivate
141 protected void deactivate() {
142 listeners.removeListeners();
143
144 pruner.shutdownNow();
145 history.clear();
146
147 log.info("Stopped");
148 }
149
150 @Override
151 public Deque<Event<?, ?>> history() {
152 return UnmodifiableDeque.unmodifiableDeque(history);
153 }
154
155 @Override
156 public void clear() {
157 history.clear();
158 }
159
160 // This method assumes only 1 call is in flight at the same time.
161 private void pruneEventHistoryTask() {
162 int size = history.size();
163 int overflows = size - sizeLimit;
164 if (overflows > 0) {
165 for (int i = 0; i < overflows; ++i) {
166 history.poll();
167 }
168 }
169 }
170
171 private void addEvent(Event<?, ?> event) {
172 history.offer(event);
173 }
174
175 class InternalMastershipListener
176 implements MastershipListener {
177
178 @Override
179 public void event(MastershipEvent event) {
180 addEvent(event);
181 }
182 }
183
184 class InternalDeviceListener
185 implements DeviceListener {
186
187 @Override
188 public boolean isRelevant(DeviceEvent event) {
189 if (excludeStatsEvent) {
190 return event.type() != DeviceEvent.Type.PORT_STATS_UPDATED;
191 } else {
192 return true;
193 }
194 }
195
196 @Override
197 public void event(DeviceEvent event) {
198 addEvent(event);
199 }
200 }
201
202 class InternalLinkListener
203 implements LinkListener {
204
205 @Override
206 public void event(LinkEvent event) {
207 addEvent(event);
208 }
209 }
210
211 class InternalTopologyListener
212 implements TopologyListener {
213
214 @Override
215 public void event(TopologyEvent event) {
216 addEvent(event);
217 }
218 }
219
220 class InternalHostListener
221 implements HostListener {
222
223 @Override
224 public void event(HostEvent event) {
225 addEvent(event);
226 }
227 }
228
229 class InternalClusterListener
230 implements ClusterEventListener {
231
232 @Override
233 public void event(ClusterEvent event) {
234 addEvent(event);
235 }
236 }
237
238}