kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 1 | /* |
Joey Armstrong | 4de0b45 | 2023-01-18 15:26:54 -0500 | [diff] [blame] | 2 | * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [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 | |
| 17 | package org.opencord.aaa.impl; |
| 18 | |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 19 | import com.google.common.base.Strings; |
| 20 | import org.onlab.util.KryoNamespace; |
| 21 | import org.onlab.util.SafeRecurringTask; |
| 22 | import org.onlab.util.Tools; |
| 23 | import org.onosproject.cluster.ClusterService; |
| 24 | import org.onosproject.cluster.LeadershipService; |
| 25 | import org.onosproject.cluster.NodeId; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 26 | import org.onosproject.event.AbstractListenerManager; |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 27 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 28 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 29 | import org.onosproject.store.cluster.messaging.MessageSubject; |
| 30 | import org.onosproject.store.serializers.KryoNamespaces; |
| 31 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 32 | import org.onosproject.store.service.Serializer; |
| 33 | import org.onosproject.store.service.StorageService; |
| 34 | import org.onosproject.store.service.WallClockTimestamp; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 35 | import org.opencord.aaa.AaaStatistics; |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 36 | import org.opencord.aaa.AaaStatisticsSnapshot; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 37 | import org.opencord.aaa.AuthenticationStatisticsEvent; |
| 38 | import org.opencord.aaa.AuthenticationStatisticsEventListener; |
| 39 | import org.opencord.aaa.AuthenticationStatisticsService; |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 40 | import org.osgi.service.component.ComponentContext; |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 41 | import org.osgi.service.component.annotations.Activate; |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 42 | import org.osgi.service.component.annotations.Component; |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 43 | import org.osgi.service.component.annotations.Deactivate; |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 44 | import org.osgi.service.component.annotations.Modified; |
| 45 | import org.osgi.service.component.annotations.Reference; |
| 46 | import org.osgi.service.component.annotations.ReferenceCardinality; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 47 | import org.slf4j.Logger; |
| 48 | |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 49 | import java.util.Dictionary; |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 50 | import java.util.HashMap; |
| 51 | import java.util.Map; |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 52 | import java.util.Objects; |
| 53 | import java.util.concurrent.Executors; |
| 54 | import java.util.concurrent.ScheduledExecutorService; |
| 55 | import java.util.concurrent.ScheduledFuture; |
| 56 | import java.util.concurrent.TimeUnit; |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 57 | import java.util.concurrent.atomic.AtomicLong; |
| 58 | |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 59 | import static org.opencord.aaa.impl.OsgiPropertyConstants.STATISTICS_GENERATION_PERIOD; |
| 60 | import static org.opencord.aaa.impl.OsgiPropertyConstants.STATISTICS_GENERATION_PERIOD_DEFAULT; |
| 61 | import static org.opencord.aaa.impl.OsgiPropertyConstants.STATISTICS_SYNC_PERIOD; |
| 62 | import static org.opencord.aaa.impl.OsgiPropertyConstants.STATISTICS_SYNC_PERIOD_DEFAULT; |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 63 | import static org.slf4j.LoggerFactory.getLogger; |
| 64 | |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 65 | /** |
| 66 | * Manages collection and publishing of statistics for the AAA application. |
| 67 | */ |
| 68 | @Component(immediate = true, property = { |
| 69 | STATISTICS_GENERATION_PERIOD + ":Integer=" + STATISTICS_GENERATION_PERIOD_DEFAULT, |
| 70 | STATISTICS_SYNC_PERIOD + ":Integer=" + STATISTICS_SYNC_PERIOD_DEFAULT, |
| 71 | }) |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 72 | public class AaaStatisticsManager |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 73 | extends AbstractListenerManager<AuthenticationStatisticsEvent, AuthenticationStatisticsEventListener> |
| 74 | implements AuthenticationStatisticsService { |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 75 | |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 76 | private static final String AAA_STATISTICS_LEADERSHIP = "aaa-statistics"; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 77 | |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 78 | private static final MessageSubject RESET_SUBJECT = new MessageSubject("aaa-statistics-reset"); |
| 79 | |
| 80 | private int statisticsGenerationPeriodInSeconds = STATISTICS_GENERATION_PERIOD_DEFAULT; |
| 81 | private int statisticsSyncPeriodInSeconds = STATISTICS_SYNC_PERIOD_DEFAULT; |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 84 | protected StorageService storageService; |
| 85 | |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 87 | protected ClusterService clusterService; |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 90 | protected LeadershipService leadershipService; |
| 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 93 | protected ClusterCommunicationService clusterCommunicationService; |
| 94 | |
| 95 | private ScheduledExecutorService executor; |
| 96 | |
| 97 | private ScheduledFuture<?> publisherTask; |
| 98 | private ScheduledFuture<?> syncTask; |
| 99 | |
| 100 | private EventuallyConsistentMap<NodeId, AaaStatisticsSnapshot> statistics; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 101 | |
| 102 | private final Logger log = getLogger(getClass()); |
| 103 | private AaaStatistics aaaStats; |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 104 | private Map<Byte, Long> outgoingPacketMap = new HashMap<>(); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 105 | private static final int PACKET_COUNT_FOR_AVERAGE_RTT_CALCULATION = 5; |
| 106 | |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 107 | KryoNamespace serializer = KryoNamespace.newBuilder() |
| 108 | .register(KryoNamespaces.API) |
| 109 | .register(AaaStatisticsSnapshot.class) |
| 110 | .register(ClusterMessage.class) |
| 111 | .register(MessageSubject.class) |
| 112 | .build(); |
| 113 | |
Vijaykumar Kushwaha | a54ce55 | 2019-06-18 09:37:42 +0000 | [diff] [blame] | 114 | @Override |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 115 | public AaaStatistics getAaaStats() { |
| 116 | return aaaStats; |
| 117 | } |
| 118 | |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 119 | @Override |
| 120 | public AaaStatisticsSnapshot getClusterStatistics() { |
| 121 | return aggregate(); |
| 122 | } |
| 123 | |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 124 | @Activate |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 125 | public void activate(ComponentContext context) { |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 126 | log.info("Activate aaaStatisticsManager"); |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 127 | modified(context); |
| 128 | |
| 129 | statistics = storageService.<NodeId, AaaStatisticsSnapshot>eventuallyConsistentMapBuilder() |
| 130 | .withName("aaa-statistics") |
| 131 | .withSerializer(serializer) |
| 132 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 133 | .build(); |
| 134 | |
| 135 | AaaStatisticsSnapshot snapshot = statistics.get(clusterService.getLocalNode().id()); |
| 136 | if (snapshot == null) { |
| 137 | aaaStats = new AaaStatistics(); |
| 138 | } else { |
| 139 | aaaStats = AaaStatistics.fromSnapshot(snapshot); |
| 140 | } |
| 141 | |
| 142 | leadershipService.runForLeadership(AAA_STATISTICS_LEADERSHIP); |
| 143 | |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 144 | eventDispatcher.addSink(AuthenticationStatisticsEvent.class, listenerRegistry); |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 145 | |
| 146 | executor = Executors.newScheduledThreadPool(1); |
| 147 | |
| 148 | clusterCommunicationService.addSubscriber(RESET_SUBJECT, Serializer.using(serializer)::decode, |
| 149 | this::resetLocal, executor); |
| 150 | |
| 151 | syncTask = executor.scheduleAtFixedRate(SafeRecurringTask.wrap(this::syncStats), |
| 152 | 0, statisticsSyncPeriodInSeconds, TimeUnit.SECONDS); |
| 153 | |
| 154 | publisherTask = executor.scheduleAtFixedRate(SafeRecurringTask.wrap(this::publishStats), |
| 155 | 0, statisticsGenerationPeriodInSeconds, TimeUnit.SECONDS); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Deactivate |
| 159 | public void deactivate() { |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 160 | clusterCommunicationService.removeSubscriber(RESET_SUBJECT); |
| 161 | |
| 162 | publisherTask.cancel(true); |
| 163 | syncTask.cancel(true); |
| 164 | executor.shutdownNow(); |
| 165 | |
| 166 | leadershipService.withdraw(AAA_STATISTICS_LEADERSHIP); |
| 167 | |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 168 | eventDispatcher.removeSink(AuthenticationStatisticsEvent.class); |
| 169 | } |
| 170 | |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 171 | @Modified |
| 172 | public void modified(ComponentContext context) { |
| 173 | Dictionary<String, Object> properties = context.getProperties(); |
| 174 | |
| 175 | String s = Tools.get(properties, "statisticsGenerationPeriodInSeconds"); |
| 176 | statisticsGenerationPeriodInSeconds = Strings.isNullOrEmpty(s) ? STATISTICS_GENERATION_PERIOD_DEFAULT |
| 177 | : Integer.parseInt(s.trim()); |
| 178 | |
| 179 | s = Tools.get(properties, "statisticsSyncPeriodInSeconds"); |
| 180 | statisticsSyncPeriodInSeconds = Strings.isNullOrEmpty(s) ? STATISTICS_SYNC_PERIOD_DEFAULT |
| 181 | : Integer.parseInt(s.trim()); |
| 182 | } |
| 183 | |
Vijaykumar Kushwaha | a54ce55 | 2019-06-18 09:37:42 +0000 | [diff] [blame] | 184 | @Override |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 185 | public void handleRoundtripTime(byte inPacketIdentifier) { |
| 186 | long inTimeInMilis = System.currentTimeMillis(); |
| 187 | if (outgoingPacketMap.containsKey(inPacketIdentifier)) { |
Vijaykumar Kushwaha | a54ce55 | 2019-06-18 09:37:42 +0000 | [diff] [blame] | 188 | if (aaaStats.getPacketRoundTripTimeListSize() > PACKET_COUNT_FOR_AVERAGE_RTT_CALCULATION) { |
| 189 | aaaStats.getPacketRoundTripTimeListRemoveFirst(); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 190 | } |
Vijaykumar Kushwaha | a54ce55 | 2019-06-18 09:37:42 +0000 | [diff] [blame] | 191 | aaaStats.getPacketRoundTripTimeListAdd(inTimeInMilis - outgoingPacketMap.get(inPacketIdentifier)); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Vijaykumar Kushwaha | a54ce55 | 2019-06-18 09:37:42 +0000 | [diff] [blame] | 195 | @Override |
| 196 | public void resetAllCounters() { |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 197 | ClusterMessage reset = new ClusterMessage(clusterService.getLocalNode().id(), RESET_SUBJECT, new byte[]{}); |
| 198 | clusterCommunicationService.broadcastIncludeSelf(reset, RESET_SUBJECT, Serializer.using(serializer)::encode); |
Vijaykumar Kushwaha | a54ce55 | 2019-06-18 09:37:42 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 202 | public void calculatePacketRoundtripTime() { |
Vijaykumar Kushwaha | a54ce55 | 2019-06-18 09:37:42 +0000 | [diff] [blame] | 203 | if (aaaStats.getPacketRoundTripTimeListSize() > 0) { |
| 204 | long avg = (long) aaaStats.getPacketRoundTripTimeList().stream().mapToLong(i -> i).average().getAsDouble(); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 205 | aaaStats.setRequestRttMilis(new AtomicLong(avg)); |
| 206 | } |
| 207 | } |
| 208 | |
Vijaykumar Kushwaha | a54ce55 | 2019-06-18 09:37:42 +0000 | [diff] [blame] | 209 | @Override |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 210 | public void putOutgoingIdentifierToMap(byte outPacketIdentifier) { |
| 211 | outgoingPacketMap.put(outPacketIdentifier, System.currentTimeMillis()); |
| 212 | } |
| 213 | |
| 214 | /** |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 215 | * Pushes in-memory stats into the eventually-consistent map for cluster-wide retention. |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 216 | */ |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 217 | private void syncStats() { |
| 218 | calculatePacketRoundtripTime(); |
| 219 | |
| 220 | statistics.put(clusterService.getLocalNode().id(), aaaStats.snapshot()); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Aggregates cluster-wise stats from the ec-map. |
| 225 | * |
| 226 | * @return aggregate stats |
| 227 | */ |
| 228 | private AaaStatisticsSnapshot aggregate() { |
| 229 | return statistics.values().stream() |
| 230 | .reduce(new AaaStatisticsSnapshot(), AaaStatisticsSnapshot::add); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Publishes cluster-wide stats. |
| 235 | */ |
| 236 | private void publishStats() { |
| 237 | // only publish if we are the leader |
| 238 | if (!Objects.equals(leadershipService.getLeader(AAA_STATISTICS_LEADERSHIP), |
| 239 | clusterService.getLocalNode().id())) { |
| 240 | return; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 241 | } |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 242 | |
| 243 | AaaStatisticsSnapshot clusterStats = aggregate(); |
| 244 | |
| 245 | if (log.isDebugEnabled()) { |
| 246 | log.debug("Notifying stats: {}", clusterStats); |
| 247 | } |
| 248 | |
| 249 | post(new AuthenticationStatisticsEvent(AuthenticationStatisticsEvent.Type.STATS_UPDATE, |
| 250 | AaaStatistics.fromSnapshot(clusterStats))); |
| 251 | } |
| 252 | |
| 253 | private void resetLocal(ClusterMessage m) { |
| 254 | aaaStats.resetAllCounters(); |
| 255 | syncStats(); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 256 | } |
| 257 | } |