Refactor truststore merger logic
[oom/platform/cert-service.git] / trustStoreMerger / src / main / java / org / onap / oom / truststoremerger / merger / model / JavaTruststore.java
@@ -17,7 +17,7 @@
  * ============LICENSE_END=========================================================
  */
 
-package org.onap.oom.truststoremerger.certification.file.provider;
+package org.onap.oom.truststoremerger.merger.model;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -28,33 +28,40 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import org.onap.oom.truststoremerger.api.ExitableException;
-import org.onap.oom.truststoremerger.certification.file.exception.AliasConflictException;
-import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException;
-import org.onap.oom.truststoremerger.certification.file.exception.MissingTruststoreException;
-import org.onap.oom.truststoremerger.certification.file.exception.TruststoreDataOperationException;
-import org.onap.oom.truststoremerger.certification.file.exception.WriteTruststoreFileException;
-import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias;
-import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAliasFactory;
+import org.onap.oom.truststoremerger.merger.exception.AliasConflictException;
+import org.onap.oom.truststoremerger.merger.exception.LoadTruststoreException;
+import org.onap.oom.truststoremerger.merger.exception.MissingTruststoreException;
+import org.onap.oom.truststoremerger.merger.exception.TruststoreDataOperationException;
+import org.onap.oom.truststoremerger.merger.exception.WriteTruststoreFileException;
+import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAlias;
+import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAliasFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class JavaCertificateStoreController implements CertificateController {
+public final class JavaTruststore extends Truststore {
 
-    private static final Logger LOGGER = LoggerFactory.getLogger(JavaCertificateStoreController.class);
+    private static final Logger LOGGER = LoggerFactory.getLogger(JavaTruststore.class);
 
     private final CertificateWithAliasFactory factory = new CertificateWithAliasFactory();
     private final KeyStore keyStore;
-    private final File storeFile;
     private final String password;
 
 
-    public JavaCertificateStoreController(KeyStore keyStore, File storeFile, String password) {
+    private JavaTruststore(KeyStore keyStore, File storeFile, String password) {
+        super(storeFile);
         this.keyStore = keyStore;
-        this.storeFile = storeFile;
         this.password = password;
     }
 
-    public List<CertificateWithAlias> getNotEmptyCertificateList() throws ExitableException {
+    public static JavaTruststore createWithLoadingFile(KeyStore keyStore, File storeFile, String password)
+        throws LoadTruststoreException {
+        JavaTruststore javaTruststore = new JavaTruststore(keyStore, storeFile, password);
+        javaTruststore.loadFile();
+        return javaTruststore;
+    }
+
+    public List<CertificateWithAlias> getCertificates() throws ExitableException {
+        LOGGER.debug("Attempt to read certificates from file: {}", storeFile.getPath());
         List<String> aliases = getTruststoreAliases();
         if (aliases.isEmpty()) {
             throw new MissingTruststoreException("Missing certificate aliases in file: " + storeFile.getPath());
@@ -64,6 +71,7 @@ public class JavaCertificateStoreController implements CertificateController {
 
     public void addCertificates(List<CertificateWithAlias> certificatesWithAliases)
         throws ExitableException {
+        LOGGER.debug("Attempt to add certificates for saving to file");
         if (getTruststoreAliases().isEmpty()) {
             throw new MissingTruststoreException("Missing certificate aliases in file: " + storeFile.getPath());
         }
@@ -73,7 +81,8 @@ public class JavaCertificateStoreController implements CertificateController {
     }
 
     public void saveFile() throws WriteTruststoreFileException {
-        try (FileOutputStream outputStream = new FileOutputStream(this.storeFile)) {
+        LOGGER.debug("Attempt to save file: {}", storeFile.getPath());
+        try (FileOutputStream outputStream = new FileOutputStream(storeFile)) {
             keyStore.store(outputStream, this.password.toCharArray());
         } catch (Exception e) {
             LOGGER.error("Cannot write truststore file");
@@ -81,11 +90,11 @@ public class JavaCertificateStoreController implements CertificateController {
         }
     }
 
-    public void loadFile() throws LoadTruststoreException {
+    private void loadFile() throws LoadTruststoreException {
         try {
-            keyStore.load(new FileInputStream(this.storeFile), this.password.toCharArray());
+            keyStore.load(new FileInputStream(storeFile), this.password.toCharArray());
         } catch (Exception e) {
-            LOGGER.error("Cannot load file: {}", this.storeFile.getPath());
+            LOGGER.error("Cannot load file: {}", storeFile.getPath());
             throw new LoadTruststoreException(e);
         }
     }