write /nodes/node/table/0 with ensure parents when node first appears
[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 org.opendaylight.controller.config.api.ConflictingVersionException;
11 import org.opendaylight.controller.config.api.ValidationException;
12 import org.opendaylight.controller.config.api.jmx.CommitStatus;
13 import org.opendaylight.controller.config.manager.impl.ConfigRegistryImpl;
14 import org.opendaylight.controller.config.spi.ModuleFactory;
15 import org.osgi.framework.ServiceReference;
16 import org.osgi.util.tracker.ServiceTrackerCustomizer;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import javax.management.ObjectName;
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     private final ConfigRegistryImpl configRegistry;
31
32     public BlankTransactionServiceTracker(ConfigRegistryImpl configRegistry) {
33         this.configRegistry = configRegistry;
34     }
35
36     @Override
37     public Object addingService(ServiceReference<ModuleFactory> moduleFactoryServiceReference) {
38         blankTransaction();
39         return null;
40     }
41
42     synchronized void blankTransaction() {
43         // race condition check: config-persister might push new configuration while server is starting up.
44         ConflictingVersionException lastException = null;
45         int maxAttempts = 10;
46         for (int i = 0; i < maxAttempts; i++) {
47             try {
48                 // create transaction
49                 boolean blankTransaction = true;
50                 ObjectName tx = configRegistry.beginConfig(blankTransaction);
51                 CommitStatus commitStatus = configRegistry.commitConfig(tx);
52                 logger.debug("Committed blank transaction with status {}", commitStatus);
53                 return;
54             } catch (ConflictingVersionException e) {
55                 lastException = e;
56                 try {
57                     Thread.sleep(1000);
58                 } catch (InterruptedException interruptedException) {
59                     Thread.currentThread().interrupt();
60                     throw new IllegalStateException(interruptedException);
61                 }
62             } catch (ValidationException e) {
63                 logger.error("Validation exception while running blank transaction indicates programming error", e);
64                 throw new RuntimeException("Validation exception while running blank transaction indicates programming error", e);
65             }
66         }
67         throw new RuntimeException("Maximal number of attempts reached and still cannot get optimistic lock from " +
68                 "config manager",lastException);
69     }
70
71     @Override
72     public void modifiedService(ServiceReference <ModuleFactory> moduleFactoryServiceReference, Object o) {
73         blankTransaction();
74     }
75
76     @Override
77     public void removedService(ServiceReference<ModuleFactory> moduleFactoryServiceReference, Object o) {
78         blankTransaction();
79     }
80 }