Merge "Bug 809: Enhancements to the toaster example"
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / ModuleInternalInfo.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.dependencyresolver.DestroyedModule;
14 import org.opendaylight.controller.config.manager.impl.dynamicmbean.DynamicReadableWrapper;
15 import org.opendaylight.controller.config.manager.impl.jmx.ModuleJMXRegistrator;
16 import org.opendaylight.controller.config.manager.impl.jmx.RootRuntimeBeanRegistratorImpl;
17 import org.opendaylight.controller.config.manager.impl.osgi.BeanToOsgiServiceManager.OsgiRegistration;
18 import org.opendaylight.controller.config.spi.ModuleFactory;
19 import org.opendaylight.yangtools.concepts.Identifiable;
20 import org.osgi.framework.BundleContext;
21
22 /**
23  * Provides metadata about Module from controller to registry.
24  */
25 public class ModuleInternalInfo implements Comparable<ModuleInternalInfo>,
26                 Identifiable<ModuleIdentifier>{
27
28     private final ModuleIdentifier name;
29     // this registrator is passed to runtime bean registrator and config
30     // registry to register read only module.
31     // writable modules are registered using TransactionJMXRegistrator
32     @Nullable
33     private final DynamicReadableWrapper readableModule;
34
35     private final RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator;
36     // added when bean instance is registered to Osgi
37     // can be unregistered using this registration
38     private final OsgiRegistration osgiRegistration;
39     private final ModuleJMXRegistrator moduleJMXRegistrator;
40     private final int orderingIdx;
41     private final boolean isDefaultBean;
42     private final ModuleFactory moduleFactory;
43     private final BundleContext bundleContext;
44
45     public ModuleInternalInfo(ModuleIdentifier name,
46             @Nullable DynamicReadableWrapper readableModule,
47             OsgiRegistration osgiRegistration,
48             RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator,
49             ModuleJMXRegistrator moduleJMXRegistrator, int orderingIdx,
50             boolean isDefaultBean, ModuleFactory moduleFactory, BundleContext bundleContext) {
51
52         if (osgiRegistration == null) {
53             throw new IllegalArgumentException(
54                     "Parameter 'osgiRegistration' is missing");
55         }
56         if (runtimeBeanRegistrator == null) {
57             throw new IllegalArgumentException(
58                     "Parameter 'runtimeBeanRegistrator' is missing");
59         }
60         this.readableModule = readableModule;
61         this.osgiRegistration = osgiRegistration;
62         this.runtimeBeanRegistrator = runtimeBeanRegistrator;
63         this.name = name;
64         this.moduleJMXRegistrator = moduleJMXRegistrator;
65         this.orderingIdx = orderingIdx;
66         this.isDefaultBean = isDefaultBean;
67         this.moduleFactory = moduleFactory;
68         this.bundleContext = bundleContext;
69     }
70
71     public DynamicReadableWrapper getReadableModule() {
72         return readableModule;
73     }
74
75     public ModuleJMXRegistrator getModuleJMXRegistrator() {
76         return moduleJMXRegistrator;
77     }
78
79     /**
80      *
81      * @return iif an running instance exists in the system.
82      */
83     public boolean hasReadableModule() {
84         return readableModule != null;
85     }
86
87     @Override
88     public String toString() {
89         return "ModuleInternalInfo [name=" + name + "]";
90     }
91
92     public RootRuntimeBeanRegistratorImpl getRuntimeBeanRegistrator() {
93         return runtimeBeanRegistrator;
94     }
95
96     public OsgiRegistration getOsgiRegistration() {
97         return osgiRegistration;
98     }
99
100     /**
101      * Get index representing dependency ordering within a transaction.
102      */
103     public int getOrderingIdx() {
104         return orderingIdx;
105     }
106
107     /**
108      * Compare using orderingIdx
109      */
110     @Override
111     public int compareTo(ModuleInternalInfo o) {
112         return Integer.compare(orderingIdx, o.orderingIdx);
113     }
114
115     public DestroyedModule toDestroyedModule() {
116         return new DestroyedModule(getIdentifier(),
117                 getReadableModule().getInstance(), getModuleJMXRegistrator(),
118                 getOsgiRegistration(), getOrderingIdx());
119     }
120
121     @Override
122     public ModuleIdentifier getIdentifier() {
123         return name;
124     }
125
126     public boolean isDefaultBean() {
127         return isDefaultBean;
128     }
129
130     public ModuleFactory getModuleFactory() {
131         return moduleFactory;
132     }
133
134     public BundleContext getBundleContext() {
135         return bundleContext;
136     }
137 }