Refactor truststore merger logic
[oom/platform/cert-service.git] / trustStoreMerger / src / main / java / org / onap / oom / truststoremerger / TrustStoreMerger.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.ExitStatus;
24 import org.onap.oom.truststoremerger.api.ExitableException;
25 import org.onap.oom.truststoremerger.merger.TruststoreFilesProvider;
26 import org.onap.oom.truststoremerger.merger.model.Truststore;
27 import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAlias;
28 import org.onap.oom.truststoremerger.configuration.MergerConfigurationProvider;
29 import org.onap.oom.truststoremerger.configuration.model.MergerConfiguration;
30 import org.onap.oom.truststoremerger.configuration.path.DelimitedPathsReader;
31 import org.onap.oom.truststoremerger.configuration.path.DelimitedPathsReaderFactory;
32 import org.onap.oom.truststoremerger.configuration.path.env.EnvProvider;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 class TrustStoreMerger {
37
38     private static final Logger LOGGER = LoggerFactory.getLogger(TrustStoreMerger.class);
39     private static final int FIRST_TRUSTSTORE_INDEX = 0;
40     private static final int SECOND_TRUSTSTORE_INDEX = 1;
41
42     private final AppExitHandler appExitHandler;
43
44     TrustStoreMerger(AppExitHandler appExitHandler) {
45         this.appExitHandler = appExitHandler;
46     }
47
48     void run() {
49         try {
50             mergeTruststores();
51             appExitHandler.exit(ExitStatus.SUCCESS);
52         } catch (ExitableException e) {
53             LOGGER.error("Truststore Merger fails in execution: ", e);
54             appExitHandler.exit(e.applicationExitStatus());
55         } catch (Exception e) {
56             LOGGER.error("Truststore Merger fails in execution: ", e);
57             appExitHandler.exit(ExitStatus.UNEXPECTED_EXCEPTION);
58         }
59     }
60
61     private void mergeTruststores() throws ExitableException {
62         MergerConfiguration configuration = loadConfiguration();
63         List<Truststore> truststoreFilesList = getTruststoreFiles(configuration);
64
65         Truststore baseFile = truststoreFilesList.get(FIRST_TRUSTSTORE_INDEX);
66         baseFile.createBackup();
67
68         for (int i = SECOND_TRUSTSTORE_INDEX; i < truststoreFilesList.size(); i++) {
69             Truststore truststore = truststoreFilesList.get(i);
70             List<CertificateWithAlias> certificateWrappers = truststore.getCertificates();
71             baseFile.addCertificates(certificateWrappers);
72         }
73
74         baseFile.saveFile();
75     }
76
77     private MergerConfiguration loadConfiguration() throws ExitableException {
78         DelimitedPathsReaderFactory readerFactory = new DelimitedPathsReaderFactory(new EnvProvider());
79         DelimitedPathsReader certificatesPathsReader = readerFactory.createCertificatePathsReader();
80         DelimitedPathsReader passwordsPathsReader = readerFactory.createPasswordPathsReader();
81         DelimitedPathsReader copierPathsReader = readerFactory.createKeystoreCopierPathsReader();
82         MergerConfigurationProvider factory = new MergerConfigurationProvider(certificatesPathsReader,
83             passwordsPathsReader,
84             copierPathsReader);
85         return factory.createConfiguration();
86     }
87
88     private static List<Truststore> getTruststoreFiles(MergerConfiguration configuration) throws ExitableException {
89         return TruststoreFilesProvider
90             .getTruststoreFiles(
91                 configuration.getTruststoreFilePaths(),
92                 configuration.getTruststoreFilePasswordPaths()
93             );
94     }
95 }