blob: 31aad5ba863739925aa851bf3e3aaa3bdc69af58 [file] [log] [blame]
kartikey dubeye1545422019-05-22 12:53:45 +00001/*
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
17package org.opencord.aaa.impl;
18
19import static org.slf4j.LoggerFactory.getLogger;
20
21import java.util.HashMap;
kartikey dubeye1545422019-05-22 12:53:45 +000022import java.util.Map;
23import java.util.concurrent.atomic.AtomicLong;
24
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Service;
27import org.onosproject.event.AbstractListenerManager;
28import org.opencord.aaa.AaaStatistics;
29import org.opencord.aaa.AuthenticationStatisticsDelegate;
30import org.opencord.aaa.AuthenticationStatisticsEvent;
31import org.opencord.aaa.AuthenticationStatisticsEventListener;
32import org.opencord.aaa.AuthenticationStatisticsService;
Kartikey Dubeyf72e1952019-06-24 07:09:00 +000033import org.apache.felix.scr.annotations.Activate;
34import org.apache.felix.scr.annotations.Deactivate;
kartikey dubeye1545422019-05-22 12:53:45 +000035import org.slf4j.Logger;
36
37
38@Service
39@Component(immediate = true)
40public class AaaStatisticsManager
Vijaykumar Kushwahaa54ce552019-06-18 09:37:42 +000041extends AbstractListenerManager<AuthenticationStatisticsEvent, AuthenticationStatisticsEventListener>
42implements AuthenticationStatisticsService {
kartikey dubeye1545422019-05-22 12:53:45 +000043
44 private AuthenticationStatisticsDelegate statsDelegate;
45
Vijaykumar Kushwahaa54ce552019-06-18 09:37:42 +000046 @Override
kartikey dubeye1545422019-05-22 12:53:45 +000047 public AuthenticationStatisticsDelegate getStatsDelegate() {
48 return statsDelegate;
49 }
50
51 private final Logger log = getLogger(getClass());
52 private AaaStatistics aaaStats;
kartikey dubeye1545422019-05-22 12:53:45 +000053 public Map<Byte, Long> outgoingPacketMap = new HashMap<Byte, Long>();
54 private static final int PACKET_COUNT_FOR_AVERAGE_RTT_CALCULATION = 5;
55
Vijaykumar Kushwahaa54ce552019-06-18 09:37:42 +000056 @Override
kartikey dubeye1545422019-05-22 12:53:45 +000057 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 Kushwahaa54ce552019-06-18 09:37:42 +000074 @Override
kartikey dubeye1545422019-05-22 12:53:45 +000075 public void handleRoundtripTime(byte inPacketIdentifier) {
76 long inTimeInMilis = System.currentTimeMillis();
77 if (outgoingPacketMap.containsKey(inPacketIdentifier)) {
Vijaykumar Kushwahaa54ce552019-06-18 09:37:42 +000078 if (aaaStats.getPacketRoundTripTimeListSize() > PACKET_COUNT_FOR_AVERAGE_RTT_CALCULATION) {
79 aaaStats.getPacketRoundTripTimeListRemoveFirst();
kartikey dubeye1545422019-05-22 12:53:45 +000080 }
Vijaykumar Kushwahaa54ce552019-06-18 09:37:42 +000081 aaaStats.getPacketRoundTripTimeListAdd(inTimeInMilis - outgoingPacketMap.get(inPacketIdentifier));
kartikey dubeye1545422019-05-22 12:53:45 +000082 }
83 }
84
Vijaykumar Kushwahaa54ce552019-06-18 09:37:42 +000085 @Override
86 public void resetAllCounters() {
87 aaaStats.resetAllCounters();
88 }
89
90 @Override
kartikey dubeye1545422019-05-22 12:53:45 +000091 public void calculatePacketRoundtripTime() {
Vijaykumar Kushwahaa54ce552019-06-18 09:37:42 +000092 if (aaaStats.getPacketRoundTripTimeListSize() > 0) {
93 long avg = (long) aaaStats.getPacketRoundTripTimeList().stream().mapToLong(i -> i).average().getAsDouble();
kartikey dubeye1545422019-05-22 12:53:45 +000094 aaaStats.setRequestRttMilis(new AtomicLong(avg));
95 }
96 }
97
Vijaykumar Kushwahaa54ce552019-06-18 09:37:42 +000098 @Override
kartikey dubeye1545422019-05-22 12:53:45 +000099 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}