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