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.java;h=c329f525a952d162eb96c5ba960dc002ecf3f9cc;hb=a59286ee063c90817958e9cef4836e15aec3c7a6;hp=1514a15d1132417849a0efb29e53b0ea26113b8f;hpb=9a6190b662c5f8de44df872b0ba7e8dd17f8da13;p=controller.git 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 1514a15d11..c329f525a9 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 @@ -10,12 +10,14 @@ package org.opendaylight.controller.sal.restconf.impl; import com.google.common.base.Optional; import com.google.common.base.Preconditions; +import com.google.common.base.Predicate; 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.Iterables; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.google.common.util.concurrent.CheckedFuture; import com.google.common.util.concurrent.Futures; @@ -24,6 +26,7 @@ import java.net.URI; import java.net.URISyntaxException; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.HashMap; @@ -48,6 +51,7 @@ import org.opendaylight.controller.md.sal.dom.api.DOMRpcException; import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult; import org.opendaylight.controller.md.sal.dom.api.DOMRpcService; import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult; +import org.opendaylight.controller.md.sal.rest.common.RestconfValidationUtils; import org.opendaylight.controller.sal.rest.api.Draft02; import org.opendaylight.controller.sal.rest.api.RestconfService; import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag; @@ -64,6 +68,7 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgum import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; +import org.opendaylight.yangtools.yang.data.api.schema.LeafNode; import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.MapNode; @@ -87,7 +92,13 @@ import org.opendaylight.yangtools.yang.model.api.RpcDefinition; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaPath; +import org.opendaylight.yangtools.yang.model.util.EmptyType; import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; +import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder; +import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder; +import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder; +import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder; +import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -320,12 +331,67 @@ public class RestconfImpl implements RestconfService { return operationsFromModulesToNormalizedContext(modules, mountPoint); } + private static final Predicate GROUPING_FILTER = new Predicate() { + @Override + public boolean apply(final GroupingBuilder g) { + return Draft02.RestConfModule.RESTCONF_GROUPING_SCHEMA_NODE.equals(g.getQName().getLocalName()); + } + }; + private NormalizedNodeContext operationsFromModulesToNormalizedContext(final Set modules, final DOMMountPoint mountPoint) { - // FIXME find best way to change restconf-netconf yang schema for provide this functionality - final String errMsg = "We are not able support view operations functionality yet."; - throw new RestconfDocumentedException(errMsg, ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED); + final Module restconfModule = getRestconfModule(); + final ModuleBuilder restConfModuleBuilder = new ModuleBuilder(restconfModule); + final Set gropingBuilders = restConfModuleBuilder.getGroupingBuilders(); + final Iterable filteredGroups = Iterables.filter(gropingBuilders, GROUPING_FILTER); + final GroupingBuilder restconfGroupingBuilder = Iterables.getFirst(filteredGroups, null); + final ContainerSchemaNodeBuilder restContainerSchemaNodeBuilder = (ContainerSchemaNodeBuilder) restconfGroupingBuilder + .getDataChildByName(Draft02.RestConfModule.RESTCONF_CONTAINER_SCHEMA_NODE); + final ContainerSchemaNodeBuilder containerSchemaNodeBuilder = (ContainerSchemaNodeBuilder) restContainerSchemaNodeBuilder + .getDataChildByName(Draft02.RestConfModule.OPERATIONS_CONTAINER_SCHEMA_NODE); + + final ContainerSchemaNodeBuilder fakeOperationsSchemaNodeBuilder = containerSchemaNodeBuilder; + final SchemaPath fakeSchemaPath = fakeOperationsSchemaNodeBuilder.getPath().createChild(QName.create("dummy")); + + final List> operationsAsData = new ArrayList<>(); + + for (final Module module : modules) { + final Set rpcs = module.getRpcs(); + for (final RpcDefinition rpc : rpcs) { + final QName rpcQName = rpc.getQName(); + final String name = module.getName(); + + final QName qName = QName.create(restconfModule.getQNameModule(), rpcQName.getLocalName()); + final LeafSchemaNodeBuilder leafSchemaNodeBuilder = new LeafSchemaNodeBuilder(name, 0, qName, fakeSchemaPath); + final LeafSchemaNodeBuilder fakeRpcSchemaNodeBuilder = leafSchemaNodeBuilder; + fakeRpcSchemaNodeBuilder.setAugmenting(true); + + final EmptyType instance = EmptyType.getInstance(); + fakeRpcSchemaNodeBuilder.setType(instance); + final LeafSchemaNode fakeRpcSchemaNode = fakeRpcSchemaNodeBuilder.build(); + fakeOperationsSchemaNodeBuilder.addChildNode(fakeRpcSchemaNode); + + final LeafNode leaf = Builders.leafBuilder(fakeRpcSchemaNode).build(); + operationsAsData.add(leaf); + } + } + + final ContainerSchemaNode operContainerSchemaNode = fakeOperationsSchemaNodeBuilder.build(); + final DataContainerNodeAttrBuilder operContainerNode = Builders.containerBuilder(operContainerSchemaNode); + + for (final LeafNode oper : operationsAsData) { + operContainerNode.withChild(oper); + } + + final Set fakeRpcModules = Collections.singleton(restConfModuleBuilder.build()); + + final YangParserImpl yangParser = new YangParserImpl(); + final SchemaContext fakeSchemaCx = yangParser.resolveSchemaContext(fakeRpcModules); + + final InstanceIdentifierContext fakeIICx = new InstanceIdentifierContext<>(null, operContainerSchemaNode, mountPoint, fakeSchemaCx); + + return new NormalizedNodeContext(fakeIICx, operContainerNode.build()); } private Module getRestconfModule() { @@ -417,18 +483,13 @@ public class RestconfImpl implements RestconfService { return retValue; } throw new RestconfDocumentedException("RpcError message", null, retValue.getErrors()); - } - catch (final InterruptedException e) { + } catch (final InterruptedException e) { throw new RestconfDocumentedException( "The operation was interrupted while executing and did not complete.", ErrorType.RPC, ErrorTag.PARTIAL_OPERATION); - } - catch (final ExecutionException e) { + } catch (final ExecutionException e) { Throwable cause = e.getCause(); - if (cause instanceof CancellationException) { - throw new RestconfDocumentedException("The operation was cancelled while executing.", ErrorType.RPC, - ErrorTag.PARTIAL_OPERATION); - } else if (cause != null) { + if (cause != null) { while (cause.getCause() != null) { cause = cause.getCause(); } @@ -444,6 +505,9 @@ public class RestconfImpl implements RestconfService { throw new RestconfDocumentedException("The operation encountered an unexpected error while executing.", e); } + } catch (final CancellationException e) { + throw new RestconfDocumentedException("The operation was cancelled while executing.", ErrorType.RPC, + ErrorTag.PARTIAL_OPERATION); } } @@ -673,7 +737,7 @@ public class RestconfImpl implements RestconfService { validateInput(iiWithData.getSchemaNode(), payload); validateTopLevelNodeName(payload, iiWithData.getInstanceIdentifier()); - validateListKeysEqualityInPayloadAndUri(iiWithData, payload.getData()); + validateListKeysEqualityInPayloadAndUri(payload); final DOMMountPoint mountPoint = iiWithData.getMountPoint(); final YangInstanceIdentifier normalizedII = iiWithData.getInstanceIdentifier(); @@ -749,41 +813,38 @@ public class RestconfImpl implements RestconfService { * if key values or key count in payload and URI isn't equal * */ - private void validateListKeysEqualityInPayloadAndUri(final InstanceIdentifierContext iiWithData, - final NormalizedNode payload) { - if (iiWithData.getSchemaNode() instanceof ListSchemaNode) { - final List keyDefinitions = ((ListSchemaNode) iiWithData.getSchemaNode()).getKeyDefinition(); - final PathArgument lastPathArgument = iiWithData.getInstanceIdentifier().getLastPathArgument(); - if (lastPathArgument instanceof NodeIdentifierWithPredicates) { - final Map uriKeyValues = ((NodeIdentifierWithPredicates) lastPathArgument) - .getKeyValues(); - isEqualUriAndPayloadKeyValues(uriKeyValues, payload, keyDefinitions); + private static void validateListKeysEqualityInPayloadAndUri(final NormalizedNodeContext payload) { + Preconditions.checkArgument(payload != null); + final InstanceIdentifierContext iiWithData = payload.getInstanceIdentifierContext(); + final PathArgument lastPathArgument = iiWithData.getInstanceIdentifier().getLastPathArgument(); + final SchemaNode schemaNode = iiWithData.getSchemaNode(); + final NormalizedNode data = payload.getData(); + if (schemaNode instanceof ListSchemaNode) { + final List keyDefinitions = ((ListSchemaNode) schemaNode).getKeyDefinition(); + if (lastPathArgument instanceof NodeIdentifierWithPredicates && data instanceof MapEntryNode) { + final Map uriKeyValues = ((NodeIdentifierWithPredicates) lastPathArgument).getKeyValues(); + isEqualUriAndPayloadKeyValues(uriKeyValues, (MapEntryNode) data, keyDefinitions); } } } - private void isEqualUriAndPayloadKeyValues(final Map uriKeyValues, final NormalizedNode payload, - final List keyDefinitions) { + private static void isEqualUriAndPayloadKeyValues(final Map uriKeyValues, + final MapEntryNode payload, final List keyDefinitions) { + + final Map mutableCopyUriKeyValues = Maps.newHashMap(uriKeyValues); for (final QName keyDefinition : keyDefinitions) { - final Object uriKeyValue = uriKeyValues.get(keyDefinition); + final Object uriKeyValue = mutableCopyUriKeyValues.remove(keyDefinition); // should be caught during parsing URI to InstanceIdentifier - if (uriKeyValue == null) { - final String errMsg = "Missing key " + keyDefinition + " in URI."; - throw new RestconfDocumentedException(errMsg, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING); + RestconfValidationUtils.checkDocumentedError(uriKeyValue != null, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING, + "Missing key " + keyDefinition + " in URI."); + + final Object dataKeyValue = payload.getIdentifier().getKeyValues().get(keyDefinition); + + if ( ! uriKeyValue.equals(dataKeyValue)) { + final String errMsg = "The value '" + uriKeyValue + "' for key '" + keyDefinition.getLocalName() + + "' specified in the URI doesn't match the value '" + dataKeyValue + "' specified in the message body. "; + throw new RestconfDocumentedException(errMsg, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE); } - // TODO thing about the possibility to fix -// final List> payloadKeyValues = payload.getSimpleNodesByName(keyDefinition.getLocalName()); -// if (payloadKeyValues.isEmpty()) { -// final String errMsg = "Missing key " + keyDefinition.getLocalName() + " in the message body."; -// throw new RestconfDocumentedException(errMsg, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING); -// } -// -// final Object payloadKeyValue = payloadKeyValues.iterator().next().getValue(); -// if (!uriKeyValue.equals(payloadKeyValue)) { -// final String errMsg = "The value '" + uriKeyValue + "' for key '" + keyDefinition.getLocalName() + -// "' specified in the URI doesn't match the value '" + payloadKeyValue + "' specified in the message body. "; -// throw new RestconfDocumentedException(errMsg, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE); -// } } }