Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * Originally created by David Erickson, Stanford University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 17 | |
| 18 | package net.floodlightcontroller.core.util; |
| 19 | |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.HashSet; |
| 22 | import java.util.List; |
| 23 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 24 | import net.floodlightcontroller.core.IListener; |
| 25 | import net.floodlightcontroller.core.annotations.LogMessageDoc; |
| 26 | |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 30 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 31 | * Maintain lists of listeners ordered by dependency. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 32 | * |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 33 | * @author readams |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 34 | */ |
| 35 | public class ListenerDispatcher<U, T extends IListener<U>> { |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 36 | protected final static Logger logger = LoggerFactory.getLogger(ListenerDispatcher.class); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 37 | List<T> listeners = null; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 38 | |
| 39 | private void visit(List<T> newlisteners, U type, HashSet<T> visited, |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 40 | List<T> ordering, T listener) { |
| 41 | if (!visited.contains(listener)) { |
| 42 | visited.add(listener); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 43 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 44 | for (T i : newlisteners) { |
| 45 | if (ispre(type, i, listener)) { |
| 46 | visit(newlisteners, type, visited, ordering, i); |
| 47 | } |
| 48 | } |
| 49 | ordering.add(listener); |
| 50 | } |
| 51 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 52 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 53 | private boolean ispre(U type, T l1, T l2) { |
| 54 | return (l2.isCallbackOrderingPrereq(type, l1.getName()) || |
| 55 | l1.isCallbackOrderingPostreq(type, l2.getName())); |
| 56 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 57 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 58 | /** |
| 59 | * Add a listener to the list of listeners |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 60 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 61 | * @param listener |
| 62 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 63 | @LogMessageDoc(level = "ERROR", |
| 64 | message = "No listener dependency solution: " + |
| 65 | "No listeners without incoming dependencies", |
| 66 | explanation = "The set of listeners installed " + |
| 67 | "have dependencies with no solution", |
| 68 | recommendation = "Install a different set of listeners " + |
| 69 | "or install all dependencies. This is a defect in " + |
| 70 | "the controller installation.") |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 71 | public void addListener(U type, T listener) { |
| 72 | List<T> newlisteners = new ArrayList<T>(); |
| 73 | if (listeners != null) |
| 74 | newlisteners.addAll(listeners); |
| 75 | |
| 76 | newlisteners.add(listener); |
| 77 | // Find nodes without outgoing edges |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 78 | List<T> terminals = new ArrayList<T>(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 79 | for (T i : newlisteners) { |
| 80 | boolean isterm = true; |
| 81 | for (T j : newlisteners) { |
| 82 | if (ispre(type, i, j)) { |
| 83 | isterm = false; |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | if (isterm) { |
| 88 | terminals.add(i); |
| 89 | } |
| 90 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 91 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 92 | if (terminals.size() == 0) { |
| 93 | logger.error("No listener dependency solution: " + |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 94 | "No listeners without incoming dependencies"); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 95 | listeners = newlisteners; |
| 96 | return; |
| 97 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 98 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 99 | // visit depth-first traversing in the opposite order from |
| 100 | // the dependencies. Note we will not generally detect cycles |
| 101 | HashSet<T> visited = new HashSet<T>(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 102 | List<T> ordering = new ArrayList<T>(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 103 | for (T term : terminals) { |
| 104 | visit(newlisteners, type, visited, ordering, term); |
| 105 | } |
| 106 | listeners = ordering; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Remove the given listener |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 111 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 112 | * @param listener the listener to remove |
| 113 | */ |
| 114 | public void removeListener(T listener) { |
| 115 | if (listeners != null) { |
| 116 | List<T> newlisteners = new ArrayList<T>(); |
| 117 | newlisteners.addAll(listeners); |
| 118 | newlisteners.remove(listener); |
| 119 | listeners = newlisteners; |
| 120 | } |
| 121 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 122 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 123 | /** |
| 124 | * Clear all listeners |
| 125 | */ |
| 126 | public void clearListeners() { |
| 127 | listeners = new ArrayList<T>(); |
| 128 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Get the ordered list of listeners ordered by dependencies |
| 132 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 133 | * @return |
| 134 | */ |
| 135 | public List<T> getOrderedListeners() { |
| 136 | return listeners; |
| 137 | } |
| 138 | } |