Extensibility support (serialization part)
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / connection / SwitchConnectionProviderImpl.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. 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
10 package org.opendaylight.openflowjava.protocol.impl.connection;
11
12 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
13 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration.FEATURE_SUPPORT;
14 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
18 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler;
19 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;
20 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
21 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.google.common.util.concurrent.ListenableFuture;
27 import com.google.common.util.concurrent.SettableFuture;
28
29 /**
30  * Exposed class for server handling
31  * @author mirehak
32  * @author michal.polkorab
33  */
34 public class SwitchConnectionProviderImpl implements SwitchConnectionProvider {
35
36     private static final Logger LOGGER = LoggerFactory
37             .getLogger(SwitchConnectionProviderImpl.class);
38     private SwitchConnectionHandler switchConnectionHandler;
39     private ServerFacade serverFacade;
40     private ConnectionConfiguration connConfig;
41     private SerializationFactory serializationFactory;
42     private SerializerRegistry serializerRegistry;
43
44     /** Constructor */
45     public SwitchConnectionProviderImpl() {
46         serializerRegistry = new SerializerRegistryImpl();
47         serializerRegistry.init();
48         serializationFactory = new SerializationFactory();
49         serializationFactory.setSerializerTable(serializerRegistry);
50     }
51
52     @Override
53     public void setConfiguration(ConnectionConfiguration connConfig) {
54         this.connConfig = connConfig;
55     }
56
57     @Override
58     public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
59         LOGGER.debug("setSwitchConnectionHandler");
60         this.switchConnectionHandler = switchConnectionHandler;
61     }
62
63     @Override
64     public ListenableFuture<Boolean> shutdown() {
65         LOGGER.debug("Shutdown summoned");
66         //TODO: provide exception in case of: not started, not configured (already stopped)
67         ListenableFuture<Boolean> result = serverFacade.shutdown();
68         return result;
69     }
70
71     @Override
72     public ListenableFuture<Boolean> startup() {
73         LOGGER.debug("Startup summoned");
74         serverFacade = createAndConfigureServer();
75         
76         LOGGER.debug("Starting ..");
77         ListenableFuture<Boolean> result = null;
78         try {
79             if (serverFacade == null) {
80                 throw new IllegalStateException("No server configured");
81             }
82             if (serverFacade.getIsOnlineFuture().isDone()) {
83                 throw new IllegalStateException("Server already running");
84             }
85             if (switchConnectionHandler == null) {
86                 throw new IllegalStateException("switchConnectionHandler is not set");
87             }
88             new Thread(serverFacade).start();
89             result = serverFacade.getIsOnlineFuture();
90         } catch (Exception e) {
91             SettableFuture<Boolean> exResult = SettableFuture.create();
92             exResult.setException(e);
93             result = exResult;
94         }
95         return result;
96     }
97
98     /**
99      * @return
100      */
101     private TcpHandler createAndConfigureServer() {
102         LOGGER.debug("Configuring ..");
103         TcpHandler server = new TcpHandler(connConfig.getAddress(), connConfig.getPort());
104         server.setSwitchConnectionHandler(switchConnectionHandler);
105         server.setSwitchIdleTimeout(connConfig.getSwitchIdleTimeout());
106         boolean tlsSupported = FEATURE_SUPPORT.REQUIRED.equals(connConfig.getTlsSupport());
107         server.setEncryption(tlsSupported);
108         server.setSerializationFactory(serializationFactory);
109         return server;
110     }
111
112     /**
113      * @return servers
114      */
115     public ServerFacade getServerFacade() {
116         return serverFacade;
117     }
118
119     @Override
120     public <E extends DataObject> void registerCustomSerializer(MessageTypeKey<E> key,
121             OFSerializer<E> serializer) {
122         serializerRegistry.registerSerializer(key, serializer);
123     }
124
125     @Override
126     public void close() throws Exception {
127         shutdown();
128     }
129 }