Mark AD-SAL interfaces as deprecated
[controller.git] / opendaylight / adsal / sal / api / src / main / java / org / opendaylight / controller / sal / utils / ListenerDispatcher.java
1 /*
2  * Copyright (c) 2011 Big Switch Networks, Inc.
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the
5  * "License"); you may not use this file except in compliance with the
6  * License. You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/legal/epl-v10.html
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
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  *
16  * This file incorporates work covered by the following copyright and
17  * permission notice:
18  *
19  *    Originally created by David Erickson, Stanford University
20  *
21  *    Licensed under the Apache License, Version 2.0 (the "License");
22  *    you may not use this file except in compliance with the
23  *    License. You may obtain a copy of the License at
24  *
25  *         http://www.apache.org/licenses/LICENSE-2.0
26  *
27  *    Unless required by applicable law or agreed to in writing,
28  *    software distributed under the License is distributed on an "AS
29  *    IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
30  *    express or implied. See the License for the specific language
31  *    governing permissions and limitations under the License.
32  */
33
34 package org.opendaylight.controller.sal.utils;
35
36 import java.util.ArrayList;
37 import java.util.HashSet;
38 import java.util.List;
39
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * Maintain lists of listeners ordered by dependency.
45  *
46  * @author readams
47  *
48  */
49 @Deprecated
50 public class ListenerDispatcher<U, T extends IListener<U>> {
51     protected static Logger logger = LoggerFactory
52             .getLogger(ListenerDispatcher.class);
53     volatile List<T> listeners = new ArrayList<T>();
54
55     private void visit(List<T> newlisteners, U type, HashSet<T> visited,
56             List<T> ordering, T listener) {
57         if (!visited.contains(listener)) {
58             visited.add(listener);
59
60             for (T i : newlisteners) {
61                 if (ispre(type, i, listener)) {
62                     visit(newlisteners, type, visited, ordering, i);
63                 }
64             }
65             ordering.add(listener);
66         }
67     }
68
69     private boolean ispre(U type, T l1, T l2) {
70         return (l2.isCallbackOrderingPrereq(type, l1.getName()) || l1
71                 .isCallbackOrderingPostreq(type, l2.getName()));
72     }
73
74     /**
75      * Add a listener to the list of listeners
76      *
77      * @param listener
78      */
79     public void addListener(U type, T listener) {
80         List<T> newlisteners = new ArrayList<T>();
81         if (listeners != null)
82             newlisteners.addAll(listeners);
83
84         newlisteners.add(listener);
85         // Find nodes without outgoing edges
86         List<T> terminals = new ArrayList<T>();
87         for (T i : newlisteners) {
88             boolean isterm = true;
89             for (T j : newlisteners) {
90                 if (ispre(type, i, j)) {
91                     isterm = false;
92                     break;
93                 }
94             }
95             if (isterm) {
96                 terminals.add(i);
97             }
98         }
99
100         if (terminals.size() == 0) {
101             logger.error("No listener dependency solution: "
102                     + "No listeners without incoming dependencies");
103             listeners = newlisteners;
104             return;
105         }
106
107         // visit depth-first traversing in the opposite order from
108         // the dependencies. Note we will not generally detect cycles
109         HashSet<T> visited = new HashSet<T>();
110         List<T> ordering = new ArrayList<T>();
111         for (T term : terminals) {
112             visit(newlisteners, type, visited, ordering, term);
113         }
114         listeners = ordering;
115     }
116
117     /**
118      * Remove the given listener
119      *
120      * @param listener
121      *            the listener to remove
122      */
123     public void removeListener(T listener) {
124         if (listeners != null) {
125             List<T> newlisteners = new ArrayList<T>();
126             newlisteners.addAll(listeners);
127             newlisteners.remove(listener);
128             listeners = newlisteners;
129         }
130     }
131
132     /**
133      * Clear all listeners
134      */
135     public void clearListeners() {
136         listeners = new ArrayList<T>();
137     }
138
139     /**
140      * Get the ordered list of listeners ordered by dependencies
141      *
142      * @return
143      */
144     public List<T> getOrderedListeners() {
145         return listeners;
146     }
147 }