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.java;h=e24500a76ce57ff5a7b607dec7a7e208990c35c3;hp=b68bee2f0c9cc30e04a7c6ae62288f6cf6f4bdb6;hb=508aa010e688575a1c8eeb840d00a5e3f39c25fa;hpb=25eb29c9dc9c6eb9604251f8973ba2f78948e7a8 diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java index b68bee2f0c..e24500a76c 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java @@ -9,18 +9,22 @@ package org.opendaylight.controller.sal.restconf.impl; import com.google.common.base.Objects; +import com.google.common.base.Optional; import com.google.common.base.Preconditions; +import com.google.common.base.Predicates; import com.google.common.base.Splitter; import com.google.common.base.Strings; +import com.google.common.base.Throwables; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; +import java.math.BigInteger; import java.net.URI; +import java.net.URISyntaxException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; @@ -33,9 +37,11 @@ import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriInfo; import org.apache.commons.lang3.StringUtils; -import org.opendaylight.controller.md.sal.common.api.TransactionStatus; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException; +import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; +import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer; import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; import org.opendaylight.controller.sal.rest.api.Draft02; import org.opendaylight.controller.sal.rest.api.RestconfService; @@ -50,7 +56,6 @@ import org.opendaylight.controller.sal.streams.websockets.WebSocketServer; import org.opendaylight.yangtools.concepts.Codec; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.QNameModule; -import org.opendaylight.yangtools.yang.common.RpcError; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.data.api.CompositeNode; import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode; @@ -60,12 +65,9 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; -import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; -import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; -import org.opendaylight.yangtools.yang.data.api.schema.MapNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.api.schema.tree.ModifiedNodeDoesNotExistException; import org.opendaylight.yangtools.yang.data.composite.node.schema.cnsn.parser.CnSnToNormalizedNodeParserFactory; -import org.opendaylight.yangtools.yang.data.composite.node.schema.cnsn.serializer.CnSnFromNormalizedNodeSerializerFactory; import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode; import org.opendaylight.yangtools.yang.data.impl.NodeFactory; import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode; @@ -82,20 +84,24 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition; import org.opendaylight.yangtools.yang.model.util.EmptyType; +import org.opendaylight.yangtools.yang.model.util.ExtendedType; +import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder; import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class RestconfImpl implements RestconfService { + private enum UriParameters { PRETTY_PRINT("prettyPrint"), DEPTH("depth"); private String uriParameterName; - UriParameters(String uriParameterName) { + UriParameters(final String uriParameterName) { this.uriParameterName = uriParameterName; } @@ -105,6 +111,16 @@ public class RestconfImpl implements RestconfService { } } + private static class TypeDef { + public final TypeDefinition typedef; + public final QName qName; + + TypeDef(final TypeDefinition typedef, final QName qName) { + this.typedef = typedef; + this.qName = qName; + } + } + private final static RestconfImpl INSTANCE = new RestconfImpl(); private static final int NOTIFICATION_PORT = 8181; @@ -137,13 +153,24 @@ public class RestconfImpl implements RestconfService { private static final String SCOPE_PARAM_NAME = "scope"; + private static final String NETCONF_BASE = "urn:ietf:params:xml:ns:netconf:base:1.0"; + + private static final String NETCONF_BASE_PAYLOAD_NAME = "data"; + + private static final QName NETCONF_BASE_QNAME; + static { try { EVENT_SUBSCRIPTION_AUGMENT_REVISION = new SimpleDateFormat("yyyy-MM-dd").parse("2014-07-08"); + NETCONF_BASE_QNAME = QName.create(QNameModule.create(new URI(NETCONF_BASE), null), NETCONF_BASE_PAYLOAD_NAME ); } catch (ParseException e) { throw new RestconfDocumentedException( "It wasn't possible to convert revision date of sal-remote-augment to date", ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED); + } catch (URISyntaxException e) { + throw new RestconfDocumentedException( + "It wasn't possible to create instance of URI class with "+NETCONF_BASE+" URI", ErrorType.APPLICATION, + ErrorTag.OPERATION_FAILED); } } @@ -207,7 +234,7 @@ public class RestconfImpl implements RestconfService { Set modules = null; DOMMountPoint mountPoint = null; if (identifier.contains(ControllerContext.MOUNT)) { - InstanceIdWithSchemaNode mountPointIdentifier = this.controllerContext.toMountPointIdentifier(identifier); + InstanceIdentifierContext mountPointIdentifier = this.controllerContext.toMountPointIdentifier(identifier); mountPoint = mountPointIdentifier.getMountPoint(); modules = this.controllerContext.getAllModules(mountPoint); } else { @@ -238,7 +265,7 @@ public class RestconfImpl implements RestconfService { Module module = null; DOMMountPoint mountPoint = null; if (identifier.contains(ControllerContext.MOUNT)) { - InstanceIdWithSchemaNode mountPointIdentifier = this.controllerContext.toMountPointIdentifier(identifier); + InstanceIdentifierContext mountPointIdentifier = this.controllerContext.toMountPointIdentifier(identifier); mountPoint = mountPointIdentifier.getMountPoint(); module = this.controllerContext.findModuleByNameAndRevision(mountPoint, moduleNameAndRevision); } else { @@ -269,7 +296,7 @@ public class RestconfImpl implements RestconfService { Set modules = null; DOMMountPoint mountPoint = null; if (identifier.contains(ControllerContext.MOUNT)) { - InstanceIdWithSchemaNode mountPointIdentifier = this.controllerContext.toMountPointIdentifier(identifier); + InstanceIdentifierContext mountPointIdentifier = this.controllerContext.toMountPointIdentifier(identifier); mountPoint = mountPointIdentifier.getMountPoint(); modules = this.controllerContext.getAllModules(mountPoint); } else { @@ -282,7 +309,7 @@ public class RestconfImpl implements RestconfService { } private StructuredData operationsFromModulesToStructuredData(final Set modules, - final DOMMountPoint mountPoint, boolean prettyPrint) { + final DOMMountPoint mountPoint, final boolean prettyPrint) { final List> operationsAsData = new ArrayList>(); Module restconfModule = this.getRestconfModule(); final DataSchemaNode operationsSchemaNode = controllerContext.getRestconfModuleRestConfSchemaNode( @@ -358,31 +385,31 @@ public class RestconfImpl implements RestconfService { private CompositeNode toStreamCompositeNode(final String streamName, final DataSchemaNode streamSchemaNode) { final List> streamNodeValues = new ArrayList>(); - List instanceDataChildrenByName = this.controllerContext.findInstanceDataChildrenByName( + List instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName( ((DataNodeContainer) streamSchemaNode), "name"); final DataSchemaNode nameSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null); streamNodeValues - .add(NodeFactory. createImmutableSimpleNode(nameSchemaNode.getQName(), null, streamName)); + .add(NodeFactory. createImmutableSimpleNode(nameSchemaNode.getQName(), null, streamName)); - instanceDataChildrenByName = this.controllerContext.findInstanceDataChildrenByName( + instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName( ((DataNodeContainer) streamSchemaNode), "description"); final DataSchemaNode descriptionSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null); streamNodeValues.add(NodeFactory. createImmutableSimpleNode(descriptionSchemaNode.getQName(), null, "DESCRIPTION_PLACEHOLDER")); - instanceDataChildrenByName = this.controllerContext.findInstanceDataChildrenByName( + instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName( ((DataNodeContainer) streamSchemaNode), "replay-support"); final DataSchemaNode replaySupportSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null); streamNodeValues.add(NodeFactory. createImmutableSimpleNode(replaySupportSchemaNode.getQName(), null, Boolean.valueOf(true))); - instanceDataChildrenByName = this.controllerContext.findInstanceDataChildrenByName( + instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName( ((DataNodeContainer) streamSchemaNode), "replay-log-creation-time"); final DataSchemaNode replayLogCreationTimeSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null); streamNodeValues.add(NodeFactory. createImmutableSimpleNode(replayLogCreationTimeSchemaNode.getQName(), null, "")); - instanceDataChildrenByName = this.controllerContext.findInstanceDataChildrenByName( + instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName( ((DataNodeContainer) streamSchemaNode), "events"); final DataSchemaNode eventsSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null); streamNodeValues.add(NodeFactory. createImmutableSimpleNode(eventsSchemaNode.getQName(), null, "")); @@ -392,26 +419,26 @@ public class RestconfImpl implements RestconfService { private CompositeNode toModuleCompositeNode(final Module module, final DataSchemaNode moduleSchemaNode) { final List> moduleNodeValues = new ArrayList>(); - List instanceDataChildrenByName = this.controllerContext.findInstanceDataChildrenByName( + List instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName( ((DataNodeContainer) moduleSchemaNode), "name"); final DataSchemaNode nameSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null); moduleNodeValues.add(NodeFactory. createImmutableSimpleNode(nameSchemaNode.getQName(), null, module.getName())); - instanceDataChildrenByName = this.controllerContext.findInstanceDataChildrenByName( + instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName( ((DataNodeContainer) moduleSchemaNode), "revision"); final DataSchemaNode revisionSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null); Date _revision = module.getRevision(); moduleNodeValues.add(NodeFactory. createImmutableSimpleNode(revisionSchemaNode.getQName(), null, REVISION_FORMAT.format(_revision))); - instanceDataChildrenByName = this.controllerContext.findInstanceDataChildrenByName( + instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName( ((DataNodeContainer) moduleSchemaNode), "namespace"); final DataSchemaNode namespaceSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null); moduleNodeValues.add(NodeFactory. createImmutableSimpleNode(namespaceSchemaNode.getQName(), null, module.getNamespace().toString())); - instanceDataChildrenByName = this.controllerContext.findInstanceDataChildrenByName( + instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName( ((DataNodeContainer) moduleSchemaNode), "feature"); final DataSchemaNode featureSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null); for (final FeatureDefinition feature : module.getFeatures()) { @@ -475,7 +502,8 @@ public class RestconfImpl implements RestconfService { if (!Iterables.isEmpty(pathIdentifier.getPathArguments())) { String fullRestconfIdentifier = this.controllerContext.toFullRestconfIdentifier(pathIdentifier); - LogicalDatastoreType datastore = parseEnumTypeParameter(value, LogicalDatastoreType.class, DATASTORE_PARAM_NAME); + LogicalDatastoreType datastore = parseEnumTypeParameter(value, LogicalDatastoreType.class, + DATASTORE_PARAM_NAME); datastore = datastore == null ? DEFAULT_DATASTORE : datastore; DataChangeScope scope = parseEnumTypeParameter(value, DataChangeScope.class, SCOPE_PARAM_NAME); @@ -519,7 +547,7 @@ public class RestconfImpl implements RestconfService { DOMMountPoint mountPoint = null; if (identifier.contains(ControllerContext.MOUNT)) { // mounted RPC call - look up mount instance. - InstanceIdWithSchemaNode mountPointId = controllerContext.toMountPointIdentifier(identifier); + InstanceIdentifierContext mountPointId = controllerContext.toMountPointIdentifier(identifier); mountPoint = mountPointId.getMountPoint(); int startOfRemoteRpcName = identifier.lastIndexOf(ControllerContext.MOUNT) @@ -575,7 +603,7 @@ public class RestconfImpl implements RestconfService { return null; } - private StructuredData callRpc(final RpcExecutor rpcExecutor, final CompositeNode payload, boolean prettyPrint) { + private StructuredData callRpc(final RpcExecutor rpcExecutor, final CompositeNode payload, final boolean prettyPrint) { if (rpcExecutor == null) { throw new RestconfDocumentedException("RPC does not exist.", ErrorType.RPC, ErrorTag.UNKNOWN_ELEMENT); } @@ -610,38 +638,26 @@ public class RestconfImpl implements RestconfService { private void checkRpcSuccessAndThrowException(final RpcResult rpcResult) { if (rpcResult.isSuccessful() == false) { - Collection rpcErrors = rpcResult.getErrors(); - if (rpcErrors == null || rpcErrors.isEmpty()) { - throw new RestconfDocumentedException( - "The operation was not successful and there were no RPC errors returned", ErrorType.RPC, - ErrorTag.OPERATION_FAILED); - } - - List errorList = Lists.newArrayList(); - for (RpcError rpcError : rpcErrors) { - errorList.add(new RestconfError(rpcError)); - } - - throw new RestconfDocumentedException(errorList); + throw new RestconfDocumentedException("The operation was not successful", null, + rpcResult.getErrors()); } } @Override - public StructuredData readConfigurationData(final String identifier, final UriInfo uriInfo) { - final InstanceIdWithSchemaNode iiWithData = normalizeInstanceIdentifierWithSchemaNode( - this.controllerContext.toInstanceIdentifier(identifier), true); + public NormalizedNodeContext readConfigurationData(final String identifier, final UriInfo uriInfo) { + final InstanceIdentifierContext iiWithData = controllerContext.toInstanceIdentifier(identifier); DOMMountPoint mountPoint = iiWithData.getMountPoint(); NormalizedNode data = null; + YangInstanceIdentifier normalizedII; if (mountPoint != null) { - data = broker.readConfigurationData(mountPoint, iiWithData.getInstanceIdentifier()); + normalizedII = new DataNormalizer(mountPoint.getSchemaContext()).toNormalized(iiWithData + .getInstanceIdentifier()); + data = broker.readConfigurationData(mountPoint, normalizedII); } else { - data = broker.readConfigurationData(iiWithData.getInstanceIdentifier()); + normalizedII = controllerContext.toNormalized(iiWithData.getInstanceIdentifier()); + data = broker.readConfigurationData(normalizedII); } - CompositeNode compositeNode = datastoreNormalizedNodeToCompositeNode(data, iiWithData.getSchemaNode()); - - compositeNode = pruneDataAtDepth(compositeNode, parseDepthParameter(uriInfo)); - boolean prettyPrintMode = parsePrettyPrintParameter(uriInfo); - return new StructuredData(compositeNode, iiWithData.getSchemaNode(), iiWithData.getMountPoint(), prettyPrintMode); + return new NormalizedNodeContext(iiWithData, data); } @SuppressWarnings("unchecked") @@ -687,57 +703,115 @@ public class RestconfImpl implements RestconfService { } @Override - public StructuredData readOperationalData(final String identifier, final UriInfo info) { - final InstanceIdWithSchemaNode iiWithData = normalizeInstanceIdentifierWithSchemaNode( - this.controllerContext.toInstanceIdentifier(identifier), true); - NormalizedNode data = null; - + public NormalizedNodeContext readOperationalData(final String identifier, final UriInfo info) { + final InstanceIdentifierContext iiWithData = controllerContext.toInstanceIdentifier(identifier); DOMMountPoint mountPoint = iiWithData.getMountPoint(); + NormalizedNode data = null; + YangInstanceIdentifier normalizedII; if (mountPoint != null) { - data = broker.readOperationalData(mountPoint, iiWithData.getInstanceIdentifier()); + normalizedII = new DataNormalizer(mountPoint.getSchemaContext()).toNormalized(iiWithData + .getInstanceIdentifier()); + data = broker.readOperationalData(mountPoint, normalizedII); } else { - data = broker.readOperationalData(iiWithData.getInstanceIdentifier()); + normalizedII = controllerContext.toNormalized(iiWithData.getInstanceIdentifier()); + data = broker.readOperationalData(normalizedII); } - final CompositeNode compositeNode = datastoreNormalizedNodeToCompositeNode(data, iiWithData.getSchemaNode()); - final CompositeNode prunedCompositeNode = pruneDataAtDepth(compositeNode, parseDepthParameter(info)); - final boolean prettyPrintMode = parsePrettyPrintParameter(info); - return new StructuredData(prunedCompositeNode, iiWithData.getSchemaNode(), mountPoint,prettyPrintMode); + return new NormalizedNodeContext(iiWithData, data); } - private boolean parsePrettyPrintParameter(UriInfo info) { + private boolean parsePrettyPrintParameter(final UriInfo info) { String param = info.getQueryParameters(false).getFirst(UriParameters.PRETTY_PRINT.toString()); return Boolean.parseBoolean(param); } @Override public Response updateConfigurationData(final String identifier, final Node payload) { - final InstanceIdWithSchemaNode iiWithData = normalizeInstanceIdentifierWithSchemaNode(this.controllerContext - .toInstanceIdentifier(identifier)); + final InstanceIdentifierContext iiWithData = this.controllerContext.toInstanceIdentifier(identifier); validateInput(iiWithData.getSchemaNode(), payload); DOMMountPoint mountPoint = iiWithData.getMountPoint(); + validateTopLevelNodeName(payload, iiWithData.getInstanceIdentifier()); final CompositeNode value = this.normalizeNode(payload, iiWithData.getSchemaNode(), mountPoint); validateListKeysEqualityInPayloadAndUri(iiWithData, value); final NormalizedNode datastoreNormalizedNode = compositeNodeToDatastoreNormalizedNode(value, iiWithData.getSchemaNode()); - try { - if (mountPoint != null) { - broker.commitConfigurationDataPut(mountPoint, iiWithData.getInstanceIdentifier(), - datastoreNormalizedNode).get(); - } else { - broker.commitConfigurationDataPut(iiWithData.getInstanceIdentifier(), datastoreNormalizedNode) - .get(); + + YangInstanceIdentifier normalizedII; + if (mountPoint != null) { + normalizedII = new DataNormalizer(mountPoint.getSchemaContext()).toNormalized( + iiWithData.getInstanceIdentifier()); + } else { + normalizedII = controllerContext.toNormalized(iiWithData.getInstanceIdentifier()); + } + + /* + * There is a small window where another write transaction could be updating the same data + * simultaneously and we get an OptimisticLockFailedException. This error is likely + * transient and The WriteTransaction#submit API docs state that a retry will likely + * succeed. So we'll try again if that scenario occurs. If it fails a third time then it + * probably will never succeed so we'll fail in that case. + * + * By retrying we're attempting to hide the internal implementation of the data store and + * how it handles concurrent updates from the restconf client. The client has instructed us + * to put the data and we should make every effort to do so without pushing optimistic lock + * failures back to the client and forcing them to handle it via retry (and having to + * document the behavior). + */ + int tries = 2; + while(true) { + try { + if (mountPoint != null) { + broker.commitConfigurationDataPut(mountPoint, normalizedII, + datastoreNormalizedNode).checkedGet(); + } else { + broker.commitConfigurationDataPut(normalizedII, + datastoreNormalizedNode).checkedGet(); + } + + break; + } catch (TransactionCommitFailedException e) { + if(e instanceof OptimisticLockFailedException) { + if(--tries <= 0) { + LOG.debug("Got OptimisticLockFailedException on last try - failing"); + throw new RestconfDocumentedException(e.getMessage(), e, e.getErrorList()); + } + + LOG.debug("Got OptimisticLockFailedException - trying again"); + } else { + throw new RestconfDocumentedException(e.getMessage(), e, e.getErrorList()); + } } - } catch (Exception e) { - throw new RestconfDocumentedException("Error updating data", e); } return Response.status(Status.OK).build(); } + private void validateTopLevelNodeName(final Node node, + final YangInstanceIdentifier identifier) { + final String payloadName = getName(node); + final Iterator pathArguments = identifier.getReversePathArguments().iterator(); + + //no arguments + if (!pathArguments.hasNext()) { + //no "data" payload + if (!node.getNodeType().equals(NETCONF_BASE_QNAME)) { + throw new RestconfDocumentedException("Instance identifier has to contain at least one path argument", + ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE); + } + //any arguments + } else { + final String identifierName = pathArguments.next().getNodeType().getLocalName(); + if (!payloadName.equals(identifierName)) { + throw new RestconfDocumentedException("Payload name (" + payloadName + + ") is different from identifier name (" + identifierName + ")", ErrorType.PROTOCOL, + ErrorTag.MALFORMED_MESSAGE); + } + } + } + /** * Validates whether keys in {@code payload} are equal to values of keys in {@code iiWithData} for list schema node * @@ -745,7 +819,7 @@ public class RestconfImpl implements RestconfService { * if key values or key count in payload and URI isn't equal * */ - private void validateListKeysEqualityInPayloadAndUri(final InstanceIdWithSchemaNode iiWithData, + private void validateListKeysEqualityInPayloadAndUri(final InstanceIdentifierContext iiWithData, final CompositeNode payload) { if (iiWithData.getSchemaNode() instanceof ListSchemaNode) { final List keyDefinitions = ((ListSchemaNode) iiWithData.getSchemaNode()).getKeyDefinition(); @@ -796,7 +870,7 @@ public class RestconfImpl implements RestconfService { ErrorType.PROTOCOL, ErrorTag.UNKNOWN_NAMESPACE); } - InstanceIdWithSchemaNode iiWithData = null; + InstanceIdentifierContext iiWithData = null; CompositeNode value = null; if (this.representsMountPointRootData(payload)) { // payload represents mount point data and URI represents path to the mount point @@ -812,7 +886,7 @@ public class RestconfImpl implements RestconfService { value = this.normalizeNode(payload, iiWithData.getSchemaNode(), iiWithData.getMountPoint()); } else { - final InstanceIdWithSchemaNode incompleteInstIdWithData = this.controllerContext + final InstanceIdentifierContext incompleteInstIdWithData = this.controllerContext .toInstanceIdentifier(identifier); final DataNodeContainer parentSchema = (DataNodeContainer) incompleteInstIdWithData.getSchemaNode(); DOMMountPoint mountPoint = incompleteInstIdWithData.getMountPoint(); @@ -823,25 +897,29 @@ public class RestconfImpl implements RestconfService { } String payloadName = this.getName(payload); - final DataSchemaNode schemaNode = this.controllerContext.findInstanceDataChildByNameAndNamespace( + final DataSchemaNode schemaNode = ControllerContext.findInstanceDataChildByNameAndNamespace( parentSchema, payloadName, module.getNamespace()); value = this.normalizeNode(payload, schemaNode, mountPoint); - iiWithData = normalizeInstanceIdentifierWithSchemaNode(this.addLastIdentifierFromData( - incompleteInstIdWithData, value, schemaNode)); + iiWithData = addLastIdentifierFromData(incompleteInstIdWithData, value, schemaNode,incompleteInstIdWithData.getSchemaContext()); } final NormalizedNode datastoreNormalizedData = compositeNodeToDatastoreNormalizedNode(value, iiWithData.getSchemaNode()); DOMMountPoint mountPoint = iiWithData.getMountPoint(); + YangInstanceIdentifier normalizedII; + try { if (mountPoint != null) { - broker.commitConfigurationDataPost(mountPoint, - iiWithData.getInstanceIdentifier(), datastoreNormalizedData); + normalizedII = new DataNormalizer(mountPoint.getSchemaContext()).toNormalized(iiWithData + .getInstanceIdentifier()); + broker.commitConfigurationDataPost(mountPoint, normalizedII, datastoreNormalizedData); } else { - broker.commitConfigurationDataPost( - iiWithData.getInstanceIdentifier(), datastoreNormalizedData); + normalizedII = controllerContext.toNormalized(iiWithData.getInstanceIdentifier()); + broker.commitConfigurationDataPost(normalizedII, datastoreNormalizedData); } + } catch(RestconfDocumentedException e) { + throw e; } catch (Exception e) { throw new RestconfDocumentedException("Error creating data", e); } @@ -870,22 +948,26 @@ public class RestconfImpl implements RestconfService { } String payloadName = this.getName(payload); - final DataSchemaNode schemaNode = this.controllerContext.findInstanceDataChildByNameAndNamespace(module, + final DataSchemaNode schemaNode = ControllerContext.findInstanceDataChildByNameAndNamespace(module, payloadName, module.getNamespace()); final CompositeNode value = this.normalizeNode(payload, schemaNode, null); - final InstanceIdWithSchemaNode iiWithData = this.addLastIdentifierFromData(null, value, schemaNode); - RpcResult status = null; + final InstanceIdentifierContext iiWithData = this.addLastIdentifierFromData(null, value, schemaNode,ControllerContext.getInstance().getGlobalSchema()); final NormalizedNode datastoreNormalizedData = compositeNodeToDatastoreNormalizedNode(value, schemaNode); DOMMountPoint mountPoint = iiWithData.getMountPoint(); + YangInstanceIdentifier normalizedII; try { if (mountPoint != null) { - broker.commitConfigurationDataPost(mountPoint, - iiWithData.getInstanceIdentifier(), datastoreNormalizedData); + normalizedII = new DataNormalizer(mountPoint.getSchemaContext()).toNormalized(iiWithData + .getInstanceIdentifier()); + broker.commitConfigurationDataPost(mountPoint, normalizedII, datastoreNormalizedData); + } else { - broker.commitConfigurationDataPost( - iiWithData.getInstanceIdentifier(), datastoreNormalizedData); + normalizedII = controllerContext.toNormalized(iiWithData.getInstanceIdentifier()); + broker.commitConfigurationDataPost(normalizedII, datastoreNormalizedData); } + } catch(RestconfDocumentedException e) { + throw e; } catch (Exception e) { throw new RestconfDocumentedException("Error creating data", e); } @@ -895,33 +977,36 @@ public class RestconfImpl implements RestconfService { @Override public Response deleteConfigurationData(final String identifier) { - final InstanceIdWithSchemaNode iiWithData = normalizeInstanceIdentifierWithSchemaNode(this.controllerContext - .toInstanceIdentifier(identifier)); - RpcResult status = null; + final InstanceIdentifierContext iiWithData = controllerContext.toInstanceIdentifier(identifier); DOMMountPoint mountPoint = iiWithData.getMountPoint(); + YangInstanceIdentifier normalizedII; try { if (mountPoint != null) { - broker.commitConfigurationDataDelete(mountPoint, iiWithData.getInstanceIdentifier()).get(); + normalizedII = new DataNormalizer(mountPoint.getSchemaContext()).toNormalized(iiWithData + .getInstanceIdentifier()); + broker.commitConfigurationDataDelete(mountPoint, normalizedII); } else { - broker.commitConfigurationDataDelete(iiWithData.getInstanceIdentifier()).get(); + normalizedII = controllerContext.toNormalized(iiWithData.getInstanceIdentifier()); + broker.commitConfigurationDataDelete(normalizedII).get(); } } catch (Exception e) { - throw new RestconfDocumentedException("Error creating data", e); + final Optional searchedException = Iterables.tryFind(Throwables.getCausalChain(e), + Predicates.instanceOf(ModifiedNodeDoesNotExistException.class)); + if (searchedException.isPresent()) { + throw new RestconfDocumentedException("Data specified for deleting doesn't exist.", ErrorType.APPLICATION, ErrorTag.DATA_MISSING); + } + throw new RestconfDocumentedException("Error while deleting data", e); } - return Response.status(Status.OK).build(); } /** - * Subscribes to some path in schema context (stream) to listen on changes - * on this stream. + * Subscribes to some path in schema context (stream) to listen on changes on this stream. * - * Additional parameters for subscribing to stream are loaded via rpc input - * parameters: + * Additional parameters for subscribing to stream are loaded via rpc input parameters: *
    - *
  • datastore
  • - default CONFIGURATION (other values of - * {@link LogicalDatastoreType} enum type) + *
  • datastore
  • - default CONFIGURATION (other values of {@link LogicalDatastoreType} enum type) *
  • scope
  • - default BASE (other values of {@link DataChangeScope}) *
*/ @@ -971,8 +1056,7 @@ public class RestconfImpl implements RestconfService { * * @param compNode * contains value - * @return enum object if its string value is equal to {@code paramName}. In - * other cases null. + * @return enum object if its string value is equal to {@code paramName}. In other cases null. */ private T parseEnumTypeParameter(final CompositeNode compNode, final Class classDescriptor, final String paramName) { @@ -991,11 +1075,10 @@ public class RestconfImpl implements RestconfService { } /** - * Checks whether {@code value} is one of the string representation of - * enumeration {@code classDescriptor} + * Checks whether {@code value} is one of the string representation of enumeration {@code classDescriptor} * - * @return enum object if string value of {@code classDescriptor} - * enumeration is equal to {@code value}. Other cases null. + * @return enum object if string value of {@code classDescriptor} enumeration is equal to {@code value}. Other cases + * null. */ private T parserURIEnumParameter(final Class classDescriptor, final String value) { if (Strings.isNullOrEmpty(value)) { @@ -1004,7 +1087,7 @@ public class RestconfImpl implements RestconfService { return resolveAsEnum(classDescriptor, value); } - private T resolveAsEnum(Class classDescriptor, String value) { + private T resolveAsEnum(final Class classDescriptor, final String value) { T[] enumConstants = classDescriptor.getEnumConstants(); if (enumConstants != null) { for (T enm : classDescriptor.getEnumConstants()) { @@ -1016,7 +1099,7 @@ public class RestconfImpl implements RestconfService { return null; } - private Map resolveValuesFromUri(String uri) { + private Map resolveValuesFromUri(final String uri) { Map result = new HashMap<>(); String[] tokens = uri.split("/"); for (int i = 1; i < tokens.length; i++) { @@ -1064,8 +1147,8 @@ public class RestconfImpl implements RestconfService { return module; } - private InstanceIdWithSchemaNode addLastIdentifierFromData(final InstanceIdWithSchemaNode identifierWithSchemaNode, - final CompositeNode data, final DataSchemaNode schemaOfData) { + private InstanceIdentifierContext addLastIdentifierFromData(final InstanceIdentifierContext identifierWithSchemaNode, + final CompositeNode data, final DataSchemaNode schemaOfData, SchemaContext schemaContext) { YangInstanceIdentifier instanceIdentifier = null; if (identifierWithSchemaNode != null) { instanceIdentifier = identifierWithSchemaNode.getInstanceIdentifier(); @@ -1079,15 +1162,21 @@ public class RestconfImpl implements RestconfService { iiBuilder = YangInstanceIdentifier.builder(iiOriginal); } - iiBuilder.node(schemaOfData.getQName()); + if ((schemaOfData instanceof ListSchemaNode)) { + HashMap keys = this.resolveKeysFromData(((ListSchemaNode) schemaOfData), data); + iiBuilder.nodeWithKey(schemaOfData.getQName(), keys); + } else { + iiBuilder.node(schemaOfData.getQName()); + } YangInstanceIdentifier instance = iiBuilder.toInstance(); DOMMountPoint mountPoint = null; + SchemaContext schemaCtx = null; if (identifierWithSchemaNode != null) { mountPoint = identifierWithSchemaNode.getMountPoint(); } - return new InstanceIdWithSchemaNode(instance, schemaOfData, mountPoint); + return new InstanceIdentifierContext(instance, schemaOfData, mountPoint,schemaContext); } private HashMap resolveKeysFromData(final ListSchemaNode listNode, final CompositeNode dataNode) { @@ -1125,10 +1214,10 @@ public class RestconfImpl implements RestconfService { private boolean representsMountPointRootData(final Node data) { URI namespace = this.namespace(data); return (SchemaContext.NAME.getNamespace().equals(namespace) /* - * || MOUNT_POINT_MODULE_NAME .equals( namespace . - * toString( ) ) - */) - && SchemaContext.NAME.getLocalName().equals(this.localName(data)); + * || MOUNT_POINT_MODULE_NAME .equals( namespace . + * toString( ) ) + */) + && SchemaContext.NAME.getLocalName().equals(this.localName(data)); } private String addMountPointIdentifier(final String identifier) { @@ -1142,8 +1231,9 @@ public class RestconfImpl implements RestconfService { private CompositeNode normalizeNode(final Node node, final DataSchemaNode schema, final DOMMountPoint mountPoint) { if (schema == null) { - QName nodeType = node == null ? null : node.getNodeType(); - String localName = nodeType == null ? null : nodeType.getLocalName(); + String localName = node == null ? null : + node instanceof NodeWrapper ? ((NodeWrapper)node).getLocalName() : + node.getNodeType().getLocalName(); throw new RestconfDocumentedException("Data schema node was not found for " + localName, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE); @@ -1162,7 +1252,9 @@ public class RestconfImpl implements RestconfService { try { this.normalizeNode(nodeWrap, schema, null, mountPoint); } catch (IllegalArgumentException e) { - throw new RestconfDocumentedException(e.getMessage(), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE); + RestconfDocumentedException restconfDocumentedException = new RestconfDocumentedException(e.getMessage(), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE); + restconfDocumentedException.addSuppressed(e); + throw restconfDocumentedException; } if (nodeWrap instanceof CompositeNodeWrapper) { return ((CompositeNodeWrapper) nodeWrap).unwrap(); @@ -1239,12 +1331,20 @@ public class RestconfImpl implements RestconfService { final DOMMountPoint mountPoint) { final Object value = simpleNode.getValue(); Object inputValue = value; - TypeDefinition typeDefinition = this.typeDefinition(schema); - if ((typeDefinition instanceof IdentityrefTypeDefinition)) { - if ((value instanceof String)) { - inputValue = new IdentityValuesDTO(simpleNode.getNamespace().toString(), (String) value, null, - (String) value); - } // else value is already instance of IdentityValuesDTO + TypeDef typeDef = this.typeDefinition(schema); + TypeDefinition typeDefinition = typeDef != null ? typeDef.typedef : null; + + // For leafrefs, extract the type it is pointing to + if(typeDefinition instanceof LeafrefTypeDefinition) { + if (schema.getQName().equals(typeDef.qName)) { + typeDefinition = SchemaContextUtil.getBaseTypeForLeafRef(((LeafrefTypeDefinition) typeDefinition), mountPoint == null ? this.controllerContext.getGlobalSchema() : mountPoint.getSchemaContext(), schema); + } else { + typeDefinition = SchemaContextUtil.getBaseTypeForLeafRef(((LeafrefTypeDefinition) typeDefinition), mountPoint == null ? this.controllerContext.getGlobalSchema() : mountPoint.getSchemaContext(), typeDef.qName); + } + } + + if (typeDefinition instanceof IdentityrefTypeDefinition) { + inputValue = parseToIdentityValuesDTO(simpleNode, value, inputValue); } Object outputValue = inputValue; @@ -1257,12 +1357,20 @@ public class RestconfImpl implements RestconfService { simpleNode.setValue(outputValue); } + private Object parseToIdentityValuesDTO(final SimpleNodeWrapper simpleNode, final Object value, Object inputValue) { + if ((value instanceof String)) { + inputValue = new IdentityValuesDTO(simpleNode.getNamespace().toString(), (String) value, null, + (String) value); + } // else value is already instance of IdentityValuesDTO + return inputValue; + } + private void normalizeCompositeNode(final CompositeNodeWrapper compositeNodeBuilder, final DataNodeContainer schema, final DOMMountPoint mountPoint, final QName currentAugment) { final List> children = compositeNodeBuilder.getValues(); checkNodeMultiplicityAccordingToSchema(schema, children); for (final NodeWrapper child : children) { - final List potentialSchemaNodes = this.controllerContext.findInstanceDataChildrenByName( + final List potentialSchemaNodes = ControllerContext.findInstanceDataChildrenByName( schema, child.getLocalName()); if (potentialSchemaNodes.size() > 1 && child.getNamespace() == null) { @@ -1357,15 +1465,16 @@ public class RestconfImpl implements RestconfService { } if (nodeBuilder.getNamespace() == null || Objects.equal(nodeBuilder.getNamespace(), validQName.getNamespace()) - || Objects.equal(nodeBuilder.getNamespace().toString(), moduleName) /* - * || Note : this check is wrong - - * can never be true as it compares - * a URI with a String not sure what - * the intention is so commented out - * ... Objects . equal ( nodeBuilder - * . getNamespace ( ) , - * MOUNT_POINT_MODULE_NAME ) - */) { + || Objects.equal(nodeBuilder.getNamespace().toString(), moduleName)) { + /* + * || Note : this check is wrong - + * can never be true as it compares + * a URI with a String not sure what + * the intention is so commented out + * ... Objects . equal ( nodeBuilder + * . getNamespace ( ) , + * MOUNT_POINT_MODULE_NAME ) + */ nodeBuilder.setQname(validQName); } @@ -1403,29 +1512,25 @@ public class RestconfImpl implements RestconfService { } } - private TypeDefinition _typeDefinition(final LeafSchemaNode node) { - TypeDefinition baseType = node.getType(); + private TypeDef typeDefinition(final TypeDefinition type, final QName nodeQName) { + TypeDefinition baseType = type; + QName qName = nodeQName; while (baseType.getBaseType() != null) { + if (baseType instanceof ExtendedType) { + qName = baseType.getQName(); + } baseType = baseType.getBaseType(); } - return baseType; - } + return new TypeDef(baseType, qName); - private TypeDefinition typeDefinition(final LeafListSchemaNode node) { - TypeDefinition baseType = node.getType(); - while (baseType.getBaseType() != null) { - baseType = baseType.getBaseType(); - } - - return baseType; } - private TypeDefinition typeDefinition(final DataSchemaNode node) { + private TypeDef typeDefinition(final DataSchemaNode node) { if (node instanceof LeafListSchemaNode) { - return typeDefinition((LeafListSchemaNode) node); + return typeDefinition(((LeafListSchemaNode)node).getType(), node.getQName()); } else if (node instanceof LeafSchemaNode) { - return _typeDefinition((LeafSchemaNode) node); + return typeDefinition(((LeafSchemaNode)node).getType(), node.getQName()); } else if (node instanceof AnyXmlSchemaNode) { return null; } else { @@ -1433,26 +1538,16 @@ public class RestconfImpl implements RestconfService { } } - private CompositeNode datastoreNormalizedNodeToCompositeNode(NormalizedNode dataNode, DataSchemaNode schema) { - Iterable> nodes = null; + private CompositeNode datastoreNormalizedNodeToCompositeNode(final NormalizedNode dataNode, final DataSchemaNode schema) { + Node nodes = null; if (dataNode == null) { throw new RestconfDocumentedException(new RestconfError(ErrorType.APPLICATION, ErrorTag.DATA_MISSING, "No data was found.")); } - if (schema instanceof ContainerSchemaNode && dataNode instanceof ContainerNode) { - nodes = CnSnFromNormalizedNodeSerializerFactory.getInstance().getContainerNodeSerializer() - .serialize((ContainerSchemaNode) schema, (ContainerNode) dataNode); - } else if (schema instanceof ListSchemaNode && dataNode instanceof MapNode) { - nodes = CnSnFromNormalizedNodeSerializerFactory.getInstance().getMapNodeSerializer() - .serialize((ListSchemaNode) schema, (MapNode) dataNode); - } else if (schema instanceof ListSchemaNode && dataNode instanceof MapEntryNode) { - nodes = CnSnFromNormalizedNodeSerializerFactory.getInstance().getMapEntryNodeSerializer() - .serialize((ListSchemaNode) schema, (MapEntryNode) dataNode); - } + nodes = DataNormalizer.toLegacy(dataNode); if (nodes != null) { - if (nodes.iterator().hasNext()) { - Node nodeOldStruct = nodes.iterator().next(); - return (CompositeNode) nodeOldStruct; + if (nodes instanceof CompositeNode) { + return (CompositeNode) nodes; } else { LOG.error("The node " + dataNode.getNodeType() + " couldn't be transformed to compositenode."); } @@ -1465,14 +1560,15 @@ public class RestconfImpl implements RestconfService { "It wasn't possible to correctly interpret data.")); } - private NormalizedNode compositeNodeToDatastoreNormalizedNode(CompositeNode compNode, DataSchemaNode schema) { + private NormalizedNode compositeNodeToDatastoreNormalizedNode(final CompositeNode compNode, + final DataSchemaNode schema) { List> lst = new ArrayList>(); lst.add(compNode); if (schema instanceof ContainerSchemaNode) { return CnSnToNormalizedNodeParserFactory.getInstance().getContainerNodeParser() .parse(lst, (ContainerSchemaNode) schema); } else if (schema instanceof ListSchemaNode) { - return CnSnToNormalizedNodeParserFactory.getInstance().getMapNodeParser() + return CnSnToNormalizedNodeParserFactory.getInstance().getMapEntryNodeParser() .parse(lst, (ListSchemaNode) schema); } @@ -1482,19 +1578,20 @@ public class RestconfImpl implements RestconfService { "It wasn't possible to translate specified data to datastore readable form.")); } - private InstanceIdWithSchemaNode normalizeInstanceIdentifierWithSchemaNode(InstanceIdWithSchemaNode iiWithSchemaNode) { + private InstanceIdentifierContext normalizeInstanceIdentifierWithSchemaNode( + final InstanceIdentifierContext iiWithSchemaNode) { return normalizeInstanceIdentifierWithSchemaNode(iiWithSchemaNode, false); } - private InstanceIdWithSchemaNode normalizeInstanceIdentifierWithSchemaNode( - InstanceIdWithSchemaNode iiWithSchemaNode, boolean unwrapLastListNode) { - return new InstanceIdWithSchemaNode(instanceIdentifierToReadableFormForNormalizeNode( + private InstanceIdentifierContext normalizeInstanceIdentifierWithSchemaNode( + final InstanceIdentifierContext iiWithSchemaNode, final boolean unwrapLastListNode) { + return new InstanceIdentifierContext(instanceIdentifierToReadableFormForNormalizeNode( iiWithSchemaNode.getInstanceIdentifier(), unwrapLastListNode), iiWithSchemaNode.getSchemaNode(), - iiWithSchemaNode.getMountPoint()); + iiWithSchemaNode.getMountPoint(),iiWithSchemaNode.getSchemaContext()); } - private YangInstanceIdentifier instanceIdentifierToReadableFormForNormalizeNode(YangInstanceIdentifier instIdentifier, - boolean unwrapLastListNode) { + private YangInstanceIdentifier instanceIdentifierToReadableFormForNormalizeNode( + final YangInstanceIdentifier instIdentifier, final boolean unwrapLastListNode) { Preconditions.checkNotNull(instIdentifier, "Instance identifier can't be null"); final List result = new ArrayList(); final Iterator iter = instIdentifier.getPathArguments().iterator(); @@ -1527,4 +1624,9 @@ public class RestconfImpl implements RestconfService { } return false; } + + public BigInteger getOperationalReceived() { + // TODO Auto-generated method stub + return null; + } }