blob: 549b35816687167d5d22081c958930312987d813 [file] [log] [blame]
HIGUCHI Yuta9092db82016-01-03 18:45:01 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080018import org.onlab.util.UnmodifiableDeque;
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080019import org.onosproject.cluster.ClusterService;
20import org.onosproject.core.ApplicationId;
21import org.onosproject.core.CoreService;
22import org.onosproject.event.Event;
23import org.onosproject.event.ListenerTracker;
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080024import org.onosproject.mastership.MastershipService;
Yuta HIGUCHI858ad8c2016-12-08 10:59:33 -080025import org.onosproject.net.config.NetworkConfigService;
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080026import org.onosproject.net.device.DeviceEvent;
27import org.onosproject.net.device.DeviceListener;
28import org.onosproject.net.device.DeviceService;
Yuta HIGUCHI4e3af862016-07-21 16:03:34 -070029import org.onosproject.net.edge.EdgePortService;
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080030import org.onosproject.net.host.HostService;
Jon Halld66228c2017-08-09 11:24:24 -070031import org.onosproject.net.intent.IntentService;
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080032import org.onosproject.net.link.LinkService;
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080033import org.onosproject.net.topology.TopologyService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070034import org.osgi.service.component.annotations.Activate;
35import org.osgi.service.component.annotations.Component;
36import org.osgi.service.component.annotations.Deactivate;
37import org.osgi.service.component.annotations.Reference;
38import org.osgi.service.component.annotations.ReferenceCardinality;
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080039import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
Ray Milkeyd84f89b2018-08-17 14:54:17 -070042import java.util.Deque;
43import java.util.concurrent.ConcurrentLinkedDeque;
44import java.util.concurrent.ScheduledExecutorService;
45import java.util.concurrent.TimeUnit;
46
47import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
48import static org.onlab.util.Tools.groupedThreads;
49import static org.onlab.util.Tools.minPriority;
50
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080051/**
52 * Application to store history of instance local ONOS Events.
53 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070054@Component(immediate = true, service = EventHistoryManager.class)
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080055public class EventHistoryManager
56 implements EventHistoryService {
57
58 private final Logger log = LoggerFactory.getLogger(getClass());
59
Ray Milkeyd84f89b2018-08-17 14:54:17 -070060 @Reference(cardinality = ReferenceCardinality.MANDATORY)
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080061 protected CoreService coreService;
62
Ray Milkeyd84f89b2018-08-17 14:54:17 -070063 @Reference(cardinality = ReferenceCardinality.MANDATORY)
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080064 protected MastershipService mastershipService;
65
Ray Milkeyd84f89b2018-08-17 14:54:17 -070066 @Reference(cardinality = ReferenceCardinality.MANDATORY)
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080067 protected DeviceService deviceService;
68
Ray Milkeyd84f89b2018-08-17 14:54:17 -070069 @Reference(cardinality = ReferenceCardinality.MANDATORY)
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080070 protected LinkService linkService;
71
Ray Milkeyd84f89b2018-08-17 14:54:17 -070072 @Reference(cardinality = ReferenceCardinality.MANDATORY)
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080073 protected TopologyService topologyService;
74
Ray Milkeyd84f89b2018-08-17 14:54:17 -070075 @Reference(cardinality = ReferenceCardinality.MANDATORY)
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080076 protected HostService hostService;
77
Ray Milkeyd84f89b2018-08-17 14:54:17 -070078 @Reference(cardinality = ReferenceCardinality.MANDATORY)
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080079 protected ClusterService clusterService;
80
Ray Milkeyd84f89b2018-08-17 14:54:17 -070081 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Yuta HIGUCHI4e3af862016-07-21 16:03:34 -070082 protected EdgePortService edgeService;
83
Ray Milkeyd84f89b2018-08-17 14:54:17 -070084 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jon Halld66228c2017-08-09 11:24:24 -070085 protected IntentService intentService;
86
Ray Milkeyd84f89b2018-08-17 14:54:17 -070087 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Yuta HIGUCHI858ad8c2016-12-08 10:59:33 -080088 protected NetworkConfigService netcfgService;
89
Ray Milkeyd84f89b2018-08-17 14:54:17 -070090 //@Property(name = "excludeStatsEvent", boolValue = true,
91 // label = "Exclude stats related events")
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080092 private boolean excludeStatsEvent = true;
93
Ray Milkeyd84f89b2018-08-17 14:54:17 -070094 //@Property(name = "sizeLimit", intValue = 10_000,
95 // label = "Number of event history to store")
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080096 private int sizeLimit = 10_000;
97
98 private ApplicationId appId;
99
100 private ListenerTracker listeners;
101
102 // Using Deque so that it'll be possible to iterate from both ends
103 // (Tail-end is the most recent event)
104 private final Deque<Event<?, ?>> history = new ConcurrentLinkedDeque<>();
105
106 private ScheduledExecutorService pruner;
107
108 // pruneEventHistoryTask() execution interval in seconds
109 private long pruneInterval = 5;
110
111
112 @Activate
113 protected void activate() {
114 appId = coreService.registerApplication("org.onosproject.events");
115 log.debug("Registered as {}", appId);
116
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800117 pruner = newSingleThreadScheduledExecutor(
118 minPriority(groupedThreads("onos/events", "history-pruner", log)));
HIGUCHI Yuta9092db82016-01-03 18:45:01 -0800119
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();
Yuta HIGUCHId90dbc92016-08-04 18:35:21 -0700124 listeners.addListener(mastershipService, this::addEvent)
HIGUCHI Yuta9092db82016-01-03 18:45:01 -0800125 .addListener(deviceService, new InternalDeviceListener())
Yuta HIGUCHId90dbc92016-08-04 18:35:21 -0700126 .addListener(linkService, this::addEvent)
127 .addListener(topologyService, this::addEvent)
128 .addListener(hostService, this::addEvent)
129 .addListener(clusterService, this::addEvent)
Yuta HIGUCHI858ad8c2016-12-08 10:59:33 -0800130 .addListener(edgeService, this::addEvent)
Jon Halld66228c2017-08-09 11:24:24 -0700131 .addListener(intentService, this::addEvent)
Yuta HIGUCHI858ad8c2016-12-08 10:59:33 -0800132 .addListener(netcfgService, this::addEvent);
HIGUCHI Yuta9092db82016-01-03 18:45:01 -0800133
134 log.info("Started");
135 }
136
137 @Deactivate
138 protected void deactivate() {
139 listeners.removeListeners();
140
141 pruner.shutdownNow();
142 history.clear();
143
144 log.info("Stopped");
145 }
146
147 @Override
148 public Deque<Event<?, ?>> history() {
149 return UnmodifiableDeque.unmodifiableDeque(history);
150 }
151
152 @Override
153 public void clear() {
154 history.clear();
155 }
156
157 // This method assumes only 1 call is in flight at the same time.
158 private void pruneEventHistoryTask() {
159 int size = history.size();
160 int overflows = size - sizeLimit;
161 if (overflows > 0) {
162 for (int i = 0; i < overflows; ++i) {
163 history.poll();
164 }
165 }
166 }
167
168 private void addEvent(Event<?, ?> event) {
Jon Halld66228c2017-08-09 11:24:24 -0700169 if (log.isTraceEnabled()) {
170 log.trace(event.toString());
171 }
HIGUCHI Yuta9092db82016-01-03 18:45:01 -0800172 history.offer(event);
173 }
174
HIGUCHI Yuta9092db82016-01-03 18:45:01 -0800175 class InternalDeviceListener
176 implements DeviceListener {
177
178 @Override
179 public boolean isRelevant(DeviceEvent event) {
180 if (excludeStatsEvent) {
181 return event.type() != DeviceEvent.Type.PORT_STATS_UPDATED;
182 } else {
183 return true;
184 }
185 }
186
187 @Override
188 public void event(DeviceEvent event) {
189 addEvent(event);
190 }
191 }
192
HIGUCHI Yuta9092db82016-01-03 18:45:01 -0800193}