Corrected response status codes from restconf
[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 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec
16 import org.opendaylight.yangtools.yang.model.api.TypeDefinition
17 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode
18 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode
19 import org.opendaylight.yangtools.yang.common.QName
20 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode
21 import static javax.ws.rs.core.Response.Status.*
22
23 class RestconfImpl implements RestconfService {
24
25     val static RestconfImpl INSTANCE = new RestconfImpl
26
27     @Property
28     BrokerFacade broker
29
30     @Property
31     extension ControllerContext controllerContext
32
33     private new() {
34         if (INSTANCE !== null) {
35             throw new IllegalStateException("Already instantiated");
36         }
37     }
38
39     static def getInstance() {
40         return INSTANCE
41     }
42
43     override readAllData() {
44 //        return broker.readOperationalData("".toInstanceIdentifier.getInstanceIdentifier);
45         throw new UnsupportedOperationException("Reading all data is currently not supported.")
46     }
47
48     override getModules() {
49         throw new UnsupportedOperationException("TODO: auto-generated method stub")
50     }
51
52     override getRoot() {
53         return null;
54     }
55
56     override readData(String identifier) {
57         val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier
58         val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier);
59         return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode)
60     }
61
62     override createConfigurationData(String identifier, CompositeNode payload) {
63         val identifierWithSchemaNode = identifier.resolveInstanceIdentifier
64         val value = normalizeNode(payload, identifierWithSchemaNode.schemaNode)
65         val status = broker.commitConfigurationDataPut(identifierWithSchemaNode.instanceIdentifier, value).get();
66         switch status.result {
67             case TransactionStatus.COMMITED: Response.status(NO_CONTENT).build
68             default: Response.status(INTERNAL_SERVER_ERROR).build
69         }
70     }
71
72     override updateConfigurationData(String identifier, CompositeNode payload) {
73         val identifierWithSchemaNode = identifier.resolveInstanceIdentifier
74         val value = normalizeNode(payload, identifierWithSchemaNode.schemaNode)
75         val status = broker.commitConfigurationDataPut(identifierWithSchemaNode.instanceIdentifier, value).get();
76         switch status.result {
77             case TransactionStatus.COMMITED: Response.status(OK).build
78             default: Response.status(INTERNAL_SERVER_ERROR).build
79         }
80     }
81
82     override invokeRpc(String identifier, CompositeNode payload) {
83         val rpc = identifier.rpcDefinition
84         if (rpc === null) {
85             throw new ResponseException(NOT_FOUND, "RPC does not exist.");
86         }
87         val value = normalizeNode(payload, rpc.input)
88         val List<Node<?>> input = new ArrayList
89         input.add(value)
90         val rpcRequest = NodeFactory.createMutableCompositeNode(rpc.QName, null, input, null, null)
91         val rpcResult = broker.invokeRpc(rpc.QName, rpcRequest);
92         if (!rpcResult.successful) {
93             throw new ResponseException(INTERNAL_SERVER_ERROR, "Operation failed")
94         }
95         if (rpcResult.result === null) {
96             return null
97         }
98         return new StructuredData(rpcResult.result, rpc.output)
99     }
100
101     override readConfigurationData(String identifier) {
102         val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier
103         val data = broker.readConfigurationData(instanceIdentifierWithSchemaNode.getInstanceIdentifier);
104         return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode)
105     }
106
107     override readOperationalData(String identifier) {
108         val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier
109         val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier);
110         return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode)
111     }
112
113     override updateConfigurationDataLegacy(String identifier, CompositeNode payload) {
114         updateConfigurationData(identifier, payload);
115     }
116
117     override createConfigurationDataLegacy(String identifier, CompositeNode payload) {
118         createConfigurationData(identifier, payload);
119     }
120
121     private def InstanceIdWithSchemaNode resolveInstanceIdentifier(String identifier) {
122         val identifierWithSchemaNode = identifier.toInstanceIdentifier
123         if (identifierWithSchemaNode === null) {
124             throw new ResponseException(BAD_REQUEST, "URI has bad format");
125         }
126         return identifierWithSchemaNode
127     }
128
129     private def CompositeNode normalizeNode(CompositeNode node, DataSchemaNode schema) {
130         if (node instanceof CompositeNodeWrapper) {
131             normalizeNode(node as CompositeNodeWrapper, schema, null)
132             return (node as CompositeNodeWrapper).unwrap()
133         }
134         return node
135     }
136
137     private def void normalizeNode(NodeWrapper<?> nodeBuilder, DataSchemaNode schema, QName previousAugment) {
138         if (schema === null) {
139             throw new ResponseException(BAD_REQUEST,
140                 "Data has bad format\n" + nodeBuilder.localName + " does not exist in yang schema.");
141         }
142         var validQName = schema.QName
143         var currentAugment = previousAugment;
144         if (schema.augmenting) {
145             currentAugment = schema.QName
146         } else if (previousAugment !== null && schema.QName.namespace !== previousAugment.namespace) {
147             validQName = QName.create(currentAugment, schema.QName.localName);
148         }
149         val moduleName = controllerContext.findModuleByNamespace(validQName.namespace);
150         if (nodeBuilder.namespace === null || nodeBuilder.namespace == validQName.namespace ||
151             nodeBuilder.namespace.path == moduleName) {
152             nodeBuilder.qname = validQName
153         } else {
154             throw new ResponseException(BAD_REQUEST,
155                 "Data has bad format\nIf data is in XML format then namespace for " + nodeBuilder.localName +
156                     " should be " + schema.QName.namespace + ".\n If data is in Json format then module name for " +
157                     nodeBuilder.localName + " should be " + moduleName + ".");
158         }
159
160         if (nodeBuilder instanceof CompositeNodeWrapper) {
161             val List<NodeWrapper<?>> children = (nodeBuilder as CompositeNodeWrapper).getValues
162             for (child : children) {
163                 normalizeNode(child,
164                     findFirstSchemaByLocalName(child.localName, (schema as DataNodeContainer).childNodes),
165                     currentAugment)
166             }
167         } else if (nodeBuilder instanceof SimpleNodeWrapper) {
168             val simpleNode = (nodeBuilder as SimpleNodeWrapper)
169             val stringValue = simpleNode.value as String;
170
171             val objectValue = TypeDefinitionAwareCodec.from(schema.typeDefinition)?.deserialize(stringValue);
172             simpleNode.setValue(objectValue)
173         } else if (nodeBuilder instanceof EmptyNodeWrapper) {
174             val emptyNodeBuilder = nodeBuilder as EmptyNodeWrapper
175             if (schema instanceof LeafSchemaNode) {
176                 emptyNodeBuilder.setComposite(false);
177             } else if (schema instanceof ContainerSchemaNode) {
178
179                 // FIXME: Add presence check
180                 emptyNodeBuilder.setComposite(true);
181             }
182         }
183     }
184
185     private def dispatch TypeDefinition<?> typeDefinition(LeafSchemaNode node) {
186         node.type
187     }
188
189     private def dispatch TypeDefinition<?> typeDefinition(LeafListSchemaNode node) {
190         node.type
191     }
192
193     private def DataSchemaNode findFirstSchemaByLocalName(String localName, Set<DataSchemaNode> schemas) {
194         for (schema : schemas) {
195             if (schema instanceof ChoiceNode) {
196                 for (caze : (schema as ChoiceNode).cases) {
197                     val result = findFirstSchemaByLocalName(localName, caze.childNodes)
198                     if (result !== null) {
199                         return result
200                     }
201                 }
202             } else {
203                 val result = schemas.findFirst[n|n.QName.localName.equals(localName)]
204                 if (result !== null) {
205                     return result;
206
207                 }
208             }
209         }
210         return null
211     }
212
213 }