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