Improve code quality in ResourceConfig
[dcaegen2/platform.git] / mod / bpgenerator / src / main / java / org / onap / blueprintgenerator / models / blueprint / ResourceConfig.java
index f1a9a17..3618be3 100644 (file)
@@ -1,7 +1,8 @@
-/**============LICENSE_START=======================================================
+/*============LICENSE_START=======================================================
  org.onap.dcae
  ================================================================================
  Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
+ 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.
 
 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 com.fasterxml.jackson.annotation.JsonInclude;
-
 import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Getter;
@@ -33,93 +35,81 @@ import lombok.Setter;
 
 //TODO: Auto-generated Javadoc
 /* (non-Javadoc)
-* @see java.lang.Object#toString()
-*/
-@Getter @Setter
+ * @see java.lang.Object#toString()
+ */
+@Getter
+@Setter
 
 /* (non-Javadoc)
-* @see java.lang.Object#toString()
-*/
+ * @see java.lang.Object#toString()
+ */
 @Builder
 
 /**
-* Instantiates a new resource config obj.
-*/
+ * Instantiates a new resource config obj.
+ */
 @NoArgsConstructor
 
 /**
-* Instantiates a new resource config obj.
-*
-* @param limits the limits
-* @param requests the requests
-*/
+ * Instantiates a new resource config obj.
+ *
+ * @param limits the limits
+ * @param requests the requests
+ */
 @AllArgsConstructor
 
 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 name the name
-        * @return the tree map
-        */
-       public TreeMap<String, LinkedHashMap<String, Object>> createResourceConfig(TreeMap<String, LinkedHashMap<String, Object>> inps, String name){
-               TreeMap<String, LinkedHashMap<String, Object>> retInputs = inps;
-
-               LinkedHashMap<String, Object> mi = new LinkedHashMap<String, Object>();
-               mi.put("type", "string");
-               mi.put("default", "128Mi");
-
-               LinkedHashMap<String, Object> m = new LinkedHashMap<String, Object>();
-               m.put("type", "string");
-               m.put("default", "250m");
+    private TreeMap<String, GetInput> limits;
+    private TreeMap<String, GetInput> requests;
 
 
-               if(!name.equals("")) {
-                       name = name + "_";
-               }
+    /**
+     * Creates the resource config.
+     *
+     * @param inputs the inputs
+     * @param name the name
+     * @return the tree map
+     */
+    public TreeMap<String, LinkedHashMap<String, Object>> createResourceConfig(
+        TreeMap<String, LinkedHashMap<String, Object>> inputs, String name) {
 
-               //set the limits
-               TreeMap<String, GetInput> lim = new TreeMap<String, GetInput>();
+        String namePrefix = getNamePrefix(name);
 
-               GetInput cpu = new GetInput();
-               cpu.setGet_input(name + "cpu_limit");
-               lim.put("cpu", cpu);
+        limits = createInputs(inputs, namePrefix, "limit");
+        requests = createInputs(inputs, namePrefix, "request");
 
-               GetInput memL = new GetInput();
-               memL.setGet_input(name + "memory_limit");
-               lim.put("memory", memL);
+        return inputs;
+    }
 
-               retInputs.put(name + "cpu_limit", m);
-               retInputs.put(name + "memory_limit", mi);
+    private TreeMap<String, GetInput> createInputs(TreeMap<String, LinkedHashMap<String, Object>> inputs,
+        String namePrefix,
+        String inputType) {
 
-               this.setLimits(lim);
+        LinkedHashMap<String, Object> memoryLimit = createStringInput(MEMORY_LIMIT);
+        LinkedHashMap<String, Object> cpuLimit = createStringInput(CPU_LIMIT);
 
-               //set the requests
-               TreeMap<String, GetInput> req = new TreeMap<String, GetInput>();
+        final String cpuKey = namePrefix + "cpu_" + inputType;
+        final String memoryKey = namePrefix + "memory_" + inputType;
+        TreeMap<String, GetInput> inps = new TreeMap<>();
 
-               GetInput cpuR = new GetInput();
-               cpuR.setGet_input(name + "cpu_request");
-               req.put("cpu", cpuR);
+        insertInput("cpu", cpuKey, inps);
+        insertInput("memory", memoryKey, inps);
 
-               GetInput memR = new GetInput();
-               memR.setGet_input(name + "memory_request");
-               req.put("memory", memR);
+        inputs.put(cpuKey, cpuLimit);
+        inputs.put(memoryKey, memoryLimit);
 
-               retInputs.put(name + "cpu_request", m);
-               retInputs.put(name + "memory_request", mi);
+        return inps;
+    }
 
-               this.setRequests(req);
+    private void insertInput(String type, String name, TreeMap<String, GetInput> inputs) {
+        GetInput input = new GetInput();
+        input.setBpInputName(name);
+        inputs.put(type, input);
+    }
 
-               return retInputs;
-       }
+    private String getNamePrefix(String name) {
+        return (name == null || name.isEmpty()) ? "" : name + "_";
+    }
 }