diff options
author | Dan Timoney <dtimoney@att.com> | 2023-01-05 09:31:25 -0500 |
---|---|---|
committer | Dan Timoney <dtimoney@att.com> | 2023-01-05 09:31:25 -0500 |
commit | d4d6fbd430eb502cce6cb01a667ec799d487a510 (patch) | |
tree | aea94837cd313f27f68e625d2b8277960fdf7af6 /netconf/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/md | |
parent | d0508aaeb7af3f29095d1d0ef7dbf69af18f0d99 (diff) |
Seed code for biermann restconf
Seed initial code from OpenDaylight netconf project for
Biermann draft version of restconf API
Issue-ID: CCSDK-3783
Signed-off-by: Dan Timoney <dtimoney@att.com>
Change-Id: I8a1ad2050ee7addbb480f01bd448922803bff31f
Diffstat (limited to 'netconf/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/md')
4 files changed, 230 insertions, 0 deletions
diff --git a/netconf/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/md/sal/rest/schema/SchemaExportContentYangBodyWriter.java b/netconf/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/md/sal/rest/schema/SchemaExportContentYangBodyWriter.java new file mode 100644 index 0000000..c377650 --- /dev/null +++ b/netconf/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/md/sal/rest/schema/SchemaExportContentYangBodyWriter.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.netconf.md.sal.rest.schema; + +import java.io.IOException; +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.util.concurrent.ExecutionException; +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; +import javax.ws.rs.ext.Provider; +import org.opendaylight.restconf.common.schema.SchemaExportContext; +import org.opendaylight.yangtools.yang.common.YangConstants; +import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier; +import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource; + +@Provider +@Produces({ YangConstants.RFC6020_YANG_MEDIA_TYPE }) +public class SchemaExportContentYangBodyWriter implements MessageBodyWriter<SchemaExportContext> { + + @Override + public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations, + final MediaType mediaType) { + return type.equals(SchemaExportContext.class); + } + + @Override + public long getSize(final SchemaExportContext context, final Class<?> type, final Type genericType, + final Annotation[] annotations, final MediaType mediaType) { + return -1; + } + + @Override + public void writeTo(final SchemaExportContext context, final Class<?> type, final Type genericType, + final Annotation[] annotations, final MediaType mediaType, + final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException, + WebApplicationException { + final RevisionSourceIdentifier sourceId = RevisionSourceIdentifier.create(context.getModule().getName(), + context.getModule().getQNameModule().getRevision()); + final YangTextSchemaSource yangTextSchemaSource; + try { + yangTextSchemaSource = context.getSourceProvider().getSource(sourceId).get(); + } catch (InterruptedException | ExecutionException e) { + throw new WebApplicationException("Unable to retrieve source from SourceProvider.", e); + } + yangTextSchemaSource.copyTo(entityStream); + } +} diff --git a/netconf/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/md/sal/rest/schema/SchemaExportContentYinBodyWriter.java b/netconf/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/md/sal/rest/schema/SchemaExportContentYinBodyWriter.java new file mode 100644 index 0000000..9f4f8be --- /dev/null +++ b/netconf/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/md/sal/rest/schema/SchemaExportContentYinBodyWriter.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.netconf.md.sal.rest.schema; + +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; +import javax.ws.rs.ext.Provider; +import javax.xml.stream.XMLStreamException; +import org.opendaylight.restconf.common.schema.SchemaExportContext; +import org.opendaylight.yangtools.yang.common.YangConstants; +import org.opendaylight.yangtools.yang.model.export.YinExportUtils; + +@Provider +@Produces({ YangConstants.RFC6020_YIN_MEDIA_TYPE }) +public class SchemaExportContentYinBodyWriter implements MessageBodyWriter<SchemaExportContext> { + + @Override + public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations, + final MediaType mediaType) { + return type.equals(SchemaExportContext.class); + } + + @Override + public long getSize(final SchemaExportContext context, final Class<?> type, final Type genericType, + final Annotation[] annotations, final MediaType mediaType) { + return -1; + } + + @Override + public void writeTo(final SchemaExportContext context, final Class<?> type, final Type genericType, + final Annotation[] annotations, final MediaType mediaType, + final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws + WebApplicationException { + try { + YinExportUtils.writeModuleAsYinText(context.getModule(), entityStream); + } catch (final XMLStreamException e) { + throw new IllegalStateException(e); + } + } +} diff --git a/netconf/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/md/sal/rest/schema/SchemaRetrievalService.java b/netconf/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/md/sal/rest/schema/SchemaRetrievalService.java new file mode 100644 index 0000000..a20d041 --- /dev/null +++ b/netconf/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/md/sal/rest/schema/SchemaRetrievalService.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.netconf.md.sal.rest.schema; + +import com.google.common.annotations.Beta; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import org.opendaylight.restconf.common.schema.SchemaExportContext; +import org.opendaylight.yangtools.yang.common.YangConstants; + +/** + * Retrieval of the YANG modules which server supports. + * + * @deprecated do not use this api. It is replaced by RestconfSchemaService + */ +@Deprecated +@Beta +public interface SchemaRetrievalService { + @GET + @Produces({YangConstants.RFC6020_YIN_MEDIA_TYPE, YangConstants.RFC6020_YANG_MEDIA_TYPE}) + @Path("/modules/module/{identifier:.+}/schema") + SchemaExportContext getSchema(@PathParam("identifier") String mountAndModuleId); +} diff --git a/netconf/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/md/sal/rest/schema/SchemaRetrievalServiceImpl.java b/netconf/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/md/sal/rest/schema/SchemaRetrievalServiceImpl.java new file mode 100644 index 0000000..ad23992 --- /dev/null +++ b/netconf/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/md/sal/rest/schema/SchemaRetrievalServiceImpl.java @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.netconf.md.sal.rest.schema; + +import com.google.common.base.Splitter; +import com.google.common.collect.Iterables; +import java.time.format.DateTimeParseException; +import java.util.Iterator; +import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider; +import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; +import org.opendaylight.restconf.common.context.InstanceIdentifierContext; +import org.opendaylight.restconf.common.errors.RestconfDocumentedException; +import org.opendaylight.restconf.common.schema.SchemaExportContext; +import org.opendaylight.restconf.common.validation.RestconfValidationUtils; +import org.opendaylight.yangtools.yang.common.ErrorTag; +import org.opendaylight.yangtools.yang.common.ErrorType; +import org.opendaylight.yangtools.yang.common.Revision; +import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; + +public class SchemaRetrievalServiceImpl implements SchemaRetrievalService { + + private final ControllerContext salContext; + + private static final Splitter SLASH_SPLITTER = Splitter.on("/"); + private static final String MOUNT_ARG = ControllerContext.MOUNT; + + public SchemaRetrievalServiceImpl(final ControllerContext controllerContext) { + salContext = controllerContext; + } + + + @Override + public SchemaExportContext getSchema(final String mountAndModule) { + final SchemaContext schemaContext; + final Iterable<String> pathComponents = SLASH_SPLITTER.split(mountAndModule); + final Iterator<String> componentIter = pathComponents.iterator(); + if (!Iterables.contains(pathComponents, MOUNT_ARG)) { + schemaContext = salContext.getGlobalSchema(); + } else { + final StringBuilder pathBuilder = new StringBuilder(); + while (componentIter.hasNext()) { + final String current = componentIter.next(); + // It is argument, not last element. + if (pathBuilder.length() != 0) { + pathBuilder.append("/"); + } + pathBuilder.append(current); + if (MOUNT_ARG.equals(current)) { + // We stop right at mountpoint, last two arguments should + // be module name and revision + break; + } + } + schemaContext = getMountSchemaContext(pathBuilder.toString()); + + } + + RestconfDocumentedException.throwIf(!componentIter.hasNext(), "Module name must be supplied.", + ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE); + final String moduleName = componentIter.next(); + RestconfDocumentedException.throwIf(!componentIter.hasNext(), "Revision date must be supplied.", + ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE); + final String revisionString = componentIter.next(); + return getExportUsingNameAndRevision(schemaContext, moduleName, revisionString, + salContext.getYangTextSourceProvider()); + } + + private static SchemaExportContext getExportUsingNameAndRevision(final SchemaContext schemaContext, + final String moduleName, final String revisionStr, + final DOMYangTextSourceProvider yangTextSourceProvider) { + try { + final Module module = schemaContext.findModule(moduleName, Revision.of(revisionStr)).orElse(null); + return new SchemaExportContext( + schemaContext, RestconfValidationUtils.checkNotNullDocumented(module, moduleName), + yangTextSourceProvider); + } catch (final DateTimeParseException e) { + throw new RestconfDocumentedException("Supplied revision is not in expected date format YYYY-mm-dd", e); + } + } + + private SchemaContext getMountSchemaContext(final String identifier) { + final InstanceIdentifierContext mountContext = salContext.toMountPointIdentifier(identifier); + return mountContext.getSchemaContext(); + } +} + |