X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2FRestconfImpl.xtend;h=a65c0ff97aa73324083a0826186462d59c7006c6;hb=54bed4dd6dfbada9a8e2ddf70ca84952aec3f55a;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..a65c0ff97a 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,367 @@ package org.opendaylight.controller.sal.restconf.impl -import org.opendaylight.controller.sal.core.api.model.SchemaService +import java.util.ArrayList +import java.util.HashMap +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.InstanceIdentifier +import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.InstanceIdentifierBuilder +import org.opendaylight.yangtools.yang.data.api.Node +import org.opendaylight.yangtools.yang.data.impl.NodeFactory +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.Module +import org.opendaylight.yangtools.yang.model.api.TypeDefinition +import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition -import static com.google.common.base.Preconditions.* +import static javax.ws.rs.core.Response.Status.* +import org.opendaylight.yangtools.yang.model.api.RpcDefinition 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 invokeRpc(String identifier, CompositeNode payload) { + return callRpc(identifier.rpcDefinition, payload) } - override readData(String identifier) { - val instanceIdentifierWithSchemaNode = identifier.removePrefixes.toInstanceIdentifier - val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier); - jsonMapper.convert(instanceIdentifierWithSchemaNode.getSchemaNode, data) + override invokeRpc(String identifier) { + return callRpc(identifier.rpcDefinition, null) } + private def StructuredData callRpc(RpcDefinition rpc, CompositeNode payload) { + if (rpc === null) { + throw new ResponseException(NOT_FOUND, "RPC does not exist."); + } + var CompositeNode rpcRequest; + if (payload === null) { + rpcRequest = NodeFactory.createMutableCompositeNode(rpc.QName, null, null, null, null) + } else { + val value = normalizeNode(payload, rpc.input, null) + val List> input = new ArrayList + input.add(value) + 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 readData(String identifier) { + val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier + val data = broker.readOperationalData(instanceIdentifierWithSchemaNode.getInstanceIdentifier); + return new StructuredData(data, instanceIdentifierWithSchemaNode.schemaNode) + } + + 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 updateConfigurationData(String identifier, CompositeNode payload) { + val identifierWithSchemaNode = identifier.resolveInstanceIdentifier + val value = normalizeNode(payload, identifierWithSchemaNode.schemaNode, identifierWithSchemaNode.mountPoint) + 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 createConfigurationDataLegacy(String identifier, CompositeNode payload) { + createConfigurationData(identifier, payload); + } + override createConfigurationData(String identifier, CompositeNode payload) { - return broker.commitConfigurationDataCreate(identifier.removePrefixes.toInstanceIdentifier.getInstanceIdentifier, payload); + val uncompleteIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier + var schemaNode = (uncompleteIdentifierWithSchemaNode.schemaNode as DataNodeContainer).getSchemaChildNode(payload) + if (schemaNode === null) { + schemaNode = payload.findModule(uncompleteIdentifierWithSchemaNode.instanceIdentifier)?.getSchemaChildNode(payload) + } + val value = normalizeNode(payload, schemaNode, uncompleteIdentifierWithSchemaNode.instanceIdentifier) + val completeIdentifierWithSchemaNode = uncompleteIdentifierWithSchemaNode.addLastIdentifierFromData(value, schemaNode) + val status = broker.commitConfigurationDataPost(completeIdentifierWithSchemaNode.instanceIdentifier, value)?.get(); + if (status === null) { + return Response.status(ACCEPTED).build + } + 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.removePrefixes.toInstanceIdentifier.getInstanceIdentifier, payload); + override createConfigurationData(CompositeNode payload) { + val schemaNode = payload.findModule(null)?.getSchemaChildNode(payload) + val value = normalizeNode(payload, schemaNode, null) + val identifierWithSchemaNode = addLastIdentifierFromData(null, value, schemaNode) + val status = broker.commitConfigurationDataPost(identifierWithSchemaNode.instanceIdentifier, value)?.get(); + if (status === null) { + return Response.status(ACCEPTED).build + } + switch status.result { + case TransactionStatus.COMMITED: Response.status(NO_CONTENT).build + default: Response.status(INTERNAL_SERVER_ERROR).build + } } - override invokeRpc(String identifier, CompositeNode payload) { - val rpcResult = broker.invokeRpc(identifier.removePrefixes.toRpcQName, payload); - jsonMapper.convert(identifier.removePrefixes.toInstanceIdentifier.getSchemaNode, rpcResult.result); + override deleteConfigurationData(String identifier) { + val instanceIdentifierWithSchemaNode = identifier.resolveInstanceIdentifier + val status = broker.commitConfigurationDataDelete(instanceIdentifierWithSchemaNode.getInstanceIdentifier).get; + switch status.result { + case TransactionStatus.COMMITED: Response.status(OK).build + default: Response.status(INTERNAL_SERVER_ERROR).build + } + } + + 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 dispatch Module findModule(CompositeNode data, InstanceIdentifier partialPath) { + if (partialPath !== null && !partialPath.path.empty) { + return data.nodeType.namespace.findModuleByNamespace(partialPath) + } else { + return data.nodeType.namespace.findModuleByNamespace + } + } + + private def dispatch Module findModule(CompositeNodeWrapper data, InstanceIdentifier partialPath) { + var Module module = null; + if (partialPath !== null && !partialPath.path.empty) { + module = data.namespace.findModuleByNamespace(partialPath) // namespace from XML + if (module === null) { + module = data.namespace.toString.findModuleByName(partialPath) // namespace (module name) from JSON + } + } else { + module = data.namespace.findModuleByNamespace // namespace from XML + if (module === null) { + module = data.namespace.toString.findModuleByName // namespace (module name) from JSON + } + } + return module } - private def String removePrefixes(String path) { - return path; + private def dispatch DataSchemaNode getSchemaChildNode(DataNodeContainer parentSchemaNode, CompositeNode data) { + return parentSchemaNode?.getDataChildByName(data.nodeType.localName) } -} \ No newline at end of file + private def dispatch DataSchemaNode getSchemaChildNode(DataNodeContainer parentSchemaNode, CompositeNodeWrapper data) { + return parentSchemaNode?.getDataChildByName(data.localName) + } + + private def InstanceIdWithSchemaNode addLastIdentifierFromData(InstanceIdWithSchemaNode identifierWithSchemaNode, CompositeNode data, DataSchemaNode schemaOfData) { + val iiOriginal = identifierWithSchemaNode?.instanceIdentifier + var InstanceIdentifierBuilder iiBuilder = null + if (iiOriginal === null) { + iiBuilder = InstanceIdentifier.builder + } else { + iiBuilder = InstanceIdentifier.builder(iiOriginal) + } + + if (schemaOfData instanceof ListSchemaNode) { + iiBuilder.nodeWithKey(schemaOfData.QName, (schemaOfData as ListSchemaNode).resolveKeysFromData(data)) + } else { + iiBuilder.node(schemaOfData.QName) + } + return new InstanceIdWithSchemaNode(iiBuilder.toInstance, schemaOfData, identifierWithSchemaNode?.mountPoint) + } + + private def resolveKeysFromData(ListSchemaNode listNode, CompositeNode dataNode) { + val keyValues = new HashMap(); + for (key : listNode.keyDefinition) { + val dataNodeKeyValueObject = dataNode.getSimpleNodesByName(key.localName)?.head?.value + if (dataNodeKeyValueObject === null) { + throw new ResponseException(BAD_REQUEST, "Data contains list \"" + dataNode.nodeType.localName + "\" which does not contain key: \"" + key.localName + "\"") + } + keyValues.put(key, dataNodeKeyValueObject); + } + return keyValues + } + + private def CompositeNode normalizeNode(CompositeNode node, DataSchemaNode schema, InstanceIdentifier mountPoint) { + if (schema !== null && !schema.containerOrList) { + throw new ResponseException(BAD_REQUEST, "Root element has to be container or list yang datatype."); + } + if (node instanceof CompositeNodeWrapper) { + if ((node as CompositeNodeWrapper).changeAllowed) { + normalizeNode(node as CompositeNodeWrapper, schema, null, mountPoint) + } + return (node as CompositeNodeWrapper).unwrap() + } + return node + } + + private def isContainerOrList(DataSchemaNode schemaNode) { + return (schemaNode instanceof ContainerSchemaNode) || (schemaNode instanceof ListSchemaNode) + } + + private def void normalizeNode(NodeWrapper nodeBuilder, DataSchemaNode schema, QName previousAugment, + InstanceIdentifier mountPoint) { + 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); + } + var moduleName = controllerContext.findModuleNameByNamespace(validQName.namespace); + if (moduleName === null && mountPoint !== null && !mountPoint.path.empty) { + moduleName = controllerContext.findModuleByNamespace(validQName.namespace, mountPoint)?.name + } + if (nodeBuilder.namespace === null || nodeBuilder.namespace == validQName.namespace || + nodeBuilder.namespace.toString == 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 + "\".\nIf 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, mountPoint) + } + 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 in URI \"" + listKey.localName + "\" of list \"" + schema.QName.localName + "\"") + } + } + } + } else if (nodeBuilder instanceof SimpleNodeWrapper) { + val simpleNode = (nodeBuilder as SimpleNodeWrapper) + val value = simpleNode.value + var inputValue = value; + + if (schema.typeDefinition instanceof IdentityrefTypeDefinition) { + if (value instanceof String) { + inputValue = new IdentityValuesDTO(validQName.namespace.toString, value as String, null) + } // else value is instance of ValuesDTO + } + + val outputValue = RestCodec.from(schema.typeDefinition)?.deserialize(inputValue); + simpleNode.setValue(outputValue) + } 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) { + var baseType = node.type + while (baseType.baseType !== null) { + baseType = baseType.baseType; + } + baseType + } + + private def dispatch TypeDefinition typeDefinition(LeafListSchemaNode node) { + var TypeDefinition baseType = node.type + while (baseType.baseType !== null) { + baseType = baseType.baseType; + } + baseType + } + + 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 + } + +}