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