Fix checkstyle warnings for config-netconf-connector
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / operations / runtimerpc / RuntimeRpcElementResolved.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
9 package org.opendaylight.controller.netconf.confignetconfconnector.operations.runtimerpc;
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.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;
25
26 /**
27  * Represents parsed xpath to runtime bean instance
28  */
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;
35
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");
43     }
44
45     public String getModuleName() {
46         return moduleName;
47     }
48
49     @VisibleForTesting
50     Map<String, String> getAdditionalAttributes() {
51         return additionalAttributes;
52     }
53
54     public String getInstanceName() {
55         return instanceName;
56     }
57
58     public String getNamespace() {
59         return namespace;
60     }
61
62     public String getRuntimeBeanName() {
63         return runtimeBeanName;
64     }
65
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));
74         }
75         return ObjectNameUtil.createRuntimeBeanName(moduleName, instanceName, additionalAttributesJavaNames);
76     }
77
78     /**
79      * Pattern for an absolute instance identifier xpath pointing to a runtime bean instance e.g:
80      * <pre>
81      * /modules/module[name=instanceName][type=moduleType]
82      * </pre>
83      * or
84      * <pre>
85      * /a:modules/a:module[a:name=instanceName][a:type=moduleType]
86      * </pre>
87      */
88     private static final String xpathPatternBlueprint =
89             "/" + getRegExForPrefixedName(Modules.QNAME.getLocalName())+ "/" + getRegExForPrefixedName(Module.QNAME.getLocalName())
90
91                     + "\\["
92                     + "(?<key1>" + getRegExForPrefixedName(XmlNetconfConstants.TYPE_KEY) + "|" + getRegExForPrefixedName(XmlNetconfConstants.NAME_KEY) + ")"
93                     + "=('|\")?(?<value1>[^'\"\\]]+)('|\")?"
94                     + "( and |\\]\\[)"
95                     + "(?<key2>" + getRegExForPrefixedName(XmlNetconfConstants.TYPE_KEY) + "|" + getRegExForPrefixedName(XmlNetconfConstants.NAME_KEY) + ")"
96                     + "=('|\")?(?<value2>[^'\"\\]]+)('|\")?"
97                     + "\\]"
98
99                     + "(?<additional>.*)";
100
101     /**
102      * Return reg ex that matches either the name with or without a prefix
103      */
104     private static String getRegExForPrefixedName(final String name) {
105         return "([^:]+:)?" + name;
106     }
107
108     private static final Pattern xpathPattern = Pattern.compile(xpathPatternBlueprint);
109
110     /**
111      * Pattern for additional path elements inside xpath for instance identifier pointing to an inner runtime bean e.g:
112      * <pre>
113      * /modules/module[name=instanceName and type=moduleType]/inner[key=b]
114      * </pre>
115      */
116     private static final String additionalPatternBlueprint = getRegExForPrefixedName("(?<additionalKey>.+)") + "\\[(?<prefixedKey>" + getRegExForPrefixedName("(.+)") + ")=('|\")?(?<additionalValue>[^'\"\\]]+)('|\")?\\]";
117     private static final Pattern additionalPattern = Pattern.compile(additionalPatternBlueprint);
118
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);
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(XmlNetconfConstants.TYPE_KEY) ? value1 : value2;
152         }
153
154         String getInstanceName() {
155             return key1.contains(XmlNetconfConstants.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 }