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.*
29 class ControllerContext {
32 SchemaContext schemas;
34 private val BiMap<URI, String> uriToModuleName = HashBiMap.create();
35 private val Map<String, URI> moduleNameToUri = uriToModuleName.inverse();
37 public def InstanceIdWithSchemaNode toInstanceIdentifier(String restconfInstance) {
38 val ret = InstanceIdentifier.builder();
39 val pathArgs = restconfInstance.split("/");
43 val schemaNode = ret.collectPathArguments(pathArgs, restconfInstance.findModule);
44 new InstanceIdWithSchemaNode(ret.toInstance, schemaNode)
47 def findModule(String restconfInstance) {
48 checkNotNull(restconfInstance);
49 val pathArgs = restconfInstance.split("/");
53 val modulWithFirstYangStatement = pathArgs.filter[s|s.contains(":")].head
54 val startModule = modulWithFirstYangStatement.toModuleName();
55 schemas.getLatestModule(startModule)
58 private def getLatestModule(SchemaContext schema, String moduleName) {
60 checkArgument(moduleName != null && !moduleName.empty)
61 val modules = schema.modules.filter[m|m.name == moduleName]
62 var latestModule = modules.head
63 for (module : modules) {
64 if (module.revision.after(latestModule.revision)) {
71 def String toFullRestconfIdentifier(InstanceIdentifier path) {
72 val elements = path.path;
73 val ret = new StringBuilder();
74 val startQName = elements.get(0).nodeType;
75 val initialModule = schemas.findModuleByNamespaceAndRevision(startQName.namespace, startQName.revision)
76 var node = initialModule as DataSchemaNode;
77 for (element : elements) {
78 node = node.childByQName(element.nodeType);
79 ret.append(element.toRestconfIdentifier(node));
84 private def dispatch CharSequence toRestconfIdentifier(NodeIdentifier argument, DataSchemaNode node) {
85 '''/«argument.nodeType.toRestconfIdentifier()»'''
88 private def dispatch CharSequence toRestconfIdentifier(NodeIdentifierWithPredicates argument, ListSchemaNode node) {
89 val nodeIdentifier = argument.nodeType.toRestconfIdentifier();
90 val keyValues = argument.keyValues;
91 return '''/«nodeIdentifier»/«FOR key : node.keyDefinition SEPARATOR "/"»«keyValues.get(key).toUriString»«ENDFOR»'''
94 private def dispatch CharSequence toRestconfIdentifier(PathArgument argument, DataSchemaNode node) {
95 throw new IllegalArgumentException("Conversion of generic path argument is not supported");
98 public def CharSequence toRestconfIdentifier(QName qname) {
99 var module = uriToModuleName.get(qname.namespace)
100 if (module == null) {
101 val moduleSchema = schemas.findModuleByNamespaceAndRevision(qname.namespace, qname.revision);
102 if(moduleSchema == null) throw new IllegalArgumentException()
103 uriToModuleName.put(qname.namespace, moduleSchema.name)
104 module = moduleSchema.name;
106 return '''«module»:«qname.localName»''';
109 private static dispatch def DataSchemaNode childByQName(ChoiceNode container, QName name) {
110 for (caze : container.cases) {
111 val ret = caze.childByQName(name)
119 private static dispatch def DataSchemaNode childByQName(ChoiceCaseNode container, QName name) {
120 val ret = container.getDataChildByName(name);
124 private static dispatch def DataSchemaNode childByQName(ContainerSchemaNode container, QName name) {
125 return container.dataNodeChildByQName(name);
128 private static dispatch def DataSchemaNode childByQName(ListSchemaNode container, QName name) {
129 return container.dataNodeChildByQName(name);
132 private static dispatch def DataSchemaNode childByQName(DataSchemaNode container, QName name) {
136 private static def DataSchemaNode dataNodeChildByQName(DataNodeContainer container, QName name) {
137 var ret = container.getDataChildByName(name);
140 // Find in Choice Cases
141 for (node : container.childNodes) {
142 if (node instanceof ChoiceCaseNode) {
143 val caseNode = (node as ChoiceCaseNode);
144 ret = caseNode.childByQName(name);
154 private def toUriString(Object object) {
155 if(object == null) return "";
156 return URLEncoder.encode(object.toString)
159 def DataSchemaNode collectPathArguments(InstanceIdentifierBuilder builder, List<String> strings,
160 DataNodeContainer parentNode) {
161 checkNotNull(strings)
163 return parentNode as DataSchemaNode;
165 val nodeRef = strings.head;
167 //val moduleName = nodeRef.toModuleName();
168 val nodeName = nodeRef.toNodeName();
169 val targetNode = parentNode.getDataChildByName(nodeName);
170 if (targetNode == null) {
174 // Number of consumed elements
176 if (targetNode instanceof ListSchemaNode) {
177 val listNode = targetNode as ListSchemaNode;
178 val keysSize = listNode.keyDefinition.size
179 val uriKeyValues = strings.subList(consumed, consumed + keysSize);
180 val keyValues = new HashMap<QName, Object>();
182 for (key : listNode.keyDefinition) {
183 val uriKeyValue = uriKeyValues.get(i);
184 keyValues.addKeyValue(listNode.getDataChildByName(key), uriKeyValue);
187 consumed = consumed + i;
188 builder.nodeWithKey(targetNode.QName, keyValues);
191 // Only one instance of node is allowed
192 builder.node(targetNode.QName);
194 if (targetNode instanceof DataNodeContainer) {
195 val remaining = strings.subList(consumed, strings.length);
196 val result = builder.collectPathArguments(remaining, targetNode as DataNodeContainer);
203 def void addKeyValue(HashMap<QName, Object> map, DataSchemaNode node, String uriValue) {
204 checkNotNull(uriValue);
205 checkArgument(node instanceof LeafSchemaNode);
206 val decoded = URLDecoder.decode(uriValue);
207 map.put(node.QName, decoded);
211 def String toModuleName(String str) {
213 if (str.contains(":")) {
214 val args = str.split(":");
215 checkArgument(args.size === 2);
222 def String toNodeName(String str) {
223 if (str.contains(":")) {
224 val args = str.split(":");
225 checkArgument(args.size === 2);
232 public def QName toRpcQName(String name) {