fdc007458f47bc36f4051851d102e0ebb2694782
[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
30 import java.io.IOException;
31 import java.util.HashMap;
32 import java.util.Map;
33
34 import javax.annotation.PostConstruct;
35
36 import org.onap.clamp.clds.exception.sdc.controller.SdcParametersException;
37 import org.onap.clamp.clds.util.JacksonUtils;
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
43 /**
44  * This class maps the SDC config JSON file. This JSON can have multiple
45  * sdc-controller config. So the json is loaded in a static way and the instance
46  * must specify the controller name that it represents.
47  */
48 public class SdcControllersConfiguration {
49
50     private static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcControllersConfiguration.class);
51     public 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 JsonNode jsonRootNode;
63
64     @PostConstruct
65     public void loadConfiguration() throws IOException {
66         Resource resource = appContext.getResource(sdcControllerFile);
67         // Try to load json tree
68         jsonRootNode = JacksonUtils.getObjectMapperInstance().readValue(resource.getInputStream(), JsonNode.class);
69     }
70
71     public SdcSingleControllerConfiguration getSdcSingleControllerConfiguration(String controllerName) {
72         return getAllDefinedControllers().get(controllerName);
73     }
74
75     /**
76      * This method reads all Controllers configurations and returns them.
77      *
78      * @return A list of controller Names defined in the config
79      */
80     public Map<String, SdcSingleControllerConfiguration> getAllDefinedControllers() {
81         Map<String, SdcSingleControllerConfiguration> result = new HashMap<>();
82         if (jsonRootNode.get(CONTROLLER_SUBTREE_KEY) != null) {
83             jsonRootNode.get(CONTROLLER_SUBTREE_KEY).fields().forEachRemaining(entry -> result.put(entry.getKey(),
84                     new SdcSingleControllerConfiguration(entry.getValue(), entry.getKey())));
85         } else {
86             throw new SdcParametersException(
87                     CONTROLLER_SUBTREE_KEY + " key not found in the file: " + sdcControllerFile);
88         }
89         return result;
90     }
91 }