Convert AAAEncryptionServiceImpl to OSGi DS 54/104254/2
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 5 Feb 2023 16:01:12 +0000 (17:01 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 6 Feb 2023 09:20:27 +0000 (10:20 +0100)
Inject the intended configuration to Service Registry and pick it up
from there, allowing users to rely on the service being provided by OSGi
DS.

Since the configuration is provided by Blueprint, we need to explicitly
mention it in Provide-Capability.

JIRA: AAA-250
Change-Id: Id6b33f8b0aa75e72321022ebf085bf89659cca42
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
aaa-encrypt-service/impl/pom.xml
aaa-encrypt-service/impl/src/main/java/org/opendaylight/aaa/encrypt/impl/AAAEncryptionServiceConfigurator.java
aaa-encrypt-service/impl/src/main/java/org/opendaylight/aaa/encrypt/impl/AAAEncryptionServiceImpl.java
aaa-encrypt-service/impl/src/main/java/org/opendaylight/aaa/encrypt/impl/EncryptServiceConfigSupplier.java [new file with mode: 0644]
aaa-encrypt-service/impl/src/main/resources/OSGI-INF/blueprint/encryptservice.xml

index dd12da632a194eefcc7204278f66e1d37bccd7fb..9be3a4e6b356ad2da19b25fec14b880bea02d397 100644 (file)
@@ -45,6 +45,10 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
       <groupId>org.opendaylight.aaa</groupId>
       <artifactId>aaa-encrypt-service</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.service.component.annotations</artifactId>
+    </dependency>
 
     <dependency>
       <groupId>org.opendaylight.yangtools</groupId>
@@ -53,6 +57,7 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
     </dependency>
   </dependencies>
 
+  <!-- FIXME: AAA-204: remove all this once we do not have Blueprint -->
   <build>
     <plugins>
       <plugin>
@@ -77,6 +82,20 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
           </execution>
         </executions>
       </plugin>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <extensions>true</extensions>
+        <configuration>
+          <instructions>
+            <!-- Mixed OSGi DS/Blueprint services, we have to override to get Blueprint bits here -->
+            <Provide-Capability>
+                osgi.service;objectClass:List&lt;String&gt;="org.opendaylight.aaa.encrypt.AAAEncryptionService";uses:="org.opendaylight.aaa.encrypt",
+                osgi.service;objectClass:List&lt;String&gt;="org.opendaylight.aaa.encrypt.impl.EncryptServiceConfigSupplier";uses:="org.opendaylight.aaa.encrypt.impl"
+            </Provide-Capability>
+          </instructions>
+        </configuration>
+      </plugin>
     </plugins>
   </build>
 </project>
index 31e6f77c488916ee4afec83a535fe33e86517179..1f6458e58ddd6d9a907edd5639550676becce919 100644 (file)
@@ -41,7 +41,7 @@ import org.xml.sax.SAXException;
  * Intermediate component dealing with establishing initial configuration for {@link AAAEncryptionServiceImpl}. In
  * particular it deals with generating and persisting of encryption salt and encryption password.
  */
-public final class AAAEncryptionServiceConfigurator implements EncryptServiceConfig {
+public final class AAAEncryptionServiceConfigurator implements EncryptServiceConfigSupplier, EncryptServiceConfig {
     private static final Logger LOG = LoggerFactory.getLogger(AAAEncryptionServiceConfigurator.class);
 
     // Note: this is a strong binding to Blueprint, which is loading etc/opendaylight/datastore/initial/config
@@ -136,6 +136,11 @@ public final class AAAEncryptionServiceConfigurator implements EncryptServiceCon
         }
     }
 
+    @Override
+    public EncryptServiceConfig get() {
+        return this;
+    }
+
     @Override
     public Class<? extends EncryptServiceConfig> implementedInterface() {
         throw new UnsupportedOperationException();
index 2ebe15246e06ddd38727d300de3f8b7fd2295d3b..41178bde1d7375ce9a0ffa3f1a9fdded26d0dfd5 100644 (file)
@@ -24,7 +24,10 @@ import javax.crypto.spec.IvParameterSpec;
 import javax.crypto.spec.PBEKeySpec;
 import javax.crypto.spec.SecretKeySpec;
 import org.opendaylight.aaa.encrypt.AAAEncryptionService;
-import org.opendaylight.yang.gen.v1.config.aaa.authn.encrypt.service.config.rev160915.EncryptServiceConfig;
+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;
 
@@ -34,14 +37,18 @@ import org.slf4j.LoggerFactory;
  * @author - Sharon Aicler (saichler@gmail.com)
  */
 @Deprecated
-public class AAAEncryptionServiceImpl implements AAAEncryptionService {
+@Component(immediate = true)
+public final class AAAEncryptionServiceImpl implements AAAEncryptionService {
     private static final Logger LOG = LoggerFactory.getLogger(AAAEncryptionServiceImpl.class);
 
     private final SecretKey key;
     private final Cipher encryptCipher;
     private final Cipher decryptCipher;
 
-    public AAAEncryptionServiceImpl(final EncryptServiceConfig encrySrvConfig) {
+    @Activate
+    public AAAEncryptionServiceImpl(@Reference final EncryptServiceConfigSupplier configProvider) {
+        final var encrySrvConfig = configProvider.get();
+
         final byte[] encryptionKeySalt = encrySrvConfig.requireEncryptSalt();
         IvParameterSpec tempIvSpec = null;
         SecretKey tempKey = null;
@@ -74,6 +81,12 @@ public class AAAEncryptionServiceImpl implements AAAEncryptionService {
             LOG.error("Failed to create decrypt cipher.", e);
         }
         decryptCipher = cipher;
+        LOG.info("AAAEncryptionService activated");
+    }
+
+    @Deactivate
+    void deactivate() {
+        LOG.info("AAAEncryptionService deactivated");
     }
 
     @Override
diff --git a/aaa-encrypt-service/impl/src/main/java/org/opendaylight/aaa/encrypt/impl/EncryptServiceConfigSupplier.java b/aaa-encrypt-service/impl/src/main/java/org/opendaylight/aaa/encrypt/impl/EncryptServiceConfigSupplier.java
new file mode 100644 (file)
index 0000000..7a7eddf
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * 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.aaa.encrypt.impl;
+
+import java.util.function.Supplier;
+import org.opendaylight.yang.gen.v1.config.aaa.authn.encrypt.service.config.rev160915.EncryptServiceConfig;
+
+/**
+ * Blueprint compatibility hack. Direct injection would result in
+ * <pre>
+ *   Caused by: java.lang.IncompatibleClassChangeError: class Proxy601f54db_5975_4863_a33b_758ee248518f cannot implement
+ *              sealed interface org.opendaylight.yangtools.yang.binding.DataContainer
+ * </pre>
+ * and hence we wrap it with a DataObject, which is not sealed.
+ */
+@Deprecated
+public interface EncryptServiceConfigSupplier extends Supplier<EncryptServiceConfig> {
+
+}
index 800ef51a2936092e658bbda8f08fba1a433f0a0f..5009bce3ab835c957e6d4c6dab3978d872799e67 100644 (file)
     <argument ref="dataBroker"/>
     <argument ref="encryptConfig"/>
   </bean>
-
-  <bean id="encryptService" class="org.opendaylight.aaa.encrypt.impl.AAAEncryptionServiceImpl">
-    <argument ref="encryptServiceConfigurator"/>
-  </bean>
-  <service ref="encryptService" interface="org.opendaylight.aaa.encrypt.AAAEncryptionService"/>
+  <service ref="encryptServiceConfigurator" interface="org.opendaylight.aaa.encrypt.impl.EncryptServiceConfigSupplier"/>
 
 </blueprint>