/* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.openflowplugin.impl.statistics.ofpspecific; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicLongFieldUpdater; public class SessionStatistics { private static final Map> SESSION_EVENTS = new HashMap<>(); public static void countEvent(final String sessionId, final ConnectionStatus connectionStatus) { Map sessionsConnectionEvents = getConnectionEvents(sessionId); EventCounter connectionEvent = getConnectionEvent(sessionsConnectionEvents, connectionStatus); connectionEvent.increment(); } private static EventCounter getConnectionEvent(final Map sessionsConnectionEvents, final ConnectionStatus connectionStatus) { EventCounter eventCounter = sessionsConnectionEvents.get(connectionStatus); if (null == eventCounter) { eventCounter = new EventCounter(); sessionsConnectionEvents.put(connectionStatus, eventCounter); } return eventCounter; } private static Map getConnectionEvents(final String sessionId) { Map sessionConnectionEvents = SESSION_EVENTS.get(sessionId); if (null == sessionConnectionEvents) { sessionConnectionEvents = new HashMap<>(); SESSION_EVENTS.put(sessionId, sessionConnectionEvents); } return sessionConnectionEvents; } public static List provideStatistics() { List dump = new ArrayList<>(); for (Map.Entry> sessionEntries : SESSION_EVENTS.entrySet()) { Map sessionEvents = sessionEntries.getValue(); dump.add(String.format("SESSION : %s", sessionEntries.getKey())); for (Map.Entry sessionEvent : sessionEvents.entrySet()) { dump.add(String.format(" %s : %d", sessionEvent.getKey().toString(), sessionEvent.getValue().getCount())); } } return dump; } public enum ConnectionStatus { CONNECTION_CREATED, CONNECTION_DISCONNECTED_BY_DEVICE, CONNECTION_DISCONNECTED_BY_OFP } private static final class EventCounter { private final AtomicLongFieldUpdater updater = AtomicLongFieldUpdater.newUpdater(EventCounter.class, "count"); private volatile long count; public long getCount() { return count; } public void increment() { count = updater.incrementAndGet(this); } } public static void resetAllCounters() { SESSION_EVENTS.clear(); } }