Deprecate all MD-SAL APIs
[controller.git] / opendaylight / md-sal / mdsal-trace / dom-impl / src / main / java / org / opendaylight / controller / md / sal / trace / closetracker / impl / CloseTrackedRegistry.java
1 /*
2  * Copyright (c) 2017 Red Hat, 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 package org.opendaylight.controller.md.sal.trace.closetracker.impl;
9
10 import static java.util.Arrays.asList;
11 import static java.util.Collections.emptyList;
12
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.util.Arrays;
15 import java.util.Collections;
16 import java.util.Comparator;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.concurrent.ConcurrentSkipListSet;
23
24 /**
25  * Registry of {@link CloseTracked} instances. This class is thread-safe.
26  *
27  * @author Michael Vorburger.ch
28  */
29 @Deprecated
30 public class CloseTrackedRegistry<T extends CloseTracked<T>> {
31
32     private final Object anchor;
33     private final String createDescription;
34
35     private final Set<CloseTracked<T>> tracked =
36         new ConcurrentSkipListSet<>(Comparator.comparingInt(System::identityHashCode));
37
38     private final boolean isDebugContextEnabled;
39
40     /**
41      * Constructor.
42      *
43      * @param anchor
44      *            object where this registry is stored in, used for human output in
45      *            logging and other output
46      * @param createDescription
47      *            description of creator of instances of this registry, typically
48      *            e.g. name of method in the anchor class
49      * @param isDebugContextEnabled
50      *            whether or not the call stack should be preserved; this is (of
51      *            course) an expensive operation, and should only be used during
52      *            troubleshooting
53      */
54     public CloseTrackedRegistry(Object anchor, String createDescription, boolean isDebugContextEnabled) {
55         this.anchor = anchor;
56         this.createDescription = createDescription;
57         this.isDebugContextEnabled = isDebugContextEnabled;
58     }
59
60     public boolean isDebugContextEnabled() {
61         return isDebugContextEnabled;
62     }
63
64     public Object getAnchor() {
65         return anchor;
66     }
67
68     public String getCreateDescription() {
69         return createDescription;
70     }
71
72     // package protected, not public; only CloseTrackedTrait invokes this
73     void add(CloseTracked<T> closeTracked) {
74         tracked.add(closeTracked);
75     }
76
77     // package protected, not public; only CloseTrackedTrait invokes this
78     void remove(CloseTracked<T> closeTracked) {
79         tracked.remove(closeTracked);
80     }
81
82     /**
83      * Creates and returns a "report" of (currently) tracked but not (yet) closed
84      * instances.
85      *
86      * @return Set of CloseTrackedRegistryReportEntry, of which each the stack trace
87      *         element identifies a unique allocation context (or an empty List if
88      *         debugContextEnabled is false), and value is the number of open
89      *         instances created at that place in the code.
90      */
91     // For some reason, FB sees 'map' as useless but it clearly isn't.
92     @SuppressFBWarnings("UC_USELESS_OBJECT")
93     public Set<CloseTrackedRegistryReportEntry<T>> getAllUnique() {
94         Map<List<StackTraceElement>, Long> map = new HashMap<>();
95         Set<CloseTracked<T>> copyOfTracked = new HashSet<>(tracked);
96         for (CloseTracked<T> closeTracked : copyOfTracked) {
97             final StackTraceElement[] stackTraceArray = closeTracked.getAllocationContextStackTrace();
98             List<StackTraceElement> stackTraceElements =
99                     stackTraceArray != null ? Arrays.asList(stackTraceArray) : Collections.emptyList();
100             map.merge(stackTraceElements, 1L, (oldValue, value) -> oldValue + 1);
101         }
102
103         Set<CloseTrackedRegistryReportEntry<T>> report = new HashSet<>();
104         map.forEach((stackTraceElements, number) -> copyOfTracked.stream().filter(closeTracked -> {
105             StackTraceElement[] closeTrackedStackTraceArray = closeTracked.getAllocationContextStackTrace();
106             List<StackTraceElement> closeTrackedStackTraceElements =
107                 closeTrackedStackTraceArray != null ? asList(closeTrackedStackTraceArray) : emptyList();
108             return closeTrackedStackTraceElements.equals(stackTraceElements);
109         }).findAny().ifPresent(exampleCloseTracked -> report.add(
110             new CloseTrackedRegistryReportEntry<>(exampleCloseTracked, number, stackTraceElements))));
111         return report;
112     }
113
114 }