X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=restconf%2Fsal-rest-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Frestconf%2Frest%2Fservices%2Fimpl%2FRestconfOperationsServiceImpl.java;h=bddd086bd702a5427d63c99000a66df654ad74ea;hb=a8f0f0bc9a3a27bdf012c5321882c96ef7de46f4;hp=fec4c3f659390d694a8f92c7505bca4b6199c514;hpb=dbc82390d9d0a4559e2dda27ca8fe143f5377c41;p=netconf.git diff --git a/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/rest/services/impl/RestconfOperationsServiceImpl.java b/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/rest/services/impl/RestconfOperationsServiceImpl.java index fec4c3f659..bddd086bd7 100644 --- a/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/rest/services/impl/RestconfOperationsServiceImpl.java +++ b/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/rest/services/impl/RestconfOperationsServiceImpl.java @@ -7,13 +7,38 @@ */ package org.opendaylight.restconf.rest.services.impl; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import javax.ws.rs.core.UriInfo; +import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; +import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; +import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext; import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext; import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException; +import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag; +import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType; +import org.opendaylight.restconf.common.references.SchemaContextRef; import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler; import org.opendaylight.restconf.handlers.SchemaContextHandler; import org.opendaylight.restconf.rest.services.api.RestconfOperationsService; +import org.opendaylight.restconf.utils.RestconfConstants; +import org.opendaylight.restconf.utils.parser.ParserIdentifier; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; +import org.opendaylight.yangtools.yang.data.api.schema.LeafNode; +import org.opendaylight.yangtools.yang.data.impl.schema.Builders; +import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder; +import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; +import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; +import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.model.api.RpcDefinition; import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Implementation of {@link RestconfOperationsService} @@ -21,6 +46,8 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext; */ public class RestconfOperationsServiceImpl implements RestconfOperationsService { + private static final Logger LOG = LoggerFactory.getLogger(RestconfOperationsServiceImpl.class); + private final SchemaContextHandler schemaContextHandler; private final DOMMountPointServiceHandler domMountPointServiceHandler; @@ -40,13 +67,71 @@ public class RestconfOperationsServiceImpl implements RestconfOperationsService @Override public NormalizedNodeContext getOperations(final UriInfo uriInfo) { - throw new RestconfDocumentedException("Not yet implemented.", new UnsupportedOperationException()); + final SchemaContextRef ref = new SchemaContextRef(this.schemaContextHandler.get()); + return getOperations(ref.getModules(), null); } - @Override public NormalizedNodeContext getOperations(final String identifier, final UriInfo uriInfo) { - throw new RestconfDocumentedException("Not yet implemented.", new UnsupportedOperationException()); + Set modules = null; + DOMMountPoint mountPoint = null; + final SchemaContextRef ref = new SchemaContextRef(this.schemaContextHandler.get()); + if (identifier.contains(RestconfConstants.MOUNT)) { + final InstanceIdentifierContext mountPointIdentifier = ParserIdentifier.toInstanceIdentifier(identifier, + ref.get()); + mountPoint = mountPointIdentifier.getMountPoint(); + modules = ref.getModules(mountPoint); + + } else { + final String errMsg = "URI has bad format. If operations behind mount point should be showed, URI has to end with "; + LOG.debug(errMsg + ControllerContext.MOUNT + " for " + identifier); + throw new RestconfDocumentedException(errMsg + ControllerContext.MOUNT, ErrorType.PROTOCOL, + ErrorTag.INVALID_VALUE); + } + + return getOperations(modules, mountPoint); + } + + /** + * Special case only for GET restconf/operations use (since moment of old + * Yang parser and old Yang model API removal). The method is creating fake + * schema context with fake module and fake data by use own implementations + * of schema nodes and module. + * + * @param modules + * - set of modules for get RPCs from every module + * @param mountPoint + * - mount point, if in use otherwise null + * @return {@link NormalizedNodeContext} + */ + private static NormalizedNodeContext getOperations(final Set modules, final DOMMountPoint mountPoint) { + final ContainerSchemaNodeImpl fakeCont = new ContainerSchemaNodeImpl(); + final List> listRpcNodes = new ArrayList<>(); + for (final Module m : modules) { + for (final RpcDefinition rpc : m.getRpcs()) { + + final LeafSchemaNode fakeLeaf = new LeafSchemaNodeImpl(fakeCont.getPath(), + QName.create(ModuleImpl.moduleQName, m.getName() + ":" + rpc.getQName().getLocalName())); + fakeCont.addNodeChild(fakeLeaf); + listRpcNodes.add(Builders.leafBuilder(fakeLeaf).build()); + } + } + final ContainerSchemaNode fakeContSchNode = fakeCont; + final DataContainerNodeAttrBuilder containerBuilder = Builders + .containerBuilder(fakeContSchNode); + + for (final LeafNode rpcNode : listRpcNodes) { + containerBuilder.withChild(rpcNode); + } + + final Module fakeModule = new ModuleImpl(fakeContSchNode); + + final Set fakeModules = new HashSet<>(); + fakeModules.add(fakeModule); + final SchemaContext fakeSchemaCtx = EffectiveSchemaContext.resolveSchemaContext(fakeModules); + final InstanceIdentifierContext instanceIdentifierContext = new InstanceIdentifierContext<>( + null, fakeContSchNode, mountPoint, fakeSchemaCtx); + return new NormalizedNodeContext(instanceIdentifierContext, containerBuilder.build()); } }