+++ /dev/null
-/*
- * Copyright (c) 2023 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.netconf.client.mdsal;
-
-import static java.util.Objects.requireNonNull;
-
-import com.google.common.base.MoreObjects;
-import java.io.Reader;
-import java.io.StringReader;
-import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
-import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
-import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
-
-/**
- * A {@link YangTextSource} cached from a remote service.
- */
-// FIXME: subclass StringYangTextSource and cleanup addToStringAttributes()
-public final class CachedYangTextSchemaSource extends YangTextSource {
- private final @NonNull SourceIdentifier sourceId;
- private final @NonNull RemoteDeviceId id;
- private final @NonNull String symbolicName;
- private final @NonNull String schemaString;
-
- public CachedYangTextSchemaSource(final RemoteDeviceId id, final SourceIdentifier sourceId,
- final String symbolicName, final String schemaString) {
- this.id = requireNonNull(id);
- this.sourceId = requireNonNull(sourceId);
- this.symbolicName = requireNonNull(symbolicName);
- this.schemaString = requireNonNull(schemaString);
- }
-
- @Override
- public Reader openStream() {
- return new StringReader(schemaString);
- }
-
- @Override
- public SourceIdentifier sourceId() {
- return sourceId;
- }
-
- @Override
- public String symbolicName() {
- return symbolicName;
- }
-
- @Override
- protected MoreObjects.ToStringHelper addToStringAttributes(final MoreObjects.ToStringHelper toStringHelper) {
- return toStringHelper.add("device", id);
- }
-}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2023 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.netconf.client.mdsal;
+
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.base.MoreObjects;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
+import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
+import org.opendaylight.yangtools.yang.model.spi.source.StringYangTextSource;
+
+/**
+ * A {@link YangTextSource} cached from a remote service.
+ */
+@NonNullByDefault
+public final class CachedYangTextSource extends StringYangTextSource {
+ private final RemoteDeviceId deviceId;
+
+ public CachedYangTextSource(final RemoteDeviceId deviceId, final SourceIdentifier sourceId, final String content,
+ final @Nullable String symbolicName) {
+ super(sourceId, content, symbolicName);
+ this.deviceId = requireNonNull(deviceId);
+ }
+
+ public RemoteDeviceId deviceId() {
+ return deviceId;
+ }
+
+ @Override
+ protected MoreObjects.ToStringHelper addToStringAttributes(final MoreObjects.ToStringHelper toStringHelper) {
+ return super.addToStringAttributes(toStringHelper.add("deviceId", deviceId));
+ }
+}
"Unable to download remote schema for " + sourceIdentifier + " from " + url, e));
}
- final var yangSource = new CachedYangTextSchemaSource(id, sourceIdentifier, url.toString(),
- new String(schemaContent, StandardCharsets.UTF_8));
+ final var yangSource = new CachedYangTextSource(id, sourceIdentifier,
+ new String(schemaContent, StandardCharsets.UTF_8), url.toString());
LOG.debug("Source {} downloaded from a yang library's url {}", sourceIdentifier, url);
return Futures.immediateFuture(yangSource);
}
id + ": Unexpected response to get-schema, schema not present in message for: "
+ sourceIdentifier));
LOG.debug("{}: YANG Schema successfully retrieved for {}:{}", id, moduleName, revision);
- return new CachedYangTextSchemaSource(id, sourceIdentifier, moduleName, schemaString);
+ return new CachedYangTextSource(id, sourceIdentifier, schemaString, null);
}
LOG.warn("{}: YANG schema was not successfully retrieved for {}. Errors: {}", id, sourceIdentifier,
*/
package org.opendaylight.netconf.client.mdsal;
-import static org.hamcrest.CoreMatchers.containsString;
-import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.ExecutionException;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
public class LibrarySchemaYangSourceProviderTest {
- private SourceIdentifier workingSid;
- private LibrarySchemaSourceProvider yangLibrarySchemaYangSourceProvider;
-
- @Before
- public void setUp() throws Exception {
- workingSid = new SourceIdentifier("abc");
- yangLibrarySchemaYangSourceProvider = new LibrarySchemaSourceProvider(
- new RemoteDeviceId("id", new InetSocketAddress("localhost", 22)),
- Map.of(workingSid, getClass().getResource("/schemas/config-test-rpc.yang")));
- }
+ private final SourceIdentifier workingSid = new SourceIdentifier("abc");
+ private final LibrarySchemaSourceProvider yangLibrarySchemaYangSourceProvider = new LibrarySchemaSourceProvider(
+ new RemoteDeviceId("id", new InetSocketAddress("localhost", 22)),
+ Map.of(workingSid, LibrarySchemaYangSourceProviderTest.class.getResource("/schemas/config-test-rpc.yang")));
@Test
- public void testGetSource() throws Exception {
+ void testGetSource() throws Exception {
var source = yangLibrarySchemaYangSourceProvider.getSource(workingSid);
- final String x = source.get().read();
- assertThat(x, containsString("module config-test-rpc"));
+ assertThat(source.get().read(), startsWith("module config-test-rpc"));
}
@Test
- public void testGetSourceFailure() throws InterruptedException, MalformedURLException {
+ void testGetSourceFailure() throws InterruptedException, MalformedURLException {
final var sourceIdentifierURLMap = Map.of(workingSid, new URL("http://non-existing-entity.yang"));
final var failingYangLibrarySchemaYangSourceProvider = new LibrarySchemaSourceProvider(
new RemoteDeviceId("id", new InetSocketAddress("localhost", 22)), sourceIdentifierURLMap);
final var future = failingYangLibrarySchemaYangSourceProvider.getSource(workingSid);
final var ex = assertThrows(ExecutionException.class, () -> future.get());
- assertThat(ex.getCause(), instanceOf(SchemaSourceException.class));
+ assertInstanceOf(SchemaSourceException.class, ex.getCause());
}
@Test
- public void testGetSourceNotAvailable() {
+ void testGetSourceNotAvailable() {
assertThrows(IllegalArgumentException.class,
() -> yangLibrarySchemaYangSourceProvider.getSource(new SourceIdentifier("aaaaa")));
}