Mechanical code cleanup (config)
[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 com.google.common.util.concurrent.ThreadFactoryBuilder;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.Executors;
14 import javax.management.ObjectName;
15 import org.opendaylight.controller.config.api.ConflictingVersionException;
16 import org.opendaylight.controller.config.api.ValidationException;
17 import org.opendaylight.controller.config.api.jmx.CommitStatus;
18 import org.opendaylight.controller.config.manager.impl.ConfigRegistryImpl;
19 import org.opendaylight.controller.config.spi.ModuleFactory;
20 import org.osgi.framework.ServiceReference;
21 import org.osgi.util.tracker.ServiceTrackerCustomizer;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Every time factory is added or removed, blank transaction is triggered to handle
27  * {@link org.opendaylight.controller.config.spi.ModuleFactory#getDefaultModules(org.opendaylight.controller.config.api.DependencyResolverFactory, org.osgi.framework.BundleContext)}
28  * functionality.
29  */
30 public class BlankTransactionServiceTracker implements ServiceTrackerCustomizer<ModuleFactory, Object> {
31     private static final Logger LOG = LoggerFactory.getLogger(BlankTransactionServiceTracker.class);
32
33     public static final int DEFAULT_MAX_ATTEMPTS = 10;
34
35     private final BlankTransaction blankTransaction;
36     private final ExecutorService txExecutor;
37     private final int maxAttempts;
38
39     public BlankTransactionServiceTracker(final ConfigRegistryImpl configRegistry) {
40         this(new BlankTransaction() {
41             @Override
42             public CommitStatus hit()
43                     throws ValidationException, ConflictingVersionException {
44                 ObjectName tx = configRegistry.beginConfig(true);
45                 return configRegistry.commitConfig(tx);
46             }
47         });
48     }
49
50     public BlankTransactionServiceTracker(final BlankTransaction blankTransaction) {
51         this(blankTransaction, DEFAULT_MAX_ATTEMPTS, Executors.newSingleThreadExecutor(new ThreadFactoryBuilder()
52                 .setNameFormat("config-blank-txn-%d").build()));
53     }
54
55     @VisibleForTesting
56     BlankTransactionServiceTracker(final BlankTransaction blankTx, final int maxAttempts,
57             final ExecutorService txExecutor) {
58         this.blankTransaction = blankTx;
59         this.maxAttempts = maxAttempts;
60         this.txExecutor = txExecutor;
61     }
62
63     @Override
64     public Object addingService(ServiceReference<ModuleFactory> moduleFactoryServiceReference) {
65         blankTransactionAsync();
66         return null;
67     }
68
69     private void blankTransactionAsync() {
70         txExecutor.execute(() -> { blankTransactionSync(); });
71     }
72
73     void blankTransactionSync() {
74         // race condition check: config-persister might push new configuration while server is starting up.
75         ConflictingVersionException lastException = null;
76         for (int i = 0; i < maxAttempts; i++) {
77             try {
78                 // create transaction
79                 CommitStatus commitStatus = blankTransaction.hit();
80                 LOG.debug("Committed blank transaction with status {}", commitStatus);
81                 return;
82             } catch (ConflictingVersionException e) {
83                 lastException = e;
84                 try {
85                     Thread.sleep(1000);
86                 } catch (InterruptedException interruptedException) {
87                     Thread.currentThread().interrupt();
88                     LOG.debug("blankTransactionSync was interrupted");
89                     return;
90                 }
91             } catch (ValidationException e) {
92                 LOG.error("Validation exception while running blank transaction indicates programming error", e);
93             }
94         }
95
96         LOG.error("Maximal number of attempts reached and still cannot get optimistic lock from config manager",
97                 lastException);
98     }
99
100     @Override
101     public void modifiedService(ServiceReference <ModuleFactory> moduleFactoryServiceReference, Object o) {
102         blankTransactionAsync();
103     }
104
105     @Override
106     public void removedService(ServiceReference<ModuleFactory> moduleFactoryServiceReference, Object o) {
107         blankTransactionAsync();
108     }
109
110     @VisibleForTesting
111     interface BlankTransaction {
112         CommitStatus hit() throws ValidationException, ConflictingVersionException;
113     }
114 }

©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.