Merge "Remove unnecessary declaration of <prerequisites> in features"
[controller.git] / opendaylight / config / config-api / src / main / java / org / opendaylight / controller / config / spi / AbstractModule.java
1 package org.opendaylight.controller.config.spi;
2
3 import org.opendaylight.controller.config.api.DependencyResolver;
4 import org.opendaylight.controller.config.api.ModuleIdentifier;
5 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory;
7
8 /**
9  * Base implementation of Module. This implementation contains base logic for Module reconfiguration with associated fields.
10  * @param <M> Type of module implementation. Enables easier implementation for the {@link #isSame} method
11  */
12 public abstract class AbstractModule<M extends AbstractModule<M>> implements org.opendaylight.controller.config.spi.Module {
13
14     private static final Logger LOG = LoggerFactory.getLogger(AbstractModule.class);
15
16     private final M oldModule;
17     private final AutoCloseable oldInstance;
18     protected final ModuleIdentifier identifier;
19     private AutoCloseable instance;
20     protected final DependencyResolver dependencyResolver;
21
22     /**
23      * Called when module is configured.
24      *
25      * @param identifier id of current instance.
26      * @param dependencyResolver resolver used in dependency injection and validation.
27      */
28     public AbstractModule(ModuleIdentifier identifier, DependencyResolver dependencyResolver) {
29         this(identifier, dependencyResolver, null, null);
30     }
31
32     /**
33      * Called when module is reconfigured.
34      *
35      * @param identifier id of current instance.
36      * @param dependencyResolver resolver used in dependency injection and validation.
37      * @param oldModule old instance of module that is being reconfigred(replaced) by current instance. The old instance can be examined for reuse.
38      * @param oldInstance old instance wrapped by the old module. This is the resource that is actually being reused if possible or closed otherwise.
39      */
40     public AbstractModule(ModuleIdentifier identifier, DependencyResolver dependencyResolver, M oldModule, AutoCloseable oldInstance) {
41         this.identifier = identifier;
42         this.dependencyResolver = dependencyResolver;
43         this.oldModule = oldModule;
44         this.oldInstance = oldInstance;
45     }
46
47     @Override
48     public ModuleIdentifier getIdentifier() {
49         return identifier;
50     }
51
52     /**
53      *
54      * General algorithm for spawning/closing and reusing wrapped instances.
55      *
56      * @return current instance of wrapped resource either by reusing the old one (if present) or constructing a brand new.
57      */
58     @Override
59     public final AutoCloseable getInstance() {
60         if(instance==null) {
61             if(oldInstance!=null && canReuseInstance(oldModule)) {
62                 resolveDependencies();
63                 instance = reuseInstance(oldInstance);
64             } else {
65                 if(oldInstance!=null) {
66                     try {
67                         oldInstance.close();
68                     } catch(Exception e) {
69                         LOG.error("An error occurred while closing old instance {} for module {}", oldInstance, getIdentifier(), e);
70                     }
71                 }
72                 resolveDependencies();
73                 instance = createInstance();
74                 if (instance == null) {
75                     throw new IllegalStateException("Error in createInstance - null is not allowed as return value. Module: " + getIdentifier());
76                 }
77             }
78         }
79         return instance;
80     }
81
82     /**
83      * @return Brand new instance of wrapped class in case no previous instance is present or reconfiguration is impossible.
84      */
85     protected abstract AutoCloseable createInstance();
86
87     @Override
88     public final boolean canReuse(Module oldModule) {
89         // Just cast into a specific instance
90         // TODO unify this method with canReuseInstance (required Module interface to be generic which requires quite a lot of changes)
91         return getClass().isInstance(oldModule) ? canReuseInstance((M) oldModule) : false;
92     }
93
94     /**
95      *
96      * Users are welcome to override this method to provide custom logic for advanced reusability detection.
97      *
98      * @param oldModule old instance of a Module
99      * @return true if the old instance is reusable false if a new one should be spawned
100      */
101     protected abstract boolean canReuseInstance(final M oldModule);
102
103     /**
104      * By default the oldInstance is returned since this method is by default called only if the oldModule had the same configuration and dependencies configured.
105      * Users are welcome to override this method to provide custom logic for advanced reusability.
106      *
107      * @param oldInstance old instance of a class wrapped by the module
108      * @return reused instance
109      */
110     protected AutoCloseable reuseInstance(AutoCloseable oldInstance) {
111         // implement if instance reuse should be supported. Override canReuseInstance to change the criteria.
112         return oldInstance;
113     }
114
115     /**
116      * Inject all the dependencies using dependency resolver instance.
117      */
118     protected abstract void resolveDependencies();
119 }