blob: 89be1fe807d4de0439ee4b11ea8376da1235477c [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;
22import java.util.LinkedList;
23import java.util.Map;
24import java.util.concurrent.atomic.AtomicLong;
25
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Service;
28import org.onosproject.event.AbstractListenerManager;
29import org.opencord.aaa.AaaStatistics;
30import org.opencord.aaa.AuthenticationStatisticsDelegate;
31import org.opencord.aaa.AuthenticationStatisticsEvent;
32import org.opencord.aaa.AuthenticationStatisticsEventListener;
33import org.opencord.aaa.AuthenticationStatisticsService;
34import org.osgi.service.component.annotations.Activate;
35import org.osgi.service.component.annotations.Deactivate;
36import org.slf4j.Logger;
37
38
39@Service
40@Component(immediate = true)
41public class AaaStatisticsManager
42 extends AbstractListenerManager<AuthenticationStatisticsEvent, AuthenticationStatisticsEventListener>
43 implements AuthenticationStatisticsService {
44
45 private AuthenticationStatisticsDelegate statsDelegate;
46
47 public AuthenticationStatisticsDelegate getStatsDelegate() {
48 return statsDelegate;
49 }
50
51 private final Logger log = getLogger(getClass());
52 private AaaStatistics aaaStats;
53 private LinkedList<Long> packetRoundTripTimeList = new LinkedList<Long>();
54 public Map<Byte, Long> outgoingPacketMap = new HashMap<Byte, Long>();
55 private static final int PACKET_COUNT_FOR_AVERAGE_RTT_CALCULATION = 5;
56
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
74 public void handleRoundtripTime(byte inPacketIdentifier) {
75 long inTimeInMilis = System.currentTimeMillis();
76 if (outgoingPacketMap.containsKey(inPacketIdentifier)) {
77 if (packetRoundTripTimeList.size() > PACKET_COUNT_FOR_AVERAGE_RTT_CALCULATION) {
78 packetRoundTripTimeList.removeFirst();
79 }
80 packetRoundTripTimeList.add(inTimeInMilis - outgoingPacketMap.get(inPacketIdentifier));
81 }
82 }
83
84 public void calculatePacketRoundtripTime() {
85 if (packetRoundTripTimeList.size() > 0) {
86 long avg = (long) packetRoundTripTimeList.stream().mapToLong(i -> i).average().getAsDouble();
87 aaaStats.setRequestRttMilis(new AtomicLong(avg));
88 }
89 }
90
91 public void putOutgoingIdentifierToMap(byte outPacketIdentifier) {
92 outgoingPacketMap.put(outPacketIdentifier, System.currentTimeMillis());
93 }
94
95 /**
96 *Delegate allowing the StateMachine to notify us of events.
97 */
98 private class InternalAuthenticationDelegateForStatistics implements AuthenticationStatisticsDelegate {
99 @Override
100 public void notify(AuthenticationStatisticsEvent authenticationStatisticsEvent) {
101 log.debug("Authentication Statistics event {} for {}", authenticationStatisticsEvent.type(),
102 authenticationStatisticsEvent.subject());
103 post(authenticationStatisticsEvent);
104 }
105 }
106}