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