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
9 package org.opendaylight.controller.netconf.confignetconfconnector.operations.runtimerpc;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Strings;
14 import com.google.common.collect.Maps;
15 import java.util.HashMap;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19 import javax.management.ObjectName;
20 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
21 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
22 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc.ModuleRpcs;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.Modules;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.modules.Module;
27 * Represents parsed xpath to runtime bean instance
29 public final class RuntimeRpcElementResolved {
30 private final String moduleName;
31 private final String instanceName;
32 private final String namespace;
33 private final String runtimeBeanName;
34 private final Map<String, String> additionalAttributes;
36 private RuntimeRpcElementResolved(String namespace, String moduleName, String instanceName, String runtimeBeanName,
37 Map<String, String> additionalAttributes) {
38 this.moduleName = Preconditions.checkNotNull(moduleName, "Module name");
39 this.instanceName = Preconditions.checkNotNull(instanceName, "Instance name");
40 this.additionalAttributes = additionalAttributes;
41 this.namespace = Preconditions.checkNotNull(namespace, "Namespace");
42 this.runtimeBeanName = Preconditions.checkNotNull(runtimeBeanName, "Runtime bean name");
45 public String getModuleName() {
50 Map<String, String> getAdditionalAttributes() {
51 return additionalAttributes;
54 public String getInstanceName() {
58 public String getNamespace() {
62 public String getRuntimeBeanName() {
63 return runtimeBeanName;
66 public ObjectName getObjectName(ModuleRpcs rpcMapping) {
67 Map<String, String> additionalAttributesJavaNames = Maps
68 .newHashMapWithExpectedSize(additionalAttributes.size());
69 for (String attributeYangName : additionalAttributes.keySet()) {
70 String attributeJavaName = rpcMapping.getRbeJavaName(attributeYangName);
71 Preconditions.checkState(attributeJavaName != null,
72 "Cannot find java name for runtime bean wtih yang name %s", attributeYangName);
73 additionalAttributesJavaNames.put(attributeJavaName, additionalAttributes.get(attributeYangName));
75 return ObjectNameUtil.createRuntimeBeanName(moduleName, instanceName, additionalAttributesJavaNames);
79 * Pattern for an absolute instance identifier xpath pointing to a runtime bean instance e.g:
81 * /modules/module[name=instanceName][type=moduleType]
85 * /a:modules/a:module[a:name=instanceName][a:type=moduleType]
88 private static final String xpathPatternBlueprint =
89 "/" + getRegExForPrefixedName(Modules.QNAME.getLocalName())+ "/" + getRegExForPrefixedName(Module.QNAME.getLocalName())
92 + "(?<key1>" + getRegExForPrefixedName(XmlNetconfConstants.TYPE_KEY) + "|" + getRegExForPrefixedName(XmlNetconfConstants.NAME_KEY) + ")"
93 + "=('|\")?(?<value1>[^'\"\\]]+)('|\")?"
95 + "(?<key2>" + getRegExForPrefixedName(XmlNetconfConstants.TYPE_KEY) + "|" + getRegExForPrefixedName(XmlNetconfConstants.NAME_KEY) + ")"
96 + "=('|\")?(?<value2>[^'\"\\]]+)('|\")?"
99 + "(?<additional>.*)";
102 * Return reg ex that matches either the name with or without a prefix
104 private static String getRegExForPrefixedName(final String name) {
105 return "([^:]+:)?" + name;
108 private static final Pattern xpathPattern = Pattern.compile(xpathPatternBlueprint);
111 * Pattern for additional path elements inside xpath for instance identifier pointing to an inner runtime bean e.g:
113 * /modules/module[name=instanceName and type=moduleType]/inner[key=b]
116 private static final String additionalPatternBlueprint = getRegExForPrefixedName("(?<additionalKey>.+)") + "\\[(?<prefixedKey>" + getRegExForPrefixedName("(.+)") + ")=('|\")?(?<additionalValue>[^'\"\\]]+)('|\")?\\]";
117 private static final Pattern additionalPattern = Pattern.compile(additionalPatternBlueprint);
119 public static RuntimeRpcElementResolved fromXpath(String xpath, String elementName, String namespace) {
120 Matcher matcher = xpathPattern.matcher(xpath);
121 Preconditions.checkState(matcher.matches(),
122 "Node %s with value '%s' not in required form on rpc element %s, required format is %s",
123 RuntimeRpc.CONTEXT_INSTANCE, xpath, elementName, xpathPatternBlueprint);
125 PatternGroupResolver groups = new PatternGroupResolver(matcher.group("key1"), matcher.group("value1"),
126 matcher.group("value2"), matcher.group("additional"));
128 String moduleName = groups.getModuleName();
129 String instanceName = groups.getInstanceName();
131 Map<String, String> additionalAttributes = groups.getAdditionalKeys(elementName, moduleName);
133 return new RuntimeRpcElementResolved(namespace, moduleName, instanceName, groups.getRuntimeBeanYangName(),
134 additionalAttributes);
137 private static final class PatternGroupResolver {
139 private final String key1, value1, value2;
140 private final String additional;
141 private String runtimeBeanYangName;
143 PatternGroupResolver(String key1, String value1, String value2, String additional) {
144 this.key1 = Preconditions.checkNotNull(key1);
145 this.value1 = Preconditions.checkNotNull(value1);
146 this.value2 = Preconditions.checkNotNull(value2);
147 this.additional = Preconditions.checkNotNull(additional);
150 String getModuleName() {
151 return key1.contains(XmlNetconfConstants.TYPE_KEY) ? value1 : value2;
154 String getInstanceName() {
155 return key1.contains(XmlNetconfConstants.NAME_KEY) ? value1 : value2;
159 Map<String, String> getAdditionalKeys(String elementName, String moduleName) {
160 HashMap<String, String> additionalAttributes = Maps.newHashMap();
162 runtimeBeanYangName = moduleName;
163 for (String additionalKeyValue : additional.split("/")) {
164 if (Strings.isNullOrEmpty(additionalKeyValue)){
167 Matcher matcher = additionalPattern.matcher(additionalKeyValue);
171 "Attribute %s not in required form on rpc element %s, required format for additional attributes is: %s",
172 additionalKeyValue, elementName, additionalPatternBlueprint);
173 String name = matcher.group("additionalKey");
174 runtimeBeanYangName = name;
175 additionalAttributes.put(name, matcher.group("additionalValue"));
177 return additionalAttributes;
180 private String getRuntimeBeanYangName() {
181 Preconditions.checkState(runtimeBeanYangName!=null);
182 return runtimeBeanYangName;