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