Merge changes from topic 'feature/sdc-controller'
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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 import com.fasterxml.jackson.databind.JsonNode;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30
31 import java.io.IOException;
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.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.beans.factory.annotation.Value;
40 import org.springframework.context.ApplicationContext;
41 import org.springframework.core.io.Resource;
42 import org.springframework.stereotype.Component;
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 @Component
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("${org.onap.clamp.config.files.sdcController:'classpath:/clds/sdc-controllers-config.json'}")
60     protected String sdcControllerFile;
61     /**
62      * The root of the JSON.
63      */
64     private JsonNode jsonRootNode;
65
66     @PostConstruct
67     public void loadConfiguration() throws IOException {
68         Resource resource = appContext.getResource(sdcControllerFile);
69         // Try to load json tree
70         jsonRootNode = new ObjectMapper().readValue(resource.getInputStream(), JsonNode.class);
71     }
72
73     public SdcSingleControllerConfiguration getSdcSingleControllerConfiguration(String controllerName) {
74         Map<String, SdcSingleControllerConfiguration> controllerMap = getAllDefinedControllers();
75         return controllerMap.get(controllerName);
76     }
77
78     /**
79      * This method reads all Controllers configurations and returns them.
80      *
81      * @return A list of controller Names defined in the config
82      */
83     public Map<String, SdcSingleControllerConfiguration> getAllDefinedControllers() {
84         Map<String, SdcSingleControllerConfiguration> result = new HashMap<>();
85         if (jsonRootNode.get(CONTROLLER_SUBTREE_KEY) != null) {
86             jsonRootNode.get(CONTROLLER_SUBTREE_KEY).fields().forEachRemaining(entry -> result.put(entry.getKey(),
87                     new SdcSingleControllerConfiguration(entry.getValue(), entry.getKey())));
88         } else {
89             throw new SdcParametersException(
90                     CONTROLLER_SUBTREE_KEY + " key not found in the file: " + sdcControllerFile);
91         }
92         return result;
93     }
94 }