Merge "Resolve Bug:707 - ConfigPusher should wait for netconf-impl to register JMX...
[controller.git] / opendaylight / netconf / config-persister-impl / src / main / java / org / opendaylight / controller / netconf / persist / impl / osgi / ConfigPersisterActivator.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.controller.netconf.persist.impl.osgi;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
13 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
14 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
15 import org.opendaylight.controller.netconf.persist.impl.ConfigPersisterNotificationHandler;
16 import org.opendaylight.controller.netconf.persist.impl.ConfigPusher;
17 import org.opendaylight.controller.netconf.persist.impl.PersisterAggregator;
18 import org.opendaylight.controller.netconf.util.CloseableUtil;
19 import org.osgi.framework.BundleActivator;
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.Constants;
22 import org.osgi.framework.Filter;
23 import org.osgi.framework.InvalidSyntaxException;
24 import org.osgi.framework.ServiceReference;
25 import org.osgi.util.tracker.ServiceTracker;
26 import org.osgi.util.tracker.ServiceTrackerCustomizer;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import javax.management.MBeanServer;
31 import java.lang.management.ManagementFactory;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.concurrent.TimeUnit;
35
36 public class ConfigPersisterActivator implements BundleActivator {
37
38     private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterActivator.class);
39     private static final MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
40
41     public static final String MAX_WAIT_FOR_CAPABILITIES_MILLIS_PROPERTY = "maxWaitForCapabilitiesMillis";
42     private static final long MAX_WAIT_FOR_CAPABILITIES_MILLIS_DEFAULT = TimeUnit.MINUTES.toMillis(2);
43     public static final String CONFLICTING_VERSION_TIMEOUT_MILLIS_PROPERTY = "conflictingVersionTimeoutMillis";
44     private static final long CONFLICTING_VERSION_TIMEOUT_MILLIS_DEFAULT = TimeUnit.SECONDS.toMillis(30);
45
46     public static final String NETCONF_CONFIG_PERSISTER = "netconf.config.persister";
47
48     public static final String STORAGE_ADAPTER_CLASS_PROP_SUFFIX = "storageAdapterClass";
49
50     private List<AutoCloseable> autoCloseables;
51
52
53     @Override
54     public void start(final BundleContext context) throws Exception {
55         logger.debug("ConfigPersister starting");
56         autoCloseables = new ArrayList<>();
57         PropertiesProviderBaseImpl propertiesProvider = new PropertiesProviderBaseImpl(context);
58
59         final PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(propertiesProvider);
60         autoCloseables.add(persisterAggregator);
61         long maxWaitForCapabilitiesMillis = getMaxWaitForCapabilitiesMillis(propertiesProvider);
62         List<ConfigSnapshotHolder> configs = persisterAggregator.loadLastConfigs();
63         long conflictingVersionTimeoutMillis = getConflictingVersionTimeoutMillis(propertiesProvider);
64         logger.trace("Following configs will be pushed: {}", configs);
65
66         InnerCustomizer innerCustomizer = new InnerCustomizer(configs, maxWaitForCapabilitiesMillis,
67                 conflictingVersionTimeoutMillis, persisterAggregator);
68         OuterCustomizer outerCustomizer = new OuterCustomizer(context, innerCustomizer);
69         new ServiceTracker<>(context, NetconfOperationProvider.class, outerCustomizer).open();
70     }
71
72     private long getConflictingVersionTimeoutMillis(PropertiesProviderBaseImpl propertiesProvider) {
73         String timeoutProperty = propertiesProvider.getProperty(CONFLICTING_VERSION_TIMEOUT_MILLIS_PROPERTY);
74         return timeoutProperty == null ? CONFLICTING_VERSION_TIMEOUT_MILLIS_DEFAULT : Long.valueOf(timeoutProperty);
75     }
76
77     private long getMaxWaitForCapabilitiesMillis(PropertiesProviderBaseImpl propertiesProvider) {
78         String timeoutProperty = propertiesProvider.getProperty(MAX_WAIT_FOR_CAPABILITIES_MILLIS_PROPERTY);
79         return timeoutProperty == null ? MAX_WAIT_FOR_CAPABILITIES_MILLIS_DEFAULT : Long.valueOf(timeoutProperty);
80     }
81
82     @Override
83     public synchronized void stop(BundleContext context) throws Exception {
84         CloseableUtil.closeAll(autoCloseables);
85     }
86
87
88     @VisibleForTesting
89     public static String getFilterString() {
90         return "(&" +
91                 "(" + Constants.OBJECTCLASS + "=" + NetconfOperationServiceFactory.class.getName() + ")" +
92                 "(name" + "=" + "config-netconf-connector" + ")" +
93                 ")";
94     }
95
96     class OuterCustomizer implements ServiceTrackerCustomizer<NetconfOperationProvider, NetconfOperationProvider> {
97         private final BundleContext context;
98         private final InnerCustomizer innerCustomizer;
99
100         OuterCustomizer(BundleContext context, InnerCustomizer innerCustomizer) {
101             this.context = context;
102             this.innerCustomizer = innerCustomizer;
103         }
104
105         @Override
106         public NetconfOperationProvider addingService(ServiceReference<NetconfOperationProvider> reference) {
107             logger.trace("Got OuterCustomizer.addingService {}", reference);
108             // JMX was registered, track config-netconf-connector
109             Filter filter;
110             try {
111                 filter = context.createFilter(getFilterString());
112             } catch (InvalidSyntaxException e) {
113                 throw new IllegalStateException(e);
114             }
115             new ServiceTracker<>(context, filter, innerCustomizer).open();
116             return null;
117         }
118
119         @Override
120         public void modifiedService(ServiceReference<NetconfOperationProvider> reference, NetconfOperationProvider service) {
121
122         }
123
124         @Override
125         public void removedService(ServiceReference<NetconfOperationProvider> reference, NetconfOperationProvider service) {
126
127         }
128     }
129
130     class InnerCustomizer implements ServiceTrackerCustomizer<NetconfOperationServiceFactory, NetconfOperationServiceFactory> {
131         private final List<ConfigSnapshotHolder> configs;
132         private final PersisterAggregator persisterAggregator;
133         private final long maxWaitForCapabilitiesMillis, conflictingVersionTimeoutMillis;
134
135
136         InnerCustomizer(List<ConfigSnapshotHolder> configs, long maxWaitForCapabilitiesMillis, long conflictingVersionTimeoutMillis,
137                         PersisterAggregator persisterAggregator) {
138             this.configs = configs;
139             this.maxWaitForCapabilitiesMillis = maxWaitForCapabilitiesMillis;
140             this.conflictingVersionTimeoutMillis = conflictingVersionTimeoutMillis;
141             this.persisterAggregator = persisterAggregator;
142         }
143
144         @Override
145         public NetconfOperationServiceFactory addingService(ServiceReference<NetconfOperationServiceFactory> reference) {
146             logger.trace("Got InnerCustomizer.addingService {}", reference);
147             NetconfOperationServiceFactory service = reference.getBundle().getBundleContext().getService(reference);
148
149             final ConfigPusher configPusher = new ConfigPusher(service, maxWaitForCapabilitiesMillis, conflictingVersionTimeoutMillis);
150             logger.debug("Configuration Persister got {}", service);
151             final Thread pushingThread = new Thread(new Runnable() {
152                 @Override
153                 public void run() {
154                     configPusher.pushConfigs(configs);
155                     logger.info("Configuration Persister initialization completed.");
156                     ConfigPersisterNotificationHandler jmxNotificationHandler = new ConfigPersisterNotificationHandler(platformMBeanServer, persisterAggregator);
157                     synchronized (ConfigPersisterActivator.this) {
158                         autoCloseables.add(jmxNotificationHandler);
159                     }
160                 }
161             }, "config-pusher");
162             synchronized (ConfigPersisterActivator.this) {
163                 autoCloseables.add(new AutoCloseable() {
164                     @Override
165                     public void close() throws Exception {
166                         pushingThread.interrupt();
167                     }
168                 });
169             }
170             pushingThread.start();
171             return service;
172         }
173
174         @Override
175         public void modifiedService(ServiceReference<NetconfOperationServiceFactory> reference, NetconfOperationServiceFactory service) {
176         }
177
178         @Override
179         public void removedService(ServiceReference<NetconfOperationServiceFactory> reference, NetconfOperationServiceFactory service) {
180         }
181
182     }
183 }
184

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.