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