Solved bugs and added tests in sal-rest-connector
[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 import java.util.Date
28
29 class ControllerContext {
30
31     @Property
32     SchemaContext schemas;
33
34     private val BiMap<URI, String> uriToModuleName = HashBiMap.create();
35     private val Map<String, URI> moduleNameToUri = uriToModuleName.inverse();
36
37     public def InstanceIdWithSchemaNode toInstanceIdentifier(String restconfInstance) {
38         val ret = InstanceIdentifier.builder();
39         val pathArgs = restconfInstance.split("/");
40         if (pathArgs.empty) {
41             return null;
42         }
43         val schemaNode = ret.collectPathArguments(pathArgs, restconfInstance.findModule);
44         new InstanceIdWithSchemaNode(ret.toInstance, schemaNode)
45     }
46
47     def findModule(String restconfInstance) {
48         checkNotNull(restconfInstance);
49         val pathArgs = restconfInstance.split("/");
50         if (pathArgs.empty) {
51             return null;
52         }
53         val modulWithFirstYangStatement = pathArgs.filter[s|s.contains(":")].head
54         val startModule = modulWithFirstYangStatement.toModuleName();
55         schemas.getLatestModule(startModule)
56     }
57
58     private def getLatestModule(SchemaContext schema, String moduleName) {
59         checkNotNull(schema)
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)) {
65                 latestModule = module
66             }
67         }
68         return latestModule
69     }
70
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));
80         }
81         return ret.toString
82     }
83
84     private def dispatch CharSequence toRestconfIdentifier(NodeIdentifier argument, DataSchemaNode node) {
85         '''/«argument.nodeType.toRestconfIdentifier()»'''
86     }
87
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»'''
92     }
93
94     private def dispatch CharSequence toRestconfIdentifier(PathArgument argument, DataSchemaNode node) {
95         throw new IllegalArgumentException("Conversion of generic path argument is not supported");
96     }
97
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;
105         }
106         return '''«module»:«qname.localName»''';
107     }
108
109     private static dispatch def DataSchemaNode childByQName(ChoiceNode container, QName name) {
110         for (caze : container.cases) {
111             val ret = caze.childByQName(name)
112             if (ret !== null) {
113                 return ret;
114             }
115         }
116         return null;
117     }
118
119     private static dispatch def DataSchemaNode childByQName(ChoiceCaseNode container, QName name) {
120         val ret = container.getDataChildByName(name);
121         return ret;
122     }
123
124     private static dispatch def DataSchemaNode childByQName(ContainerSchemaNode container, QName name) {
125         return container.dataNodeChildByQName(name);
126     }
127
128     private static dispatch def DataSchemaNode childByQName(ListSchemaNode container, QName name) {
129         return container.dataNodeChildByQName(name);
130     }
131
132     private static dispatch def DataSchemaNode childByQName(DataSchemaNode container, QName name) {
133         return null;
134     }
135
136     private static def DataSchemaNode dataNodeChildByQName(DataNodeContainer container, QName name) {
137         var ret = container.getDataChildByName(name);
138         if (ret === null) {
139
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);
145                     if (ret !== null) {
146                         return ret;
147                     }
148                 }
149             }
150         }
151         return ret;
152     }
153
154     private def toUriString(Object object) {
155         if(object == null) return "";
156         return URLEncoder.encode(object.toString)
157     }
158
159     def DataSchemaNode collectPathArguments(InstanceIdentifierBuilder builder, List<String> strings,
160         DataNodeContainer parentNode) {
161         checkNotNull(strings)
162         if (strings.empty) {
163             return parentNode as DataSchemaNode;
164         }
165         val nodeRef = strings.head;
166
167         //val moduleName = nodeRef.toModuleName();
168         val nodeName = nodeRef.toNodeName();
169         val targetNode = parentNode.getDataChildByName(nodeName);
170         if (targetNode == null) {
171             return null
172         }
173
174         // Number of consumed elements
175         var consumed = 1;
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>();
181             var i = 0;
182             for (key : listNode.keyDefinition) {
183                 val uriKeyValue = uriKeyValues.get(i);
184                 keyValues.addKeyValue(listNode.getDataChildByName(key), uriKeyValue);
185                 i = i + 1;
186             }
187             consumed = consumed + i;
188             builder.nodeWithKey(targetNode.QName, keyValues);
189         } else {
190
191             // Only one instance of node is allowed
192             builder.node(targetNode.QName);
193         }
194         if (targetNode instanceof DataNodeContainer) {
195             val remaining = strings.subList(consumed, strings.length);
196             val result = builder.collectPathArguments(remaining, targetNode as DataNodeContainer);
197             return result
198         }
199
200         return targetNode
201     }
202
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);
208
209     }
210
211     def String toModuleName(String str) {
212         checkNotNull(str)
213         if (str.contains(":")) {
214             val args = str.split(":");
215             checkArgument(args.size === 2);
216             return args.get(0);
217         } else {
218             return null;
219         }
220     }
221
222     def String toNodeName(String str) {
223         if (str.contains(":")) {
224             val args = str.split(":");
225             checkArgument(args.size === 2);
226             return args.get(1);
227         } else {
228             return str;
229         }
230     }
231
232     public def QName toRpcQName(String name) {
233     }
234 }