Refactor truststore merger logic
[oom/platform/cert-service.git] / trustStoreMerger / src / main / java / org / onap / oom / truststoremerger / CertificatePostProcessor.java
1 /*============LICENSE_START=======================================================
2  * oom-truststore-merger
3  * ================================================================================
4  * Copyright (C) 2020 Nokia. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END=========================================================
18  */
19
20 package org.onap.oom.truststoremerger;
21
22 import java.util.List;
23 import org.onap.oom.truststoremerger.api.ExitableException;
24 import org.onap.oom.truststoremerger.configuration.MergerConfigurationProvider;
25 import org.onap.oom.truststoremerger.configuration.model.MergerConfiguration;
26 import org.onap.oom.truststoremerger.configuration.path.DelimitedPathsReader;
27 import org.onap.oom.truststoremerger.configuration.path.DelimitedPathsReaderFactory;
28 import org.onap.oom.truststoremerger.configuration.path.env.EnvProvider;
29 import org.onap.oom.truststoremerger.merger.TruststoreFilesProvider;
30 import org.onap.oom.truststoremerger.merger.model.Truststore;
31 import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAlias;
32
33 class CertificatePostProcessor implements Runnable {
34
35     private static final int FIRST_TRUSTSTORE_INDEX = 0;
36     private static final int SECOND_TRUSTSTORE_INDEX = 1;
37
38     public void run() throws ExitableException {
39         mergeTruststores();
40     }
41
42     private void mergeTruststores() throws ExitableException {
43         MergerConfiguration configuration = loadConfiguration();
44         List<Truststore> truststoreFilesList = getTruststoreFiles(configuration);
45
46         Truststore baseFile = truststoreFilesList.get(FIRST_TRUSTSTORE_INDEX);
47         baseFile.createBackup();
48
49         for (int i = SECOND_TRUSTSTORE_INDEX; i < truststoreFilesList.size(); i++) {
50             Truststore truststore = truststoreFilesList.get(i);
51             List<CertificateWithAlias> certificateWrappers = truststore.getCertificates();
52             baseFile.addCertificates(certificateWrappers);
53         }
54
55         baseFile.saveFile();
56     }
57
58     private MergerConfiguration loadConfiguration() throws ExitableException {
59         DelimitedPathsReaderFactory readerFactory = new DelimitedPathsReaderFactory(new EnvProvider());
60         DelimitedPathsReader certificatesPathsReader = readerFactory.createCertificatePathsReader();
61         DelimitedPathsReader passwordsPathsReader = readerFactory.createPasswordPathsReader();
62         DelimitedPathsReader copierPathsReader = readerFactory.createKeystoreCopierPathsReader();
63         MergerConfigurationProvider factory = new MergerConfigurationProvider(certificatesPathsReader,
64             passwordsPathsReader,
65             copierPathsReader);
66         return factory.createConfiguration();
67     }
68
69     private static List<Truststore> getTruststoreFiles(MergerConfiguration configuration) throws ExitableException {
70         return TruststoreFilesProvider
71             .getTruststoreFiles(
72                 configuration.getTruststoreFilePaths(),
73                 configuration.getTruststoreFilePasswordPaths()
74             );
75     }
76 }