Fix sonar issues
[dcaegen2/platform.git] / mod / bpgenerator / onap / src / main / java / org / onap / blueprintgenerator / service / common / ResourceConfigService.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
5  *  *  ================================================================================
6  *  *  Copyright (c) 2020  AT&T Intellectual Property. All rights reserved.
7  *  *  Copyright (c) 2021 Nokia. All rights reserved.
8  *  *  ================================================================================
9  *  *  Licensed under the Apache License, Version 2.0 (the "License");
10  *  *  you may not use this file except in compliance with the License.
11  *  *  You may obtain a copy of the License at
12  *  *
13  *  *       http://www.apache.org/licenses/LICENSE-2.0
14  *  *
15  *  *  Unless required by applicable law or agreed to in writing, software
16  *  *  distributed under the License is distributed on an "AS IS" BASIS,
17  *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  *  *  See the License for the specific language governing permissions and
19  *  *  limitations under the License.
20  *  *  ============LICENSE_END=========================================================
21  *
22  *
23  */
24
25 package org.onap.blueprintgenerator.service.common;
26
27 import org.onap.blueprintgenerator.constants.Constants;
28 import org.onap.blueprintgenerator.model.common.GetInput;
29 import org.onap.blueprintgenerator.model.common.ResourceConfig;
30 import org.onap.blueprintgenerator.service.base.BlueprintHelperService;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.beans.factory.annotation.Value;
33 import org.springframework.stereotype.Service;
34
35 import java.util.HashMap;
36 import java.util.Map;
37 import java.util.TreeMap;
38
39 /**
40  * @author : Ravi Mantena
41  * @date 10/16/2020 Application: ONAP - Blueprint Generator Common ONAP Service to add
42  * ResourceConfig
43  */
44 @Service("onapResourceConfigService")
45 public class ResourceConfigService {
46
47     @Autowired
48     private BlueprintHelperService blueprintHelperService;
49
50     @Value("${resources.cpuLimit}")
51     private String defaultCpuLimit;
52
53     @Value("${resources.memoryLimit}")
54     private String defaultMemoryLimit;
55
56     /**
57      * Creates Resouce Config for properties
58      *
59      * @param inputs Inputs
60      * @param name Name
61      * @return
62      */
63     public Map<String, Object> createResourceConfig(
64         Map<String, Map<String, Object>> inputs, String name) {
65         Map<String, Object> response = new HashMap<>();
66         ResourceConfig resourceConfig = new ResourceConfig();
67
68         Map<String, Object> memoryLimit =
69             blueprintHelperService.createStringInput(defaultMemoryLimit);
70
71         Map<String, Object> cpuLimit =
72             blueprintHelperService.createStringInput(defaultCpuLimit);
73
74         name = blueprintHelperService.getNamePrefix(name);
75
76         Map<String, GetInput> lim = new TreeMap<>();
77
78         GetInput cpu = new GetInput();
79         cpu.setBpInputName(name + Constants.CPU_LIMIT);
80         lim.put("cpu", cpu);
81
82         GetInput memL = new GetInput();
83         memL.setBpInputName(name + Constants.MEMORY_LIMIT);
84         lim.put("memory", memL);
85
86         inputs.put(name + Constants.CPU_LIMIT, cpuLimit);
87         inputs.put(name + Constants.MEMORY_LIMIT, memoryLimit);
88
89         resourceConfig.setLimits(lim);
90
91         Map<String, GetInput> req = new TreeMap<>();
92
93         GetInput cpuR = new GetInput();
94         cpuR.setBpInputName(name + Constants.CPU_REQUEST);
95         req.put("cpu", cpuR);
96
97         GetInput memR = new GetInput();
98         memR.setBpInputName(name + Constants.MEMORY_REQUEST);
99         req.put("memory", memR);
100
101         inputs.put(name + Constants.CPU_REQUEST, cpuLimit);
102         inputs.put(name + Constants.MEMORY_REQUEST, memoryLimit);
103
104         resourceConfig.setRequests(req);
105
106         response.put("resourceConfig", resourceConfig);
107         response.put("inputs", inputs);
108         return response;
109     }
110 }