Added case with Choice-case to instance identifier building
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / ControllerContext.xtend
1 package org.opendaylight.controller.sal.restconf.impl
2
3 import com.google.common.collect.BiMap
4 import com.google.common.collect.HashBiMap
5 import java.net.URI
6 import java.net.URLDecoder
7 import java.net.URLEncoder
8 import java.util.HashMap
9 import java.util.List
10 import java.util.Map
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
25
26 import static com.google.common.base.Preconditions.*
27
28 class ControllerContext {
29     
30     val static NULL_VALUE = "null"
31
32     @Property
33     SchemaContext schemas;
34
35     private val BiMap<URI, String> uriToModuleName = HashBiMap.create();
36     private val Map<String, URI> moduleNameToUri = uriToModuleName.inverse();
37
38     public def InstanceIdWithSchemaNode toInstanceIdentifier(String restconfInstance) {
39         val ret = InstanceIdentifier.builder();
40         val pathArgs = restconfInstance.split("/");
41         if (pathArgs.empty) {
42             return null;
43         }
44         if (pathArgs.head.empty) {
45             pathArgs.remove(0)
46         }
47         val schemaNode = ret.collectPathArguments(pathArgs, restconfInstance.findModule);
48         if (schemaNode == null) {
49             return null
50         }
51         new InstanceIdWithSchemaNode(ret.toInstance, schemaNode)
52     }
53
54     private def findModule(String restconfInstance) {
55         checkNotNull(restconfInstance);
56         val pathArgs = restconfInstance.split("/");
57         if (pathArgs.empty) {
58             return null;
59         }
60         val modulWithFirstYangStatement = pathArgs.filter[s|s.contains(":")].head
61         val startModule = modulWithFirstYangStatement.toModuleName();
62         schemas.getLatestModule(startModule)
63     }
64
65     private def getLatestModule(SchemaContext schema, String moduleName) {
66         checkNotNull(schema)
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)) {
72                 latestModule = module
73             }
74         }
75         return latestModule
76     }
77
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));
87         }
88         return ret.toString
89     }
90
91     private def dispatch CharSequence toRestconfIdentifier(NodeIdentifier argument, DataSchemaNode node) {
92         '''/«argument.nodeType.toRestconfIdentifier()»'''
93     }
94
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»'''
99     }
100
101     private def dispatch CharSequence toRestconfIdentifier(PathArgument argument, DataSchemaNode node) {
102         throw new IllegalArgumentException("Conversion of generic path argument is not supported");
103     }
104
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;
112         }
113         return '''«module»:«qname.localName»''';
114     }
115
116     private static dispatch def DataSchemaNode childByQName(ChoiceNode container, QName name) {
117         for (caze : container.cases) {
118             val ret = caze.childByQName(name)
119             if (ret !== null) {
120                 return ret;
121             }
122         }
123         return null;
124     }
125
126     private static dispatch def DataSchemaNode childByQName(ChoiceCaseNode container, QName name) {
127         val ret = container.getDataChildByName(name);
128         return ret;
129     }
130
131     private static dispatch def DataSchemaNode childByQName(ContainerSchemaNode container, QName name) {
132         return container.dataNodeChildByQName(name);
133     }
134
135     private static dispatch def DataSchemaNode childByQName(ListSchemaNode container, QName name) {
136         return container.dataNodeChildByQName(name);
137     }
138
139     private static dispatch def DataSchemaNode childByQName(DataSchemaNode container, QName name) {
140         return null;
141     }
142
143     private static def DataSchemaNode dataNodeChildByQName(DataNodeContainer container, QName name) {
144         var ret = container.getDataChildByName(name);
145         if (ret === null) {
146
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);
152                     if (ret !== null) {
153                         return ret;
154                     }
155                 }
156             }
157         }
158         return ret;
159     }
160
161     private def toUriString(Object object) {
162         if(object == null) return "";
163         return URLEncoder.encode(object.toString)
164     }
165
166     private def DataSchemaNode collectPathArguments(InstanceIdentifierBuilder builder, List<String> strings,
167         DataNodeContainer parentNode) {
168         checkNotNull(strings)
169         if (strings.empty) {
170             return parentNode as DataSchemaNode;
171         }
172         val nodeRef = strings.head;
173
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);
183                         if (result != null)
184                             return result
185                     }
186                 }
187             }
188             return null
189         }
190         if (targetNode instanceof ChoiceNode) {
191             return null
192         }
193
194         // Number of consumed elements
195         var consumed = 1;
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) {
201                 return null;
202             }
203             val uriKeyValues = strings.subList(consumed, consumed + keysSize);
204             val keyValues = new HashMap<QName, Object>();
205             var i = 0;
206             for (key : listNode.keyDefinition) {
207                 val uriKeyValue = uriKeyValues.get(i);
208                 // key value cannot be NULL
209                 if (uriKeyValue.equals(NULL_VALUE)) {
210                     return null
211                 }
212                 keyValues.addKeyValue(listNode.getDataChildByName(key), uriKeyValue);
213                 i = i + 1;
214             }
215             consumed = consumed + i;
216             builder.nodeWithKey(targetNode.QName, keyValues);
217         } else {
218             // Only one instance of node is allowed
219             builder.node(targetNode.QName);
220         }
221         if (targetNode instanceof DataNodeContainer) {
222             val remaining = strings.subList(consumed, strings.length);
223             val result = builder.collectPathArguments(remaining, targetNode as DataNodeContainer);
224             return result
225         }
226
227         return targetNode
228     }
229
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);
235
236     }
237
238     private def String toModuleName(String str) {
239         checkNotNull(str)
240         if (str.contains(":")) {
241             val args = str.split(":");
242             checkArgument(args.size === 2);
243             return args.get(0);
244         } else {
245             return null;
246         }
247     }
248
249     private def String toNodeName(String str) {
250         if (str.contains(":")) {
251             val args = str.split(":");
252             checkArgument(args.size === 2);
253             return args.get(1);
254         } else {
255             return str;
256         }
257     }
258
259     public def QName toRpcQName(String name) {
260     }
261 }