2 * Copyright (c) 2013 Cisco Systems, Inc. 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
8 package org.opendaylight.controller.config.manager.impl.osgi;
10 import com.google.common.annotations.VisibleForTesting;
11 import javax.management.ObjectName;
12 import org.opendaylight.controller.config.api.ConflictingVersionException;
13 import org.opendaylight.controller.config.api.ValidationException;
14 import org.opendaylight.controller.config.api.jmx.CommitStatus;
15 import org.opendaylight.controller.config.manager.impl.ConfigRegistryImpl;
16 import org.opendaylight.controller.config.spi.ModuleFactory;
17 import org.osgi.framework.ServiceReference;
18 import org.osgi.util.tracker.ServiceTrackerCustomizer;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Every time factory is added or removed, blank transaction is triggered to handle
24 * {@link org.opendaylight.controller.config.spi.ModuleFactory#getDefaultModules(org.opendaylight.controller.config.api.DependencyResolverFactory, org.osgi.framework.BundleContext)}
27 public class BlankTransactionServiceTracker implements ServiceTrackerCustomizer<ModuleFactory, Object> {
28 private static final Logger LOG = LoggerFactory.getLogger(BlankTransactionServiceTracker.class);
30 public static final int DEFAULT_MAX_ATTEMPTS = 10;
32 private final BlankTransaction blankTransaction;
33 private int maxAttempts;
35 public BlankTransactionServiceTracker(final ConfigRegistryImpl configRegistry) {
36 this(new BlankTransaction() {
38 public CommitStatus hit() throws ValidationException, ConflictingVersionException {
39 ObjectName tx = configRegistry.beginConfig(true);
40 return configRegistry.commitConfig(tx);
45 public BlankTransactionServiceTracker(final BlankTransaction blankTransaction) {
46 this(blankTransaction, DEFAULT_MAX_ATTEMPTS);
50 BlankTransactionServiceTracker(final BlankTransaction blankTx, final int maxAttempts) {
51 this.blankTransaction = blankTx;
52 this.maxAttempts = maxAttempts;
56 public Object addingService(ServiceReference<ModuleFactory> moduleFactoryServiceReference) {
61 synchronized void blankTransaction() {
62 // race condition check: config-persister might push new configuration while server is starting up.
63 ConflictingVersionException lastException = null;
64 for (int i = 0; i < maxAttempts; i++) {
67 CommitStatus commitStatus = blankTransaction.hit();
68 LOG.debug("Committed blank transaction with status {}", commitStatus);
70 } catch (ConflictingVersionException e) {
74 } catch (InterruptedException interruptedException) {
75 Thread.currentThread().interrupt();
76 throw new IllegalStateException(interruptedException);
78 } catch (ValidationException e) {
79 LOG.error("Validation exception while running blank transaction indicates programming error", e);
80 throw new RuntimeException("Validation exception while running blank transaction indicates programming error", e);
83 throw new RuntimeException("Maximal number of attempts reached and still cannot get optimistic lock from " +
84 "config manager",lastException);
88 public void modifiedService(ServiceReference <ModuleFactory> moduleFactoryServiceReference, Object o) {
93 public void removedService(ServiceReference<ModuleFactory> moduleFactoryServiceReference, Object o) {
98 static interface BlankTransaction {
99 CommitStatus hit() throws ValidationException, ConflictingVersionException;