Merge "Cleanup RpcRoutingStrategy definition"
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / BlankTransactionServiceTracker.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 package org.opendaylight.controller.config.manager.impl.osgi;
9
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;
21
22 /**
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)}
25  * functionality.
26  */
27 public class BlankTransactionServiceTracker implements ServiceTrackerCustomizer<ModuleFactory, Object> {
28     private static final Logger LOGGER = LoggerFactory.getLogger(BlankTransactionServiceTracker.class);
29
30     public static final int DEFAULT_MAX_ATTEMPTS = 10;
31
32     private final BlankTransaction blankTransaction;
33     private int maxAttempts;
34
35     public BlankTransactionServiceTracker(final ConfigRegistryImpl configRegistry) {
36         this(new BlankTransaction() {
37             @Override
38             public CommitStatus hit() throws ValidationException, ConflictingVersionException {
39                 ObjectName tx = configRegistry.beginConfig(true);
40                 return configRegistry.commitConfig(tx);
41             }
42         });
43     }
44
45     public BlankTransactionServiceTracker(final BlankTransaction blankTransaction) {
46         this(blankTransaction, DEFAULT_MAX_ATTEMPTS);
47     }
48
49     @VisibleForTesting
50     BlankTransactionServiceTracker(final BlankTransaction blankTx, final int maxAttempts) {
51         this.blankTransaction = blankTx;
52         this.maxAttempts = maxAttempts;
53     }
54
55     @Override
56     public Object addingService(ServiceReference<ModuleFactory> moduleFactoryServiceReference) {
57         blankTransaction();
58         return null;
59     }
60
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++) {
65             try {
66                 // create transaction
67                 CommitStatus commitStatus = blankTransaction.hit();
68                 LOGGER.debug("Committed blank transaction with status {}", commitStatus);
69                 return;
70             } catch (ConflictingVersionException e) {
71                 lastException = e;
72                 try {
73                     Thread.sleep(1000);
74                 } catch (InterruptedException interruptedException) {
75                     Thread.currentThread().interrupt();
76                     throw new IllegalStateException(interruptedException);
77                 }
78             } catch (ValidationException e) {
79                 LOGGER.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);
81             }
82         }
83         throw new RuntimeException("Maximal number of attempts reached and still cannot get optimistic lock from " +
84                 "config manager",lastException);
85     }
86
87     @Override
88     public void modifiedService(ServiceReference <ModuleFactory> moduleFactoryServiceReference, Object o) {
89         blankTransaction();
90     }
91
92     @Override
93     public void removedService(ServiceReference<ModuleFactory> moduleFactoryServiceReference, Object o) {
94         blankTransaction();
95     }
96
97     @VisibleForTesting
98     static interface BlankTransaction {
99         CommitStatus hit() throws ValidationException, ConflictingVersionException;
100     }
101 }