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=02a4b184a8cc4f75308b4a9933aa01b9ee2de835;hb=117fb72ff33b2d847dc356f1781014987e8844e2;hp=fc0dd1017fea6fbb9e77cd5f3b5a84d616b9e58f;hpb=b5daa3678322a764f9b0e2483f82781f4d39d263;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 fc0dd1017f..02a4b184a8 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,10 +1,29 @@ package org.opendaylight.controller.sal.restconf.impl -import org.opendaylight.yangtools.yang.data.api.CompositeNode +import java.util.ArrayList +import java.util.List +import java.util.Set +import javax.ws.rs.core.Response +import org.opendaylight.controller.md.sal.common.api.TransactionStatus import org.opendaylight.controller.sal.rest.api.RestconfService +import org.opendaylight.yangtools.yang.common.QName +import org.opendaylight.yangtools.yang.data.api.CompositeNode +import org.opendaylight.yangtools.yang.data.api.Node +import org.opendaylight.yangtools.yang.data.impl.NodeFactory +import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec +import org.opendaylight.yangtools.yang.model.api.ChoiceNode +import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode +import org.opendaylight.yangtools.yang.model.api.DataNodeContainer +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode +import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode +import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode +import org.opendaylight.yangtools.yang.model.api.ListSchemaNode +import org.opendaylight.yangtools.yang.model.api.TypeDefinition + +import static javax.ws.rs.core.Response.Status.* class RestconfImpl implements RestconfService { - + val static RestconfImpl INSTANCE = new RestconfImpl @Property @@ -12,20 +31,20 @@ class RestconfImpl implements RestconfService { @Property extension ControllerContext controllerContext - + private new() { - if (INSTANCE != null) { + if (INSTANCE !== null) { throw new IllegalStateException("Already instantiated"); } } - + static def getInstance() { return INSTANCE } override readAllData() { // return broker.readOperationalData("".toInstanceIdentifier.getInstanceIdentifier); - throw new UnsupportedOperationException("TODO: auto-generated method stub") + throw new UnsupportedOperationException("Reading all data is currently not supported.") } override getModules() { @@ -33,29 +52,179 @@ class RestconfImpl implements RestconfService { } override getRoot() { - throw new UnsupportedOperationException("TODO: auto-generated method stub") - + return null; } override readData(String identifier) { - val instanceIdentifierWithSchemaNode = identifier.toInstanceIdentifier + 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.toInstanceIdentifier.getInstanceIdentifier, payload); - throw new UnsupportedOperationException("TODO: auto-generated method stub") + val identifierWithSchemaNode = identifier.resolveInstanceIdentifier + val value = normalizeNode(payload, identifierWithSchemaNode.schemaNode) + val status = broker.commitConfigurationDataPut(identifierWithSchemaNode.instanceIdentifier, value).get(); + switch status.result { + case TransactionStatus.COMMITED: Response.status(NO_CONTENT).build + default: Response.status(INTERNAL_SERVER_ERROR).build + } } override updateConfigurationData(String identifier, CompositeNode payload) { -// return broker.commitConfigurationDataCreate(identifier.toInstanceIdentifier.getInstanceIdentifier, payload); - throw new UnsupportedOperationException("TODO: auto-generated method stub") + val identifierWithSchemaNode = identifier.resolveInstanceIdentifier + val value = normalizeNode(payload, identifierWithSchemaNode.schemaNode) + val status = broker.commitConfigurationDataPut(identifierWithSchemaNode.instanceIdentifier, value).get(); + switch status.result { + case TransactionStatus.COMMITED: Response.status(OK).build + default: Response.status(INTERNAL_SERVER_ERROR).build + } } override invokeRpc(String identifier, CompositeNode payload) { - val rpcResult = broker.invokeRpc(identifier.toRpcQName, payload); - return new StructuredData(rpcResult.result, identifier.toInstanceIdentifier.getSchemaNode) + val rpc = identifier.rpcDefinition + if (rpc === null) { + throw new ResponseException(NOT_FOUND, "RPC does not exist."); + } + val value = normalizeNode(payload, rpc.input) + val List> input = new ArrayList + input.add(value) + val rpcRequest = NodeFactory.createMutableCompositeNode(rpc.QName, null, input, null, null) + val rpcResult = broker.invokeRpc(rpc.QName, rpcRequest); + if (!rpcResult.successful) { + throw new ResponseException(INTERNAL_SERVER_ERROR, "Operation failed") + } + if (rpcResult.result === null) { + return null + } + return new StructuredData(rpcResult.result, rpc.output) + } + + override readConfigurationData(String identifier) { + val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier + val data = broker.readConfigurationData(instanceIdentifierWithSchemaNode.getInstanceIdentifier); + return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode) + } + + override readOperationalData(String identifier) { + val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier + val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier); + return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode) + } + + override updateConfigurationDataLegacy(String identifier, CompositeNode payload) { + updateConfigurationData(identifier, payload); + } + + override createConfigurationDataLegacy(String identifier, CompositeNode payload) { + createConfigurationData(identifier, payload); + } + + private def InstanceIdWithSchemaNode resolveInstanceIdentifier(String identifier) { + val identifierWithSchemaNode = identifier.toInstanceIdentifier + if (identifierWithSchemaNode === null) { + throw new ResponseException(BAD_REQUEST, "URI has bad format"); + } + return identifierWithSchemaNode + } + + private def CompositeNode normalizeNode(CompositeNode node, DataSchemaNode schema) { + if (node instanceof CompositeNodeWrapper) { + normalizeNode(node as CompositeNodeWrapper, schema, null) + return (node as CompositeNodeWrapper).unwrap() + } + return node + } + + private def void normalizeNode(NodeWrapper nodeBuilder, DataSchemaNode schema, QName previousAugment) { + if (schema === null) { + throw new ResponseException(BAD_REQUEST, + "Data has bad format\n" + nodeBuilder.localName + " does not exist in yang schema."); + } + var validQName = schema.QName + var currentAugment = previousAugment; + if (schema.augmenting) { + currentAugment = schema.QName + } else if (previousAugment !== null && schema.QName.namespace !== previousAugment.namespace) { + validQName = QName.create(currentAugment, schema.QName.localName); + } + val moduleName = controllerContext.findModuleByNamespace(validQName.namespace); + if (nodeBuilder.namespace === null || nodeBuilder.namespace == validQName.namespace || + nodeBuilder.namespace.path == moduleName) { + nodeBuilder.qname = validQName + } else { + throw new ResponseException(BAD_REQUEST, + "Data has bad format\nIf data is in XML format then namespace for " + nodeBuilder.localName + + " should be " + schema.QName.namespace + ".\n If data is in Json format then module name for " + + nodeBuilder.localName + " should be " + moduleName + "."); + } + + if (nodeBuilder instanceof CompositeNodeWrapper) { + val List> children = (nodeBuilder as CompositeNodeWrapper).getValues + for (child : children) { + normalizeNode(child, + findFirstSchemaByLocalName(child.localName, (schema as DataNodeContainer).childNodes), + currentAugment) + } + if(schema instanceof ListSchemaNode) { + val listKeys = (schema as ListSchemaNode).keyDefinition + for (listKey : listKeys) { + var foundKey = false + for (child : children) { + if (child.unwrap.nodeType.localName == listKey.localName) { + foundKey = true; + } + } + if (!foundKey) { + throw new ResponseException(BAD_REQUEST, + "Missing key \"" + listKey.localName + "\" of list \"" + schema.QName.localName + "\"") + } + } + } + } else if (nodeBuilder instanceof SimpleNodeWrapper) { + val simpleNode = (nodeBuilder as SimpleNodeWrapper) + val stringValue = simpleNode.value as String; + + val objectValue = TypeDefinitionAwareCodec.from(schema.typeDefinition)?.deserialize(stringValue); + simpleNode.setValue(objectValue) + } else if (nodeBuilder instanceof EmptyNodeWrapper) { + val emptyNodeBuilder = nodeBuilder as EmptyNodeWrapper + if (schema instanceof LeafSchemaNode) { + emptyNodeBuilder.setComposite(false); + } else if (schema instanceof ContainerSchemaNode) { + + // FIXME: Add presence check + emptyNodeBuilder.setComposite(true); + } + } + } + + private def dispatch TypeDefinition typeDefinition(LeafSchemaNode node) { + node.type + } + + private def dispatch TypeDefinition typeDefinition(LeafListSchemaNode node) { + node.type + } + + private def DataSchemaNode findFirstSchemaByLocalName(String localName, Set schemas) { + for (schema : schemas) { + if (schema instanceof ChoiceNode) { + for (caze : (schema as ChoiceNode).cases) { + val result = findFirstSchemaByLocalName(localName, caze.childNodes) + if (result !== null) { + return result + } + } + } else { + val result = schemas.findFirst[n|n.QName.localName.equals(localName)] + if (result !== null) { + return result; + + } + } + } + return null } }