Fixed generic Sonar issue in the clamp project
[clamp.git] / src / main / java / org / onap / clamp / clds / config / sdc / SdcControllersConfiguration.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END============================================
22  * ===================================================================
23  * 
24  */
25
26 package org.onap.clamp.clds.config.sdc;
27
28 import com.google.gson.JsonObject;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31 import java.nio.charset.StandardCharsets;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import javax.annotation.PostConstruct;
36
37 import org.onap.clamp.clds.exception.sdc.controller.SdcParametersException;
38 import org.onap.clamp.clds.util.JsonUtils;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.beans.factory.annotation.Value;
41 import org.springframework.context.ApplicationContext;
42 import org.springframework.core.io.Resource;
43
44 /**
45  * This class maps the SDC config JSON file. This JSON can have multiple
46  * sdc-controller config. So the json is loaded in a static way and the instance
47  * must specify the controller name that it represents.
48  */
49 public class SdcControllersConfiguration {
50
51     private static final String CONTROLLER_SUBTREE_KEY = "sdc-connections";
52     @Autowired
53     protected ApplicationContext appContext;
54     /**
55      * The file name that will be loaded by Spring.
56      */
57     @Value("${clamp.config.files.sdcController:'classpath:/clds/sdc-controllers-config.json'}")
58     protected String sdcControllerFile;
59     /**
60      * The root of the JSON.
61      */
62     private JsonObject jsonRootNode;
63
64     /**
65      * Loads configuration from SDC controller config file.
66      *
67      * @throws IOException IO Exception
68      */
69     @PostConstruct
70     public void loadConfiguration() throws IOException {
71         Resource resource = appContext.getResource(sdcControllerFile);
72         // Try to load json tree
73         jsonRootNode = JsonUtils.GSON.fromJson(new InputStreamReader(
74                 resource.getInputStream(), StandardCharsets.UTF_8),
75                                                JsonObject.class);
76     }
77
78     public SdcSingleControllerConfiguration getSdcSingleControllerConfiguration(String controllerName) {
79         return getAllDefinedControllers().get(controllerName);
80     }
81
82     /**
83      * This method reads all Controllers configurations and returns them.
84      *
85      * @return A list of controller Names defined in the config
86      */
87     public Map<String, SdcSingleControllerConfiguration> getAllDefinedControllers() {
88         Map<String, SdcSingleControllerConfiguration> result = new HashMap<>();
89         if (jsonRootNode.get(CONTROLLER_SUBTREE_KEY) != null) {
90             jsonRootNode.get(CONTROLLER_SUBTREE_KEY).getAsJsonObject().entrySet().forEach(
91                 entry -> result.put(entry.getKey(),
92                     new SdcSingleControllerConfiguration(entry.getValue().getAsJsonObject(), entry.getKey())));
93         } else {
94             throw new SdcParametersException(
95                     CONTROLLER_SUBTREE_KEY + " key not found in the file: " + sdcControllerFile);
96         }
97         return result;
98     }
99 }