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