Changing interface for message listener
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / MDController.java
1 /**
2  * Copyright (c) 2013 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.openflow.md.core;
10
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.ConcurrentMap;
17 import java.util.concurrent.Future;
18
19 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
20 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
21 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
22 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.google.common.collect.Lists;
28
29 /**
30  * @author mirehak
31  *
32  */
33 public class MDController implements IMDController {
34
35     private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
36
37     private SwitchConnectionProvider switchConnectionProvider;
38
39     private ConcurrentMap<Class<? extends DataObject>, Collection<IMDMessageListener>> messageListeners;
40
41     public Map<Class<? extends DataObject>, Collection<IMDMessageListener>> getMessageListeners() {
42         return messageListeners;
43     }
44
45
46     public void init() {
47         LOG.debug("Initializing!");
48         this.messageListeners = new ConcurrentHashMap<Class<? extends DataObject>, Collection<IMDMessageListener>>();
49     }
50
51     /**
52      * @param switchConnectionProvider
53      *            the switchConnectionProvider to set
54      */
55     public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
56         this.switchConnectionProvider = switchConnectionProvider;
57     }
58
59     /**
60      * @param switchConnectionProviderToUnset
61      *            the switchConnectionProvider to unset
62      */
63     public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
64         if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
65             this.switchConnectionProvider = null;
66         }
67     }
68
69     /**
70      * Function called by dependency manager after "init ()" is called and after
71      * the services provided by the class are registered in the service registry
72      *
73      */
74     public void start() {
75         LOG.debug("starting ..");
76         LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
77         // setup handler
78         SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
79         switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
80         // configure and startup library servers
81         switchConnectionProvider.configure(getConnectionConfiguration());
82         Future<List<Boolean>> srvStarted = switchConnectionProvider.startup();
83     }
84
85     /**
86      * @return wished connections configurations
87      */
88     private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
89         // TODO:: get config from state manager
90         ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
91         return Lists.newArrayList(configuration);
92     }
93
94     /**
95      * Function called by the dependency manager before the services exported by
96      * the component are unregistered, this will be followed by a "destroy ()"
97      * calls
98      *
99      */
100     public void stop() {
101         LOG.debug("stopping");
102     }
103
104     /**
105      * Function called by the dependency manager when at least one dependency
106      * become unsatisfied or when the component is shutting down because for
107      * example bundle is being stopped.
108      *
109      */
110     public void destroy() {
111         // do nothing
112     }
113
114     @Override
115     public void addMessageListener(Class<? extends DataObject> messageType, IMDMessageListener listener) {
116
117         Collection<IMDMessageListener> existingValues = messageListeners.get(messageType);
118         if (existingValues == null) {
119                existingValues = new ArrayList<IMDMessageListener>();
120                messageListeners.put(messageType, existingValues);
121         }
122         existingValues.add(listener);
123         // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
124         OFSessionUtil.getSessionManager().setListenerMapping(messageListeners);
125         LOG.debug("{} is now listened by {}", messageType, listener);
126     }
127
128     @Override
129     public void removeMessageListener(Class<? extends DataObject> messageType, IMDMessageListener listener) {
130
131         Collection<IMDMessageListener> values = messageListeners.get(messageType);
132         if (values != null) {
133                     values.remove(listener);
134                     if (values.size() == 0) {
135                         messageListeners.remove(messageType);
136                     }
137                     //Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
138                     OFSessionUtil.getSessionManager().setListenerMapping(messageListeners);
139                     LOG.debug("{} is now removed", listener);
140          }
141     }
142
143
144
145 }