Convert yang-data-impl to a JPMS module
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / InMemoryDataTreeFactory.java
index b36776d88c479ed86695185e768c801830cce934..17e330eb09e544c6c5ea11f4ca1ebc66140cb35b 100644 (file)
@@ -7,7 +7,12 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkArgument;
+
+import java.util.Optional;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import org.eclipse.jdt.annotation.NonNull;
 import org.kohsuke.MetaInfServices;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
@@ -27,18 +32,35 @@ import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
+import org.opendaylight.yangtools.yang.model.api.ContainerLike;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * A factory for creating in-memory data trees.
  */
 @MetaInfServices
+@Singleton
+@Component(immediate = true)
 public final class InMemoryDataTreeFactory implements DataTreeFactory {
-    private static final NormalizedNode<?, ?> ROOT_CONTAINER = ImmutableNodes.containerNode(SchemaContext.NAME);
+    private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTreeFactory.class);
+    // FIXME: YANGTOOLS-1074: we do not want this name
+    private static final @NonNull NormalizedNode<?, ?> ROOT_CONTAINER =
+            ImmutableNodes.containerNode(SchemaContext.NAME);
+
+    @Inject
+    public InMemoryDataTreeFactory() {
+        // Exposed for DI
+    }
 
     @Override
     public DataTree create(final DataTreeConfiguration treeConfig) {
@@ -47,17 +69,17 @@ public final class InMemoryDataTreeFactory implements DataTreeFactory {
     }
 
     @Override
-    public DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext) {
-        return create(treeConfig, initialSchemaContext, true);
+    public DataTree create(final DataTreeConfiguration treeConfig, final EffectiveModelContext initialSchemaContext) {
+        return createDataTree(treeConfig, initialSchemaContext, true);
     }
 
     @Override
-    public DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext,
+    public DataTree create(final DataTreeConfiguration treeConfig, final EffectiveModelContext initialSchemaContext,
             final NormalizedNodeContainer<?, ?, ?> initialRoot) throws DataValidationFailedException {
-        final DataTree ret = create(treeConfig, initialSchemaContext, false);
+        final DataTree ret = createDataTree(treeConfig, initialSchemaContext, false);
 
         final DataTreeModification mod = ret.takeSnapshot().newModification();
-        mod.write(YangInstanceIdentifier.EMPTY, initialRoot);
+        mod.write(YangInstanceIdentifier.empty(), initialRoot);
         mod.ready();
 
         ret.validate(mod);
@@ -66,8 +88,20 @@ public final class InMemoryDataTreeFactory implements DataTreeFactory {
         return ret;
     }
 
-    private static DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext,
-            final boolean maskMandatory) {
+    @Activate
+    @SuppressWarnings("static-method")
+    void activate() {
+        LOG.info("In-memory Data Tree activated");
+    }
+
+    @Deactivate
+    @SuppressWarnings("static-method")
+    void deactivate() {
+        LOG.info("In-memory Data Tree deactivated");
+    }
+
+    private static @NonNull DataTree createDataTree(final DataTreeConfiguration treeConfig,
+            final EffectiveModelContext initialSchemaContext, final boolean maskMandatory) {
         final DataSchemaNode rootSchemaNode = getRootSchemaNode(initialSchemaContext, treeConfig.getRootPath());
         final NormalizedNode<?, ?> rootDataNode = createRoot((DataNodeContainer)rootSchemaNode,
             treeConfig.getRootPath());
@@ -75,44 +109,31 @@ public final class InMemoryDataTreeFactory implements DataTreeFactory {
             initialSchemaContext, rootSchemaNode, maskMandatory);
     }
 
-    private static DataSchemaNode getRootSchemaNode(final SchemaContext schemaContext,
-            final YangInstanceIdentifier rootPath) {
-        final DataSchemaContextTree contextTree = DataSchemaContextTree.from(schemaContext);
-        final DataSchemaContextNode<?> rootContextNode = contextTree.getChild(rootPath);
-        Preconditions.checkArgument(rootContextNode != null, "Failed to find root %s in schema context", rootPath);
-
-        final DataSchemaNode rootSchemaNode = rootContextNode.getDataSchemaNode();
-        Preconditions.checkArgument(rootSchemaNode instanceof DataNodeContainer,
-            "Root %s resolves to non-container type %s", rootPath, rootSchemaNode);
-        return rootSchemaNode;
-    }
-
-    private static NormalizedNode<?, ?> createRoot(final DataNodeContainer schemaNode,
+    private static @NonNull NormalizedNode<?, ?> createRoot(final DataNodeContainer schemaNode,
             final YangInstanceIdentifier path) {
         if (path.isEmpty()) {
-            Preconditions.checkArgument(schemaNode instanceof ContainerSchemaNode,
+            checkArgument(schemaNode instanceof ContainerLike,
                 "Conceptual tree root has to be a container, not %s", schemaNode);
             return ROOT_CONTAINER;
         }
 
         final PathArgument arg = path.getLastPathArgument();
         if (schemaNode instanceof ContainerSchemaNode) {
-            Preconditions.checkArgument(arg instanceof NodeIdentifier, "Mismatched container %s path %s", schemaNode,
-                path);
+            checkArgument(arg instanceof NodeIdentifier, "Mismatched container %s path %s", schemaNode, path);
             return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
         } else if (schemaNode instanceof ListSchemaNode) {
             // This can either be a top-level list or its individual entry
             if (arg instanceof NodeIdentifierWithPredicates) {
                 return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
             }
-            Preconditions.checkArgument(arg instanceof NodeIdentifier, "Mismatched list %s path %s", schemaNode, path);
+            checkArgument(arg instanceof NodeIdentifier, "Mismatched list %s path %s", schemaNode, path);
             return ImmutableNodes.mapNodeBuilder().withNodeIdentifier((NodeIdentifier) arg).build();
         } else {
             throw new IllegalArgumentException("Unsupported root schema " + schemaNode);
         }
     }
 
-    private static NormalizedNode<?, ?> createRoot(final YangInstanceIdentifier path) {
+    private static @NonNull NormalizedNode<?, ?> createRoot(final YangInstanceIdentifier path) {
         if (path.isEmpty()) {
             return ROOT_CONTAINER;
         }
@@ -128,4 +149,16 @@ public final class InMemoryDataTreeFactory implements DataTreeFactory {
         // FIXME: implement augmentations and leaf-lists
         throw new IllegalArgumentException("Unsupported root node " + arg);
     }
+
+    private static DataSchemaNode getRootSchemaNode(final EffectiveModelContext schemaContext,
+            final YangInstanceIdentifier rootPath) {
+        final DataSchemaContextTree contextTree = DataSchemaContextTree.from(schemaContext);
+        final Optional<DataSchemaContextNode<?>> rootContextNode = contextTree.findChild(rootPath);
+        checkArgument(rootContextNode.isPresent(), "Failed to find root %s in schema context", rootPath);
+
+        final DataSchemaNode rootSchemaNode = rootContextNode.get().getDataSchemaNode();
+        checkArgument(rootSchemaNode instanceof DataNodeContainer, "Root %s resolves to non-container type %s",
+            rootPath, rootSchemaNode);
+        return rootSchemaNode;
+    }
 }