2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
10 package org.opendaylight.openflowjava.protocol.impl.connection;
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;
26 import com.google.common.util.concurrent.ListenableFuture;
27 import com.google.common.util.concurrent.SettableFuture;
30 * Exposed class for server handling
32 * @author michal.polkorab
34 public class SwitchConnectionProviderImpl implements SwitchConnectionProvider {
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;
45 public SwitchConnectionProviderImpl() {
46 serializerRegistry = new SerializerRegistryImpl();
47 serializerRegistry.init();
48 serializationFactory = new SerializationFactory();
49 serializationFactory.setSerializerTable(serializerRegistry);
53 public void setConfiguration(ConnectionConfiguration connConfig) {
54 this.connConfig = connConfig;
58 public void setSwitchConnectionHandler(SwitchConnectionHandler switchConnectionHandler) {
59 LOGGER.debug("setSwitchConnectionHandler");
60 this.switchConnectionHandler = switchConnectionHandler;
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();
72 public ListenableFuture<Boolean> startup() {
73 LOGGER.debug("Startup summoned");
74 serverFacade = createAndConfigureServer();
76 LOGGER.debug("Starting ..");
77 ListenableFuture<Boolean> result = null;
79 if (serverFacade == null) {
80 throw new IllegalStateException("No server configured");
82 if (serverFacade.getIsOnlineFuture().isDone()) {
83 throw new IllegalStateException("Server already running");
85 if (switchConnectionHandler == null) {
86 throw new IllegalStateException("switchConnectionHandler is not set");
88 new Thread(serverFacade).start();
89 result = serverFacade.getIsOnlineFuture();
90 } catch (Exception e) {
91 SettableFuture<Boolean> exResult = SettableFuture.create();
92 exResult.setException(e);
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);
115 public ServerFacade getServerFacade() {
120 public <E extends DataObject> void registerCustomSerializer(MessageTypeKey<E> key,
121 OFSerializer<E> serializer) {
122 serializerRegistry.registerSerializer(key, serializer);
126 public void close() throws Exception {