Refactor source export 50/96850/3
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 9 Jul 2021 09:53:51 +0000 (11:53 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 9 Jul 2021 12:24:38 +0000 (14:24 +0200)
Add AbstractSchemaExportBodyWriter and specialize it for YIN/YANG.
Also eliminate jersey.provides.schema package, as it does not really
make sense.

JIRA: NETCONF-773
Change-Id: Ia099e927da10f451cd843ba947a79312dd35af19
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/AbstractRestconfApplication.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/AbstractSchemaExportBodyWriter.java [new file with mode: 0644]
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/YangSchemaExportBodyWriter.java [moved from restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/schema/SchemaExportContentYangBodyWriter.java with 75% similarity]
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/YinSchemaExportBodyWriter.java [moved from restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/schema/SchemaExportContentYinBodyWriter.java with 64% similarity]

index 88ce0f83e1da2b1b3273e91177f9f392005faf64..b4906a2162a8c9c708984945e8e716145c8e43a2 100644 (file)
@@ -19,13 +19,13 @@ import org.opendaylight.restconf.nb.rfc8040.jersey.providers.JsonNormalizedNodeB
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.NormalizedNodeJsonBodyWriter;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.NormalizedNodeXmlBodyWriter;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.XmlNormalizedNodeBodyReader;
+import org.opendaylight.restconf.nb.rfc8040.jersey.providers.YangSchemaExportBodyWriter;
+import org.opendaylight.restconf.nb.rfc8040.jersey.providers.YinSchemaExportBodyWriter;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.errors.RestconfDocumentedExceptionMapper;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.JsonToPatchBodyReader;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.PatchJsonBodyWriter;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.PatchXmlBodyWriter;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.XmlToPatchBodyReader;
-import org.opendaylight.restconf.nb.rfc8040.jersey.providers.schema.SchemaExportContentYangBodyWriter;
-import org.opendaylight.restconf.nb.rfc8040.jersey.providers.schema.SchemaExportContentYinBodyWriter;
 
 /**
  * Abstract Restconf Application.
@@ -46,7 +46,7 @@ abstract class AbstractRestconfApplication extends Application {
     public final Set<Class<?>> getClasses() {
         return Set.of(
             NormalizedNodeJsonBodyWriter.class, NormalizedNodeXmlBodyWriter.class,
-            SchemaExportContentYinBodyWriter.class, SchemaExportContentYangBodyWriter.class,
+            YinSchemaExportBodyWriter.class, YangSchemaExportBodyWriter.class,
             PatchJsonBodyWriter.class, PatchXmlBodyWriter.class);
     }
 
diff --git a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/AbstractSchemaExportBodyWriter.java b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/AbstractSchemaExportBodyWriter.java
new file mode 100644 (file)
index 0000000..1cb874c
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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.restconf.nb.rfc8040.jersey.providers;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.ext.MessageBodyWriter;
+import org.opendaylight.restconf.common.schema.SchemaExportContext;
+
+abstract class AbstractSchemaExportBodyWriter implements MessageBodyWriter<SchemaExportContext> {
+    @Override
+    public final boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
+            final MediaType mediaType) {
+        return type.equals(SchemaExportContext.class);
+    }
+}
@@ -5,7 +5,7 @@
  * 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.restconf.nb.rfc8040.jersey.providers.schema;
+package org.opendaylight.restconf.nb.rfc8040.jersey.providers;
 
 import java.io.IOException;
 import java.io.OutputStream;
@@ -16,7 +16,6 @@ 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;
@@ -24,20 +23,12 @@ 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);
-    }
-
+@Produces(YangConstants.RFC6020_YANG_MEDIA_TYPE)
+public class YangSchemaExportBodyWriter extends AbstractSchemaExportBodyWriter {
     @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 MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException {
         final RevisionSourceIdentifier sourceId = RevisionSourceIdentifier.create(context.getModule().getName(),
                 context.getModule().getQNameModule().getRevision());
         final YangTextSchemaSource yangTextSchemaSource;
@@ -5,16 +5,15 @@
  * 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.restconf.nb.rfc8040.jersey.providers.schema;
+package org.opendaylight.restconf.nb.rfc8040.jersey.providers;
 
+import java.io.IOException;
 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;
@@ -22,24 +21,16 @@ 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);
-    }
-
+@Produces(YangConstants.RFC6020_YIN_MEDIA_TYPE)
+public class YinSchemaExportBodyWriter extends AbstractSchemaExportBodyWriter {
     @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 {
+            final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException {
         try {
-            YinExportUtils.writeModuleAsYinText(context.getModule(), entityStream);
+            YinExportUtils.writeModuleAsYinText(context.getModule().asEffectiveStatement(), entityStream);
         } catch (final XMLStreamException e) {
-            throw new IllegalStateException(e);
+            throw new IOException("Failed to export module", e);
         }
     }
 }