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 java.util.concurrent.ConcurrentHashMap
12 import javax.ws.rs.core.Response
13 import org.opendaylight.controller.sal.core.api.model.SchemaServiceListener
14 import org.opendaylight.controller.sal.rest.impl.RestconfProvider
15 import org.opendaylight.yangtools.yang.common.QName
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
17 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.InstanceIdentifierBuilder
18 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier
19 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates
20 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument
21 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode
22 import org.opendaylight.yangtools.yang.model.api.ChoiceNode
23 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode
24 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode
26 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode
27 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode
28 import org.opendaylight.yangtools.yang.model.api.RpcDefinition
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext
30 import org.opendaylight.yangtools.yang.model.api.SchemaNode
31 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition
32 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil
34 import static com.google.common.base.Preconditions.*
36 class ControllerContext implements SchemaServiceListener {
38 val static ControllerContext INSTANCE = new ControllerContext
40 val static NULL_VALUE = "null"
42 var SchemaContext schemas;
44 private val BiMap<URI, String> uriToModuleName = HashBiMap.create();
45 private val Map<String, URI> moduleNameToUri = uriToModuleName.inverse();
46 private val Map<QName, RpcDefinition> qnameToRpc = new ConcurrentHashMap();
49 if (INSTANCE !== null) {
50 throw new IllegalStateException("Already instantiated");
54 static def getInstance() {
58 private def void checkPreconditions() {
59 if (schemas === null) {
60 throw new ResponseException(Response.Status.SERVICE_UNAVAILABLE, RestconfProvider::NOT_INITALIZED_MSG)
64 def setSchemas(SchemaContext schemas) {
65 onGlobalContextUpdated(schemas)
68 public def InstanceIdWithSchemaNode toInstanceIdentifier(String restconfInstance) {
69 val ret = InstanceIdentifier.builder();
70 val pathArgs = restconfInstance.split("/");
74 if (pathArgs.head.empty) {
77 val schemaNode = ret.collectPathArguments(pathArgs, restconfInstance.findModule);
78 if (schemaNode === null) {
81 return new InstanceIdWithSchemaNode(ret.toInstance, schemaNode)
84 private def findModule(String restconfInstance) {
86 checkNotNull(restconfInstance);
87 val pathArgs = restconfInstance.split("/");
91 val modulWithFirstYangStatement = pathArgs.filter[s|s.contains(":")].head
92 val startModule = modulWithFirstYangStatement.toModuleName();
93 return getLatestModule(startModule)
96 private def getLatestModule(String moduleName) {
98 checkArgument(moduleName !== null && !moduleName.empty)
99 val modules = schemas.modules.filter[m|m.name == moduleName]
100 var latestModule = modules.head
101 for (module : modules) {
102 if (module.revision.after(latestModule.revision)) {
103 latestModule = module
109 def String toFullRestconfIdentifier(InstanceIdentifier path) {
111 val elements = path.path;
112 val ret = new StringBuilder();
113 val startQName = elements.get(0).nodeType;
114 val initialModule = schemas.findModuleByNamespaceAndRevision(startQName.namespace, startQName.revision)
115 var node = initialModule as DataSchemaNode;
116 for (element : elements) {
117 node = node.childByQName(element.nodeType);
118 ret.append(element.toRestconfIdentifier(node));
123 private def dispatch CharSequence toRestconfIdentifier(NodeIdentifier argument, DataSchemaNode node) {
124 '''/«argument.nodeType.toRestconfIdentifier()»'''
127 private def dispatch CharSequence toRestconfIdentifier(NodeIdentifierWithPredicates argument, ListSchemaNode node) {
128 val nodeIdentifier = argument.nodeType.toRestconfIdentifier();
129 val keyValues = argument.keyValues;
130 return '''/«nodeIdentifier»/«FOR key : node.keyDefinition SEPARATOR "/"»«keyValues.get(key).toUriString»«ENDFOR»'''
133 private def dispatch CharSequence toRestconfIdentifier(PathArgument argument, DataSchemaNode node) {
134 throw new IllegalArgumentException("Conversion of generic path argument is not supported");
137 def findModuleByNamespace(URI namespace) {
139 var module = uriToModuleName.get(namespace)
140 if (module === null) {
141 val moduleSchemas = schemas.findModuleByNamespace(namespace);
142 if(moduleSchemas === null) throw new IllegalArgumentException()
143 var latestModule = moduleSchemas.head
144 for (m : moduleSchemas) {
145 if (m.revision.after(latestModule.revision)) {
149 if(latestModule === null) throw new IllegalArgumentException()
150 uriToModuleName.put(namespace, latestModule.name)
151 module = latestModule.name;
156 def CharSequence toRestconfIdentifier(QName qname) {
158 var module = uriToModuleName.get(qname.namespace)
159 if (module === null) {
160 val moduleSchema = schemas.findModuleByNamespaceAndRevision(qname.namespace, qname.revision);
161 if(moduleSchema === null) throw new IllegalArgumentException()
162 uriToModuleName.put(qname.namespace, moduleSchema.name)
163 module = moduleSchema.name;
165 return '''«module»:«qname.localName»''';
168 private static dispatch def DataSchemaNode childByQName(ChoiceNode container, QName name) {
169 for (caze : container.cases) {
170 val ret = caze.childByQName(name)
178 private static dispatch def DataSchemaNode childByQName(ChoiceCaseNode container, QName name) {
179 val ret = container.getDataChildByName(name);
183 private static dispatch def DataSchemaNode childByQName(ContainerSchemaNode container, QName name) {
184 return container.dataNodeChildByQName(name);
187 private static dispatch def DataSchemaNode childByQName(ListSchemaNode container, QName name) {
188 return container.dataNodeChildByQName(name);
191 private static dispatch def DataSchemaNode childByQName(DataSchemaNode container, QName name) {
195 private static def DataSchemaNode dataNodeChildByQName(DataNodeContainer container, QName name) {
196 var ret = container.getDataChildByName(name);
199 // Find in Choice Cases
200 for (node : container.childNodes) {
201 if (node instanceof ChoiceCaseNode) {
202 val caseNode = (node as ChoiceCaseNode);
203 ret = caseNode.childByQName(name);
213 private def toUriString(Object object) {
214 if(object === null) return "";
215 return URLEncoder.encode(object.toString)
218 private def DataSchemaNode collectPathArguments(InstanceIdentifierBuilder builder, List<String> strings,
219 DataNodeContainer parentNode) {
220 checkNotNull(strings)
221 if (parentNode === null) {
225 return parentNode as DataSchemaNode;
227 val nodeRef = strings.head;
229 val nodeName = nodeRef.toNodeName();
230 val targetNode = parentNode.getDataChildByName(nodeName);
231 if (targetNode === null) {
232 val children = parentNode.childNodes
233 for (child : children) {
234 if (child instanceof ChoiceNode) {
235 val choice = child as ChoiceNode
236 for (caze : choice.cases) {
237 val result = builder.collectPathArguments(strings, caze as DataNodeContainer);
245 if (targetNode instanceof ChoiceNode) {
249 // Number of consumed elements
251 if (targetNode instanceof ListSchemaNode) {
252 val listNode = targetNode as ListSchemaNode;
253 val keysSize = listNode.keyDefinition.size
255 // every key has to be filled
256 if ((strings.length - consumed) < keysSize) {
259 val uriKeyValues = strings.subList(consumed, consumed + keysSize);
260 val keyValues = new HashMap<QName, Object>();
262 for (key : listNode.keyDefinition) {
263 val uriKeyValue = uriKeyValues.get(i);
265 // key value cannot be NULL
266 if (uriKeyValue.equals(NULL_VALUE)) {
269 keyValues.addKeyValue(listNode.getDataChildByName(key), uriKeyValue);
272 consumed = consumed + i;
273 builder.nodeWithKey(targetNode.QName, keyValues);
276 // Only one instance of node is allowed
277 builder.node(targetNode.QName);
279 if (targetNode instanceof DataNodeContainer) {
280 val remaining = strings.subList(consumed, strings.length);
281 val result = builder.collectPathArguments(remaining, targetNode as DataNodeContainer);
288 private def void addKeyValue(HashMap<QName, Object> map, DataSchemaNode node, String uriValue) {
289 checkNotNull(uriValue);
290 checkArgument(node instanceof LeafSchemaNode);
291 val decoded = URLDecoder.decode(uriValue);
292 map.put(node.QName, decoded);
296 private def String toModuleName(String str) {
298 if (str.contains(":")) {
299 val args = str.split(":");
300 checkArgument(args.size === 2);
307 private def String toNodeName(String str) {
308 if (str.contains(":")) {
309 val args = str.split(":");
310 checkArgument(args.size === 2);
317 private def QName toQName(String name) {
318 val module = name.toModuleName;
319 val node = name.toNodeName;
320 val namespace = moduleNameToUri.get(module);
321 return new QName(namespace, null, node);
324 def getRpcDefinition(String name) {
325 return qnameToRpc.get(name.toQName)
328 override onGlobalContextUpdated(SchemaContext context) {
329 this.schemas = context;
330 for (operation : context.operations) {
331 val qname = new QName(operation.QName.namespace, null, operation.QName.localName);
332 qnameToRpc.put(qname, operation);
337 * Resolve target type from leafref type.
339 * According to RFC 6020 referenced element has to be leaf (chapter 9.9).
340 * Therefore if other element is referenced then null value is returned.
342 * Currently only cases without path-predicate are supported.
346 * data schema node which contains reference
347 * @return type if leaf is referenced and it is possible to find referenced
348 * node in schema context. In other cases null value is returned
350 def LeafSchemaNode resolveTypeFromLeafref(LeafrefTypeDefinition leafRef, DataSchemaNode schemaNode) {
351 val xPath = leafRef.getPathStatement();
352 val module = SchemaContextUtil.findParentModule(schemas, schemaNode);
354 var SchemaNode foundSchemaNode
355 if (xPath.isAbsolute()) {
356 foundSchemaNode = SchemaContextUtil.findDataSchemaNode(schemas, module, xPath);
358 foundSchemaNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemas, module, schemaNode, xPath);
361 if (foundSchemaNode instanceof LeafSchemaNode) {
362 return foundSchemaNode as LeafSchemaNode;