Remove SchemaContext references 92/105692/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 26 Apr 2023 00:01:53 +0000 (02:01 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 26 Apr 2023 00:26:54 +0000 (02:26 +0200)
SchemaContext is going away, improve passed argument to
EffectiveModelContext and add a FIXME for migration.

Change-Id: I20bbae6277b677f991709e756d8589b3d5d30436
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/netconf/mdsal/connector/MdsalNetconfOperationServiceFactory.java
netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/netconf/mdsal/connector/ops/get/Bug8084.java
netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/netconf/mdsal/connector/ops/get/FilterContentValidatorTest.java
netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/netconf/mdsal/connector/ops/get/Netconf506Test.java

index 9f726cd8feac9489733d6d838ea092507548556f..c59d625581fa19dada12e14afec7c9bdf23f06ee 100644 (file)
@@ -31,10 +31,8 @@ import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener;
 import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.common.Revision;
-import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.ModuleLike;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.Submodule;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
@@ -89,20 +87,23 @@ public final class MdsalNetconfOperationServiceFactory implements NetconfOperati
 
     @Override
     public Set<Capability> getCapabilities() {
+        // FIXME: cache returned set
         return transformCapabilities(currentSchemaContext.getCurrentContext(), rootSchemaSourceProviderDependency);
     }
 
+    // FIXME: ImmutableSet
     static Set<Capability> transformCapabilities(
-            final SchemaContext currentContext,
+            final EffectiveModelContext currentContext,
             final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProviderDependency) {
-        final Set<Capability> capabilities = new HashSet<>();
+        final var capabilities = new HashSet<Capability>();
 
         // Added by netconf-impl by default
         // capabilities.add(new BasicCapability("urn:ietf:params:netconf:capability:candidate:1.0"));
 
-        for (final Module module : currentContext.getModules()) {
+        // FIXME: rework in terms of ModuleEffectiveStatement
+        for (var module : currentContext.getModules()) {
             moduleToCapability(module, rootSchemaSourceProviderDependency).ifPresent(capabilities::add);
-            for (final Submodule submodule : module.getSubmodules()) {
+            for (var submodule : module.getSubmodules()) {
                 moduleToCapability(submodule, rootSchemaSourceProviderDependency).ifPresent(capabilities::add);
             }
         }
index f1a1f355b1b3eaccbdd8b247177ed3f0e097f398..7dcc3cb382f2514582b3a1f26b5593b94027881a 100644 (file)
@@ -11,10 +11,9 @@ import static org.junit.Assert.assertEquals;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 
+import com.google.common.collect.ImmutableMap;
 import java.math.BigDecimal;
 import java.math.BigInteger;
-import java.util.HashMap;
-import java.util.Map;
 import org.junit.Test;
 import org.opendaylight.netconf.api.xml.XmlElement;
 import org.opendaylight.netconf.api.xml.XmlUtil;
@@ -22,56 +21,50 @@ import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
-import org.w3c.dom.Document;
 
 public class Bug8084 {
-
     private static final QName BASE = QName.create("urn:dummy:mod-0", "2016-03-01", "mainroot");
 
     @Test
     public void testValidateTypes() throws Exception {
-        final SchemaContext context = YangParserTestUtils.parseYangResources(Bug8084.class,
+        final var context = YangParserTestUtils.parseYangResources(Bug8084.class,
             "/yang/filter-validator-test-mod-0.yang", "/yang/filter-validator-test-augment.yang",
             "/yang/mdsal-netconf-mapping-test.yang");
-        final CurrentSchemaContext currentContext = mock(CurrentSchemaContext.class);
+        final var currentContext = mock(CurrentSchemaContext.class);
         doReturn(context).when(currentContext).getCurrentContext();
-        final FilterContentValidator validator = new FilterContentValidator(currentContext);
+        final var validator = new FilterContentValidator(currentContext);
 
-        final Document document = XmlUtil.readXmlToDocument(FilterContentValidatorTest.class
+        final var document = XmlUtil.readXmlToDocument(FilterContentValidatorTest.class
                 .getResourceAsStream("/filter/bug8084.xml"));
 
-        final XmlElement xmlElement = XmlElement.fromDomDocument(document);
-        final YangInstanceIdentifier actual = validator.validate(xmlElement);
-
-        final Map<QName, Object> inputs = new HashMap<>();
-        inputs.put(QName.create(BASE, "id1"), "aaa");
-        inputs.put(QName.create(BASE, "id2"), Byte.valueOf("-9"));
-        inputs.put(QName.create(BASE, "id3"), Short.valueOf("-30000"));
-        inputs.put(QName.create(BASE, "id4"), Integer.valueOf("-2000000000"));
-        inputs.put(QName.create(BASE, "id5"), Long.valueOf("-2000000000000000"));
-        inputs.put(QName.create(BASE, "id6"), Short.valueOf("9"));
-        inputs.put(QName.create(BASE, "id7"), Integer.valueOf("30000"));
-        inputs.put(QName.create(BASE, "id8"), Long.valueOf("2000000000"));
-        inputs.put(QName.create(BASE, "id9"), BigInteger.valueOf(Long.valueOf("2000000000000000")));
-        inputs.put(QName.create(BASE, "id10"), true);
-        inputs.put(QName.create(BASE, "id11"), BigDecimal.valueOf(128.55));
-        inputs.put(QName.create(BASE, "id12"), QName.create(BASE, "foo"));
-        inputs.put(
-                QName.create(BASE, "id13"),
-                QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "foo"));
-        final QName idActual = (QName) ((NodeIdentifierWithPredicates) actual.getLastPathArgument())
-                .getValue(QName.create(BASE, "id12"));
+        final var xmlElement = XmlElement.fromDomDocument(document);
+        final var actual = validator.validate(xmlElement);
 
-        final YangInstanceIdentifier expected = YangInstanceIdentifier.builder()
-                .node(BASE)
-                .node(QName.create(BASE, "multi-key-list2"))
-                .nodeWithKey(QName.create(BASE, "multi-key-list2"), inputs)
-                .build();
-        final QName idExpected = (QName) ((NodeIdentifierWithPredicates) expected.getLastPathArgument())
-                .getValue(QName.create(BASE, "id12"));
+        final var id12 = QName.create(BASE, "id12");
+        final var idExpected = QName.create(BASE, "foo");
+        final var idActual = (QName) ((NodeIdentifierWithPredicates) actual.getLastPathArgument()).getValue(id12);
         assertEquals(idExpected, idActual);
-        assertEquals(expected, actual);
+
+        assertEquals(YangInstanceIdentifier.builder()
+            .node(BASE)
+            .node(QName.create(BASE, "multi-key-list2"))
+            .nodeWithKey(QName.create(BASE, "multi-key-list2"), ImmutableMap.<QName, Object>builder()
+                .put(QName.create(BASE, "id1"), "aaa")
+                .put(QName.create(BASE, "id2"), Byte.valueOf("-9"))
+                .put(QName.create(BASE, "id3"), Short.valueOf("-30000"))
+                .put(QName.create(BASE, "id4"), Integer.valueOf("-2000000000"))
+                .put(QName.create(BASE, "id5"), Long.valueOf("-2000000000000000"))
+                .put(QName.create(BASE, "id6"), Short.valueOf("9"))
+                .put(QName.create(BASE, "id7"), Integer.valueOf("30000"))
+                .put(QName.create(BASE, "id8"), Long.valueOf("2000000000"))
+                .put(QName.create(BASE, "id9"), BigInteger.valueOf(Long.parseLong("2000000000000000")))
+                .put(QName.create(BASE, "id10"), true)
+                .put(QName.create(BASE, "id11"), BigDecimal.valueOf(128.55))
+                .put(id12, idExpected)
+                .put(QName.create(BASE, "id13"),
+                    QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "foo"))
+                .build())
+            .build(), actual);
     }
 }
index 180c0bbd1e306bc51e171b79c997f5f939c31f17..c2969795fbe4f86bcafa7bcbae8b1bf66615cce1 100644 (file)
@@ -10,20 +10,17 @@ package org.opendaylight.netconf.mdsal.connector.ops.get;
 import static java.util.Objects.requireNonNull;
 import static org.hamcrest.CoreMatchers.startsWith;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assume.assumeThat;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 
 import java.nio.file.Files;
-import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import org.junit.Before;
@@ -37,13 +34,11 @@ import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 import org.w3c.dom.Document;
 
 @RunWith(value = Parameterized.class)
 public class FilterContentValidatorTest {
-
     private static final int TEST_CASE_COUNT = 13;
     private static final Pattern LIST_ENTRY_PATTERN =
             Pattern.compile("(?<listName>.*)\\[\\{(?<keys>(.*)(, .*)*)\\}\\]");
@@ -55,14 +50,14 @@ public class FilterContentValidatorTest {
 
     @Parameterized.Parameters
     public static Collection<Object[]> data() throws Exception {
-        final List<Object[]> result = new ArrayList<>();
-        final Path path = Paths.get(FilterContentValidatorTest.class.getResource("/filter/expected.txt").toURI());
-        final List<String> expected = Files.readAllLines(path);
+        final var result = new ArrayList<Object[]>();
+        final var expected = Files.readAllLines(
+            Paths.get(FilterContentValidatorTest.class.getResource("/filter/expected.txt").toURI()));
         if (expected.size() != TEST_CASE_COUNT) {
             throw new InitializationError("Number of lines in results file must be same as test case count");
         }
         for (int i = 1; i <= TEST_CASE_COUNT; i++) {
-            final Document document = XmlUtil.readXmlToDocument(FilterContentValidatorTest.class.getResourceAsStream(
+            final var document = XmlUtil.readXmlToDocument(FilterContentValidatorTest.class.getResourceAsStream(
                     "/filter/f" + i + ".xml"));
             result.add(new Object[]{document, expected.get(i - 1)});
         }
@@ -76,11 +71,11 @@ public class FilterContentValidatorTest {
 
     @Before
     public void setUp() throws Exception {
-        final SchemaContext context = YangParserTestUtils.parseYangResources(FilterContentValidatorTest.class,
+        final var context = YangParserTestUtils.parseYangResources(FilterContentValidatorTest.class,
             "/yang/filter-validator-test-mod-0.yang", "/yang/filter-validator-test-augment.yang",
             "/yang/mdsal-netconf-mapping-test.yang");
 
-        final CurrentSchemaContext currentContext = mock(CurrentSchemaContext.class);
+        final var currentContext = mock(CurrentSchemaContext.class);
         doReturn(context).when(currentContext).getCurrentContext();
         validator = new FilterContentValidator(currentContext);
     }
@@ -90,7 +85,7 @@ public class FilterContentValidatorTest {
         assumeThat(expected, startsWith("success"));
 
         final String expId = expected.replace("success=", "");
-        final YangInstanceIdentifier actual = validator.validate(filterContent);
+        final var actual = validator.validate(filterContent);
         assertEquals(fromString(expId), actual);
     }
 
@@ -98,30 +93,26 @@ public class FilterContentValidatorTest {
     public void testValidateError() {
         assumeThat(expected, startsWith("error"));
 
-        try {
-            validator.validate(filterContent);
-            fail(XmlUtil.toString(filterContent) + " is not valid and should throw exception.");
-        } catch (final DocumentedException e) {
-            final String expectedExceptionClass = expected.replace("error=", "");
-            assertEquals(expectedExceptionClass, e.getClass().getName());
-        }
+        final var ex = assertThrows(DocumentedException.class, () -> validator.validate(filterContent));
+        final String expectedExceptionClass = expected.replace("error=", "");
+        assertEquals(expectedExceptionClass, ex.getClass().getName());
     }
 
     private static YangInstanceIdentifier fromString(final String input) {
         //remove first /
         final String yid = input.substring(1);
-        final List<String> pathElements = Arrays.asList(yid.split("/"));
-        final YangInstanceIdentifier.InstanceIdentifierBuilder builder = YangInstanceIdentifier.builder();
+        final var pathElements = Arrays.asList(yid.split("/"));
+        final var builder = YangInstanceIdentifier.builder();
         //if not specified, PathArguments inherit namespace and revision from previous PathArgument
         QName prev = null;
-        for (final String pathElement : pathElements) {
-            final Matcher matcher = LIST_ENTRY_PATTERN.matcher(pathElement);
+        for (var pathElement : pathElements) {
+            final var matcher = LIST_ENTRY_PATTERN.matcher(pathElement);
             if (matcher.matches()) {
                 prev = parseListEntry(builder, prev, matcher);
             } else {
-                final QName qName = createNodeQName(prev, pathElement);
-                builder.node(qName);
-                prev = qName;
+                final var qname = createNodeQName(prev, pathElement);
+                builder.node(qname);
+                prev = qname;
             }
         }
         return builder.build();
@@ -129,17 +120,14 @@ public class FilterContentValidatorTest {
 
     private static QName parseListEntry(final YangInstanceIdentifier.InstanceIdentifierBuilder builder,
                                         final QName prev, final Matcher matcher) {
-        final Map<QName, Object> keys = new HashMap<>();
+        final var keys = new HashMap<QName, Object>();
         final String listName = matcher.group("listName");
         final QName listQName = createNodeQName(prev, listName);
         final String keysString = matcher.group("keys");
-        final String[] split = keysString.split(",");
-        for (final String s : split) {
-            final Matcher keyMatcher = KEY_VALUE_PATTERN.matcher(s.trim());
+        for (var str : keysString.split(",")) {
+            final Matcher keyMatcher = KEY_VALUE_PATTERN.matcher(str.trim());
             if (keyMatcher.matches()) {
-                final QName keyName = QName.create(keyMatcher.group("key"));
-                final String keyValue = keyMatcher.group("value");
-                keys.put(keyName, keyValue);
+                keys.put(QName.create(keyMatcher.group("key")), keyMatcher.group("value"));
             }
         }
         builder.nodeWithKey(listQName, keys);
index 7aa7df12cf4ad31b7b98cbb619a7e076cf8ca0be..3b3f8c474db9059c62a51e084b2863ea23ff4ad6 100644 (file)
@@ -11,44 +11,35 @@ import static org.junit.Assert.assertEquals;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 
-import java.util.HashMap;
-import java.util.Map;
 import org.junit.Test;
 import org.opendaylight.netconf.api.xml.XmlElement;
 import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
-import org.w3c.dom.Document;
 
 public class Netconf506Test {
-
     private static final QName BASE = QName.create("urn:dummy:mod-0", "2016-03-01", "mainroot");
 
     @Test
     public void testValidateTypes() throws Exception {
-        final SchemaContext context = YangParserTestUtils.parseYangResources(Bug8084.class,
+        final var context = YangParserTestUtils.parseYangResources(Bug8084.class,
                 "/yang/filter-validator-test-mod-0.yang", "/yang/mdsal-netconf-mapping-test.yang");
-        final CurrentSchemaContext currentContext = mock(CurrentSchemaContext.class);
+        final var currentContext = mock(CurrentSchemaContext.class);
         doReturn(context).when(currentContext).getCurrentContext();
-        final FilterContentValidator validator = new FilterContentValidator(currentContext);
+        final var validator = new FilterContentValidator(currentContext);
 
-        final Document document = XmlUtil.readXmlToDocument(FilterContentValidatorTest.class
+        final var document = XmlUtil.readXmlToDocument(FilterContentValidatorTest.class
                 .getResourceAsStream("/filter/netconf506.xml"));
 
-        final XmlElement xmlElement = XmlElement.fromDomDocument(document);
-        final YangInstanceIdentifier actual = validator.validate(xmlElement);
-
-        final Map<QName, Object> inputs = new HashMap<>();
-        inputs.put(QName.create(BASE, "name"), "foo");
+        final var xmlElement = XmlElement.fromDomDocument(document);
+        final var actual = validator.validate(xmlElement);
 
-        final YangInstanceIdentifier expected = YangInstanceIdentifier.builder()
-                .node(BASE)
-                .node(QName.create(BASE, "leafref-key-list"))
-                .nodeWithKey(QName.create(BASE, "leafref-key-list"), inputs)
-                .build();
-        assertEquals(expected, actual);
+        assertEquals(YangInstanceIdentifier.builder()
+            .node(BASE)
+            .node(QName.create(BASE, "leafref-key-list"))
+            .nodeWithKey(QName.create(BASE, "leafref-key-list"), QName.create(BASE, "name"), "foo")
+            .build(), actual);
     }
 }