blob: 125898e0ba12c28682e2d019be7c9b8fe3d78623 [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
kartikey dubeye1545422019-05-22 12:53:45 +000019import org.onosproject.event.AbstractListenerManager;
20import org.opencord.aaa.AaaStatistics;
21import org.opencord.aaa.AuthenticationStatisticsDelegate;
22import org.opencord.aaa.AuthenticationStatisticsEvent;
23import org.opencord.aaa.AuthenticationStatisticsEventListener;
24import org.opencord.aaa.AuthenticationStatisticsService;
Carmelo Cascone58b53292019-09-30 12:35:31 -070025import org.osgi.service.component.annotations.Activate;
Jonathan Hart612651f2019-11-25 09:21:43 -080026import org.osgi.service.component.annotations.Component;
Carmelo Cascone58b53292019-09-30 12:35:31 -070027import org.osgi.service.component.annotations.Deactivate;
kartikey dubeye1545422019-05-22 12:53:45 +000028import org.slf4j.Logger;
29
Jonathan Hart612651f2019-11-25 09:21:43 -080030import java.util.HashMap;
31import java.util.Map;
32import java.util.concurrent.atomic.AtomicLong;
33
34import static org.slf4j.LoggerFactory.getLogger;
35
kartikey dubeye1545422019-05-22 12:53:45 +000036
Carmelo Cascone58b53292019-09-30 12:35:31 -070037
kartikey dubeye1545422019-05-22 12:53:45 +000038@Component(immediate = true)
39public class AaaStatisticsManager
Vijaykumar Kushwahaa54ce552019-06-18 09:37:42 +000040extends AbstractListenerManager<AuthenticationStatisticsEvent, AuthenticationStatisticsEventListener>
41implements AuthenticationStatisticsService {
kartikey dubeye1545422019-05-22 12:53:45 +000042
43 private AuthenticationStatisticsDelegate statsDelegate;
44
Vijaykumar Kushwahaa54ce552019-06-18 09:37:42 +000045 @Override
kartikey dubeye1545422019-05-22 12:53:45 +000046 public AuthenticationStatisticsDelegate getStatsDelegate() {
47 return statsDelegate;
48 }
49
50 private final Logger log = getLogger(getClass());
51 private AaaStatistics aaaStats;
kartikey dubeye1545422019-05-22 12:53:45 +000052 public Map<Byte, Long> outgoingPacketMap = new HashMap<Byte, Long>();
53 private static final int PACKET_COUNT_FOR_AVERAGE_RTT_CALCULATION = 5;
54
Vijaykumar Kushwahaa54ce552019-06-18 09:37:42 +000055 @Override
kartikey dubeye1545422019-05-22 12:53:45 +000056 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 Kushwahaa54ce552019-06-18 09:37:42 +000073 @Override
kartikey dubeye1545422019-05-22 12:53:45 +000074 public void handleRoundtripTime(byte inPacketIdentifier) {
75 long inTimeInMilis = System.currentTimeMillis();
76 if (outgoingPacketMap.containsKey(inPacketIdentifier)) {
Vijaykumar Kushwahaa54ce552019-06-18 09:37:42 +000077 if (aaaStats.getPacketRoundTripTimeListSize() > PACKET_COUNT_FOR_AVERAGE_RTT_CALCULATION) {
78 aaaStats.getPacketRoundTripTimeListRemoveFirst();
kartikey dubeye1545422019-05-22 12:53:45 +000079 }
Vijaykumar Kushwahaa54ce552019-06-18 09:37:42 +000080 aaaStats.getPacketRoundTripTimeListAdd(inTimeInMilis - outgoingPacketMap.get(inPacketIdentifier));
kartikey dubeye1545422019-05-22 12:53:45 +000081 }
82 }
83
Vijaykumar Kushwahaa54ce552019-06-18 09:37:42 +000084 @Override
85 public void resetAllCounters() {
86 aaaStats.resetAllCounters();
87 }
88
89 @Override
kartikey dubeye1545422019-05-22 12:53:45 +000090 public void calculatePacketRoundtripTime() {
Vijaykumar Kushwahaa54ce552019-06-18 09:37:42 +000091 if (aaaStats.getPacketRoundTripTimeListSize() > 0) {
92 long avg = (long) aaaStats.getPacketRoundTripTimeList().stream().mapToLong(i -> i).average().getAsDouble();
kartikey dubeye1545422019-05-22 12:53:45 +000093 aaaStats.setRequestRttMilis(new AtomicLong(avg));
94 }
95 }
96
Vijaykumar Kushwahaa54ce552019-06-18 09:37:42 +000097 @Override
kartikey dubeye1545422019-05-22 12:53:45 +000098 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}