1 package org.opendaylight.controller.sal.restconf.impl
3 import com.google.common.collect.BiMap
4 import com.google.common.collect.HashBiMap
6 import java.net.URLDecoder
7 import java.net.URLEncoder
8 import java.util.HashMap
11 import org.opendaylight.yangtools.yang.common.QName
12 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
13 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.InstanceIdentifierBuilder
14 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier
15 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument
17 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode
18 import org.opendaylight.yangtools.yang.model.api.ChoiceNode
19 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode
20 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode
23 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext
26 import static com.google.common.base.Preconditions.*
28 class ControllerContext {
30 val static NULL_VALUE = "null"
33 SchemaContext schemas;
35 private val BiMap<URI, String> uriToModuleName = HashBiMap.create();
36 private val Map<String, URI> moduleNameToUri = uriToModuleName.inverse();
38 public def InstanceIdWithSchemaNode toInstanceIdentifier(String restconfInstance) {
39 val ret = InstanceIdentifier.builder();
40 val pathArgs = restconfInstance.split("/");
44 if (pathArgs.head.empty) {
47 val schemaNode = ret.collectPathArguments(pathArgs, restconfInstance.findModule);
48 if (schemaNode == null) {
51 new InstanceIdWithSchemaNode(ret.toInstance, schemaNode)
54 private def findModule(String restconfInstance) {
55 checkNotNull(restconfInstance);
56 val pathArgs = restconfInstance.split("/");
60 val modulWithFirstYangStatement = pathArgs.filter[s|s.contains(":")].head
61 val startModule = modulWithFirstYangStatement.toModuleName();
62 schemas.getLatestModule(startModule)
65 private def getLatestModule(SchemaContext schema, String moduleName) {
67 checkArgument(moduleName != null && !moduleName.empty)
68 val modules = schema.modules.filter[m|m.name == moduleName]
69 var latestModule = modules.head
70 for (module : modules) {
71 if (module.revision.after(latestModule.revision)) {
78 def String toFullRestconfIdentifier(InstanceIdentifier path) {
79 val elements = path.path;
80 val ret = new StringBuilder();
81 val startQName = elements.get(0).nodeType;
82 val initialModule = schemas.findModuleByNamespaceAndRevision(startQName.namespace, startQName.revision)
83 var node = initialModule as DataSchemaNode;
84 for (element : elements) {
85 node = node.childByQName(element.nodeType);
86 ret.append(element.toRestconfIdentifier(node));
91 private def dispatch CharSequence toRestconfIdentifier(NodeIdentifier argument, DataSchemaNode node) {
92 '''/«argument.nodeType.toRestconfIdentifier()»'''
95 private def dispatch CharSequence toRestconfIdentifier(NodeIdentifierWithPredicates argument, ListSchemaNode node) {
96 val nodeIdentifier = argument.nodeType.toRestconfIdentifier();
97 val keyValues = argument.keyValues;
98 return '''/«nodeIdentifier»/«FOR key : node.keyDefinition SEPARATOR "/"»«keyValues.get(key).toUriString»«ENDFOR»'''
101 private def dispatch CharSequence toRestconfIdentifier(PathArgument argument, DataSchemaNode node) {
102 throw new IllegalArgumentException("Conversion of generic path argument is not supported");
105 def CharSequence toRestconfIdentifier(QName qname) {
106 var module = uriToModuleName.get(qname.namespace)
107 if (module == null) {
108 val moduleSchema = schemas.findModuleByNamespaceAndRevision(qname.namespace, qname.revision);
109 if(moduleSchema == null) throw new IllegalArgumentException()
110 uriToModuleName.put(qname.namespace, moduleSchema.name)
111 module = moduleSchema.name;
113 return '''«module»:«qname.localName»''';
116 private static dispatch def DataSchemaNode childByQName(ChoiceNode container, QName name) {
117 for (caze : container.cases) {
118 val ret = caze.childByQName(name)
126 private static dispatch def DataSchemaNode childByQName(ChoiceCaseNode container, QName name) {
127 val ret = container.getDataChildByName(name);
131 private static dispatch def DataSchemaNode childByQName(ContainerSchemaNode container, QName name) {
132 return container.dataNodeChildByQName(name);
135 private static dispatch def DataSchemaNode childByQName(ListSchemaNode container, QName name) {
136 return container.dataNodeChildByQName(name);
139 private static dispatch def DataSchemaNode childByQName(DataSchemaNode container, QName name) {
143 private static def DataSchemaNode dataNodeChildByQName(DataNodeContainer container, QName name) {
144 var ret = container.getDataChildByName(name);
147 // Find in Choice Cases
148 for (node : container.childNodes) {
149 if (node instanceof ChoiceCaseNode) {
150 val caseNode = (node as ChoiceCaseNode);
151 ret = caseNode.childByQName(name);
161 private def toUriString(Object object) {
162 if(object == null) return "";
163 return URLEncoder.encode(object.toString)
166 private def DataSchemaNode collectPathArguments(InstanceIdentifierBuilder builder, List<String> strings,
167 DataNodeContainer parentNode) {
168 checkNotNull(strings)
170 return parentNode as DataSchemaNode;
172 val nodeRef = strings.head;
174 val nodeName = nodeRef.toNodeName();
175 val targetNode = parentNode.getDataChildByName(nodeName);
176 if (targetNode == null) {
177 val children = parentNode.childNodes
178 for (child : children) {
179 if (child instanceof ChoiceNode) {
180 val choice = child as ChoiceNode
181 for (caze : choice.cases) {
182 val result = builder.collectPathArguments(strings, caze as DataNodeContainer);
190 if (targetNode instanceof ChoiceNode) {
194 // Number of consumed elements
196 if (targetNode instanceof ListSchemaNode) {
197 val listNode = targetNode as ListSchemaNode;
198 val keysSize = listNode.keyDefinition.size
199 // every key has to be filled
200 if ((strings.length - consumed) < keysSize) {
203 val uriKeyValues = strings.subList(consumed, consumed + keysSize);
204 val keyValues = new HashMap<QName, Object>();
206 for (key : listNode.keyDefinition) {
207 val uriKeyValue = uriKeyValues.get(i);
208 // key value cannot be NULL
209 if (uriKeyValue.equals(NULL_VALUE)) {
212 keyValues.addKeyValue(listNode.getDataChildByName(key), uriKeyValue);
215 consumed = consumed + i;
216 builder.nodeWithKey(targetNode.QName, keyValues);
218 // Only one instance of node is allowed
219 builder.node(targetNode.QName);
221 if (targetNode instanceof DataNodeContainer) {
222 val remaining = strings.subList(consumed, strings.length);
223 val result = builder.collectPathArguments(remaining, targetNode as DataNodeContainer);
230 private def void addKeyValue(HashMap<QName, Object> map, DataSchemaNode node, String uriValue) {
231 checkNotNull(uriValue);
232 checkArgument(node instanceof LeafSchemaNode);
233 val decoded = URLDecoder.decode(uriValue);
234 map.put(node.QName, decoded);
238 private def String toModuleName(String str) {
240 if (str.contains(":")) {
241 val args = str.split(":");
242 checkArgument(args.size === 2);
249 private def String toNodeName(String str) {
250 if (str.contains(":")) {
251 val args = str.split(":");
252 checkArgument(args.size === 2);
259 public def QName toRpcQName(String name) {