Merge "removing legacy listener on port 6633"
[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.Collection;
12 import java.util.LinkedHashSet;
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.openflowplugin.openflow.md.core.translator.ErrorTranslator;
27 import org.opendaylight.openflowplugin.openflow.md.core.translator.ExperimenterTranslator;
28 import org.opendaylight.openflowplugin.openflow.md.core.translator.FlowRemovedTranslator;
29 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultiPartMessageDescToNodeUpdatedTranslator;
30 import org.opendaylight.openflowplugin.openflow.md.core.translator.MultiPartReplyPortToNodeConnectorUpdatedTranslator;
31 import org.opendaylight.openflowplugin.openflow.md.core.translator.PacketInTranslator;
32 import org.opendaylight.openflowplugin.openflow.md.core.translator.PortStatusMessageToNodeConnectorUpdatedTranslator;
33 import org.opendaylight.openflowplugin.openflow.md.queue.PopListener;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
45 import org.opendaylight.yangtools.yang.binding.DataObject;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 import com.google.common.collect.Lists;
50
51 /**
52  * @author mirehak
53  *
54  */
55 public class MDController implements IMDController {
56
57     private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
58
59     private SwitchConnectionProvider switchConnectionProvider;
60
61     private ConcurrentMap<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> messageTranslators;
62     private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListeners;
63
64     final private int OF10 = 1;
65     final private int OF13 = 4;
66
67
68     /**
69      * @return translator mapping
70      */
71     public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getMessageTranslators() {
72         return messageTranslators;
73     }
74
75     /**
76      * provisioning of translator mapping
77      */
78     public void init() {
79         LOG.debug("Initializing!");
80         messageTranslators = new ConcurrentHashMap<>();
81         popListeners = new ConcurrentHashMap<>();
82         //TODO: move registration to factory
83         addMessageTranslator(ErrorMessage.class, OF10, new ErrorTranslator());
84         addMessageTranslator(ErrorMessage.class, OF13, new ErrorTranslator());
85         addMessageTranslator(FlowRemovedMessage.class, OF13, new FlowRemovedTranslator());
86         addMessageTranslator(PacketInMessage.class,OF10, new PacketInTranslator());
87         addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator());
88         addMessageTranslator(PortStatusMessage.class,OF10, new PortStatusMessageToNodeConnectorUpdatedTranslator());
89         addMessageTranslator(PortStatusMessage.class,OF13, new PortStatusMessageToNodeConnectorUpdatedTranslator());
90         addMessageTranslator(MultipartReplyMessage.class,OF13,new MultiPartReplyPortToNodeConnectorUpdatedTranslator());
91         addMessageTranslator(MultipartReplyMessage.class,OF13, new MultiPartMessageDescToNodeUpdatedTranslator());
92         addMessageTranslator(ExperimenterMessage.class, OF10, new ExperimenterTranslator());
93
94         //TODO: move registration to factory
95         NotificationPopListener<DataObject> notificationPopListener = new NotificationPopListener<DataObject>();
96         addMessagePopListener(NodeConnectorUpdated.class,notificationPopListener);
97         addMessagePopListener(PacketReceived.class,notificationPopListener);
98         addMessagePopListener(TransmitPacketInput.class, notificationPopListener);
99         addMessagePopListener(NodeUpdated.class, notificationPopListener);
100
101         // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
102         OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
103         OFSessionUtil.getSessionManager().setPopListenerMapping(popListeners);
104     }
105
106     /**
107      * @param switchConnectionProvider
108      *            the switchConnectionProvider to set
109      */
110     public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
111         this.switchConnectionProvider = switchConnectionProvider;
112     }
113
114     /**
115      * @param switchConnectionProviderToUnset
116      *            the switchConnectionProvider to unset
117      */
118     public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
119         if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
120             this.switchConnectionProvider = null;
121         }
122     }
123
124     /**
125      * Function called by dependency manager after "init ()" is called and after
126      * the services provided by the class are registered in the service registry
127      *
128      */
129     public void start() {
130         LOG.debug("starting ..");
131         LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
132         // setup handler
133         SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
134         switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
135         // configure and startup library servers
136         switchConnectionProvider.configure(getConnectionConfiguration());
137         Future<List<Boolean>> srvStarted = switchConnectionProvider.startup();
138     }
139
140     /**
141      * @return wished connections configurations
142      */
143     private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
144         // TODO:: get config from state manager
145         ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
146         ConnectionConfiguration configurationLegacy = ConnectionConfigurationFactory.getLegacy();
147         return Lists.newArrayList(configuration, configurationLegacy);
148     }
149
150     /**
151      * Function called by the dependency manager before the services exported by
152      * the component are unregistered, this will be followed by a "destroy ()"
153      * calls
154      *
155      */
156     public void stop() {
157         LOG.debug("stopping");
158         Future<List<Boolean>> srvStopped = switchConnectionProvider.shutdown();
159         try {
160             srvStopped.get(5000, TimeUnit.MILLISECONDS);
161         } catch (InterruptedException | ExecutionException | TimeoutException e) {
162             LOG.error(e.getMessage(), e);
163         }
164     }
165
166     /**
167      * Function called by the dependency manager when at least one dependency
168      * become unsatisfied or when the component is shutting down because for
169      * example bundle is being stopped.
170      *
171      */
172     public void destroy() {
173         // do nothing
174     }
175
176     @Override
177     public void addMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
178         TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
179
180         Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> existingValues = messageTranslators.get(tKey);
181         if (existingValues == null) {
182             existingValues = new LinkedHashSet<>();
183             messageTranslators.put(tKey, existingValues);
184         }
185         existingValues.add(translator);
186         LOG.debug("{} is now translated by {}", messageType, translator);
187     }
188
189     @Override
190     public void removeMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, List<DataObject>> translator) {
191         TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
192         Collection<IMDMessageTranslator<OfHeader, List<DataObject>>> values = messageTranslators.get(tKey);
193         if (values != null) {
194             values.remove(translator);
195             if (values.isEmpty()) {
196                 messageTranslators.remove(tKey);
197             }
198             LOG.debug("{} is now removed from translators", translator);
199          }
200     }
201
202     @Override
203     public void addMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
204         Collection<PopListener<DataObject>> existingValues = popListeners.get(messageType);
205         if (existingValues == null) {
206             existingValues = new LinkedHashSet<>();
207             popListeners.put(messageType, existingValues);
208         }
209         existingValues.add(popListener);
210         LOG.debug("{} is now popListened by {}", messageType, popListener);
211     }
212
213     @Override
214     public void removeMessagePopListener(Class<? extends DataObject> messageType, PopListener<DataObject> popListener) {
215         Collection<PopListener<DataObject>> values = popListeners.get(messageType);
216         if (values != null) {
217             values.remove(popListener);
218             if (values.isEmpty()) {
219                 popListeners.remove(messageType);
220             }
221             LOG.debug("{} is now removed from popListeners", popListener);
222          }
223     }
224
225
226 }