cd3d72a5c500d75a2e5393c76c2e923e2bd7b83a
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / rpc / RuntimeRpcElementResolved.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.controller.config.facade.xml.rpc;
10
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;
16 import java.util.Map;
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.config.util.xml.XmlMappingConstants;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.Modules;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.modules.Module;
24
25 /**
26  * Represents parsed xpath to runtime bean instance
27  */
28 public final class RuntimeRpcElementResolved {
29     private final String moduleName;
30     private final String instanceName;
31     private final String namespace;
32     private final String runtimeBeanName;
33     private final Map<String, String> additionalAttributes;
34
35     private RuntimeRpcElementResolved(String namespace, String moduleName, String instanceName, String runtimeBeanName,
36                                       Map<String, String> additionalAttributes) {
37         this.moduleName = Preconditions.checkNotNull(moduleName, "Module name");
38         this.instanceName =  Preconditions.checkNotNull(instanceName, "Instance name");
39         this.additionalAttributes = additionalAttributes;
40         this.namespace = Preconditions.checkNotNull(namespace, "Namespace");
41         this.runtimeBeanName = Preconditions.checkNotNull(runtimeBeanName, "Runtime bean name");
42     }
43
44     public String getModuleName() {
45         return moduleName;
46     }
47
48     @VisibleForTesting
49     Map<String, String> getAdditionalAttributes() {
50         return additionalAttributes;
51     }
52
53     public String getInstanceName() {
54         return instanceName;
55     }
56
57     public String getNamespace() {
58         return namespace;
59     }
60
61     public String getRuntimeBeanName() {
62         return runtimeBeanName;
63     }
64
65     public ObjectName getObjectName(ModuleRpcs rpcMapping) {
66         Map<String, String> additionalAttributesJavaNames = Maps
67                 .newHashMapWithExpectedSize(additionalAttributes.size());
68         for (String attributeYangName : additionalAttributes.keySet()) {
69             String attributeJavaName = rpcMapping.getRbeJavaName(attributeYangName);
70             Preconditions.checkState(attributeJavaName != null,
71                     "Cannot find java name for runtime bean wtih yang name %s", attributeYangName);
72             additionalAttributesJavaNames.put(attributeJavaName, additionalAttributes.get(attributeYangName));
73         }
74         return ObjectNameUtil.createRuntimeBeanName(moduleName, instanceName, additionalAttributesJavaNames);
75     }
76
77     /**
78      * Pattern for an absolute instance identifier xpath pointing to a runtime bean instance e.g:
79      * <pre>
80      * /modules/module[name=instanceName][type=moduleType]
81      * </pre>
82      * or
83      * <pre>
84      * /a:modules/a:module[a:name=instanceName][a:type=moduleType]
85      * </pre>
86      */
87     private static final String xpathPatternBlueprint =
88             "/" + getRegExForPrefixedName(Modules.QNAME.getLocalName())+ "/" + getRegExForPrefixedName(Module.QNAME.getLocalName())
89
90                     + "\\["
91                     + "(?<key1>" + getRegExForPrefixedName(XmlMappingConstants.TYPE_KEY) + "|" + getRegExForPrefixedName(XmlMappingConstants.NAME_KEY) + ")"
92                     + "=('|\")?(?<value1>[^'\"\\]]+)('|\")?"
93                     + "( and |\\]\\[)"
94                     + "(?<key2>" + getRegExForPrefixedName(XmlMappingConstants.TYPE_KEY) + "|" + getRegExForPrefixedName(XmlMappingConstants.NAME_KEY) + ")"
95                     + "=('|\")?(?<value2>[^'\"\\]]+)('|\")?"
96                     + "\\]"
97
98                     + "(?<additional>.*)";
99
100     /**
101      * Return reg ex that matches either the name with or without a prefix
102      */
103     private static String getRegExForPrefixedName(final String name) {
104         return "([^:]+:)?" + name;
105     }
106
107     private static final Pattern xpathPattern = Pattern.compile(xpathPatternBlueprint);
108
109     /**
110      * Pattern for additional path elements inside xpath for instance identifier pointing to an inner runtime bean e.g:
111      * <pre>
112      * /modules/module[name=instanceName and type=moduleType]/inner[key=b]
113      * </pre>
114      */
115     private static final String additionalPatternBlueprint = getRegExForPrefixedName("(?<additionalKey>.+)") + "\\[(?<prefixedKey>" + getRegExForPrefixedName("(.+)") + ")=('|\")?(?<additionalValue>[^'\"\\]]+)('|\")?\\]";
116     private static final Pattern additionalPattern = Pattern.compile(additionalPatternBlueprint);
117
118     public static RuntimeRpcElementResolved fromXpath(String xpath, String elementName, String namespace) {
119         Matcher matcher = xpathPattern.matcher(xpath);
120         Preconditions.checkState(matcher.matches(),
121                 "Node %s with value '%s' not in required form on rpc element %s, required format is %s",
122                 //TODO refactor this string, and/or unify with RPR.CONTEXT_INSTANCE from netconf
123                 "context-instance", xpath, elementName, xpathPatternBlueprint);
124
125         PatternGroupResolver groups = new PatternGroupResolver(matcher.group("key1"), matcher.group("value1"),
126                 matcher.group("value2"), matcher.group("additional"));
127
128         String moduleName = groups.getModuleName();
129         String instanceName = groups.getInstanceName();
130
131         Map<String, String> additionalAttributes = groups.getAdditionalKeys(elementName, moduleName);
132
133         return new RuntimeRpcElementResolved(namespace, moduleName, instanceName, groups.getRuntimeBeanYangName(),
134                 additionalAttributes);
135     }
136
137     private static final class PatternGroupResolver {
138
139         private final String key1, value1, value2;
140         private final String additional;
141         private String runtimeBeanYangName;
142
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);
148         }
149
150         String getModuleName() {
151             return key1.contains(XmlMappingConstants.TYPE_KEY) ? value1 : value2;
152         }
153
154         String getInstanceName() {
155             return key1.contains(XmlMappingConstants.NAME_KEY) ? value1 : value2;
156         }
157
158
159         Map<String, String> getAdditionalKeys(String elementName, String moduleName) {
160             HashMap<String, String> additionalAttributes = Maps.newHashMap();
161
162             runtimeBeanYangName = moduleName;
163             for (String additionalKeyValue : additional.split("/")) {
164                 if (Strings.isNullOrEmpty(additionalKeyValue)){
165                     continue;
166                 }
167                 Matcher matcher = additionalPattern.matcher(additionalKeyValue);
168                 Preconditions
169                         .checkState(
170                                 matcher.matches(),
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"));
176             }
177             return additionalAttributes;
178         }
179
180         private String getRuntimeBeanYangName() {
181             Preconditions.checkState(runtimeBeanYangName!=null);
182             return runtimeBeanYangName;
183         }
184     }
185 }