Fix "stale" state after controller disconnected.
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / OpenFlowPluginProviderFactoryImpl.java
1 /*
2  * Copyright (c) 2016, 2017 Brocade Communications 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 package org.opendaylight.openflowplugin.impl;
9
10 import java.io.IOException;
11 import java.util.Enumeration;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Optional;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
18 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
19 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
21 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
22 import org.opendaylight.openflowplugin.api.OFConstants;
23 import org.opendaylight.openflowplugin.api.openflow.OpenFlowPluginConfigurationService.PropertyType;
24 import org.opendaylight.openflowplugin.api.openflow.OpenFlowPluginProvider;
25 import org.opendaylight.openflowplugin.api.openflow.OpenFlowPluginProviderFactory;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
27 import org.osgi.framework.BundleContext;
28 import org.osgi.service.cm.Configuration;
29 import org.osgi.service.cm.ConfigurationAdmin;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Implementation of OpenFlowPluginProviderFactory.
35  *
36  * @author Thomas Pantelis
37  */
38 public class OpenFlowPluginProviderFactoryImpl implements OpenFlowPluginProviderFactory {
39     private static final Logger LOG = LoggerFactory.getLogger(OpenFlowPluginProviderFactoryImpl.class);
40
41     @Override
42     public OpenFlowPluginProvider newInstance(final OpenflowProviderConfig providerConfig,
43                                               final DataBroker dataBroker,
44                                               final RpcProviderRegistry rpcRegistry,
45                                               final NotificationPublishService notificationPublishService,
46                                               final EntityOwnershipService entityOwnershipService,
47                                               final List<SwitchConnectionProvider> switchConnectionProviders,
48                                               final ClusterSingletonServiceProvider singletonServiceProvider,
49                                               final BundleContext bundleContext) {
50
51         LOG.info("Initializing new OFP southbound.");
52
53         final OpenFlowPluginProviderImpl openflowPluginProvider = new OpenFlowPluginProviderImpl(
54                 switchConnectionProviders,
55                 dataBroker,
56                 rpcRegistry,
57                 notificationPublishService,
58                 singletonServiceProvider,
59                 entityOwnershipService);
60
61         LOG.info("Loading configuration from YANG file");
62         openflowPluginProvider.updateProperty(PropertyType.RPC_REQUESTS_QUOTA, providerConfig.getRpcRequestsQuota().getValue());
63         openflowPluginProvider.updateProperty(PropertyType.GLOBAL_NOTIFICATION_QUOTA, providerConfig.getGlobalNotificationQuota());
64         openflowPluginProvider.updateProperty(PropertyType.SWITCH_FEATURES_MANDATORY, providerConfig.isSwitchFeaturesMandatory());
65         openflowPluginProvider.updateProperty(PropertyType.ENABLE_FLOW_REMOVED_NOTIFICATION, providerConfig.isEnableFlowRemovedNotification());
66         openflowPluginProvider.updateProperty(PropertyType.IS_STATISTICS_RPC_ENABLED, providerConfig.isIsStatisticsRpcEnabled());
67         openflowPluginProvider.updateProperty(PropertyType.BARRIER_COUNT_LIMIT, providerConfig.getBarrierCountLimit().getValue());
68         openflowPluginProvider.updateProperty(PropertyType.BARRIER_INTERVAL_TIMEOUT_LIMIT, providerConfig.getBarrierIntervalTimeoutLimit().getValue());
69         openflowPluginProvider.updateProperty(PropertyType.ECHO_REPLY_TIMEOUT, providerConfig.getEchoReplyTimeout().getValue());
70         openflowPluginProvider.updateProperty(PropertyType.IS_STATISTICS_POLLING_ON, providerConfig.isIsStatisticsPollingOn());
71         openflowPluginProvider.updateProperty(PropertyType.SKIP_TABLE_FEATURES, providerConfig.isSkipTableFeatures());
72         openflowPluginProvider.updateProperty(PropertyType.BASIC_TIMER_DELAY, providerConfig.getBasicTimerDelay().getValue());
73         openflowPluginProvider.updateProperty(PropertyType.MAXIMUM_TIMER_DELAY, providerConfig.getMaximumTimerDelay().getValue());
74         openflowPluginProvider.updateProperty(PropertyType.USE_SINGLE_LAYER_SERIALIZATION, providerConfig.isUseSingleLayerSerialization());
75         openflowPluginProvider.updateProperty(PropertyType.THREAD_POOL_MIN_THREADS, providerConfig.getThreadPoolMinThreads());
76         openflowPluginProvider.updateProperty(PropertyType.THREAD_POOL_MAX_THREADS, providerConfig.getThreadPoolMaxThreads().getValue());
77         openflowPluginProvider.updateProperty(PropertyType.THREAD_POOL_TIMEOUT, providerConfig.getThreadPoolTimeout());
78
79         LOG.info("Loading configuration from properties file");
80         Optional.ofNullable(bundleContext.getServiceReference(ConfigurationAdmin.class.getName())).ifPresent(serviceReference -> {
81             final ConfigurationAdmin configurationAdmin = (ConfigurationAdmin) bundleContext.getService(serviceReference);
82
83             try {
84                 final Configuration configuration = configurationAdmin.getConfiguration(OFConstants.CONFIG_FILE_ID);
85
86                 Optional.ofNullable(configuration.getProperties()).ifPresent(properties -> {
87                     final Enumeration<String> keys = properties.keys();
88                     final Map<String, Object> mapProperties = new HashMap<>(properties.size());
89
90                     while (keys.hasMoreElements()) {
91                         final String key = keys.nextElement();
92                         final Object value = properties.get(key);
93                         mapProperties.put(key, value);
94                     }
95
96                     openflowPluginProvider.update(mapProperties);
97                 });
98             } catch (IOException e) {
99                 LOG.debug("Failed to load " + OFConstants.CONFIG_FILE_ID + " configuration file", e);
100             }
101         });
102
103         openflowPluginProvider.initialize();
104         return openflowPluginProvider;
105     }
106 }