Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / BlankTransactionServiceTracker.java
1 /*
2  * Copyright (c) 2013, 2017 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
27  * handle.
28  */
29 public class BlankTransactionServiceTracker implements ServiceTrackerCustomizer<ModuleFactory, Object> {
30     private static final Logger LOG = LoggerFactory.getLogger(BlankTransactionServiceTracker.class);
31
32     public static final int DEFAULT_MAX_ATTEMPTS = 10;
33
34     private final BlankTransaction blankTransaction;
35     private final ExecutorService txExecutor;
36     private final int maxAttempts;
37
38     public BlankTransactionServiceTracker(final ConfigRegistryImpl configRegistry) {
39         this(() -> {
40             ObjectName tx = configRegistry.beginConfig(true);
41             return configRegistry.commitConfig(tx);
42         });
43     }
44
45     public BlankTransactionServiceTracker(final BlankTransaction blankTransaction) {
46         this(blankTransaction, DEFAULT_MAX_ATTEMPTS, Executors
47                 .newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("config-blank-txn-%d").build()));
48     }
49
50     @VisibleForTesting
51     BlankTransactionServiceTracker(final BlankTransaction blankTx, final int maxAttempts,
52             final ExecutorService txExecutor) {
53         this.blankTransaction = blankTx;
54         this.maxAttempts = maxAttempts;
55         this.txExecutor = txExecutor;
56     }
57
58     @Override
59     public Object addingService(final ServiceReference<ModuleFactory> moduleFactoryServiceReference) {
60         blankTransactionAsync();
61         return null;
62     }
63
64     private void blankTransactionAsync() {
65         txExecutor.execute(this::blankTransactionSync);
66     }
67
68     void blankTransactionSync() {
69         // race condition check: config-persister might push new configuration while
70         // server is starting up.
71         ConflictingVersionException lastException = null;
72         for (int i = 0; i < maxAttempts; i++) {
73             try {
74                 // create transaction
75                 CommitStatus commitStatus = blankTransaction.hit();
76                 LOG.debug("Committed blank transaction with status {}", commitStatus);
77                 return;
78             } catch (final ConflictingVersionException e) {
79                 lastException = e;
80                 try {
81                     Thread.sleep(1000);
82                 } catch (final InterruptedException interruptedException) {
83                     Thread.currentThread().interrupt();
84                     LOG.debug("blankTransactionSync was interrupted");
85                     return;
86                 }
87             } catch (final ValidationException e) {
88                 LOG.error("Validation exception while running blank transaction indicates programming error", e);
89             }
90         }
91
92         LOG.error("Maximal number of attempts reached and still cannot get optimistic lock from config manager",
93                 lastException);
94     }
95
96     @Override
97     public void modifiedService(final ServiceReference<ModuleFactory> moduleFactoryServiceReference,
98             final Object object) {
99         blankTransactionAsync();
100     }
101
102     @Override
103     public void removedService(final ServiceReference<ModuleFactory> moduleFactoryServiceReference,
104             final Object object) {
105         blankTransactionAsync();
106     }
107
108     @VisibleForTesting
109     interface BlankTransaction {
110         CommitStatus hit() throws ValidationException, ConflictingVersionException;
111     }
112 }