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