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