X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2FRestconfImpl.xtend;h=a4587fa787fdff3af7b5dedab4b22505375fe37c;hb=f062dc05cc7caaf0c1811856370f1c9e2f1e5c34;hp=f75c12dc7e29a6151578e77241ba0a96dbc0e106;hpb=ec885a4eba20a0b75fb74f93535398da75d43e48;p=controller.git diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.xtend b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.xtend index f75c12dc7e..a4587fa787 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.xtend +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.xtend @@ -1,63 +1,149 @@ package org.opendaylight.controller.sal.restconf.impl -import org.opendaylight.controller.sal.core.api.model.SchemaService +import java.util.List +import javax.ws.rs.core.Response +import org.opendaylight.controller.sal.rest.api.RestconfService import org.opendaylight.yangtools.yang.data.api.CompositeNode - -import static com.google.common.base.Preconditions.* +import org.opendaylight.yangtools.yang.model.api.DataNodeContainer +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode +import org.opendaylight.controller.md.sal.common.api.TransactionStatus +import javax.ws.rs.WebApplicationException class RestconfImpl implements RestconfService { + val static RestconfImpl INSTANCE = new RestconfImpl + @Property BrokerFacade broker - + @Property extension ControllerContext controllerContext - val JsonMapper jsonMapper = new JsonMapper; - - def init(SchemaService schemaService) { - checkState(broker !== null) - checkState(controllerContext !== null) - checkState(schemaService !== null) - controllerContext.schemas = schemaService.globalContext + private new() { + if (INSTANCE !== null) { + throw new IllegalStateException("Already instantiated"); + } } + static def getInstance() { + return INSTANCE + } + override readAllData() { - return broker.readOperationalData("".removePrefixes.toInstanceIdentifier.getInstanceIdentifier); +// return broker.readOperationalData("".toInstanceIdentifier.getInstanceIdentifier); + throw new UnsupportedOperationException("Reading all data is currently not supported.") } - - + override getModules() { throw new UnsupportedOperationException("TODO: auto-generated method stub") } - + override getRoot() { - throw new UnsupportedOperationException("TODO: auto-generated method stub") - + return null; } - + override readData(String identifier) { - val instanceIdentifierWithSchemaNode = identifier.removePrefixes.toInstanceIdentifier - val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier); - jsonMapper.convert(instanceIdentifierWithSchemaNode.getSchemaNode, data) + val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier + val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier); + return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode) } - + override createConfigurationData(String identifier, CompositeNode payload) { - return broker.commitConfigurationDataCreate(identifier.removePrefixes.toInstanceIdentifier.getInstanceIdentifier, payload); + val identifierWithSchemaNode = identifier.resolveInstanceIdentifier + val value = resolveNodeNamespaceBySchema(payload, identifierWithSchemaNode.schemaNode) + val status = broker.commitConfigurationDataPut(identifierWithSchemaNode.instanceIdentifier,value).get(); + switch status.result { + case TransactionStatus.COMMITED: Response.status(Response.Status.OK).build + default: Response.status(Response.Status.INTERNAL_SERVER_ERROR).build + } + } + + override updateConfigurationData(String identifier, CompositeNode payload) { + val identifierWithSchemaNode = identifier.resolveInstanceIdentifier + val value = resolveNodeNamespaceBySchema(payload, identifierWithSchemaNode.schemaNode) + val status = broker.commitConfigurationDataPut(identifierWithSchemaNode.instanceIdentifier,value).get(); + switch status.result { + case TransactionStatus.COMMITED: Response.status(Response.Status.NO_CONTENT).build + default: Response.status(Response.Status.INTERNAL_SERVER_ERROR).build + } + } + + override invokeRpc(String identifier, CompositeNode payload) { + val rpc = identifier.toQName; + val value = resolveNodeNamespaceBySchema(payload, controllerContext.getRpcInputSchema(rpc)) + val rpcResult = broker.invokeRpc(rpc, value); + val schema = controllerContext.getRpcOutputSchema(rpc); + return new StructuredData(rpcResult.result, schema); } + override readConfigurationData(String identifier) { + val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier + val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier); + return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode) + } - override updateConfigurationData(String identifier, CompositeNode payload) { - return broker.commitConfigurationDataCreate(identifier.removePrefixes.toInstanceIdentifier.getInstanceIdentifier, payload); + override readOperationalData(String identifier) { + val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier + val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier); + return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode) } - override invokeRpc(String identifier, CompositeNode payload) { - val rpcResult = broker.invokeRpc(identifier.removePrefixes.toRpcQName, payload); - jsonMapper.convert(identifier.removePrefixes.toInstanceIdentifier.getSchemaNode, rpcResult.result); + override updateConfigurationDataLegacy(String identifier, CompositeNode payload) { + updateConfigurationData(identifier,payload); + } + + override createConfigurationDataLegacy(String identifier, CompositeNode payload) { + createConfigurationData(identifier,payload); + } + + override createOperationalData(String identifier, CompositeNode payload) { + val identifierWithSchemaNode = identifier.resolveInstanceIdentifier + val value = resolveNodeNamespaceBySchema(payload, identifierWithSchemaNode.schemaNode) + val status = broker.commitOperationalDataPut(identifierWithSchemaNode.instanceIdentifier,value).get(); + switch status.result { + case TransactionStatus.COMMITED: Response.status(Response.Status.OK).build + default: Response.status(Response.Status.INTERNAL_SERVER_ERROR).build + } + } + + override updateOperationalData(String identifier, CompositeNode payload) { + val identifierWithSchemaNode = identifier.resolveInstanceIdentifier + val value = resolveNodeNamespaceBySchema(payload, identifierWithSchemaNode.schemaNode) + val status = broker.commitOperationalDataPut(identifierWithSchemaNode.instanceIdentifier,value).get(); + switch status.result { + case TransactionStatus.COMMITED: Response.status(Response.Status.NO_CONTENT).build + default: Response.status(Response.Status.INTERNAL_SERVER_ERROR).build + } } - private def String removePrefixes(String path) { - return path; + private def InstanceIdWithSchemaNode resolveInstanceIdentifier(String identifier) { + val identifierWithSchemaNode = identifier.toInstanceIdentifier + if (identifierWithSchemaNode === null) { + throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("URI has bad format") + .build()); + } + return identifierWithSchemaNode } -} \ No newline at end of file + private def CompositeNode resolveNodeNamespaceBySchema(CompositeNode node, DataSchemaNode schema) { + if (node instanceof CompositeNodeWrapper) { + addNamespaceToNodeFromSchemaRecursively(node as CompositeNodeWrapper, schema) + return (node as CompositeNodeWrapper).unwrap(null) + } + return node + } + + private def void addNamespaceToNodeFromSchemaRecursively(NodeWrapper nodeBuilder, DataSchemaNode schema) { + if (nodeBuilder.namespace === null) { + nodeBuilder.namespace = schema.QName.namespace + } + if (nodeBuilder instanceof CompositeNodeWrapper) { + val List> children = (nodeBuilder as CompositeNodeWrapper).getValues + for (child : children) { + addNamespaceToNodeFromSchemaRecursively(child, + (schema as DataNodeContainer).childNodes.findFirst[n|n.QName.localName.equals(child.localName)]) + } + } + } + +}