b31fbcadf9e8d6b57a9caa868ee97248b670e03b
[oom/platform/cert-service.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
4  * ================================================================================
5  * Copyright (C) 2020 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aaf.certservice.certification.configuration;
22
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import java.io.File;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.net.URL;
28 import java.util.ArrayList;
29 import java.util.List;
30 import org.onap.aaf.certservice.certification.CertificationModelFactory;
31 import org.onap.aaf.certservice.certification.configuration.model.CmpServers;
32 import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 class CmpServersConfigLoader {
37     private static final Logger LOGGER = LoggerFactory.getLogger(CertificationModelFactory.class);
38
39     List<Cmpv2Server> load(String path) {
40         List<Cmpv2Server> result = new ArrayList<>();
41         try {
42             result = loadConfigFromFile(path).getCmpv2Servers();
43         } catch (FileNotFoundException e) {
44             LOGGER.error("CMP Servers configuration file not found: ", e);
45         } catch (IOException e) {
46             LOGGER.error("Exception occurred during CMP Servers configuration loading: ", e);
47         }
48         return result;
49     }
50
51     private CmpServers loadConfigFromFile(String path) throws IOException {
52         ObjectMapper objectMapper = new ObjectMapper();
53         URL resource = getClass().getClassLoader().getResource(path);
54         if (resource == null) {
55             throw new FileNotFoundException();
56         }
57         String configFilePath = resource.getFile();
58         return objectMapper.readValue(new File(configFilePath), CmpServers.class);
59     }
60 }