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