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