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