2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.clds.config.sdc;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
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;
36 import javax.annotation.PostConstruct;
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;
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.
50 public class SdcControllersConfiguration {
52 private static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcControllersConfiguration.class);
53 public static final String CONTROLLER_SUBTREE_KEY = "sdc-connections";
55 protected ApplicationContext appContext;
57 * The file name that will be loaded by Spring.
59 @Value("${clamp.config.files.sdcController:'classpath:/clds/sdc-controllers-config.json'}")
60 protected String sdcControllerFile;
62 * The root of the JSON.
64 private JsonObject jsonRootNode;
67 * Loads configuration from SDC controller config file.
69 * @throws IOException IO Exception
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),
80 public SdcSingleControllerConfiguration getSdcSingleControllerConfiguration(String controllerName) {
81 return getAllDefinedControllers().get(controllerName);
85 * This method reads all Controllers configurations and returns them.
87 * @return A list of controller Names defined in the config
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())));
96 throw new SdcParametersException(
97 CONTROLLER_SUBTREE_KEY + " key not found in the file: " + sdcControllerFile);