Fix race conditions between config-manager and persister.
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / ModuleInternalTransactionalInfo.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;
9
10 import javax.annotation.Nullable;
11
12 import org.opendaylight.controller.config.api.ModuleIdentifier;
13 import org.opendaylight.controller.config.manager.impl.dynamicmbean.DynamicReadableWrapper;
14 import org.opendaylight.controller.config.manager.impl.jmx.TransactionModuleJMXRegistrator
15         .TransactionModuleJMXRegistration;
16 import org.opendaylight.controller.config.spi.Module;
17 import org.opendaylight.controller.config.spi.ModuleFactory;
18 import org.opendaylight.yangtools.concepts.Identifiable;
19
20 public class ModuleInternalTransactionalInfo implements Identifiable<ModuleIdentifier> {
21     private final ModuleIdentifier name;
22     private final Module module;
23     private final ModuleFactory moduleFactory;
24     @Nullable
25     private final ModuleInternalInfo maybeOldInternalInfo;
26     private final TransactionModuleJMXRegistration transactionModuleJMXRegistration;
27
28     ModuleInternalTransactionalInfo(ModuleIdentifier name, Module module,
29             ModuleFactory moduleFactory,
30             ModuleInternalInfo maybeOldInternalInfo,
31             TransactionModuleJMXRegistration transactionModuleJMXRegistration) {
32         this.name = name;
33         this.module = module;
34         this.moduleFactory = moduleFactory;
35         this.maybeOldInternalInfo = maybeOldInternalInfo;
36         this.transactionModuleJMXRegistration = transactionModuleJMXRegistration;
37     }
38
39
40     /**
41      * Use {@link #getIdentifier()} instead.
42      */
43     @Deprecated
44     public ModuleIdentifier getName() {
45         return name;
46     }
47
48     public boolean hasOldModule() {
49         return maybeOldInternalInfo != null;
50     }
51
52     public DestroyedModule toDestroyedModule() {
53         if (maybeOldInternalInfo == null) {
54             throw new IllegalStateException("Cannot destroy uncommitted module");
55         }
56         DynamicReadableWrapper oldModule = maybeOldInternalInfo
57                 .getReadableModule();
58         return new DestroyedModule(name, oldModule.getInstance(),
59                 maybeOldInternalInfo.getModuleJMXRegistrator(),
60                 maybeOldInternalInfo.getOsgiRegistration(),
61                 maybeOldInternalInfo.getOrderingIdx());
62     }
63
64
65     public Module getModule() {
66         return module;
67     }
68
69     public ModuleFactory getModuleFactory() {
70         return moduleFactory;
71     }
72
73     @Nullable
74     public ModuleInternalInfo getOldInternalInfo() {
75         if (maybeOldInternalInfo == null)
76             throw new NullPointerException();
77         return maybeOldInternalInfo;
78     }
79
80     public TransactionModuleJMXRegistration getTransactionModuleJMXRegistration() {
81         return transactionModuleJMXRegistration;
82     }
83
84     @Override
85     public ModuleIdentifier getIdentifier() {
86         return name;
87     }
88 }