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.*
27 import org.opendaylight.controller.sal.core.api.model.SchemaServiceListener
29 class ControllerContext implements SchemaServiceListener {
31 val static ControllerContext INSTANCE = new ControllerContext
33 val static NULL_VALUE = "null"
36 SchemaContext schemas;
38 private val BiMap<URI, String> uriToModuleName = HashBiMap.create();
39 private val Map<String, URI> moduleNameToUri = uriToModuleName.inverse();
42 if (INSTANCE != null) {
43 throw new IllegalStateException("Already instantiated");
47 static def getInstance() {
51 public def InstanceIdWithSchemaNode toInstanceIdentifier(String restconfInstance) {
52 val ret = InstanceIdentifier.builder();
53 val pathArgs = restconfInstance.split("/");
57 if (pathArgs.head.empty) {
60 val schemaNode = ret.collectPathArguments(pathArgs, restconfInstance.findModule);
61 if (schemaNode == null) {
64 new InstanceIdWithSchemaNode(ret.toInstance, schemaNode)
67 private def findModule(String restconfInstance) {
68 checkNotNull(restconfInstance);
69 val pathArgs = restconfInstance.split("/");
73 val modulWithFirstYangStatement = pathArgs.filter[s|s.contains(":")].head
74 val startModule = modulWithFirstYangStatement.toModuleName();
75 schemas.getLatestModule(startModule)
78 private def getLatestModule(SchemaContext schema, String moduleName) {
80 checkArgument(moduleName != null && !moduleName.empty)
81 val modules = schema.modules.filter[m|m.name == moduleName]
82 var latestModule = modules.head
83 for (module : modules) {
84 if (module.revision.after(latestModule.revision)) {
91 def String toFullRestconfIdentifier(InstanceIdentifier path) {
92 val elements = path.path;
93 val ret = new StringBuilder();
94 val startQName = elements.get(0).nodeType;
95 val initialModule = schemas.findModuleByNamespaceAndRevision(startQName.namespace, startQName.revision)
96 var node = initialModule as DataSchemaNode;
97 for (element : elements) {
98 node = node.childByQName(element.nodeType);
99 ret.append(element.toRestconfIdentifier(node));
104 private def dispatch CharSequence toRestconfIdentifier(NodeIdentifier argument, DataSchemaNode node) {
105 '''/«argument.nodeType.toRestconfIdentifier()»'''
108 private def dispatch CharSequence toRestconfIdentifier(NodeIdentifierWithPredicates argument, ListSchemaNode node) {
109 val nodeIdentifier = argument.nodeType.toRestconfIdentifier();
110 val keyValues = argument.keyValues;
111 return '''/«nodeIdentifier»/«FOR key : node.keyDefinition SEPARATOR "/"»«keyValues.get(key).toUriString»«ENDFOR»'''
114 private def dispatch CharSequence toRestconfIdentifier(PathArgument argument, DataSchemaNode node) {
115 throw new IllegalArgumentException("Conversion of generic path argument is not supported");
118 def CharSequence toRestconfIdentifier(QName qname) {
119 var module = uriToModuleName.get(qname.namespace)
120 if (module == null) {
121 val moduleSchema = schemas.findModuleByNamespaceAndRevision(qname.namespace, qname.revision);
122 if(moduleSchema == null) throw new IllegalArgumentException()
123 uriToModuleName.put(qname.namespace, moduleSchema.name)
124 module = moduleSchema.name;
126 return '''«module»:«qname.localName»''';
129 private static dispatch def DataSchemaNode childByQName(ChoiceNode container, QName name) {
130 for (caze : container.cases) {
131 val ret = caze.childByQName(name)
139 private static dispatch def DataSchemaNode childByQName(ChoiceCaseNode container, QName name) {
140 val ret = container.getDataChildByName(name);
144 private static dispatch def DataSchemaNode childByQName(ContainerSchemaNode container, QName name) {
145 return container.dataNodeChildByQName(name);
148 private static dispatch def DataSchemaNode childByQName(ListSchemaNode container, QName name) {
149 return container.dataNodeChildByQName(name);
152 private static dispatch def DataSchemaNode childByQName(DataSchemaNode container, QName name) {
156 private static def DataSchemaNode dataNodeChildByQName(DataNodeContainer container, QName name) {
157 var ret = container.getDataChildByName(name);
160 // Find in Choice Cases
161 for (node : container.childNodes) {
162 if (node instanceof ChoiceCaseNode) {
163 val caseNode = (node as ChoiceCaseNode);
164 ret = caseNode.childByQName(name);
174 private def toUriString(Object object) {
175 if(object == null) return "";
176 return URLEncoder.encode(object.toString)
179 private def DataSchemaNode collectPathArguments(InstanceIdentifierBuilder builder, List<String> strings,
180 DataNodeContainer parentNode) {
181 checkNotNull(strings)
183 return parentNode as DataSchemaNode;
185 val nodeRef = strings.head;
187 val nodeName = nodeRef.toNodeName();
188 val targetNode = parentNode.getDataChildByName(nodeName);
189 if (targetNode == null) {
190 val children = parentNode.childNodes
191 for (child : children) {
192 if (child instanceof ChoiceNode) {
193 val choice = child as ChoiceNode
194 for (caze : choice.cases) {
195 val result = builder.collectPathArguments(strings, caze as DataNodeContainer);
203 if (targetNode instanceof ChoiceNode) {
207 // Number of consumed elements
209 if (targetNode instanceof ListSchemaNode) {
210 val listNode = targetNode as ListSchemaNode;
211 val keysSize = listNode.keyDefinition.size
213 // every key has to be filled
214 if ((strings.length - consumed) < keysSize) {
217 val uriKeyValues = strings.subList(consumed, consumed + keysSize);
218 val keyValues = new HashMap<QName, Object>();
220 for (key : listNode.keyDefinition) {
221 val uriKeyValue = uriKeyValues.get(i);
223 // key value cannot be NULL
224 if (uriKeyValue.equals(NULL_VALUE)) {
227 keyValues.addKeyValue(listNode.getDataChildByName(key), uriKeyValue);
230 consumed = consumed + i;
231 builder.nodeWithKey(targetNode.QName, keyValues);
234 // Only one instance of node is allowed
235 builder.node(targetNode.QName);
237 if (targetNode instanceof DataNodeContainer) {
238 val remaining = strings.subList(consumed, strings.length);
239 val result = builder.collectPathArguments(remaining, targetNode as DataNodeContainer);
246 private def void addKeyValue(HashMap<QName, Object> map, DataSchemaNode node, String uriValue) {
247 checkNotNull(uriValue);
248 checkArgument(node instanceof LeafSchemaNode);
249 val decoded = URLDecoder.decode(uriValue);
250 map.put(node.QName, decoded);
254 private def String toModuleName(String str) {
256 if (str.contains(":")) {
257 val args = str.split(":");
258 checkArgument(args.size === 2);
265 private def String toNodeName(String str) {
266 if (str.contains(":")) {
267 val args = str.split(":");
268 checkArgument(args.size === 2);
275 public def QName toRpcQName(String name) {
278 override onGlobalContextUpdated(SchemaContext context) {
279 this.schemas = context;