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