Remove yang-data-impl activator 45/87945/3
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 21 Feb 2020 15:32:58 +0000 (16:32 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 21 Feb 2020 16:07:51 +0000 (17:07 +0100)
Use declarative services, so that we can lose a class and make things
consistent with other services we provide.

JIRA: YANGTOOLS-1085
Change-Id: I6d5f6dc8ac7cba47b8f01e7ca1af668cacd13dbf
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
features/odl-yangtools-data/src/main/feature/feature.xml
yang/yang-data-impl/pom.xml
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/osgi/Activator.java [deleted file]
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/osgi/package-info.java [deleted file]
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/InMemoryDataTreeFactory.java

index dd3f693915bb5e47ea05a7e28b74067e0a5b7623..e223a3755af0f177ff882de900c102ecc0ef7d19 100644 (file)
@@ -2,5 +2,6 @@
 <features xmlns="http://karaf.apache.org/xmlns/features/v1.4.0" name="odl-yangtools-data">
     <feature name="odl-yangtools-data">
         <feature version="[6,7)">odl-antlr4</feature>
+        <feature>scr</feature>
     </feature>
 </features>
index 6d06b5cdc15285d4034c23d21c580446a1f751de..1ecbc78071da1ace75c31503fbc240fef100bf99 100644 (file)
                 <configuration>
                     <instructions>
                         <Automatic-Module-Name>org.opendaylight.yangtools.yang.data.impl</Automatic-Module-Name>
+                        <!-- FIXME: do not export data.impl -->
                         <Export-Package>
                             {local-packages},
                             org.opendaylight.yangtools.yang.data.impl.*,
                             ;-split-package:=error
                         </Export-Package>
-                        <Bundle-Activator>org.opendaylight.yangtools.yang.data.impl.osgi.Activator</Bundle-Activator>
                     </instructions>
                 </configuration>
             </plugin>
@@ -92,7 +92,7 @@
         </dependency>
         <dependency>
             <groupId>org.osgi</groupId>
-            <artifactId>org.osgi.core</artifactId>
+            <artifactId>osgi.cmpn</artifactId>
         </dependency>
 
         <dependency>
diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/osgi/Activator.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/osgi/Activator.java
deleted file mode 100644 (file)
index 183e286..0000000
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Copyright (c) 2017 Pantheon Technologies, 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.yangtools.yang.data.impl.osgi;
-
-import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeFactory;
-import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceRegistration;
-
-/**
- * YANG data implementation activator. Publishes a {@link DataTreeFactory} implementation on bundle start.
- *
- * @author Robert Varga
- */
-public final class Activator implements BundleActivator {
-    private ServiceRegistration<@NonNull DataTreeFactory> registration;
-
-    @Override
-    public void start(final BundleContext context) throws Exception {
-        registration = context.registerService(DataTreeFactory.class, new InMemoryDataTreeFactory(), null);
-    }
-
-    @Override
-    public void stop(final BundleContext context) throws Exception {
-        if (registration != null) {
-            registration.unregister();
-            registration = null;
-        }
-    }
-}
diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/osgi/package-info.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/osgi/package-info.java
deleted file mode 100644 (file)
index b184727..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-/*
- * Copyright (c) 2017 Pantheon Technologies, 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
- */
-/**
- * OSGi-specific YANG data implementation code. Contains activators and similar.
- */
-package org.opendaylight.yangtools.yang.data.impl.osgi;
\ No newline at end of file
index edb0c78d00853010e17a809f4cb1e2243447fbcb..d997c5ffa52b12c516b21a7ba517ed80c1b7da36 100644 (file)
@@ -11,6 +11,7 @@ import static com.google.common.base.Preconditions.checkArgument;
 
 import java.util.Optional;
 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;
@@ -35,15 +36,23 @@ import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 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 Logger LOG = LoggerFactory.getLogger(InMemoryDataTreeFactory.class);
     // FIXME: YANGTOOLS-1074: we do not want this name
-    private static final NormalizedNode<?, ?> ROOT_CONTAINER = ImmutableNodes.containerNode(SchemaContext.NAME);
+    private static final @NonNull NormalizedNode<?, ?> ROOT_CONTAINER =
+            ImmutableNodes.containerNode(SchemaContext.NAME);
 
     @Override
     public DataTree create(final DataTreeConfiguration treeConfig) {
@@ -53,13 +62,13 @@ public final class InMemoryDataTreeFactory implements DataTreeFactory {
 
     @Override
     public DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext) {
-        return create(treeConfig, initialSchemaContext, true);
+        return createDataTree(treeConfig, initialSchemaContext, true);
     }
 
     @Override
     public DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext 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);
@@ -71,8 +80,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 SchemaContext initialSchemaContext, final boolean maskMandatory) {
         final DataSchemaNode rootSchemaNode = getRootSchemaNode(initialSchemaContext, treeConfig.getRootPath());
         final NormalizedNode<?, ?> rootDataNode = createRoot((DataNodeContainer)rootSchemaNode,
             treeConfig.getRootPath());
@@ -80,19 +101,7 @@ 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 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;
-    }
-
-    private static NormalizedNode<?, ?> createRoot(final DataNodeContainer schemaNode,
+    private static @NonNull NormalizedNode<?, ?> createRoot(final DataNodeContainer schemaNode,
             final YangInstanceIdentifier path) {
         if (path.isEmpty()) {
             checkArgument(schemaNode instanceof ContainerSchemaNode,
@@ -116,7 +125,7 @@ public final class InMemoryDataTreeFactory implements DataTreeFactory {
         }
     }
 
-    private static NormalizedNode<?, ?> createRoot(final YangInstanceIdentifier path) {
+    private static @NonNull NormalizedNode<?, ?> createRoot(final YangInstanceIdentifier path) {
         if (path.isEmpty()) {
             return ROOT_CONTAINER;
         }
@@ -132,4 +141,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 SchemaContext 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;
+    }
 }