Convert mdsal-netconf-connector to OSGi DS 12/104312/4
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 8 Feb 2023 15:59:08 +0000 (16:59 +0100)
committerRobert Varga <nite@hq.sk>
Wed, 8 Feb 2023 17:53:43 +0000 (17:53 +0000)
This blueprint is extremely simple wiring, migrate it to Declarative
Services and also cleanup dependencies and code a bit.

JIRA: NETCONF-955
Change-Id: I54da9529206677f9d210d2db1cb7248a0ba0259b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
netconf/mdsal-netconf-connector/pom.xml
netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/netconf/mdsal/connector/CurrentSchemaContext.java
netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/netconf/mdsal/connector/MdsalNetconfOperationServiceFactory.java
netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/netconf/mdsal/connector/ops/RuntimeRpc.java
netconf/mdsal-netconf-connector/src/main/resources/OSGI-INF/blueprint/mdsal-netconf-connector.xml [deleted file]
netconf/mdsal-netconf-connector/src/test/java/org/opendaylight/netconf/mdsal/connector/ops/RuntimeRpcTest.java

index 794db89ba1c2824764cc388d89f3b39d51ff6f36..36035f0258019d630eaad12243b8e544d41c289e 100644 (file)
   <packaging>bundle</packaging>
 
   <dependencies>
+    <dependency>
+      <groupId>com.google.guava</groupId>
+      <artifactId>guava</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.opendaylight.yangtools</groupId>
-      <artifactId>yang-data-util</artifactId>
+      <artifactId>concepts</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.yangtools</groupId>
+      <artifactId>yang-common</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.yangtools</groupId>
+      <artifactId>rfc7952-data-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.yangtools</groupId>
+      <artifactId>yang-data-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.yangtools</groupId>
+      <artifactId>yang-data-codec-xml</artifactId>
     </dependency>
     <dependency>
       <groupId>org.opendaylight.yangtools</groupId>
       <artifactId>yang-data-impl</artifactId>
     </dependency>
-
     <dependency>
-      <groupId>${project.groupId}</groupId>
+      <groupId>org.opendaylight.yangtools</groupId>
+      <artifactId>yang-data-util</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.yangtools</groupId>
+      <artifactId>yang-model-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.yangtools</groupId>
+      <artifactId>yang-model-util</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.yangtools</groupId>
+      <artifactId>yang-repo-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.yangtools</groupId>
+      <artifactId>yang-repo-spi</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.mdsal</groupId>
+      <artifactId>mdsal-common-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.mdsal</groupId>
+      <artifactId>mdsal-dom-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.netconf</groupId>
       <artifactId>netconf-api</artifactId>
     </dependency>
     <dependency>
-      <groupId>${project.groupId}</groupId>
+      <groupId>org.opendaylight.netconf</groupId>
       <artifactId>netconf-mapping-api</artifactId>
     </dependency>
     <dependency>
-      <groupId>${project.groupId}</groupId>
+      <groupId>org.opendaylight.netconf</groupId>
       <artifactId>netconf-util</artifactId>
     </dependency>
     <dependency>
-      <groupId>com.google.guava</groupId>
-      <artifactId>guava</artifactId>
+      <groupId>org.opendaylight.netconf.model</groupId>
+      <artifactId>rfc6241</artifactId>
     </dependency>
-
     <dependency>
-      <groupId>org.opendaylight.mdsal</groupId>
-      <artifactId>mdsal-dom-api</artifactId>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.service.component.annotations</artifactId>
     </dependency>
+
     <dependency>
       <groupId>org.opendaylight.yangtools</groupId>
       <artifactId>mockito-configuration</artifactId>
     </dependency>
     <dependency>
-      <groupId>org.opendaylight.netconf.model</groupId>
-      <artifactId>rfc6241</artifactId>
+      <groupId>org.opendaylight.yangtools</groupId>
+      <artifactId>util</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.yangtools</groupId>
+      <artifactId>yang-test-util</artifactId>
     </dependency>
     <dependency>
       <groupId>org.opendaylight.mdsal</groupId>
       <artifactId>mdsal-dom-broker</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.opendaylight.mdsal</groupId>
+      <artifactId>mdsal-dom-spi</artifactId>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>org.opendaylight.mdsal</groupId>
       <artifactId>mdsal-dom-inmemory-datastore</artifactId>
       <scope>test</scope>
     </dependency>
     <dependency>
-      <groupId>${project.groupId}</groupId>
+      <groupId>org.opendaylight.netconf</groupId>
       <artifactId>netconf-util</artifactId>
       <type>test-jar</type>
       <scope>test</scope>
       <artifactId>xmlunit-legacy</artifactId>
       <scope>test</scope>
     </dependency>
-    <dependency>
-      <groupId>org.opendaylight.yangtools</groupId>
-      <artifactId>yang-test-util</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>org.opendaylight.yangtools</groupId>
-      <artifactId>yang-data-codec-xml</artifactId>
-    </dependency>
   </dependencies>
 </project>
index 3c1c79c776d2a86a6a2ae31dd13e1ffb22354267..3956d3a8af8a2a5156a774d4ffbaba3c0cbd6b4b 100644 (file)
@@ -62,7 +62,7 @@ public class CurrentSchemaContext implements EffectiveModelContextListener, Auto
         final Set<Capability> addedCaps = MdsalNetconfOperationServiceFactory.transformCapabilities(
                 currentContext.get(), rootSchemaSourceProvider);
         for (final CapabilityListener listener : listeners1) {
-            listener.onCapabilitiesChanged(addedCaps, Collections.emptySet());
+            listener.onCapabilitiesChanged(addedCaps, Set.of());
         }
     }
 
@@ -75,7 +75,7 @@ public class CurrentSchemaContext implements EffectiveModelContextListener, Auto
 
     public Registration registerCapabilityListener(final CapabilityListener listener) {
         listener.onCapabilitiesChanged(MdsalNetconfOperationServiceFactory.transformCapabilities(currentContext.get(),
-                rootSchemaSourceProvider), Collections.emptySet());
+                rootSchemaSourceProvider), Set.of());
         listeners1.add(listener);
         return () -> listeners1.remove(listener);
     }
index 6d2483b6854ba281abe78ebb71d4b93dcdf99fd5..ca9e57a0712585137bca10cfd4b5b43d4b2d7ad5 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.netconf.mdsal.connector;
 
-import static com.google.common.base.Preconditions.checkState;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.io.CharStreams;
@@ -15,7 +14,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.nio.charset.StandardCharsets;
-import java.util.Collections;
 import java.util.HashSet;
 import java.util.Optional;
 import java.util.Set;
@@ -39,11 +37,15 @@ 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;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+@Component(service = NetconfOperationServiceFactory.class, immediate = true, property = "type=mdsal-netconf-connector")
 public final class MdsalNetconfOperationServiceFactory implements NetconfOperationServiceFactory, AutoCloseable {
-
     private static final Logger LOG = LoggerFactory.getLogger(MdsalNetconfOperationServiceFactory.class);
     private static final BasicCapability VALIDATE_CAPABILITY =
             new BasicCapability("urn:ietf:params:netconf:capability:validate:1.0");
@@ -55,56 +57,35 @@ public final class MdsalNetconfOperationServiceFactory implements NetconfOperati
     private final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProviderDependency;
     private final NetconfOperationServiceFactoryListener netconfOperationServiceFactoryListener;
 
-    private MdsalNetconfOperationServiceFactory(
-            final DOMSchemaService schemaService,
-            final NetconfOperationServiceFactoryListener netconfOperationServiceFactoryListener,
-            final DOMDataBroker dataBroker,
-            final DOMRpcService rpcService) {
-
-        this.dataBroker = dataBroker;
-        this.rpcService = rpcService;
+    @Activate
+    public MdsalNetconfOperationServiceFactory(@Reference final DOMSchemaService schemaService,
+            @Reference final DOMDataBroker dataBroker, @Reference final DOMRpcService rpcService,
+            @Reference(target = "(type=mapper-aggregator-registry)")
+            final NetconfOperationServiceFactoryListener netconfOperationServiceFactoryListener) {
+        this.dataBroker = requireNonNull(dataBroker);
+        this.rpcService = requireNonNull(rpcService);
+        this.netconfOperationServiceFactoryListener = requireNonNull(netconfOperationServiceFactoryListener);
 
         rootSchemaSourceProviderDependency = schemaService.getExtensions()
                 .getInstance(DOMYangTextSourceProvider.class);
         currentSchemaContext = CurrentSchemaContext.create(requireNonNull(schemaService),
                 rootSchemaSourceProviderDependency);
-        this.netconfOperationServiceFactoryListener = netconfOperationServiceFactoryListener;
+        netconfOperationServiceFactoryListener.onAddNetconfOperationServiceFactory(this);
     }
 
-    // keep spotbugs from complaining about overridable method in constructor
-    public static MdsalNetconfOperationServiceFactory create(
-            final DOMSchemaService schemaService,
-            final NetconfOperationServiceFactoryListener netconfOperationServiceFactoryListener,
-            final DOMDataBroker dataBroker,
-            final DOMRpcService rpcService) {
-
-        var factory = new MdsalNetconfOperationServiceFactory(schemaService, netconfOperationServiceFactoryListener,
-                dataBroker, rpcService);
-        netconfOperationServiceFactoryListener.onAddNetconfOperationServiceFactory(factory);
-
-        return factory;
+    @Deactivate
+    @Override
+    public void close() {
+        netconfOperationServiceFactoryListener.onRemoveNetconfOperationServiceFactory(this);
+        currentSchemaContext.close();
     }
 
     @Override
     public MdsalNetconfOperationService createService(final String netconfSessionIdForReporting) {
-        checkState(dataBroker != null, "MD-SAL provider not yet initialized");
         return new MdsalNetconfOperationService(currentSchemaContext, netconfSessionIdForReporting, dataBroker,
                 rpcService);
     }
 
-    @SuppressWarnings("checkstyle:IllegalCatch")
-    @Override
-    public void close() {
-        try {
-            currentSchemaContext.close();
-            if (netconfOperationServiceFactoryListener != null) {
-                netconfOperationServiceFactoryListener.onRemoveNetconfOperationServiceFactory(this);
-            }
-        } catch (Exception e) {
-            LOG.error("Failed to close resources correctly - ignore", e);
-        }
-    }
-
     @Override
     public Set<Capability> getCapabilities() {
         return transformCapabilities(currentSchemaContext.getCurrentContext(), rootSchemaSourceProviderDependency);
@@ -170,7 +151,7 @@ public final class MdsalNetconfOperationServiceFactory implements NetconfOperati
     public Registration registerCapabilityListener(final CapabilityListener listener) {
         // Advertise validate capability only if DOMDataBroker provides DOMDataTransactionValidator
         if (dataBroker.getExtensions().get(DOMDataTransactionValidator.class) != null) {
-            listener.onCapabilitiesChanged(Collections.singleton(VALIDATE_CAPABILITY), Collections.emptySet());
+            listener.onCapabilitiesChanged(Set.of(VALIDATE_CAPABILITY), Set.of());
         }
         // Advertise namespaces of supported YANG models as NETCONF capabilities
         return currentSchemaContext.registerCapabilityListener(listener);
index cfa079b08b0ea6026d510e9b81aaf0f5d721a2f4..a8fb6d8cea61a70c7897dce7730cfa1d457c753e 100644 (file)
@@ -84,9 +84,8 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
             return HandlingPriority.CANNOT_HANDLE;
         }
 
-        getRpcDefinitionFromModule(module.get(), namespaceURI, netconfOperationName);
+        getRpcDefinitionFromModule(module.orElseThrow(), namespaceURI, netconfOperationName);
         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
-
     }
 
     @Override
@@ -137,7 +136,7 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
                     ErrorType.APPLICATION, ErrorTag.BAD_ELEMENT, ErrorSeverity.ERROR);
         }
 
-        final Optional<RpcDefinition> rpcDefinitionOptional = getRpcDefinitionFromModule(moduleOptional.get(),
+        final Optional<RpcDefinition> rpcDefinitionOptional = getRpcDefinitionFromModule(moduleOptional.orElseThrow(),
                 namespaceURI, netconfOperationName);
 
         if (rpcDefinitionOptional.isEmpty()) {
@@ -147,7 +146,7 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
                     ErrorType.APPLICATION, ErrorTag.BAD_ELEMENT, ErrorSeverity.ERROR);
         }
 
-        final RpcDefinition rpcDefinition = rpcDefinitionOptional.get();
+        final RpcDefinition rpcDefinition = rpcDefinitionOptional.orElseThrow();
         final ContainerNode inputNode = rpcToNNode(operationElement, rpcDefinition);
 
         final DOMRpcResult result;
@@ -156,11 +155,11 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
         } catch (final InterruptedException | ExecutionException e) {
             throw DocumentedException.wrap(e);
         }
-        if (result.getResult() == null) {
+        if (result.value() == null) {
             return XmlUtil.createElement(document, XmlNetconfConstants.OK,
                 Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
         }
-        return transformNormalizedNode(document, result.getResult(),
+        return transformNormalizedNode(document, result.value(),
                 Absolute.of(rpcDefinition.getQName(), rpcDefinition.getOutput().getQName()));
     }
 
diff --git a/netconf/mdsal-netconf-connector/src/main/resources/OSGI-INF/blueprint/mdsal-netconf-connector.xml b/netconf/mdsal-netconf-connector/src/main/resources/OSGI-INF/blueprint/mdsal-netconf-connector.xml
deleted file mode 100644 (file)
index 3f2364c..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
- Copyright (c) 2016 Inocybe Technologies Inc. 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
--->
-<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
-           xmlns:odl="http://opendaylight.org/xmlns/blueprint/v1.0.0"
-           odl:use-default-for-reference-types="true">
-
-    <reference id="domDataBroker" interface="org.opendaylight.mdsal.dom.api.DOMDataBroker"/>
-    <reference id="domRpcService" interface="org.opendaylight.mdsal.dom.api.DOMRpcService"/>
-    <reference id="schemaService"
-               interface="org.opendaylight.mdsal.dom.api.DOMSchemaService"/>
-    <reference id="netconfOperationServiceFactoryListener"
-               interface="org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener"
-               odl:type="mapper-aggregator-registry"/>
-
-    <bean id="mdsalNetconfOperationServiceFactory"
-          class="org.opendaylight.netconf.mdsal.connector.MdsalNetconfOperationServiceFactory"
-          factory-method="create"
-          destroy-method="close">
-        <argument ref="schemaService"/>
-        <argument ref="netconfOperationServiceFactoryListener"/>
-        <argument ref="domDataBroker"/>
-        <argument ref="domRpcService"/>
-    </bean>
-    <service ref="mdsalNetconfOperationServiceFactory"
-             interface="org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory"
-             odl:type="mdsal-netconf-connector"/>
-
-</blueprint>
index 5fdbf4637893a122a49035df624f1e7c51cc3f75..0f5a6bb4316049abd7383651dd755f4001dfad55 100644 (file)
@@ -13,12 +13,11 @@ import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doNothing;
-import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
-import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
 
 import com.google.common.base.Preconditions;
 import com.google.common.io.ByteSource;
-import com.google.common.util.concurrent.FluentFuture;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
 import java.io.IOException;
 import java.util.Collection;
 import java.util.List;
@@ -88,8 +87,8 @@ public class RuntimeRpcTest {
 
     private static final DOMRpcService RPC_SERVICE_VOID_INVOKER = new DOMRpcService() {
         @Override
-        public FluentFuture<DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
-            return immediateFluentFuture(new DefaultDOMRpcResult(null, List.of()));
+        public ListenableFuture<DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
+            return Futures.immediateFuture(new DefaultDOMRpcResult(null, List.of()));
         }
 
         @Override
@@ -100,8 +99,8 @@ public class RuntimeRpcTest {
 
     private static final DOMRpcService RPC_SERVICE_FAILED_INVOCATION = new DOMRpcService() {
         @Override
-        public FluentFuture<DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
-            return immediateFailedFluentFuture(new DOMRpcException("rpc invocation not implemented yet") {
+        public ListenableFuture<DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
+            return Futures.immediateFailedFuture(new DOMRpcException("rpc invocation not implemented yet") {
                 private static final long serialVersionUID = 1L;
             });
         }
@@ -114,7 +113,7 @@ public class RuntimeRpcTest {
 
     private final DOMRpcService rpcServiceSuccessfulInvocation = new DOMRpcService() {
         @Override
-        public FluentFuture<DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
+        public ListenableFuture<DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
             final Collection<DataContainerChild> children = input.body();
             final Module module = SCHEMA_CONTEXT.findModules(type.getNamespace()).stream()
                 .findFirst().orElse(null);
@@ -125,7 +124,7 @@ public class RuntimeRpcTest {
                     .withValue(children)
                     .build();
 
-            return immediateFluentFuture(new DefaultDOMRpcResult(node));
+            return Futures.immediateFuture(new DefaultDOMRpcResult(node));
         }
 
         @Override
@@ -171,7 +170,7 @@ public class RuntimeRpcTest {
             final SourceIdentifier sId = (SourceIdentifier) invocationOnMock.getArguments()[0];
             final YangTextSchemaSource yangTextSchemaSource =
                     YangTextSchemaSource.delegateForByteSource(sId, ByteSource.wrap("module test".getBytes()));
-            return immediateFluentFuture(yangTextSchemaSource);
+            return Futures.immediateFuture(yangTextSchemaSource);
         }).when(sourceProvider).getSource(any(SourceIdentifier.class));
 
         currentSchemaContext = CurrentSchemaContext.create(schemaService, sourceProvider);