From e4d0ef3666d38ff47f7edc905ed12a68b0656209 Mon Sep 17 00:00:00 2001 From: lubos-cicut Date: Thu, 14 Dec 2023 18:56:13 +0100 Subject: [PATCH] Refactor SchemasStreams class Refactored SchemasStream in order to have separate *Stream class for components and schemas. JIRA: NETCONF-938 Change-Id: I69a02493f370d60ffe3f8d905456275277d012b9 Signed-off-by: lubos-cicut --- .../openapi/impl/ComponentsStream.java | 87 +++++++++++++++++++ .../openapi/impl/OpenApiInputStream.java | 2 +- .../restconf/openapi/impl/SchemasStream.java | 21 ----- 3 files changed, 88 insertions(+), 22 deletions(-) create mode 100644 restconf/restconf-openapi/src/main/java/org/opendaylight/restconf/openapi/impl/ComponentsStream.java diff --git a/restconf/restconf-openapi/src/main/java/org/opendaylight/restconf/openapi/impl/ComponentsStream.java b/restconf/restconf-openapi/src/main/java/org/opendaylight/restconf/openapi/impl/ComponentsStream.java new file mode 100644 index 0000000000..c501894f16 --- /dev/null +++ b/restconf/restconf-openapi/src/main/java/org/opendaylight/restconf/openapi/impl/ComponentsStream.java @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2024 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.openapi.impl; + +import com.fasterxml.jackson.core.JsonGenerator; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.nio.charset.StandardCharsets; +import java.util.Iterator; +import java.util.Map; +import org.opendaylight.restconf.openapi.jaxrs.OpenApiBodyWriter; +import org.opendaylight.restconf.openapi.model.security.Http; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; +import org.opendaylight.yangtools.yang.model.api.Module; + +public final class ComponentsStream extends InputStream { + private static final String BASIC_AUTH_NAME = "basicAuth"; + private static final Http OPEN_API_BASIC_AUTH = new Http("basic", null, null); + + private final Iterator iterator; + private final OpenApiBodyWriter writer; + private final EffectiveModelContext context; + private final JsonGenerator generator; + private final ByteArrayOutputStream stream; + + private boolean schemasWritten; + private boolean securityWritten; + private Reader reader; + + public ComponentsStream(final EffectiveModelContext context, final OpenApiBodyWriter writer, + final JsonGenerator generator, final ByteArrayOutputStream stream, + final Iterator iterator) { + this.iterator = iterator; + this.context = context; + this.writer = writer; + this.generator = generator; + this.stream = stream; + } + + @Override + public int read() throws IOException { + if (reader == null) { + generator.writeObjectFieldStart("components"); + generator.flush(); + reader = new InputStreamReader(new ByteArrayInputStream(stream.toByteArray()), StandardCharsets.UTF_8); + stream.reset(); + } + + var read = reader.read(); + while (read == -1) { + if (!schemasWritten) { + reader = new InputStreamReader(new SchemasStream(context, writer, generator, stream, iterator), + StandardCharsets.UTF_8); + read = reader.read(); + schemasWritten = true; + continue; + } + if (!securityWritten) { + reader = new InputStreamReader(new SecuritySchemesStream(writer, Map.of(BASIC_AUTH_NAME, + OPEN_API_BASIC_AUTH)), StandardCharsets.UTF_8); + read = reader.read(); + securityWritten = true; + generator.writeEndObject(); + continue; + } + generator.flush(); + reader = new InputStreamReader(new ByteArrayInputStream(stream.toByteArray()), StandardCharsets.UTF_8); + stream.reset(); + return reader.read(); + } + return read; + } + + @Override + public int read(final byte[] array, final int off, final int len) throws IOException { + return super.read(array, off, len); + } +} diff --git a/restconf/restconf-openapi/src/main/java/org/opendaylight/restconf/openapi/impl/OpenApiInputStream.java b/restconf/restconf-openapi/src/main/java/org/opendaylight/restconf/openapi/impl/OpenApiInputStream.java index b9790ece6f..08ef39c3fd 100644 --- a/restconf/restconf-openapi/src/main/java/org/opendaylight/restconf/openapi/impl/OpenApiInputStream.java +++ b/restconf/restconf-openapi/src/main/java/org/opendaylight/restconf/openapi/impl/OpenApiInputStream.java @@ -50,7 +50,7 @@ public final class OpenApiInputStream extends InputStream { stack.add(new ServersStream(new ServersEntity(List.of(new ServerEntity(url))), writer)); stack.add(new PathsStream(context, writer, generator, stream, deviceName, urlPrefix, isForSingleModule, includeDataStore, modules.iterator())); - stack.add(new SchemasStream(context, writer, generator, stream, modules.iterator())); + stack.add(new ComponentsStream(context, writer, generator, stream, modules.iterator())); stack.add(new SecurityStream(writer, new SecurityEntity(security))); } diff --git a/restconf/restconf-openapi/src/main/java/org/opendaylight/restconf/openapi/impl/SchemasStream.java b/restconf/restconf-openapi/src/main/java/org/opendaylight/restconf/openapi/impl/SchemasStream.java index b615b8c39b..0abc53ceaf 100644 --- a/restconf/restconf-openapi/src/main/java/org/opendaylight/restconf/openapi/impl/SchemasStream.java +++ b/restconf/restconf-openapi/src/main/java/org/opendaylight/restconf/openapi/impl/SchemasStream.java @@ -21,10 +21,8 @@ import java.util.ArrayList; import java.util.Deque; import java.util.Iterator; import java.util.List; -import java.util.Map; import org.opendaylight.restconf.openapi.jaxrs.OpenApiBodyWriter; import org.opendaylight.restconf.openapi.model.SchemaEntity; -import org.opendaylight.restconf.openapi.model.security.Http; import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer; import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; @@ -36,9 +34,6 @@ import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack; public final class SchemasStream extends InputStream { - private static final String BASIC_AUTH_NAME = "basicAuth"; - private static final Http OPEN_API_BASIC_AUTH = new Http("basic", null, null); - private static final String OBJECT_TYPE = "object"; private static final String INPUT_SUFFIX = "_input"; private static final String OUTPUT_SUFFIX = "_output"; @@ -51,8 +46,6 @@ public final class SchemasStream extends InputStream { private Reader reader; private boolean schemesWritten; - private boolean eof; - private boolean eos; public SchemasStream(final EffectiveModelContext context, final OpenApiBodyWriter writer, final JsonGenerator generator, final ByteArrayOutputStream stream, @@ -67,20 +60,12 @@ public final class SchemasStream extends InputStream { @Override public int read() throws IOException { if (reader == null) { - generator.writeObjectFieldStart("components"); generator.writeObjectFieldStart("schemas"); generator.flush(); reader = new BufferedReader( new InputStreamReader(new ByteArrayInputStream(stream.toByteArray()), StandardCharsets.UTF_8)); stream.reset(); } - if (eof) { - return -1; - } - if (eos) { - eof = true; - return reader.read(); - } var read = reader.read(); while (read == -1) { @@ -93,19 +78,13 @@ public final class SchemasStream extends InputStream { } if (!schemesWritten) { generator.writeEndObject(); - reader = new BufferedReader( - new InputStreamReader(new SecuritySchemesStream(writer, Map.of(BASIC_AUTH_NAME, - OPEN_API_BASIC_AUTH)), StandardCharsets.UTF_8)); - read = reader.read(); schemesWritten = true; continue; } - generator.writeEndObject(); generator.flush(); reader = new BufferedReader( new InputStreamReader(new ByteArrayInputStream(stream.toByteArray()), StandardCharsets.UTF_8)); stream.reset(); - eos = true; return reader.read(); } -- 2.36.6