RPC input is wrapped into RPC name element
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / RestconfImpl.xtend
1 package org.opendaylight.controller.sal.restconf.impl
2
3 import java.util.ArrayList
4 import java.util.List
5 import java.util.Set
6 import javax.ws.rs.core.Response
7 import org.opendaylight.controller.md.sal.common.api.TransactionStatus
8 import org.opendaylight.controller.sal.rest.api.RestconfService
9 import org.opendaylight.yangtools.yang.data.api.CompositeNode
10 import org.opendaylight.yangtools.yang.data.api.Node
11 import org.opendaylight.yangtools.yang.data.impl.NodeFactory
12 import org.opendaylight.yangtools.yang.model.api.ChoiceNode
13 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer
14 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode
15
16 class RestconfImpl implements RestconfService {
17     
18     val static RestconfImpl INSTANCE = new RestconfImpl
19
20     @Property
21     BrokerFacade broker
22
23     @Property
24     extension ControllerContext controllerContext
25     
26     private new() {
27         if (INSTANCE !== null) {
28             throw new IllegalStateException("Already instantiated");
29         }
30     }
31     
32     static def getInstance() {
33         return INSTANCE
34     }
35
36     override readAllData() {
37 //        return broker.readOperationalData("".toInstanceIdentifier.getInstanceIdentifier);
38         throw new UnsupportedOperationException("Reading all data is currently not supported.")
39     }
40
41     override getModules() {
42         throw new UnsupportedOperationException("TODO: auto-generated method stub")
43     }
44
45     override getRoot() {
46         return null;
47     }
48
49     override readData(String identifier) {
50         val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier
51         val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier);
52         return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode)
53     }
54
55     override createConfigurationData(String identifier, CompositeNode payload) {
56         val identifierWithSchemaNode = identifier.resolveInstanceIdentifier
57         val value = resolveNodeNamespaceBySchema(payload, identifierWithSchemaNode.schemaNode)
58         val status = broker.commitConfigurationDataPut(identifierWithSchemaNode.instanceIdentifier,value).get();
59         switch status.result {
60             case TransactionStatus.COMMITED: Response.status(Response.Status.OK).build
61             default: Response.status(Response.Status.INTERNAL_SERVER_ERROR).build
62         }
63     }
64
65     override updateConfigurationData(String identifier, CompositeNode payload) {
66         val identifierWithSchemaNode = identifier.resolveInstanceIdentifier
67         val value = resolveNodeNamespaceBySchema(payload, identifierWithSchemaNode.schemaNode)
68         val status = broker.commitConfigurationDataPut(identifierWithSchemaNode.instanceIdentifier,value).get();
69         switch status.result {
70             case TransactionStatus.COMMITED: Response.status(Response.Status.NO_CONTENT).build
71             default: Response.status(Response.Status.INTERNAL_SERVER_ERROR).build
72         }
73     }
74
75     override invokeRpc(String identifier, CompositeNode payload) {
76         val rpc = identifier.rpcDefinition
77         if (rpc === null) {
78             throw new ResponseException(Response.Status.NOT_FOUND, "RPC does not exist.");
79         }
80         val value = resolveNodeNamespaceBySchema(payload, rpc.input)
81         val List<Node<?>> input = new ArrayList
82         input.add(value)
83         val rpcRequest = NodeFactory.createMutableCompositeNode(rpc.QName, null, input, null, null)
84         val rpcResult = broker.invokeRpc(rpc.QName, rpcRequest);
85         return new StructuredData(rpcResult.result, rpc.output);
86     }
87     
88     override readConfigurationData(String identifier) {
89         val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier
90         val data = broker.readConfigurationData(instanceIdentifierWithSchemaNode.getInstanceIdentifier);
91         return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode)
92     }
93     
94     override readOperationalData(String identifier) {
95         val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier
96         val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier);
97         return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode)
98     }
99     
100     override updateConfigurationDataLegacy(String identifier, CompositeNode payload) {
101         updateConfigurationData(identifier,payload);
102     }
103     
104     override createConfigurationDataLegacy(String identifier, CompositeNode payload) {
105         createConfigurationData(identifier,payload);
106     }
107     
108     private def InstanceIdWithSchemaNode resolveInstanceIdentifier(String identifier) {
109         val identifierWithSchemaNode = identifier.toInstanceIdentifier
110         if (identifierWithSchemaNode === null) {
111             throw new ResponseException(Response.Status.BAD_REQUEST, "URI has bad format");
112         }
113         return identifierWithSchemaNode
114     }
115     
116     private def CompositeNode resolveNodeNamespaceBySchema(CompositeNode node, DataSchemaNode schema) {
117         if (node instanceof CompositeNodeWrapper) {
118             addNamespaceToNodeFromSchemaRecursively(node as CompositeNodeWrapper, schema)
119             return (node as CompositeNodeWrapper).unwrap()
120         }
121         return node
122     }
123
124     private def void addNamespaceToNodeFromSchemaRecursively(NodeWrapper<?> nodeBuilder, DataSchemaNode schema) {
125         if (schema === null) {
126             throw new ResponseException(Response.Status.BAD_REQUEST,
127                 "Data has bad format\n" + nodeBuilder.localName + " does not exist in yang schema.");
128         }
129         val moduleName = controllerContext.findModuleByNamespace(schema.QName.namespace);
130         if (nodeBuilder.namespace === null || nodeBuilder.namespace == schema.QName.namespace ||
131             nodeBuilder.namespace.path == moduleName) {
132             nodeBuilder.qname = schema.QName
133         } else {
134             throw new ResponseException(Response.Status.BAD_REQUEST,
135                 "Data has bad format\nIf data is in XML format then namespace for " + nodeBuilder.localName +
136                     " should be " + schema.QName.namespace + ".\n If data is in Json format then module name for " +
137                     nodeBuilder.localName + " should be " + moduleName + ".");
138         }
139         if (nodeBuilder instanceof CompositeNodeWrapper) {
140             val List<NodeWrapper<?>> children = (nodeBuilder as CompositeNodeWrapper).getValues
141             for (child : children) {
142                 addNamespaceToNodeFromSchemaRecursively(child,
143                     findFirstSchemaByLocalName(child.localName, (schema as DataNodeContainer).childNodes))
144             }
145         }
146     }
147     
148     private def DataSchemaNode findFirstSchemaByLocalName(String localName, Set<DataSchemaNode> schemas) {
149         for (schema : schemas) {
150             if (schema instanceof ChoiceNode) {
151                 for (caze : (schema as ChoiceNode).cases) {
152                     val result =  findFirstSchemaByLocalName(localName, caze.childNodes)
153                     if (result !== null) {
154                         return result
155                     }
156                 }
157             } else {
158                 return schemas.findFirst[n|n.QName.localName.equals(localName)]
159             }
160         }
161         return null
162     }
163
164 }