Refactor truststore merger logic
[oom/platform/cert-service.git] / trustStoreMerger / src / main / java / org / onap / oom / truststoremerger / merger / model / PemTruststore.java
@@ -17,7 +17,7 @@
  * ============LICENSE_END=========================================================
  */
 
-package org.onap.oom.truststoremerger.certification.file.provider;
+package org.onap.oom.truststoremerger.merger.model;
 
 import static org.onap.oom.truststoremerger.api.CertificateConstants.BOUNCY_CASTLE_PROVIDER;
 import static org.onap.oom.truststoremerger.api.CertificateConstants.X_509_CERTIFICATE;
@@ -37,30 +37,30 @@ import org.bouncycastle.jce.provider.BouncyCastleProvider;
 import org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator;
 import org.bouncycastle.util.io.pem.PemObjectGenerator;
 import org.bouncycastle.util.io.pem.PemWriter;
-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.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.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 PemCertificateController implements CertificateController {
+public class PemTruststore extends Truststore {
 
-    private static final Logger LOGGER = LoggerFactory.getLogger(PemCertificateController.class);
+    private static final Logger LOGGER = LoggerFactory.getLogger(PemTruststore.class);
 
     private static final boolean APPEND_TO_FILE = true;
 
     private final CertificateWithAliasFactory factory = new CertificateWithAliasFactory();
     private final List<CertificateWithAlias> certificatesToBeSaved = new ArrayList<>();
-    private final File file;
 
-    public PemCertificateController(File file) {
-        this.file = file;
+    public PemTruststore(File storeFile) {
+        super(storeFile);
     }
 
-    public List<CertificateWithAlias> getNotEmptyCertificateList()
+    public List<CertificateWithAlias> getCertificates()
         throws TruststoreDataOperationException, MissingTruststoreException {
+        LOGGER.debug("Attempt to read certificates from file: {}", storeFile.getPath());
         if (isFileWithoutPemCertificate()) {
             throw new MissingTruststoreException("File does not contain any certificate");
         }
@@ -70,14 +70,16 @@ public class PemCertificateController implements CertificateController {
 
     public void addCertificates(List<CertificateWithAlias> certificates)
         throws TruststoreDataOperationException, MissingTruststoreException {
+        LOGGER.debug("Attempt to add certificates for saving to file");
         if (isFileWithoutPemCertificate()) {
-            LOGGER.error("File does not contain any certificate. File path: {} ", this.file.getPath());
+            LOGGER.error("File does not contain any certificate. File path: {} ", storeFile.getPath());
             throw new MissingTruststoreException("File does not contain any certificate");
         }
         certificatesToBeSaved.addAll(certificates);
     }
 
     public void saveFile() throws WriteTruststoreFileException, TruststoreDataOperationException {
+        LOGGER.debug("Attempt to save file: {}", storeFile.getPath());
         List<Certificate> certificates = certificatesToBeSaved.stream()
             .map(CertificateWithAlias::getCertificate)
             .collect(Collectors.toList());
@@ -104,19 +106,17 @@ public class PemCertificateController implements CertificateController {
         return sw.toString();
     }
 
-
     private List<Certificate> extractCertificatesFromFile() throws TruststoreDataOperationException {
-        try (FileInputStream inputStream = new FileInputStream(this.file)) {
+        try (FileInputStream inputStream = new FileInputStream(storeFile)) {
             Security.addProvider(new BouncyCastleProvider());
             CertificateFactory factory = CertificateFactory.getInstance(X_509_CERTIFICATE, BOUNCY_CASTLE_PROVIDER);
             return new ArrayList<>(factory.generateCertificates(inputStream));
         } catch (Exception e) {
-            LOGGER.error("Cannot read certificates from file: {}", this.file.getPath());
+            LOGGER.error("Cannot read certificates from file: {}", storeFile.getPath());
             throw new TruststoreDataOperationException(e);
         }
     }
 
-
     private List<PemObjectGenerator> transformToPemGenerators(List<Certificate> certificates)
         throws TruststoreDataOperationException {
         List<PemObjectGenerator> generators = new ArrayList<>();
@@ -145,7 +145,7 @@ public class PemCertificateController implements CertificateController {
 
     private void appendToFile(String certificatesAsString) throws WriteTruststoreFileException {
         try {
-            FileOutputStream fileOutputStream = new FileOutputStream(this.file, APPEND_TO_FILE);
+            FileOutputStream fileOutputStream = new FileOutputStream(storeFile, APPEND_TO_FILE);
             fileOutputStream.write(certificatesAsString.getBytes());
         } catch (Exception e) {
             LOGGER.error("Cannot write certificates to file");