X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2FRestconfImpl.xtend;h=b7307544397b06e4bf544a71b0fd59471cfa72fd;hp=d9ac53589fa5415bcba3e59467576fcbb98d069e;hb=1720ef0759ca8d7ff96bc82563f7673d68a20377;hpb=6a796ca360be200816e4b8f7724227a6ee1929fe 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 d9ac53589f..b730754439 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,12 +1,14 @@ package org.opendaylight.controller.sal.restconf.impl 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.data.api.CompositeNode +import org.opendaylight.yangtools.yang.model.api.ChoiceNode 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 class RestconfImpl implements RestconfService { @@ -19,7 +21,7 @@ class RestconfImpl implements RestconfService { extension ControllerContext controllerContext private new() { - if (INSTANCE != null) { + if (INSTANCE !== null) { throw new IllegalStateException("Already instantiated"); } } @@ -42,13 +44,13 @@ class RestconfImpl implements RestconfService { } 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) { - val identifierWithSchemaNode = identifier.toInstanceIdentifier + val identifierWithSchemaNode = identifier.resolveInstanceIdentifier val value = resolveNodeNamespaceBySchema(payload, identifierWithSchemaNode.schemaNode) val status = broker.commitConfigurationDataPut(identifierWithSchemaNode.instanceIdentifier,value).get(); switch status.result { @@ -58,7 +60,7 @@ class RestconfImpl implements RestconfService { } override updateConfigurationData(String identifier, CompositeNode payload) { - val identifierWithSchemaNode = identifier.toInstanceIdentifier + val identifierWithSchemaNode = identifier.resolveInstanceIdentifier val value = resolveNodeNamespaceBySchema(payload, identifierWithSchemaNode.schemaNode) val status = broker.commitConfigurationDataPut(identifierWithSchemaNode.instanceIdentifier,value).get(); switch status.result { @@ -76,13 +78,13 @@ class RestconfImpl implements RestconfService { } override readConfigurationData(String identifier) { - val instanceIdentifierWithSchemaNode = identifier.toInstanceIdentifier - val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier); + val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier + val data = broker.readConfigurationData(instanceIdentifierWithSchemaNode.getInstanceIdentifier); return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode) } override readOperationalData(String identifier) { - val instanceIdentifierWithSchemaNode = identifier.toInstanceIdentifier + val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier); return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode) } @@ -95,24 +97,12 @@ class RestconfImpl implements RestconfService { createConfigurationData(identifier,payload); } - override createOperationalData(String identifier, CompositeNode payload) { - val identifierWithSchemaNode = identifier.toInstanceIdentifier - 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) { + private def InstanceIdWithSchemaNode resolveInstanceIdentifier(String identifier) { val identifierWithSchemaNode = identifier.toInstanceIdentifier - 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 + if (identifierWithSchemaNode === null) { + throw new ResponseException(Response.Status.BAD_REQUEST, "URI has bad format"); } + return identifierWithSchemaNode } private def CompositeNode resolveNodeNamespaceBySchema(CompositeNode node, DataSchemaNode schema) { @@ -124,16 +114,43 @@ class RestconfImpl implements RestconfService { } private def void addNamespaceToNodeFromSchemaRecursively(NodeWrapper nodeBuilder, DataSchemaNode schema) { - if (nodeBuilder.namespace == null) { - nodeBuilder.namespace = schema.QName.namespace + if (schema === null) { + throw new ResponseException(Response.Status.BAD_REQUEST, + "Data has bad format\n" + nodeBuilder.localName + " does not exist in yang schema."); + } + val moduleName = controllerContext.findModuleByNamespace(schema.QName.namespace); + if (nodeBuilder.namespace === null || nodeBuilder.namespace == schema.QName.namespace || + nodeBuilder.namespace.path == moduleName) { + nodeBuilder.qname = schema.QName + } else { + throw new ResponseException(Response.Status.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) { addNamespaceToNodeFromSchemaRecursively(child, - (schema as DataNodeContainer).childNodes.findFirst[n|n.QName.localName.equals(child.localName)]) + findFirstSchemaByLocalName(child.localName, (schema as DataNodeContainer).childNodes)) + } + } + } + + 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 { + return schemas.findFirst[n|n.QName.localName.equals(localName)] } } + return null } }