HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.events; |
| 17 | |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 18 | import org.onlab.util.UnmodifiableDeque; |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 19 | import org.onosproject.cluster.ClusterService; |
| 20 | import org.onosproject.core.ApplicationId; |
| 21 | import org.onosproject.core.CoreService; |
| 22 | import org.onosproject.event.Event; |
| 23 | import org.onosproject.event.ListenerTracker; |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 24 | import org.onosproject.mastership.MastershipService; |
Yuta HIGUCHI | 858ad8c | 2016-12-08 10:59:33 -0800 | [diff] [blame] | 25 | import org.onosproject.net.config.NetworkConfigService; |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 26 | import org.onosproject.net.device.DeviceEvent; |
| 27 | import org.onosproject.net.device.DeviceListener; |
| 28 | import org.onosproject.net.device.DeviceService; |
Yuta HIGUCHI | 4e3af86 | 2016-07-21 16:03:34 -0700 | [diff] [blame] | 29 | import org.onosproject.net.edge.EdgePortService; |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 30 | import org.onosproject.net.host.HostService; |
Jon Hall | d66228c | 2017-08-09 11:24:24 -0700 | [diff] [blame] | 31 | import org.onosproject.net.intent.IntentService; |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 32 | import org.onosproject.net.link.LinkService; |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 33 | import org.onosproject.net.topology.TopologyService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 34 | import org.osgi.service.component.annotations.Activate; |
| 35 | import org.osgi.service.component.annotations.Component; |
| 36 | import org.osgi.service.component.annotations.Deactivate; |
| 37 | import org.osgi.service.component.annotations.Reference; |
| 38 | import org.osgi.service.component.annotations.ReferenceCardinality; |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 39 | import org.slf4j.Logger; |
| 40 | import org.slf4j.LoggerFactory; |
| 41 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 42 | import java.util.Deque; |
| 43 | import java.util.concurrent.ConcurrentLinkedDeque; |
| 44 | import java.util.concurrent.ScheduledExecutorService; |
| 45 | import java.util.concurrent.TimeUnit; |
| 46 | |
| 47 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 48 | import static org.onlab.util.Tools.groupedThreads; |
| 49 | import static org.onlab.util.Tools.minPriority; |
| 50 | |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 51 | /** |
| 52 | * Application to store history of instance local ONOS Events. |
| 53 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 54 | @Component(immediate = true, service = EventHistoryManager.class) |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 55 | public class EventHistoryManager |
| 56 | implements EventHistoryService { |
| 57 | |
| 58 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 59 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 61 | protected CoreService coreService; |
| 62 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 64 | protected MastershipService mastershipService; |
| 65 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 67 | protected DeviceService deviceService; |
| 68 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 70 | protected LinkService linkService; |
| 71 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 73 | protected TopologyService topologyService; |
| 74 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 76 | protected HostService hostService; |
| 77 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 79 | protected ClusterService clusterService; |
| 80 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yuta HIGUCHI | 4e3af86 | 2016-07-21 16:03:34 -0700 | [diff] [blame] | 82 | protected EdgePortService edgeService; |
| 83 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jon Hall | d66228c | 2017-08-09 11:24:24 -0700 | [diff] [blame] | 85 | protected IntentService intentService; |
| 86 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yuta HIGUCHI | 858ad8c | 2016-12-08 10:59:33 -0800 | [diff] [blame] | 88 | protected NetworkConfigService netcfgService; |
| 89 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 90 | //@Property(name = "excludeStatsEvent", boolValue = true, |
| 91 | // label = "Exclude stats related events") |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 92 | private boolean excludeStatsEvent = true; |
| 93 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 94 | //@Property(name = "sizeLimit", intValue = 10_000, |
| 95 | // label = "Number of event history to store") |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 96 | 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 Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 117 | pruner = newSingleThreadScheduledExecutor( |
| 118 | minPriority(groupedThreads("onos/events", "history-pruner", log))); |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 119 | |
| 120 | pruner.scheduleWithFixedDelay(this::pruneEventHistoryTask, |
Jian Li | 68c4fc4 | 2016-01-11 16:07:03 -0800 | [diff] [blame] | 121 | pruneInterval, pruneInterval, TimeUnit.SECONDS); |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 122 | |
| 123 | listeners = new ListenerTracker(); |
Yuta HIGUCHI | d90dbc9 | 2016-08-04 18:35:21 -0700 | [diff] [blame] | 124 | listeners.addListener(mastershipService, this::addEvent) |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 125 | .addListener(deviceService, new InternalDeviceListener()) |
Yuta HIGUCHI | d90dbc9 | 2016-08-04 18:35:21 -0700 | [diff] [blame] | 126 | .addListener(linkService, this::addEvent) |
| 127 | .addListener(topologyService, this::addEvent) |
| 128 | .addListener(hostService, this::addEvent) |
| 129 | .addListener(clusterService, this::addEvent) |
Yuta HIGUCHI | 858ad8c | 2016-12-08 10:59:33 -0800 | [diff] [blame] | 130 | .addListener(edgeService, this::addEvent) |
Jon Hall | d66228c | 2017-08-09 11:24:24 -0700 | [diff] [blame] | 131 | .addListener(intentService, this::addEvent) |
Yuta HIGUCHI | 858ad8c | 2016-12-08 10:59:33 -0800 | [diff] [blame] | 132 | .addListener(netcfgService, this::addEvent); |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 133 | |
| 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 Hall | d66228c | 2017-08-09 11:24:24 -0700 | [diff] [blame] | 169 | if (log.isTraceEnabled()) { |
| 170 | log.trace(event.toString()); |
| 171 | } |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 172 | history.offer(event); |
| 173 | } |
| 174 | |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 175 | 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 Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 193 | } |