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
31 import static com.google.common.base.Preconditions.*
33 class ControllerContext implements SchemaServiceListener {
35 val static ControllerContext INSTANCE = new ControllerContext
37 val static NULL_VALUE = "null"
39 var SchemaContext schemas;
41 private val BiMap<URI, String> uriToModuleName = HashBiMap.create();
42 private val Map<String, URI> moduleNameToUri = uriToModuleName.inverse();
43 private val Map<QName,RpcDefinition> qnameToRpc = new ConcurrentHashMap();
47 if (INSTANCE !== null) {
48 throw new IllegalStateException("Already instantiated");
52 static def getInstance() {
56 private def void checkPreconditions() {
57 if (schemas === null) {
58 throw new ResponseException(Response.Status.SERVICE_UNAVAILABLE, RestconfProvider::NOT_INITALIZED_MSG)
62 def setSchemas(SchemaContext schemas) {
63 onGlobalContextUpdated(schemas)
66 public def InstanceIdWithSchemaNode toInstanceIdentifier(String restconfInstance) {
67 val ret = InstanceIdentifier.builder();
68 val pathArgs = restconfInstance.split("/");
72 if (pathArgs.head.empty) {
75 val schemaNode = ret.collectPathArguments(pathArgs, restconfInstance.findModule);
76 if (schemaNode === null) {
79 return new InstanceIdWithSchemaNode(ret.toInstance, schemaNode)
82 private def findModule(String restconfInstance) {
84 checkNotNull(restconfInstance);
85 val pathArgs = restconfInstance.split("/");
89 val modulWithFirstYangStatement = pathArgs.filter[s|s.contains(":")].head
90 val startModule = modulWithFirstYangStatement.toModuleName();
91 return getLatestModule(startModule)
94 private def getLatestModule(String moduleName) {
96 checkArgument(moduleName !== null && !moduleName.empty)
97 val modules = schemas.modules.filter[m|m.name == moduleName]
98 var latestModule = modules.head
99 for (module : modules) {
100 if (module.revision.after(latestModule.revision)) {
101 latestModule = module
107 def String toFullRestconfIdentifier(InstanceIdentifier path) {
109 val elements = path.path;
110 val ret = new StringBuilder();
111 val startQName = elements.get(0).nodeType;
112 val initialModule = schemas.findModuleByNamespaceAndRevision(startQName.namespace, startQName.revision)
113 var node = initialModule as DataSchemaNode;
114 for (element : elements) {
115 node = node.childByQName(element.nodeType);
116 ret.append(element.toRestconfIdentifier(node));
121 private def dispatch CharSequence toRestconfIdentifier(NodeIdentifier argument, DataSchemaNode node) {
122 '''/«argument.nodeType.toRestconfIdentifier()»'''
125 private def dispatch CharSequence toRestconfIdentifier(NodeIdentifierWithPredicates argument, ListSchemaNode node) {
126 val nodeIdentifier = argument.nodeType.toRestconfIdentifier();
127 val keyValues = argument.keyValues;
128 return '''/«nodeIdentifier»/«FOR key : node.keyDefinition SEPARATOR "/"»«keyValues.get(key).toUriString»«ENDFOR»'''
131 private def dispatch CharSequence toRestconfIdentifier(PathArgument argument, DataSchemaNode node) {
132 throw new IllegalArgumentException("Conversion of generic path argument is not supported");
135 def findModuleByNamespace(URI namespace) {
137 var module = uriToModuleName.get(namespace)
138 if (module === null) {
139 val moduleSchemas = schemas.findModuleByNamespace(namespace);
140 if(moduleSchemas === null) throw new IllegalArgumentException()
141 var latestModule = moduleSchemas.head
142 for (m : moduleSchemas) {
143 if (m.revision.after(latestModule.revision)) {
147 if(latestModule === null) throw new IllegalArgumentException()
148 uriToModuleName.put(namespace, latestModule.name)
149 module = latestModule.name;
154 def CharSequence toRestconfIdentifier(QName qname) {
156 var module = uriToModuleName.get(qname.namespace)
157 if (module === null) {
158 val moduleSchema = schemas.findModuleByNamespaceAndRevision(qname.namespace, qname.revision);
159 if(moduleSchema === null) throw new IllegalArgumentException()
160 uriToModuleName.put(qname.namespace, moduleSchema.name)
161 module = moduleSchema.name;
163 return '''«module»:«qname.localName»''';
166 private static dispatch def DataSchemaNode childByQName(ChoiceNode container, QName name) {
167 for (caze : container.cases) {
168 val ret = caze.childByQName(name)
176 private static dispatch def DataSchemaNode childByQName(ChoiceCaseNode container, QName name) {
177 val ret = container.getDataChildByName(name);
181 private static dispatch def DataSchemaNode childByQName(ContainerSchemaNode container, QName name) {
182 return container.dataNodeChildByQName(name);
185 private static dispatch def DataSchemaNode childByQName(ListSchemaNode container, QName name) {
186 return container.dataNodeChildByQName(name);
189 private static dispatch def DataSchemaNode childByQName(DataSchemaNode container, QName name) {
193 private static def DataSchemaNode dataNodeChildByQName(DataNodeContainer container, QName name) {
194 var ret = container.getDataChildByName(name);
197 // Find in Choice Cases
198 for (node : container.childNodes) {
199 if (node instanceof ChoiceCaseNode) {
200 val caseNode = (node as ChoiceCaseNode);
201 ret = caseNode.childByQName(name);
211 private def toUriString(Object object) {
212 if(object === null) return "";
213 return URLEncoder.encode(object.toString)
216 private def DataSchemaNode collectPathArguments(InstanceIdentifierBuilder builder, List<String> strings,
217 DataNodeContainer parentNode) {
218 checkNotNull(strings)
219 if (parentNode === null) {
223 return parentNode as DataSchemaNode;
225 val nodeRef = strings.head;
227 val nodeName = nodeRef.toNodeName();
228 val targetNode = parentNode.getDataChildByName(nodeName);
229 if (targetNode === null) {
230 val children = parentNode.childNodes
231 for (child : children) {
232 if (child instanceof ChoiceNode) {
233 val choice = child as ChoiceNode
234 for (caze : choice.cases) {
235 val result = builder.collectPathArguments(strings, caze as DataNodeContainer);
243 if (targetNode instanceof ChoiceNode) {
247 // Number of consumed elements
249 if (targetNode instanceof ListSchemaNode) {
250 val listNode = targetNode as ListSchemaNode;
251 val keysSize = listNode.keyDefinition.size
253 // every key has to be filled
254 if ((strings.length - consumed) < keysSize) {
257 val uriKeyValues = strings.subList(consumed, consumed + keysSize);
258 val keyValues = new HashMap<QName, Object>();
260 for (key : listNode.keyDefinition) {
261 val uriKeyValue = uriKeyValues.get(i);
263 // key value cannot be NULL
264 if (uriKeyValue.equals(NULL_VALUE)) {
267 keyValues.addKeyValue(listNode.getDataChildByName(key), uriKeyValue);
270 consumed = consumed + i;
271 builder.nodeWithKey(targetNode.QName, keyValues);
274 // Only one instance of node is allowed
275 builder.node(targetNode.QName);
277 if (targetNode instanceof DataNodeContainer) {
278 val remaining = strings.subList(consumed, strings.length);
279 val result = builder.collectPathArguments(remaining, targetNode as DataNodeContainer);
286 private def void addKeyValue(HashMap<QName, Object> map, DataSchemaNode node, String uriValue) {
287 checkNotNull(uriValue);
288 checkArgument(node instanceof LeafSchemaNode);
289 val decoded = URLDecoder.decode(uriValue);
290 map.put(node.QName, decoded);
294 private def String toModuleName(String str) {
296 if (str.contains(":")) {
297 val args = str.split(":");
298 checkArgument(args.size === 2);
305 private def String toNodeName(String str) {
306 if (str.contains(":")) {
307 val args = str.split(":");
308 checkArgument(args.size === 2);
315 private def QName toQName(String name) {
316 val module = name.toModuleName;
317 val node = name.toNodeName;
318 val namespace = moduleNameToUri.get(module);
319 return new QName(namespace,null,node);
322 def getRpcDefinition(String name) {
323 return qnameToRpc.get(name.toQName)
326 override onGlobalContextUpdated(SchemaContext context) {
327 this.schemas = context;
328 for(operation : context.operations) {
329 val qname = new QName(operation.QName.namespace,null,operation.QName.localName);
330 qnameToRpc.put(qname,operation);