7ae6d0c957a87f0d443094d487eb440d5d5fa9bd
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / ofpspecific / SessionStatistics.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowplugin.impl.statistics.ofpspecific;
10
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.concurrent.atomic.AtomicLongFieldUpdater;
16
17 /**
18  * Created by Martin Bobak <mbobak@cisco.com> on 5.6.2015.
19  */
20 public class SessionStatistics {
21
22     private static final Map<String, Map<ConnectionStatus, EventCounter>> SESSION_EVENTS = new HashMap<>();
23
24     public static void countEvent(final String sessionId, final ConnectionStatus connectionStatus) {
25         Map<ConnectionStatus, EventCounter> sessionsConnectionEvents = getConnectionEvents(sessionId);
26         EventCounter connectionEvent = getConnectionEvent(sessionsConnectionEvents, connectionStatus);
27         connectionEvent.increment();
28     }
29
30     private static EventCounter getConnectionEvent(final Map<ConnectionStatus, EventCounter> sessionsConnectionEvents,
31                                                    final ConnectionStatus connectionStatus) {
32         EventCounter eventCounter = sessionsConnectionEvents.get(connectionStatus);
33         if (null == eventCounter) {
34             eventCounter = new EventCounter();
35             sessionsConnectionEvents.put(connectionStatus, eventCounter);
36         }
37         return eventCounter;
38     }
39
40     private static Map<ConnectionStatus, EventCounter> getConnectionEvents(final String sessionId) {
41         Map<ConnectionStatus, EventCounter> sessionConnectionEvents = SESSION_EVENTS.get(sessionId);
42         if (null == sessionConnectionEvents) {
43             sessionConnectionEvents = new HashMap<>();
44             SESSION_EVENTS.put(sessionId, sessionConnectionEvents);
45         }
46         return sessionConnectionEvents;
47     }
48
49
50     public static List<String> provideStatistics() {
51         List<String> dump = new ArrayList<>();
52         for (Map.Entry<String, Map<ConnectionStatus, EventCounter>> sessionEntries : SESSION_EVENTS.entrySet()) {
53             Map<ConnectionStatus, EventCounter> sessionEvents = sessionEntries.getValue();
54             dump.add(String.format("SESSION : %s", sessionEntries.getKey()));
55             for (Map.Entry<ConnectionStatus, EventCounter> sessionEvent : sessionEvents.entrySet()) {
56                 dump.add(String.format(" %s : %d", sessionEvent.getKey().toString(), sessionEvent.getValue().getCount()));
57             }
58         }
59         return dump;
60
61     }
62
63     public enum ConnectionStatus {
64         CONNECTION_CREATED, CONNECTION_DISCONNECTED_BY_DEVICE, CONNECTION_DISCONNECTED_BY_OFP;
65     }
66
67     private static final class EventCounter {
68         private final AtomicLongFieldUpdater<EventCounter> updater = AtomicLongFieldUpdater.newUpdater(EventCounter.class, "count");
69         private volatile long count;
70
71         public long getCount() {
72             return count;
73         }
74
75         public void increment() {
76             count = updater.incrementAndGet(this);
77         }
78     }
79
80     public static void resetAllCounters() {
81         SESSION_EVENTS.clear();
82     }
83
84 }