HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | |
| 18 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 19 | import static org.onlab.util.Tools.groupedThreads; |
| 20 | import static org.onlab.util.Tools.minPriority; |
| 21 | |
| 22 | import java.util.Deque; |
| 23 | import java.util.concurrent.ConcurrentLinkedDeque; |
| 24 | import java.util.concurrent.ScheduledExecutorService; |
| 25 | import java.util.concurrent.TimeUnit; |
| 26 | |
| 27 | import org.apache.felix.scr.annotations.Activate; |
| 28 | import org.apache.felix.scr.annotations.Component; |
| 29 | import org.apache.felix.scr.annotations.Deactivate; |
| 30 | import org.apache.felix.scr.annotations.Property; |
| 31 | import org.apache.felix.scr.annotations.Reference; |
| 32 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 33 | import org.apache.felix.scr.annotations.Service; |
| 34 | import org.onlab.util.UnmodifiableDeque; |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 35 | import org.onosproject.cluster.ClusterService; |
| 36 | import org.onosproject.core.ApplicationId; |
| 37 | import org.onosproject.core.CoreService; |
| 38 | import org.onosproject.event.Event; |
| 39 | import org.onosproject.event.ListenerTracker; |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 40 | import org.onosproject.mastership.MastershipService; |
| 41 | import org.onosproject.net.device.DeviceEvent; |
| 42 | import org.onosproject.net.device.DeviceListener; |
| 43 | import org.onosproject.net.device.DeviceService; |
Yuta HIGUCHI | 4e3af86 | 2016-07-21 16:03:34 -0700 | [diff] [blame] | 44 | import org.onosproject.net.edge.EdgePortService; |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 45 | import org.onosproject.net.host.HostService; |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 46 | import org.onosproject.net.link.LinkService; |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 47 | import org.onosproject.net.topology.TopologyService; |
| 48 | import org.slf4j.Logger; |
| 49 | import org.slf4j.LoggerFactory; |
| 50 | |
| 51 | /** |
| 52 | * Application to store history of instance local ONOS Events. |
| 53 | */ |
| 54 | @Component(immediate = true) |
| 55 | @Service |
| 56 | public class EventHistoryManager |
| 57 | implements EventHistoryService { |
| 58 | |
| 59 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected CoreService coreService; |
| 63 | |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected MastershipService mastershipService; |
| 66 | |
| 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected DeviceService deviceService; |
| 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected LinkService linkService; |
| 72 | |
| 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 74 | protected TopologyService topologyService; |
| 75 | |
| 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 77 | protected HostService hostService; |
| 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected ClusterService clusterService; |
| 81 | |
Yuta HIGUCHI | 4e3af86 | 2016-07-21 16:03:34 -0700 | [diff] [blame] | 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected EdgePortService edgeService; |
| 84 | |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 85 | @Property(name = "excludeStatsEvent", boolValue = true, |
| 86 | label = "Exclude stats related events") |
| 87 | private boolean excludeStatsEvent = true; |
| 88 | |
| 89 | @Property(name = "sizeLimit", intValue = 10_000, |
| 90 | label = "Number of event history to store") |
| 91 | private int sizeLimit = 10_000; |
| 92 | |
| 93 | private ApplicationId appId; |
| 94 | |
| 95 | private ListenerTracker listeners; |
| 96 | |
| 97 | // Using Deque so that it'll be possible to iterate from both ends |
| 98 | // (Tail-end is the most recent event) |
| 99 | private final Deque<Event<?, ?>> history = new ConcurrentLinkedDeque<>(); |
| 100 | |
| 101 | private ScheduledExecutorService pruner; |
| 102 | |
| 103 | // pruneEventHistoryTask() execution interval in seconds |
| 104 | private long pruneInterval = 5; |
| 105 | |
| 106 | |
| 107 | @Activate |
| 108 | protected void activate() { |
| 109 | appId = coreService.registerApplication("org.onosproject.events"); |
| 110 | log.debug("Registered as {}", appId); |
| 111 | |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 112 | pruner = newSingleThreadScheduledExecutor( |
| 113 | minPriority(groupedThreads("onos/events", "history-pruner", log))); |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 114 | |
| 115 | pruner.scheduleWithFixedDelay(this::pruneEventHistoryTask, |
Jian Li | 68c4fc4 | 2016-01-11 16:07:03 -0800 | [diff] [blame] | 116 | pruneInterval, pruneInterval, TimeUnit.SECONDS); |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 117 | |
| 118 | listeners = new ListenerTracker(); |
Yuta HIGUCHI | d90dbc9 | 2016-08-04 18:35:21 -0700 | [diff] [blame] | 119 | listeners.addListener(mastershipService, this::addEvent) |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 120 | .addListener(deviceService, new InternalDeviceListener()) |
Yuta HIGUCHI | d90dbc9 | 2016-08-04 18:35:21 -0700 | [diff] [blame] | 121 | .addListener(linkService, this::addEvent) |
| 122 | .addListener(topologyService, this::addEvent) |
| 123 | .addListener(hostService, this::addEvent) |
| 124 | .addListener(clusterService, this::addEvent) |
Yuta HIGUCHI | 4e3af86 | 2016-07-21 16:03:34 -0700 | [diff] [blame] | 125 | .addListener(edgeService, this::addEvent); |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 126 | |
| 127 | log.info("Started"); |
| 128 | } |
| 129 | |
| 130 | @Deactivate |
| 131 | protected void deactivate() { |
| 132 | listeners.removeListeners(); |
| 133 | |
| 134 | pruner.shutdownNow(); |
| 135 | history.clear(); |
| 136 | |
| 137 | log.info("Stopped"); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public Deque<Event<?, ?>> history() { |
| 142 | return UnmodifiableDeque.unmodifiableDeque(history); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void clear() { |
| 147 | history.clear(); |
| 148 | } |
| 149 | |
| 150 | // This method assumes only 1 call is in flight at the same time. |
| 151 | private void pruneEventHistoryTask() { |
| 152 | int size = history.size(); |
| 153 | int overflows = size - sizeLimit; |
| 154 | if (overflows > 0) { |
| 155 | for (int i = 0; i < overflows; ++i) { |
| 156 | history.poll(); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | private void addEvent(Event<?, ?> event) { |
| 162 | history.offer(event); |
| 163 | } |
| 164 | |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 165 | class InternalDeviceListener |
| 166 | implements DeviceListener { |
| 167 | |
| 168 | @Override |
| 169 | public boolean isRelevant(DeviceEvent event) { |
| 170 | if (excludeStatsEvent) { |
| 171 | return event.type() != DeviceEvent.Type.PORT_STATS_UPDATED; |
| 172 | } else { |
| 173 | return true; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void event(DeviceEvent event) { |
| 179 | addEvent(event); |
| 180 | } |
| 181 | } |
| 182 | |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 183 | } |