+ synchronized(autoCloseables) {
+ CloseableUtil.closeAll(autoCloseables);
+ if (registration != null) {
+ registration.unregister();
+ }
+ this.context = null;
+ }
+ }
+
+
+ @VisibleForTesting
+ public static String getFilterString() {
+ return "(&" +
+ "(" + Constants.OBJECTCLASS + "=" + NetconfOperationServiceFactory.class.getName() + ")" +
+ "(name" + "=" + "config-netconf-connector" + ")" +
+ ")";
+ }
+
+ class OuterCustomizer implements ServiceTrackerCustomizer<NetconfOperationServiceFactory, NetconfOperationServiceFactory> {
+ private final BundleContext context;
+ private final InnerCustomizer innerCustomizer;
+
+ OuterCustomizer(BundleContext context, InnerCustomizer innerCustomizer) {
+ this.context = context;
+ this.innerCustomizer = innerCustomizer;
+ }
+
+ @Override
+ public NetconfOperationServiceFactory addingService(ServiceReference<NetconfOperationServiceFactory> reference) {
+ LOG.trace("Got OuterCustomizer.addingService {}", reference);
+ // JMX was registered, track config-netconf-connector
+ Filter filter;
+ try {
+ filter = context.createFilter(getFilterString());
+ } catch (InvalidSyntaxException e) {
+ throw new IllegalStateException(e);
+ }
+ new ServiceTracker<>(context, filter, innerCustomizer).open();
+ return null;
+ }
+
+ @Override
+ public void modifiedService(ServiceReference<NetconfOperationServiceFactory> reference, NetconfOperationServiceFactory service) {
+
+ }
+
+ @Override
+ public void removedService(ServiceReference<NetconfOperationServiceFactory> reference, NetconfOperationServiceFactory service) {
+
+ }
+ }
+
+ class InnerCustomizer implements ServiceTrackerCustomizer<NetconfOperationServiceFactory, NetconfOperationServiceFactory> {
+ private final List<ConfigSnapshotHolder> configs;
+ private final PersisterAggregator persisterAggregator;
+ private final long maxWaitForCapabilitiesMillis, conflictingVersionTimeoutMillis;
+ // This inner customizer has its filter to find the right operation service, but it gets triggered after any
+ // operation service appears. This means that it could start pushing thread up to N times (N = number of operation services spawned in OSGi)
+ private final AtomicBoolean alreadyStarted = new AtomicBoolean(false);
+
+ InnerCustomizer(List<ConfigSnapshotHolder> configs, long maxWaitForCapabilitiesMillis, long conflictingVersionTimeoutMillis,
+ PersisterAggregator persisterAggregator) {
+ this.configs = configs;
+ this.maxWaitForCapabilitiesMillis = maxWaitForCapabilitiesMillis;
+ this.conflictingVersionTimeoutMillis = conflictingVersionTimeoutMillis;
+ this.persisterAggregator = persisterAggregator;
+ }
+
+ @Override
+ public NetconfOperationServiceFactory addingService(ServiceReference<NetconfOperationServiceFactory> reference) {
+ if(alreadyStarted.compareAndSet(false, true) == false) {
+ //Prevents multiple calls to this method spawning multiple pushing threads
+ return reference.getBundle().getBundleContext().getService(reference);
+ }
+ LOG.trace("Got InnerCustomizer.addingService {}", reference);
+ NetconfOperationServiceFactory service = reference.getBundle().getBundleContext().getService(reference);
+
+ LOG.debug("Creating new job queue");
+
+ final ConfigPusherImpl configPusher = new ConfigPusherImpl(service, maxWaitForCapabilitiesMillis, conflictingVersionTimeoutMillis);
+ LOG.debug("Configuration Persister got {}", service);
+ LOG.debug("Context was {}", context);
+ LOG.debug("Registration was {}", registration);
+
+ final Thread pushingThread = new Thread(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ if(configs != null && !configs.isEmpty()) {
+ configPusher.pushConfigs(configs);
+ }
+ if(context != null) {
+ registration = context.registerService(ConfigPusher.class.getName(), configPusher, null);
+ configPusher.process(autoCloseables, platformMBeanServer, persisterAggregator);
+ } else {
+ LOG.warn("Unable to process configs as BundleContext is null");
+ }
+ } catch (InterruptedException e) {
+ LOG.info("ConfigPusher thread stopped",e);
+ }
+ LOG.info("Configuration Persister initialization completed.");
+ }
+ }, "config-pusher");
+ synchronized (autoCloseables) {
+ autoCloseables.add(new AutoCloseable() {
+ @Override
+ public void close() {
+ pushingThread.interrupt();
+ }
+ });
+ }
+ pushingThread.start();
+ return service;
+ }
+
+ @Override
+ public void modifiedService(ServiceReference<NetconfOperationServiceFactory> reference, NetconfOperationServiceFactory service) {
+ LOG.trace("Got InnerCustomizer.modifiedService {}", reference);
+ }
+
+ @Override
+ public void removedService(ServiceReference<NetconfOperationServiceFactory> reference, NetconfOperationServiceFactory service) {
+ LOG.trace("Got InnerCustomizer.removedService {}", reference);
+ }
+