2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.api.jmx;
10 import java.util.Arrays;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.Hashtable;
15 import java.util.Map.Entry;
17 import javax.annotation.concurrent.ThreadSafe;
18 import javax.management.MalformedObjectNameException;
19 import javax.management.ObjectName;
20 import org.opendaylight.controller.config.api.ModuleIdentifier;
21 import org.opendaylight.controller.config.api.jmx.constants.ConfigRegistryConstants;
24 * Provides ObjectName creation. Each created ObjectName consists of domain that
25 * is defined as {@link #ON_DOMAIN} and at least one key-value pair. The only
26 * mandatory property is {@link #TYPE_KEY}. All transaction related mbeans have
27 * {@link #TRANSACTION_NAME_KEY} property set.
30 public class ObjectNameUtil {
31 private ObjectNameUtil() {
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 SERVICE_QNAME_KEY = "serviceQName";
37 public static final String INSTANCE_NAME_KEY = "instanceName";
38 public static final String TYPE_KEY = ConfigRegistryConstants.TYPE_KEY;
39 public static final String TYPE_CONFIG_TRANSACTION = "ConfigTransaction";
40 public static final String TYPE_MODULE = "Module";
41 public static final String TYPE_SERVICE_REFERENCE = "ServiceReference";
42 public static final String TYPE_RUNTIME_BEAN = "RuntimeBean";
43 public static final String TRANSACTION_NAME_KEY = "TransactionName";
44 public static final String REF_NAME_KEY = "RefName";
45 private static final String REPLACED_QUOTATION_MARK = "\\?";
46 public static final String ON_WILDCARD = "*";
48 public static ObjectName createON(String on) {
50 return new ObjectName(on);
51 } catch (MalformedObjectNameException e) {
52 throw new IllegalArgumentException(e);
56 public static ObjectName createONWithDomainAndType(String type) {
57 return ConfigRegistryConstants.createONWithDomainAndType(type);
60 public static ObjectName createON(String name, String key, String value) {
61 return ConfigRegistryConstants.createON(name, key, value);
64 public static ObjectName createON(String domain, Map<String, String> attribs) {
65 Hashtable<String, String> table = new Hashtable<>(attribs);
67 return new ObjectName(domain, table);
68 } catch (MalformedObjectNameException e) {
69 throw new IllegalArgumentException(e);
74 public static ObjectName createTransactionControllerON(
75 String transactionName) {
76 Map<String, String> onParams = new HashMap<>();
77 onParams.put(TRANSACTION_NAME_KEY, transactionName);
78 onParams.put(TYPE_KEY, TYPE_CONFIG_TRANSACTION);
79 return createON(ON_DOMAIN, onParams);
82 public static ObjectName createTransactionModuleON(String transactionName,
83 ModuleIdentifier moduleIdentifier) {
84 return createTransactionModuleON(transactionName,
85 moduleIdentifier.getFactoryName(),
86 moduleIdentifier.getInstanceName());
89 public static ObjectName createTransactionModuleON(String transactionName,
90 String moduleName, String instanceName) {
91 Map<String, String> onParams = createModuleMap(moduleName, instanceName);
92 onParams.put(TRANSACTION_NAME_KEY, transactionName);
93 return createON(ON_DOMAIN, onParams);
96 public static ObjectName createTransactionModuleON(String transactionName,
98 return createTransactionModuleON(transactionName, getFactoryName(on),
102 public static ObjectName createReadOnlyModuleON(
103 ModuleIdentifier moduleIdentifier) {
104 return createReadOnlyModuleON(moduleIdentifier.getFactoryName(),
105 moduleIdentifier.getInstanceName());
108 public static ObjectName createReadOnlyServiceON(String serviceQName, String refName) {
109 Map<String, String> onParams = createServiceMap(serviceQName, refName);
110 return createON(ON_DOMAIN, onParams);
113 public static ObjectName createTransactionServiceON(String transactionName, String serviceQName, String refName) {
114 Map<String, String> onParams = createServiceON(transactionName, serviceQName, refName);
115 return createON(ON_DOMAIN, onParams);
118 public static String getServiceQName(ObjectName objectName) {
119 checkType(objectName, TYPE_SERVICE_REFERENCE);
120 String quoted = objectName.getKeyProperty(SERVICE_QNAME_KEY);
121 return unquoteAndUnescape(objectName, quoted);
124 // ObjectName supports quotation and ignores tokens like =, but fails to ignore ? sign.
125 // It must be replaced with another character that hopefully does not collide
126 // with actual value.
127 private static String unquoteAndUnescape(ObjectName objectName, String quoted) {
128 if (quoted == null) {
129 throw new IllegalArgumentException("Cannot find " + SERVICE_QNAME_KEY + " in " + objectName);
131 if (quoted.startsWith("\"") == false || quoted.endsWith("\"") == false) {
132 throw new IllegalArgumentException("Quotes not found in " + objectName);
134 String substring = quoted.substring(1);
135 substring = substring.substring(0, substring.length() - 1);
136 substring = substring.replace(REPLACED_QUOTATION_MARK, "?");
140 private static String quoteAndEscapeValue(String serviceQName) {
141 return "\"" + serviceQName.replace("?", REPLACED_QUOTATION_MARK) + "\"";
144 public static String getReferenceName(ObjectName objectName) {
145 checkType(objectName, TYPE_SERVICE_REFERENCE);
146 return objectName.getKeyProperty(REF_NAME_KEY);
149 private static Map<String, String> createServiceON(String transactionName, String serviceQName,
151 Map<String, String> result = new HashMap<>(createServiceMap(serviceQName, refName));
152 result.put(TRANSACTION_NAME_KEY, transactionName);
156 private static Map<String, String> createServiceMap(String serviceQName,
158 Map<String, String> onParams = new HashMap<>();
159 onParams.put(TYPE_KEY, TYPE_SERVICE_REFERENCE);
160 onParams.put(SERVICE_QNAME_KEY, quoteAndEscapeValue(serviceQName));
161 onParams.put(REF_NAME_KEY, refName);
166 public static ObjectName createReadOnlyModuleON(String moduleName,
167 String instanceName) {
168 Map<String, String> onParams = createModuleMap(moduleName, instanceName);
169 return createON(ON_DOMAIN, onParams);
172 private static Map<String, String> createModuleMap(String moduleName,
173 String instanceName) {
174 Map<String, String> onParams = new HashMap<>();
175 onParams.put(TYPE_KEY, TYPE_MODULE);
176 onParams.put(MODULE_FACTORY_NAME_KEY, moduleName);
177 onParams.put(INSTANCE_NAME_KEY, instanceName);
181 public static String getFactoryName(ObjectName objectName) {
182 checkTypeOneOf(objectName, TYPE_MODULE, TYPE_RUNTIME_BEAN);
183 return objectName.getKeyProperty(MODULE_FACTORY_NAME_KEY);
186 public static String getInstanceName(ObjectName objectName) {
187 checkTypeOneOf(objectName, TYPE_MODULE, TYPE_RUNTIME_BEAN);
188 return objectName.getKeyProperty(INSTANCE_NAME_KEY);
191 public static String getTransactionName(ObjectName objectName) {
192 return objectName.getKeyProperty(TRANSACTION_NAME_KEY);
196 * Sanitize on: keep only mandatory attributes of module + metadata.
198 public static ObjectName withoutTransactionName(ObjectName inputON) {
199 checkTypeOneOf(inputON, TYPE_MODULE, TYPE_SERVICE_REFERENCE);
200 if (getTransactionName(inputON) == null) {
201 throw new IllegalArgumentException(
202 "Expected ObjectName with transaction:" + inputON);
204 if (ON_DOMAIN.equals(inputON.getDomain()) == false) {
205 throw new IllegalArgumentException("Expected different domain: "
208 Map<String, String> outputProperties;
209 if (inputON.getKeyProperty(TYPE_KEY).equals(TYPE_MODULE)) {
210 String moduleName = getFactoryName(inputON);
211 String instanceName = getInstanceName(inputON);
212 outputProperties = new HashMap<>(createModuleMap(moduleName, instanceName));
214 String serviceQName = getServiceQName(inputON);
215 String refName = getReferenceName(inputON);
216 outputProperties = new HashMap<>(createServiceMap(serviceQName, refName));
218 Map<String, String> allProperties = getAdditionalProperties(inputON);
219 for (Entry<String, String> entry : allProperties.entrySet()) {
220 if (entry.getKey().startsWith("X-")) {
221 outputProperties.put(entry.getKey(), entry.getValue());
224 return createON(ON_DOMAIN, outputProperties);
227 public static ObjectName withTransactionName(ObjectName inputON, String transactionName) {
228 Map<String, String> additionalProperties = getAdditionalProperties(inputON);
229 additionalProperties.put(TRANSACTION_NAME_KEY, transactionName);
230 return createON(inputON.getDomain(), additionalProperties);
234 private static void assertDoesNotContain(
235 Map<String, String> additionalProperties, String key) {
236 if (additionalProperties.containsKey(key)) {
237 throw new IllegalArgumentException(
238 "Map 'additionalProperties' cannot overwrite attribute "
243 public static ObjectName createRuntimeBeanName(String moduleName,
244 String instanceName, Map<String, String> additionalProperties) {
245 // check that there is no overwriting of default attributes
246 assertDoesNotContain(additionalProperties, MODULE_FACTORY_NAME_KEY);
247 assertDoesNotContain(additionalProperties, INSTANCE_NAME_KEY);
248 assertDoesNotContain(additionalProperties, TYPE_KEY);
249 assertDoesNotContain(additionalProperties, TRANSACTION_NAME_KEY);
250 Map<String, String> map = new HashMap<>(additionalProperties);
251 map.put(MODULE_FACTORY_NAME_KEY, moduleName);
252 map.put(INSTANCE_NAME_KEY, instanceName);
253 map.put(TYPE_KEY, TYPE_RUNTIME_BEAN);
254 return createON(ON_DOMAIN, map);
257 private static Set<String> blacklist = new HashSet<>(Arrays.asList(
258 MODULE_FACTORY_NAME_KEY, INSTANCE_NAME_KEY, TYPE_KEY));
260 public static Map<String, String> getAdditionalPropertiesOfRuntimeBeanName(
262 checkType(on, TYPE_RUNTIME_BEAN);
263 Map<String, String> allProperties = getAdditionalProperties(on);
264 Map<String, String> result = new HashMap<>();
265 for (Entry<String, String> entry : allProperties.entrySet()) {
266 if (blacklist.contains(entry.getKey()) == false) {
267 result.put(entry.getKey(), entry.getValue());
273 public static Map<String, String> getAdditionalProperties(ObjectName on) {
274 Map<String, String> keyPropertyList = on.getKeyPropertyList();
275 Map<String, String> result = new HashMap<>();
276 for (Entry<String, String> entry : keyPropertyList.entrySet()) {
277 result.put(entry.getKey(), entry.getValue());
282 public static void checkDomain(ObjectName objectName) {
283 if (!ON_DOMAIN.equals(objectName.getDomain())) {
284 throw new IllegalArgumentException("Wrong domain " + objectName);
289 public static void checkType(ObjectName objectName, String type) {
290 if (!type.equals(objectName.getKeyProperty(TYPE_KEY))) {
291 throw new IllegalArgumentException("Wrong type, expected '" + type
292 + "', got " + objectName);
296 public static void checkTypeOneOf(ObjectName objectName, String... types) {
297 for (String type : types) {
298 if (type.equals(objectName.getKeyProperty(TYPE_KEY))) {
302 throw new IllegalArgumentException("Wrong type, expected one of " + Arrays.asList(types)
303 + ", got " + objectName);
306 public static ObjectName createModulePattern(String moduleName,
307 String instanceName) {
308 String finalModuleName = moduleName == null ? ON_WILDCARD : moduleName;
309 String finalInstanceName = instanceName == null ? ON_WILDCARD : instanceName;
311 // do not return object names containing transaction name
312 ObjectName namePattern = ObjectNameUtil
313 .createON(ObjectNameUtil.ON_DOMAIN + ":"
314 + ObjectNameUtil.TYPE_KEY + "="
315 + ObjectNameUtil.TYPE_MODULE + ","
316 + ObjectNameUtil.MODULE_FACTORY_NAME_KEY + "="
317 + finalModuleName + "," + ""
318 + ObjectNameUtil.INSTANCE_NAME_KEY + "=" + finalInstanceName);
322 public static ObjectName createModulePattern(String ifcName,
323 String instanceName, String transactionName) {
324 String finalIfcName = ifcName == null ? ON_WILDCARD : ifcName;
325 String finalInstanceName = instanceName == null ? ON_WILDCARD : instanceName;
326 String finalTransactionName = transactionName == null ? ON_WILDCARD : transactionName;
328 return ObjectNameUtil.createON(ObjectNameUtil.ON_DOMAIN
329 + ":type=Module," + ObjectNameUtil.MODULE_FACTORY_NAME_KEY
330 + "=" + finalIfcName + "," + ObjectNameUtil.INSTANCE_NAME_KEY + "="
331 + finalInstanceName + "," + ObjectNameUtil.TRANSACTION_NAME_KEY
332 + "=" + finalTransactionName);
335 public static ObjectName createRuntimeBeanPattern(String moduleName,
336 String instanceName) {
337 String finalModuleName = moduleName == null ? ON_WILDCARD : moduleName;
338 String finalInstanceName = instanceName == null ? ON_WILDCARD : instanceName;
340 return ObjectNameUtil.createON(ObjectNameUtil.ON_DOMAIN + ":"
341 + ObjectNameUtil.TYPE_KEY + "="
342 + ObjectNameUtil.TYPE_RUNTIME_BEAN + ","
343 + ObjectNameUtil.MODULE_FACTORY_NAME_KEY + "=" + finalModuleName
344 + "," + ObjectNameUtil.INSTANCE_NAME_KEY + "=" + finalInstanceName
349 public static ModuleIdentifier fromON(ObjectName objectName,
350 String expectedType) {
351 checkType(objectName, expectedType);
352 String factoryName = getFactoryName(objectName);
353 if (factoryName == null) {
354 throw new IllegalArgumentException(
355 "ObjectName does not contain module name");
357 String instanceName = getInstanceName(objectName);
358 if (instanceName == null) {
359 throw new IllegalArgumentException(
360 "ObjectName does not contain instance name");
362 return new ModuleIdentifier(factoryName, instanceName);
365 public static boolean isServiceReference(ObjectName objectName) {
366 return TYPE_SERVICE_REFERENCE.equals(objectName.getKeyProperty(TYPE_KEY));