package org.onap.blueprintgenerator.models.blueprint;
import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.createStringInput;
+import static org.onap.blueprintgenerator.models.blueprint.BpConstants.CPU_LIMIT;
+import static org.onap.blueprintgenerator.models.blueprint.BpConstants.MEMORY_LIMIT;
import java.util.LinkedHashMap;
import java.util.TreeMap;
-
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
public class ResourceConfig {
- /**
- * The limits.
- */
private TreeMap<String, GetInput> limits;
-
- /**
- * The requests.
- */
private TreeMap<String, GetInput> requests;
/**
* Creates the resource config.
*
- * @param inps the inps
+ * @param inputs the inputs
* @param name the name
* @return the tree map
*/
public TreeMap<String, LinkedHashMap<String, Object>> createResourceConfig(
- TreeMap<String, LinkedHashMap<String, Object>> inps, String name) {
-
- LinkedHashMap<String, Object> memoryLimit = createStringInput("128Mi");
- LinkedHashMap<String, Object> cpuLimit = createStringInput("250m");
+ TreeMap<String, LinkedHashMap<String, Object>> inputs, String name) {
String namePrefix = getNamePrefix(name);
- //set the limits
- TreeMap<String, GetInput> limits = new TreeMap<>();
+ limits = createInputs(inputs, namePrefix, "limit");
+ requests = createInputs(inputs, namePrefix, "request");
- GetInput cpu = new GetInput();
- cpu.setBpInputName(namePrefix + "cpu_limit");
- limits.put("cpu", cpu);
-
- GetInput memL = new GetInput();
- memL.setBpInputName(namePrefix + "memory_limit");
- limits.put("memory", memL);
-
- inps.put(namePrefix + "cpu_limit", cpuLimit);
- inps.put(namePrefix + "memory_limit", memoryLimit);
-
- this.setLimits(limits);
+ return inputs;
+ }
- //set the requests
- TreeMap<String, GetInput> requests = new TreeMap<>();
+ private TreeMap<String, GetInput> createInputs(TreeMap<String, LinkedHashMap<String, Object>> inputs,
+ String namePrefix,
+ String inputType) {
- GetInput cpuR = new GetInput();
- cpuR.setBpInputName(namePrefix + "cpu_request");
- requests.put("cpu", cpuR);
+ LinkedHashMap<String, Object> memoryLimit = createStringInput(MEMORY_LIMIT);
+ LinkedHashMap<String, Object> cpuLimit = createStringInput(CPU_LIMIT);
- GetInput memR = new GetInput();
- memR.setBpInputName(namePrefix + "memory_request");
- requests.put("memory", memR);
+ final String cpuKey = namePrefix + "cpu_" + inputType;
+ final String memoryKey = namePrefix + "memory_" + inputType;
+ TreeMap<String, GetInput> inps = new TreeMap<>();
- inps.put(namePrefix + "cpu_request", cpuLimit);
- inps.put(namePrefix + "memory_request", memoryLimit);
+ insertInput("cpu", cpuKey, inps);
+ insertInput("memory", memoryKey, inps);
- this.setRequests(requests);
+ inputs.put(cpuKey, cpuLimit);
+ inputs.put(memoryKey, memoryLimit);
return inps;
}
+ private void insertInput(String type, String name, TreeMap<String, GetInput> inputs) {
+ GetInput input = new GetInput();
+ input.setBpInputName(name);
+ inputs.put(type, input);
+ }
+
private String getNamePrefix(String name) {
- return name.isEmpty() ? "" : name + "_";
+ return (name == null || name.isEmpty()) ? "" : name + "_";
}
}
--- /dev/null
+/*============LICENSE_START=======================================================
+ org.onap.dcae
+ ================================================================================
+ Copyright (c) 2020 Nokia. 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.blueprintgenerator.models.blueprint;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.LinkedHashMap;
+import java.util.TreeMap;
+import org.junit.Test;
+
+public class ResourceConfigTest {
+
+ @Test
+ public void createResourceConfig() {
+ TreeMap<String, LinkedHashMap<String, Object>> result = new ResourceConfig()
+ .createResourceConfig(new TreeMap<>(), "demo");
+
+ String expectedResult = "{demo_cpu_limit={type=string, default=250m}, demo_cpu_request={type=string, default=250m}, demo_memory_limit={type=string, default=128Mi}, demo_memory_request={type=string, default=128Mi}}";
+ assertEquals(expectedResult, result.toString());
+ }
+}