Remove yang-test
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / ConfigTransactionLookupRegistry.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;
9
10 import java.io.Closeable;
11 import java.util.Map;
12 import java.util.Map.Entry;
13 import java.util.Set;
14 import javax.management.InstanceAlreadyExistsException;
15 import javax.management.InstanceNotFoundException;
16 import javax.management.ObjectName;
17 import org.opendaylight.controller.config.api.LookupRegistry;
18 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
19 import org.opendaylight.controller.config.manager.impl.jmx.TransactionJMXRegistrator;
20 import org.opendaylight.controller.config.manager.impl.jmx.TransactionModuleJMXRegistrator;
21 import org.opendaylight.controller.config.manager.impl.util.LookupBeansUtil;
22 import org.opendaylight.controller.config.manager.impl.util.ModuleQNameUtil;
23 import org.opendaylight.controller.config.spi.ModuleFactory;
24 import org.osgi.framework.BundleContext;
25
26 /**
27  * Responsible for creating TransactionJMXRegistrator, registering transaction
28  * and all its beans, lookup of beans, closing of TransactionJMXRegistrator.
29  */
30 class ConfigTransactionLookupRegistry implements LookupRegistry, Closeable {
31     private final TransactionJMXRegistrator transactionJMXRegistrator;
32     private final TransactionIdentifier transactionIdentifier;
33     private final TransactionModuleJMXRegistrator txModuleJMXRegistrator;
34     private final Map<String, Map.Entry<ModuleFactory, BundleContext>> allCurrentFactories;
35
36     interface TransactionJMXRegistratorFactory {
37         TransactionJMXRegistrator create();
38     }
39
40     ConfigTransactionLookupRegistry(final TransactionIdentifier transactionIdentifier,
41             final TransactionJMXRegistratorFactory factory,
42             final Map<String, Entry<ModuleFactory, BundleContext>> allCurrentFactories) {
43         this.transactionIdentifier = transactionIdentifier;
44         this.transactionJMXRegistrator = factory.create();
45         this.txModuleJMXRegistrator = transactionJMXRegistrator.createTransactionModuleJMXRegistrator();
46         this.allCurrentFactories = allCurrentFactories;
47     }
48
49     private void checkTransactionName(final ObjectName objectName) {
50         String foundTransactionName = ObjectNameUtil.getTransactionName(objectName);
51         if (!transactionIdentifier.getName().equals(foundTransactionName)) {
52             throw new IllegalArgumentException("Wrong transaction name " + objectName);
53         }
54     }
55
56     /**
57      * {@inheritDoc}
58      */
59     @Override
60     public Set<ObjectName> lookupConfigBeans() {
61         return lookupConfigBeans("*", "*");
62     }
63
64     /**
65      * {@inheritDoc}
66      */
67     @Override
68     public Set<ObjectName> lookupConfigBeans(final String moduleName) {
69         return lookupConfigBeans(moduleName, "*");
70     }
71
72     /**
73      * {@inheritDoc}
74      */
75     @Override
76     public Set<ObjectName> lookupConfigBeans(final String moduleName, final String instanceName) {
77         ObjectName namePattern = ObjectNameUtil.createModulePattern(moduleName, instanceName,
78                 transactionIdentifier.getName());
79         return txModuleJMXRegistrator.queryNames(namePattern, null);
80     }
81
82     /**
83      * {@inheritDoc}
84      */
85     @Override
86     public ObjectName lookupConfigBean(final String moduleName, final String instanceName)
87             throws InstanceNotFoundException {
88         return LookupBeansUtil.lookupConfigBean(this, moduleName, instanceName);
89     }
90
91     @Override
92     public void checkConfigBeanExists(final ObjectName objectName) throws InstanceNotFoundException {
93         ObjectNameUtil.checkDomain(objectName);
94         ObjectNameUtil.checkType(objectName, ObjectNameUtil.TYPE_MODULE);
95         checkTransactionName(objectName);
96         // make sure exactly one match is found:
97         LookupBeansUtil.lookupConfigBean(this, ObjectNameUtil.getFactoryName(objectName),
98                 ObjectNameUtil.getInstanceName(objectName));
99     }
100
101     TransactionIdentifier getTransactionIdentifier() {
102         return transactionIdentifier;
103     }
104
105     TransactionModuleJMXRegistrator getTxModuleJMXRegistrator() {
106         return txModuleJMXRegistrator;
107     }
108
109     @Override
110     public void close() {
111         transactionJMXRegistrator.close();
112     }
113
114     public void registerMBean(final ConfigTransactionControllerInternal transactionController,
115             final ObjectName controllerObjectName) throws InstanceAlreadyExistsException {
116         transactionJMXRegistrator.registerMBean(transactionController, controllerObjectName);
117     }
118
119     @Override
120     public Set<String> getAvailableModuleFactoryQNames() {
121         return ModuleQNameUtil.getQNames(allCurrentFactories);
122     }
123
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     public Set<ObjectName> lookupRuntimeBeans() {
129         return lookupRuntimeBeans("*", "*");
130     }
131
132     /**
133      * {@inheritDoc}
134      */
135     @Override
136     public Set<ObjectName> lookupRuntimeBeans(final String moduleName, final String instanceName) {
137         String finalModuleName = moduleName == null ? "*" : moduleName;
138         String finalInstanceName = instanceName == null ? "*" : instanceName;
139         ObjectName namePattern = ObjectNameUtil.createRuntimeBeanPattern(finalModuleName, finalInstanceName);
140         return transactionJMXRegistrator.queryNames(namePattern, null);
141     }
142
143     @Override
144     public String toString() {
145         return "ConfigTransactionLookupRegistry{" + "transactionIdentifier=" + transactionIdentifier + '}';
146     }
147 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.