Updated RESTCONF implementation
[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 org.opendaylight.yangtools.yang.model.api.SchemaContext
4 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
5 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument
6 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier
7 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates
8 import java.net.URI
9 import java.util.Map
10 import java.util.HashMap
11 import org.opendaylight.yangtools.yang.common.QName
12 import org.opendaylight.yangtools.yang.model.api.ChoiceNode
13 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode
14 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode
15 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode
16 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode
17
18 import java.net.URLEncoder
19 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer
20 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.InstanceIdentifierBuilder
21 import java.util.List
22 import static com.google.common.base.Preconditions.*;
23 import com.google.common.collect.Collections2
24 import com.google.common.collect.BiMap
25 import com.google.common.collect.HashBiMap
26 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode
27 import java.net.URLDecoder
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 InstanceIdentifier toInstanceIdentifier(String restconfInstance) {
38         val ret = InstanceIdentifier.builder();
39         val pathArgs = restconfInstance.split("/");
40         val first = pathArgs.get(0);
41         val startModule = first.toModuleName();
42         val module = schemas.findModuleByNamespace(moduleNameToUri.get(startModule));
43         checkArgument(module.size == 1); // Only one version supported now
44         ret.collectPathArguments(pathArgs, module.iterator.next);
45         return ret.toInstance
46     }
47
48     def String toFullRestconfIdentifier(InstanceIdentifier path) {
49         val elements = path.path;
50         val ret = new StringBuilder();
51         val startQName = elements.get(0).nodeType;
52         val initialModule = schemas.findModuleByNamespaceAndRevision(startQName.namespace, startQName.revision)
53         var node = initialModule as DataSchemaNode;
54         for (element : elements) {
55             node = node.childByQName(element.nodeType);
56             ret.append(element.toRestconfIdentifier(node));
57         }
58         return ret.toString
59     }
60
61     private def dispatch CharSequence toRestconfIdentifier(NodeIdentifier argument, DataSchemaNode node) {
62         '''/«argument.nodeType.toRestconfIdentifier()»'''
63     }
64
65     private def dispatch CharSequence toRestconfIdentifier(NodeIdentifierWithPredicates argument, ListSchemaNode node) {
66         val nodeIdentifier = argument.nodeType.toRestconfIdentifier();
67         val keyValues = argument.keyValues;
68         return '''/«nodeIdentifier»/«FOR key : node.keyDefinition SEPARATOR "/"»«keyValues.get(key).toUriString»«ENDFOR»'''
69     }
70
71     private def dispatch CharSequence toRestconfIdentifier(PathArgument argument, DataSchemaNode node) {
72         throw new IllegalArgumentException("Conversion of generic path argument is not supported");
73     }
74
75     public def CharSequence toRestconfIdentifier(QName qname) {
76         var module = uriToModuleName.get(qname.namespace)
77         if (module == null) {
78             val moduleSchema = schemas.findModuleByNamespaceAndRevision(qname.namespace, qname.revision);
79             if(moduleSchema == null) throw new IllegalArgumentException()
80             uriToModuleName.put(qname.namespace, moduleSchema.name)
81             module = moduleSchema.name;
82         }
83         return '''«module»:«qname.localName»''';
84     }
85
86     private static dispatch def DataSchemaNode childByQName(ChoiceNode container, QName name) {
87         for (caze : container.cases) {
88             val ret = caze.childByQName(name)
89             if (ret !== null) {
90                 return ret;
91             }
92         }
93         return null;
94     }
95
96     private static dispatch def DataSchemaNode childByQName(ChoiceCaseNode container, QName name) {
97         val ret = container.getDataChildByName(name);
98         return ret;
99     }
100
101     private static dispatch def DataSchemaNode childByQName(ContainerSchemaNode container, QName name) {
102         return container.dataNodeChildByQName(name);
103     }
104
105     private static dispatch def DataSchemaNode childByQName(ListSchemaNode container, QName name) {
106         return container.dataNodeChildByQName(name);
107     }
108
109     private static dispatch def DataSchemaNode childByQName(DataSchemaNode container, QName name) {
110         return null;
111     }
112
113     private static def DataSchemaNode dataNodeChildByQName(DataNodeContainer container, QName name) {
114         var ret = container.getDataChildByName(name);
115         if (ret === null) {
116
117             // Find in Choice Cases
118             for (node : container.childNodes) {
119                 if (node instanceof ChoiceCaseNode) {
120                     val caseNode = (node as ChoiceCaseNode);
121                     ret = caseNode.childByQName(name);
122                     if (ret !== null) {
123                         return ret;
124                     }
125                 }
126             }
127         }
128         return ret;
129     }
130
131     private def toUriString(Object object) {
132         if(object == null) return "";
133         return URLEncoder.encode(object.toString)
134     }
135
136     def void collectPathArguments(InstanceIdentifierBuilder builder, List<String> strings, DataNodeContainer parentNode) {
137         checkNotNull(strings)
138         if (strings.length == 0) {
139             return;
140         }
141         val nodeRef = strings.get(0);
142
143         //val moduleName = nodeRef.toModuleName();
144         val nodeName = nodeRef.toNodeName();
145         val naiveTargetNode = parentNode.getDataChildByName(nodeName);
146
147         //var URI namespace;
148         var DataSchemaNode targetNode = naiveTargetNode;
149
150         /*if(moduleName !== null) {
151             namespace = moduleNameToUri.get(moduleName);
152             
153         }*/
154         // Number of consumed elements
155         var consumed = 1;
156         if (targetNode instanceof ListSchemaNode) {
157             val listNode = targetNode as ListSchemaNode;
158             val keysSize = listNode.keyDefinition.size
159             val uriKeyValues = strings.subList(1, keysSize);
160             val keyValues = new HashMap<QName, Object>();
161             var i = 0;
162             for (key : listNode.keyDefinition) {
163                 val uriKeyValue = uriKeyValues.get(i);
164                 keyValues.addKeyValue(listNode.getDataChildByName(key), uriKeyValue);
165                 i = i + 1;
166             }
167             consumed = consumed + i;
168             builder.nodeWithKey(targetNode.QName, keyValues);
169         } else {
170
171             // Only one instance of node is allowed
172             builder.node(targetNode.QName);
173         }
174         if (targetNode instanceof DataNodeContainer) {
175             val remaining = strings.subList(consumed, strings.length - 1);
176             builder.collectPathArguments(remaining, targetNode as DataNodeContainer);
177         }
178     }
179
180     def void addKeyValue(HashMap<QName, Object> map, DataSchemaNode node, String uriValue) {
181         checkNotNull(uriValue);
182         checkArgument(node instanceof LeafSchemaNode);
183         val decoded = URLDecoder.decode(uriValue);
184         map.put(node.QName, decoded);
185
186     }
187
188     def String toModuleName(String str) {
189         if (str.contains(":")) {
190             val args = str.split(":");
191             checkArgument(args.size === 2);
192             return args.get(0);
193         } else {
194             return null;
195         }
196     }
197
198     def String toNodeName(String str) {
199         if (str.contains(":")) {
200             val args = str.split(":");
201             checkArgument(args.size === 2);
202             return args.get(1);
203         } else {
204             return str;
205         }
206     }
207     
208     public def QName toRpcQName(String name) {
209         
210         
211     }
212 }