Improve code quality in ResourceConfig
[dcaegen2/platform.git] / mod / bpgenerator / src / main / java / org / onap / blueprintgenerator / models / blueprint / ResourceConfig.java
1 /*============LICENSE_START=======================================================
2  org.onap.dcae
3  ================================================================================
4  Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
5  Copyright (c) 2020 Nokia. All rights reserved.
6  ================================================================================
7  Licensed under the Apache License, Version 2.0 (the "License");
8  you may not use this file except in compliance with the License.
9  You may obtain a copy of the License at
10
11       http://www.apache.org/licenses/LICENSE-2.0
12
13  Unless required by applicable law or agreed to in writing, software
14  distributed under the License is distributed on an "AS IS" BASIS,
15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  See the License for the specific language governing permissions and
17  limitations under the License.
18  ============LICENSE_END=========================================================
19
20  */
21
22 package org.onap.blueprintgenerator.models.blueprint;
23
24 import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.createStringInput;
25 import static org.onap.blueprintgenerator.models.blueprint.BpConstants.CPU_LIMIT;
26 import static org.onap.blueprintgenerator.models.blueprint.BpConstants.MEMORY_LIMIT;
27
28 import java.util.LinkedHashMap;
29 import java.util.TreeMap;
30 import lombok.AllArgsConstructor;
31 import lombok.Builder;
32 import lombok.Getter;
33 import lombok.NoArgsConstructor;
34 import lombok.Setter;
35
36 //TODO: Auto-generated Javadoc
37 /* (non-Javadoc)
38  * @see java.lang.Object#toString()
39  */
40 @Getter
41 @Setter
42
43 /* (non-Javadoc)
44  * @see java.lang.Object#toString()
45  */
46 @Builder
47
48 /**
49  * Instantiates a new resource config obj.
50  */
51 @NoArgsConstructor
52
53 /**
54  * Instantiates a new resource config obj.
55  *
56  * @param limits the limits
57  * @param requests the requests
58  */
59 @AllArgsConstructor
60
61 public class ResourceConfig {
62
63     private TreeMap<String, GetInput> limits;
64     private TreeMap<String, GetInput> requests;
65
66
67     /**
68      * Creates the resource config.
69      *
70      * @param inputs the inputs
71      * @param name the name
72      * @return the tree map
73      */
74     public TreeMap<String, LinkedHashMap<String, Object>> createResourceConfig(
75         TreeMap<String, LinkedHashMap<String, Object>> inputs, String name) {
76
77         String namePrefix = getNamePrefix(name);
78
79         limits = createInputs(inputs, namePrefix, "limit");
80         requests = createInputs(inputs, namePrefix, "request");
81
82         return inputs;
83     }
84
85     private TreeMap<String, GetInput> createInputs(TreeMap<String, LinkedHashMap<String, Object>> inputs,
86         String namePrefix,
87         String inputType) {
88
89         LinkedHashMap<String, Object> memoryLimit = createStringInput(MEMORY_LIMIT);
90         LinkedHashMap<String, Object> cpuLimit = createStringInput(CPU_LIMIT);
91
92         final String cpuKey = namePrefix + "cpu_" + inputType;
93         final String memoryKey = namePrefix + "memory_" + inputType;
94         TreeMap<String, GetInput> inps = new TreeMap<>();
95
96         insertInput("cpu", cpuKey, inps);
97         insertInput("memory", memoryKey, inps);
98
99         inputs.put(cpuKey, cpuLimit);
100         inputs.put(memoryKey, memoryLimit);
101
102         return inps;
103     }
104
105     private void insertInput(String type, String name, TreeMap<String, GetInput> inputs) {
106         GetInput input = new GetInput();
107         input.setBpInputName(name);
108         inputs.put(type, input);
109     }
110
111     private String getNamePrefix(String name) {
112         return (name == null || name.isEmpty()) ? "" : name + "_";
113     }
114 }
115