Initial code drop of yang model driven configuration system
[controller.git] / opendaylight / config / config-api / src / main / java / org / opendaylight / controller / config / api / jmx / ObjectNameUtil.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.api.jmx;
9
10 import java.util.Arrays;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.Hashtable;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import java.util.Set;
17
18 import javax.annotation.concurrent.ThreadSafe;
19 import javax.management.ObjectName;
20
21 import org.opendaylight.controller.config.api.ModuleIdentifier;
22 import org.opendaylight.controller.config.api.jmx.constants.ConfigRegistryConstants;
23
24 /**
25  * Provides ObjectName creation. Each created ObjectName consists of domain that
26  * is defined as {@link #ON_DOMAIN} and at least one key-value pair. The only
27  * mandatory property is {@link #TYPE_KEY}. All transaction related mbeans have
28  * {@link #TRANSACTION_NAME_KEY} property set.
29  *
30  */
31 @ThreadSafe
32 public class ObjectNameUtil {
33
34     public static final String ON_DOMAIN = ConfigRegistryConstants.ON_DOMAIN;
35     public static final String MODULE_FACTORY_NAME_KEY = "moduleFactoryName";
36     public static final String INSTANCE_NAME_KEY = "instanceName";
37     public static final String TYPE_KEY = ConfigRegistryConstants.TYPE_KEY;
38     public static final String TYPE_CONFIG_REGISTRY = ConfigRegistryConstants.TYPE_CONFIG_REGISTRY;
39     public static final String TYPE_CONFIG_TRANSACTION = "ConfigTransaction";
40     public static final String TYPE_MODULE = "Module";
41     public static final String TYPE_RUNTIME_BEAN = "RuntimeBean";
42
43     public static final String TRANSACTION_NAME_KEY = "TransactionName";
44
45     public static ObjectName createON(String on) {
46         try {
47             return new ObjectName(on);
48         } catch (Exception e) {
49             throw new RuntimeException(e);
50         }
51     }
52
53     public static ObjectName createONWithDomainAndType(String type) {
54         return ConfigRegistryConstants.createONWithDomainAndType(type);
55     }
56
57     public static ObjectName createON(String name, String key, String value) {
58         return ConfigRegistryConstants.createON(name, key, value);
59     }
60
61     public static ObjectName createON(String name, Map<String, String> attribs) {
62         Hashtable<String, String> table = new Hashtable<>(attribs);
63         try {
64             return new ObjectName(name, table);
65         } catch (Exception e) {
66             throw new RuntimeException(e);
67         }
68
69     }
70
71     public static ObjectName createTransactionControllerON(
72             String transactionName) {
73         Map<String, String> onParams = new HashMap<>();
74         onParams.put(TRANSACTION_NAME_KEY, transactionName);
75         onParams.put(TYPE_KEY, TYPE_CONFIG_TRANSACTION);
76         return createON(ON_DOMAIN, onParams);
77     }
78
79     public static ObjectName createTransactionModuleON(String transactionName,
80             ModuleIdentifier moduleIdentifier) {
81         return createTransactionModuleON(transactionName,
82                 moduleIdentifier.getFactoryName(),
83                 moduleIdentifier.getInstanceName());
84     }
85
86     public static ObjectName createTransactionModuleON(String transactionName,
87             String moduleName, String instanceName) {
88         Map<String, String> onParams = createModuleON(moduleName, instanceName);
89         onParams.put(TRANSACTION_NAME_KEY, transactionName);
90         return createON(ON_DOMAIN, onParams);
91     }
92
93     public static ObjectName createTransactionModuleON(String transactionName,
94             ObjectName on) {
95         return createTransactionModuleON(transactionName, getFactoryName(on),
96                 getInstanceName(on));
97     }
98
99     public static ObjectName createReadOnlyModuleON(
100             ModuleIdentifier moduleIdentifier) {
101         return createReadOnlyModuleON(moduleIdentifier.getFactoryName(),
102                 moduleIdentifier.getInstanceName());
103     }
104
105     public static ObjectName createReadOnlyModuleON(String moduleName,
106             String instanceName) {
107         Map<String, String> onParams = createModuleON(moduleName, instanceName);
108         return createON(ON_DOMAIN, onParams);
109     }
110
111     private static Map<String, String> createModuleON(String moduleName,
112             String instanceName) {
113         Map<String, String> onParams = new HashMap<>();
114         onParams.put(TYPE_KEY, TYPE_MODULE);
115         onParams.put(MODULE_FACTORY_NAME_KEY, moduleName);
116         onParams.put(INSTANCE_NAME_KEY, instanceName);
117         return onParams;
118     }
119
120     public static String getFactoryName(ObjectName objectName) {
121         return objectName.getKeyProperty(MODULE_FACTORY_NAME_KEY);
122     }
123
124     public static String getInstanceName(ObjectName objectName) {
125         return objectName.getKeyProperty(INSTANCE_NAME_KEY);
126     }
127
128     public static String getTransactionName(ObjectName objectName) {
129         return objectName.getKeyProperty(TRANSACTION_NAME_KEY);
130     }
131
132     public static ObjectName withoutTransactionName(ObjectName on) {
133         if (getTransactionName(on) == null) {
134             throw new IllegalArgumentException(
135                     "Expected ObjectName with transaction:" + on);
136         }
137         if (ON_DOMAIN.equals(on.getDomain()) == false) {
138             throw new IllegalArgumentException("Expected different domain: "
139                     + on);
140         }
141         String moduleName = getFactoryName(on);
142         String instanceName = getInstanceName(on);
143         return createReadOnlyModuleON(moduleName, instanceName);
144     }
145
146     private static void assertDoesNotContain(
147             Map<String, String> additionalProperties, String key) {
148         if (additionalProperties.containsKey(key)) {
149             throw new IllegalArgumentException(
150                     "Map 'additionalProperties' cannot overwrite attribute "
151                             + key);
152         }
153     }
154
155     public static ObjectName createRuntimeBeanName(String moduleName,
156             String instanceName, Map<String, String> additionalProperties) {
157         // check that there is no overwriting of default attributes
158         assertDoesNotContain(additionalProperties, MODULE_FACTORY_NAME_KEY);
159         assertDoesNotContain(additionalProperties, INSTANCE_NAME_KEY);
160         assertDoesNotContain(additionalProperties, TYPE_KEY);
161         assertDoesNotContain(additionalProperties, TRANSACTION_NAME_KEY);
162         Map<String, String> map = new HashMap<>(additionalProperties);
163         map.put(MODULE_FACTORY_NAME_KEY, moduleName);
164         map.put(INSTANCE_NAME_KEY, instanceName);
165         map.put(TYPE_KEY, TYPE_RUNTIME_BEAN);
166         return createON(ON_DOMAIN, map);
167     }
168
169     private static Set<String> blacklist = new HashSet<>(Arrays.asList(
170             MODULE_FACTORY_NAME_KEY, INSTANCE_NAME_KEY, TYPE_KEY));
171
172     public static Map<String, String> getAdditionalPropertiesOfRuntimeBeanName(
173             ObjectName on) {
174         checkType(on, TYPE_RUNTIME_BEAN);
175         Map<String, String> allProperties = getAdditionalProperties(on);
176         Map<String, String> result = new HashMap<>();
177         for (Entry<String, String> entry : allProperties.entrySet()) {
178             if (blacklist.contains(entry.getKey()) == false) {
179                 result.put(entry.getKey(), entry.getValue());
180             }
181         }
182         return result;
183     }
184
185     public static Map<String, String> getAdditionalProperties(ObjectName on) {
186         Hashtable<String, String> keyPropertyList = on.getKeyPropertyList();
187         Map<String, String> result = new HashMap<>();
188         for (Entry<String, String> entry : keyPropertyList.entrySet()) {
189             result.put(entry.getKey(), entry.getValue());
190         }
191         return result;
192     }
193
194     public static void checkDomain(ObjectName objectName) {
195         if (!ON_DOMAIN.equals(objectName.getDomain())) {
196             throw new IllegalArgumentException("Wrong domain " + objectName);
197         }
198
199     }
200
201     public static void checkType(ObjectName objectName, String type) {
202         if (!type.equals(objectName.getKeyProperty(TYPE_KEY))) {
203             throw new IllegalArgumentException("Wrong type, expected '" + type
204                     + "', got " + objectName);
205         }
206     }
207
208     public static ObjectName createModulePattern(String moduleName,
209             String instanceName) {
210         if (moduleName == null)
211             moduleName = "*";
212         if (instanceName == null)
213             instanceName = "*";
214         // do not return object names containing transaction name
215         ObjectName namePattern = ObjectNameUtil
216                 .createON(ObjectNameUtil.ON_DOMAIN + ":"
217                         + ObjectNameUtil.TYPE_KEY + "="
218                         + ObjectNameUtil.TYPE_MODULE + ","
219                         + ObjectNameUtil.MODULE_FACTORY_NAME_KEY + "="
220                         + moduleName + "," + ""
221                         + ObjectNameUtil.INSTANCE_NAME_KEY + "=" + instanceName);
222         return namePattern;
223     }
224
225     public static ObjectName createModulePattern(String ifcName,
226             String instanceName, String transactionName) {
227         return ObjectNameUtil.createON(ObjectNameUtil.ON_DOMAIN
228                 + ":type=Module," + ObjectNameUtil.MODULE_FACTORY_NAME_KEY
229                 + "=" + ifcName + "," + ObjectNameUtil.INSTANCE_NAME_KEY + "="
230                 + instanceName + "," + ObjectNameUtil.TRANSACTION_NAME_KEY
231                 + "=" + transactionName);
232     }
233
234     public static ObjectName createRuntimeBeanPattern(String moduleName,
235             String instanceName) {
236         return ObjectNameUtil.createON(ObjectNameUtil.ON_DOMAIN + ":"
237                 + ObjectNameUtil.TYPE_KEY + "="
238                 + ObjectNameUtil.TYPE_RUNTIME_BEAN + ","
239                 + ObjectNameUtil.MODULE_FACTORY_NAME_KEY + "=" + moduleName
240                 + "," + ObjectNameUtil.INSTANCE_NAME_KEY + "=" + instanceName
241                 + ",*");
242
243     }
244
245     public static ModuleIdentifier fromON(ObjectName objectName,
246             String expectedType) {
247         checkType(objectName, expectedType);
248         String factoryName = getFactoryName(objectName);
249         if (factoryName == null)
250             throw new IllegalArgumentException(
251                     "ObjectName does not contain module name");
252         String instanceName = getInstanceName(objectName);
253         if (instanceName == null)
254             throw new IllegalArgumentException(
255                     "ObjectName does not contain instance name");
256         return new ModuleIdentifier(factoryName, instanceName);
257     }
258
259 }

©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.