kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 19 | import org.onosproject.event.AbstractListenerManager; |
| 20 | import org.opencord.aaa.AaaStatistics; |
| 21 | import org.opencord.aaa.AuthenticationStatisticsDelegate; |
| 22 | import org.opencord.aaa.AuthenticationStatisticsEvent; |
| 23 | import org.opencord.aaa.AuthenticationStatisticsEventListener; |
| 24 | import org.opencord.aaa.AuthenticationStatisticsService; |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 25 | import org.osgi.service.component.annotations.Activate; |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame^] | 26 | import org.osgi.service.component.annotations.Component; |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 27 | import org.osgi.service.component.annotations.Deactivate; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame^] | 30 | import java.util.HashMap; |
| 31 | import java.util.Map; |
| 32 | import java.util.concurrent.atomic.AtomicLong; |
| 33 | |
| 34 | import static org.slf4j.LoggerFactory.getLogger; |
| 35 | |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 36 | |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 37 | |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 38 | @Component(immediate = true) |
| 39 | public class AaaStatisticsManager |
Vijaykumar Kushwaha | a54ce55 | 2019-06-18 09:37:42 +0000 | [diff] [blame] | 40 | extends AbstractListenerManager<AuthenticationStatisticsEvent, AuthenticationStatisticsEventListener> |
| 41 | implements AuthenticationStatisticsService { |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 42 | |
| 43 | private AuthenticationStatisticsDelegate statsDelegate; |
| 44 | |
Vijaykumar Kushwaha | a54ce55 | 2019-06-18 09:37:42 +0000 | [diff] [blame] | 45 | @Override |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 46 | public AuthenticationStatisticsDelegate getStatsDelegate() { |
| 47 | return statsDelegate; |
| 48 | } |
| 49 | |
| 50 | private final Logger log = getLogger(getClass()); |
| 51 | private AaaStatistics aaaStats; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 52 | public Map<Byte, Long> outgoingPacketMap = new HashMap<Byte, Long>(); |
| 53 | private static final int PACKET_COUNT_FOR_AVERAGE_RTT_CALCULATION = 5; |
| 54 | |
Vijaykumar Kushwaha | a54ce55 | 2019-06-18 09:37:42 +0000 | [diff] [blame] | 55 | @Override |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 56 | public AaaStatistics getAaaStats() { |
| 57 | return aaaStats; |
| 58 | } |
| 59 | |
| 60 | @Activate |
| 61 | public void activate() { |
| 62 | log.info("Activate aaaStatisticsManager"); |
| 63 | aaaStats = new AaaStatistics(); |
| 64 | statsDelegate = new InternalAuthenticationDelegateForStatistics(); |
| 65 | eventDispatcher.addSink(AuthenticationStatisticsEvent.class, listenerRegistry); |
| 66 | } |
| 67 | |
| 68 | @Deactivate |
| 69 | public void deactivate() { |
| 70 | eventDispatcher.removeSink(AuthenticationStatisticsEvent.class); |
| 71 | } |
| 72 | |
Vijaykumar Kushwaha | a54ce55 | 2019-06-18 09:37:42 +0000 | [diff] [blame] | 73 | @Override |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 74 | public void handleRoundtripTime(byte inPacketIdentifier) { |
| 75 | long inTimeInMilis = System.currentTimeMillis(); |
| 76 | if (outgoingPacketMap.containsKey(inPacketIdentifier)) { |
Vijaykumar Kushwaha | a54ce55 | 2019-06-18 09:37:42 +0000 | [diff] [blame] | 77 | if (aaaStats.getPacketRoundTripTimeListSize() > PACKET_COUNT_FOR_AVERAGE_RTT_CALCULATION) { |
| 78 | aaaStats.getPacketRoundTripTimeListRemoveFirst(); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 79 | } |
Vijaykumar Kushwaha | a54ce55 | 2019-06-18 09:37:42 +0000 | [diff] [blame] | 80 | aaaStats.getPacketRoundTripTimeListAdd(inTimeInMilis - outgoingPacketMap.get(inPacketIdentifier)); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Vijaykumar Kushwaha | a54ce55 | 2019-06-18 09:37:42 +0000 | [diff] [blame] | 84 | @Override |
| 85 | public void resetAllCounters() { |
| 86 | aaaStats.resetAllCounters(); |
| 87 | } |
| 88 | |
| 89 | @Override |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 90 | public void calculatePacketRoundtripTime() { |
Vijaykumar Kushwaha | a54ce55 | 2019-06-18 09:37:42 +0000 | [diff] [blame] | 91 | if (aaaStats.getPacketRoundTripTimeListSize() > 0) { |
| 92 | long avg = (long) aaaStats.getPacketRoundTripTimeList().stream().mapToLong(i -> i).average().getAsDouble(); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 93 | aaaStats.setRequestRttMilis(new AtomicLong(avg)); |
| 94 | } |
| 95 | } |
| 96 | |
Vijaykumar Kushwaha | a54ce55 | 2019-06-18 09:37:42 +0000 | [diff] [blame] | 97 | @Override |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 98 | public void putOutgoingIdentifierToMap(byte outPacketIdentifier) { |
| 99 | outgoingPacketMap.put(outPacketIdentifier, System.currentTimeMillis()); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | *Delegate allowing the StateMachine to notify us of events. |
| 104 | */ |
| 105 | private class InternalAuthenticationDelegateForStatistics implements AuthenticationStatisticsDelegate { |
| 106 | @Override |
| 107 | public void notify(AuthenticationStatisticsEvent authenticationStatisticsEvent) { |
| 108 | log.debug("Authentication Statistics event {} for {}", authenticationStatisticsEvent.type(), |
| 109 | authenticationStatisticsEvent.subject()); |
| 110 | post(authenticationStatisticsEvent); |
| 111 | } |
| 112 | } |
| 113 | } |