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