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