Add controller config code
Add SDC config parser code for mutliple SDC client configurations + unit
tests
Issue-ID: CLAMP-81
Change-Id: Ia97007463c6baf40456f97d9627da8b0ed3f4e49
Signed-off-by: Determe, Sebastien (sd378r) <sd378r@intl.att.com>
diff --git a/src/main/java/org/onap/clamp/clds/config/sdc/SdcControllersConfiguration.java b/src/main/java/org/onap/clamp/clds/config/sdc/SdcControllersConfiguration.java
new file mode 100644
index 0000000..c99dd21
--- /dev/null
+++ b/src/main/java/org/onap/clamp/clds/config/sdc/SdcControllersConfiguration.java
@@ -0,0 +1,94 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP CLAMP
+ * ================================================================================
+ * Copyright (C) 2018 AT&T Intellectual Property. All rights
+ * reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END============================================
+ * ===================================================================
+ * ECOMP is a trademark and service mark of AT&T Intellectual Property.
+ */
+
+package org.onap.clamp.clds.config.sdc;
+
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.annotation.PostConstruct;
+
+import org.onap.clamp.clds.exception.sdc.controller.SdcParametersException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.ApplicationContext;
+import org.springframework.core.io.Resource;
+import org.springframework.stereotype.Component;
+
+/**
+ * This class maps the SDC config JSON file. This JSON can have multiple
+ * sdc-controller config. So the json is loaded in a static way and the instance
+ * must specify the controller name that it represents.
+ */
+@Component
+public class SdcControllersConfiguration {
+
+ private static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcControllersConfiguration.class);
+ public static final String CONTROLLER_SUBTREE_KEY = "sdc-connections";
+ @Autowired
+ protected ApplicationContext appContext;
+ /**
+ * The file name that will be loaded by Spring.
+ */
+ @Value("${org.onap.clamp.config.files.sdcController:'classpath:/clds/sdc-controllers-config.json'}")
+ protected String sdcControllerFile;
+ /**
+ * The root of the JSON.
+ */
+ private JsonNode jsonRootNode;
+
+ @PostConstruct
+ public void loadConfiguration() throws IOException {
+ Resource resource = appContext.getResource(sdcControllerFile);
+ // Try to load json tree
+ jsonRootNode = new ObjectMapper().readValue(resource.getInputStream(), JsonNode.class);
+ }
+
+ public SdcSingleControllerConfiguration getSdcSingleControllerConfiguration(String controllerName) {
+ Map<String, SdcSingleControllerConfiguration> controllerMap = getAllDefinedControllers();
+ return controllerMap.get(controllerName);
+ }
+
+ /**
+ * This method reads all Controllers configurations and returns them.
+ *
+ * @return A list of controller Names defined in the config
+ */
+ public Map<String, SdcSingleControllerConfiguration> getAllDefinedControllers() {
+ Map<String, SdcSingleControllerConfiguration> result = new HashMap<>();
+ if (jsonRootNode.get(CONTROLLER_SUBTREE_KEY) != null) {
+ jsonRootNode.get(CONTROLLER_SUBTREE_KEY).fields().forEachRemaining(entry -> result.put(entry.getKey(),
+ new SdcSingleControllerConfiguration(entry.getValue(), entry.getKey())));
+ } else {
+ throw new SdcParametersException(
+ CONTROLLER_SUBTREE_KEY + " key not found in the file: " + sdcControllerFile);
+ }
+ return result;
+ }
+}
diff --git a/src/test/java/org/onap/clamp/clds/it/config/SdcControllersConfigurationItCase.java b/src/test/java/org/onap/clamp/clds/it/config/SdcControllersConfigurationItCase.java
new file mode 100644
index 0000000..4b63248
--- /dev/null
+++ b/src/test/java/org/onap/clamp/clds/it/config/SdcControllersConfigurationItCase.java
@@ -0,0 +1,86 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - SO
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.clamp.clds.it.config;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+import java.util.Map;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.onap.clamp.clds.config.sdc.SdcControllersConfiguration;
+import org.onap.clamp.clds.config.sdc.SdcSingleControllerConfiguration;
+import org.onap.clamp.clds.exception.sdc.controller.SdcParametersException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.test.util.ReflectionTestUtils;
+
+/**
+ * This class tests the SDC Controller config.
+ */
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class SdcControllersConfigurationItCase {
+
+ @Autowired
+ private SdcControllersConfiguration sdcControllersConfiguration;
+
+ public final void loadFile(String fileName) throws IOException {
+ ReflectionTestUtils.setField(sdcControllersConfiguration, "sdcControllerFile", fileName);
+ sdcControllersConfiguration.loadConfiguration();
+ }
+
+ @Test
+ public void testGetAllDefinedControllers() throws IOException {
+ loadFile("classpath:/clds/sdc-controllers-config.json");
+ Map<String, SdcSingleControllerConfiguration> mapResult = sdcControllersConfiguration
+ .getAllDefinedControllers();
+ assertTrue(mapResult.size() == 2);
+ assertEquals("sdc-controller1", mapResult.get("sdc-controller1").getSdcControllerName());
+ assertEquals("sdc-controller2", mapResult.get("sdc-controller2").getSdcControllerName());
+ }
+
+ @Test
+ public void testGetSdcSingleControllerConfiguration() throws IOException {
+ loadFile("classpath:/clds/sdc-controllers-config.json");
+ assertEquals("sdc-controller1", sdcControllersConfiguration
+ .getSdcSingleControllerConfiguration("sdc-controller1").getSdcControllerName());
+ assertEquals("sdc-controller2", sdcControllersConfiguration
+ .getSdcSingleControllerConfiguration("sdc-controller2").getSdcControllerName());
+ }
+
+ @Test(expected = IOException.class)
+ public void testBadJsonLoading() throws IOException {
+ loadFile("classpath:/clds/sdc-controllers-config-bad.json");
+ fail("Should have raised an exception");
+ }
+
+ @Test(expected = SdcParametersException.class)
+ public void testMissingParamInJsonLoading() throws IOException {
+ loadFile("classpath:/clds/sdc-controllers-config-missing-param.json");
+ sdcControllersConfiguration.getAllDefinedControllers();
+ fail("Should have raised an exception");
+ }
+}
diff --git a/src/test/resources/clds/sdc-controllers-config-bad.json b/src/test/resources/clds/sdc-controllers-config-bad.json
new file mode 100644
index 0000000..0b417b7
--- /dev/null
+++ b/src/test/resources/clds/sdc-controllers-config-bad.json
@@ -0,0 +1,26 @@
+{
+ "sdc-connections":{,
+ "sdc-controller1":{
+ "user": "User1",
+ "consumerGroup": "consumerGroup1",
+ "consumerId": "consumerId1",
+ "environmentName": "environmentName1",
+ "sdcAddress": "hostname1",
+ "password": "bb3871669d893c7fb8aaacda31b77b4f537E67A081C2726889548ED7BC4C2DE6",
+ "pollingInterval":10,
+ "pollingTimeout":30
+
+ },
+ "sdc-controller2":{
+ "user": "User2",
+ "consumerGroup": "consumerGroup2",
+ "consumerId": "consumerId2",
+ "environmentName": "environmentName2",
+ "sdcAddress": "hostname2",
+ "password": "bb3871669d893c7fb8aaacda31b77b4f537E67A081C2726889548ED7BC4C2DE6",
+ "pollingInterval":10,
+ "pollingTimeout":30
+
+ }
+ }
+}
diff --git a/src/test/resources/clds/sdc-controllers-config-missing-param.json b/src/test/resources/clds/sdc-controllers-config-missing-param.json
new file mode 100644
index 0000000..d99ed58
--- /dev/null
+++ b/src/test/resources/clds/sdc-controllers-config-missing-param.json
@@ -0,0 +1,13 @@
+{
+ "sdc-connections": {
+ "sdc-controller1": {
+ "user": "testuser",
+ "consumerGroup": "consumerGroup",
+ "consumerId": "consumerId",
+ "environmentName": "environmentName",
+ "sdcAddress": "hostname1",
+ "pollingInterval": 10,
+ "pollingTimeout": 30
+ }
+ }
+}
diff --git a/src/test/resources/clds/sdc-controllers-config.json b/src/test/resources/clds/sdc-controllers-config.json
new file mode 100644
index 0000000..df73a50
--- /dev/null
+++ b/src/test/resources/clds/sdc-controllers-config.json
@@ -0,0 +1,26 @@
+{
+ "sdc-connections":{
+ "sdc-controller1":{
+ "user": "User1",
+ "consumerGroup": "consumerGroup1",
+ "consumerId": "consumerId1",
+ "environmentName": "environmentName1",
+ "sdcAddress": "hostname1",
+ "password": "bb3871669d893c7fb8aaacda31b77b4f537E67A081C2726889548ED7BC4C2DE6",
+ "pollingInterval":10,
+ "pollingTimeout":30
+
+ },
+ "sdc-controller2":{
+ "user": "User2",
+ "consumerGroup": "consumerGroup2",
+ "consumerId": "consumerId2",
+ "environmentName": "environmentName2",
+ "sdcAddress": "hostname2",
+ "password": "bb3871669d893c7fb8aaacda31b77b4f537E67A081C2726889548ED7BC4C2DE6",
+ "pollingInterval":10,
+ "pollingTimeout":30
+
+ }
+ }
+}