BPGen externalize resources limit defaults config
[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.LinkedHashMap;
37 import java.util.Map;
38 import java.util.TreeMap;
39
40 /**
41  * @author : Ravi Mantena
42  * @date 10/16/2020 Application: ONAP - Blueprint Generator Common ONAP Service to add
43  * ResourceConfig
44  */
45 @Service("onapResourceConfigService")
46 public class ResourceConfigService {
47
48     @Autowired
49     private BlueprintHelperService blueprintHelperService;
50
51     @Value("${resources.cpuLimit}")
52     private String defaultCpuLimit;
53
54     @Value("${resources.memoryLimit}")
55     private String defaultMemoryLimit;
56
57     /**
58      * Creates Resouce Config for properties
59      *
60      * @param inputs Inputs
61      * @param name Name
62      * @return
63      */
64     public Map<String, Object> createResourceConfig(
65         Map<String, LinkedHashMap<String, Object>> inputs, String name) {
66         Map<String, Object> response = new HashMap<>();
67         ResourceConfig resourceConfig = new ResourceConfig();
68
69         LinkedHashMap<String, Object> memoryLimit =
70             blueprintHelperService.createStringInput(defaultMemoryLimit);
71
72         LinkedHashMap<String, Object> cpuLimit =
73             blueprintHelperService.createStringInput(defaultCpuLimit);
74
75         name = blueprintHelperService.getNamePrefix(name);
76
77         Map<String, GetInput> lim = new TreeMap<>();
78
79         GetInput cpu = new GetInput();
80         cpu.setBpInputName(name + Constants.CPU_LIMIT);
81         lim.put("cpu", cpu);
82
83         GetInput memL = new GetInput();
84         memL.setBpInputName(name + Constants.MEMORY_LIMIT);
85         lim.put("memory", memL);
86
87         inputs.put(name + Constants.CPU_LIMIT, cpuLimit);
88         inputs.put(name + Constants.MEMORY_LIMIT, memoryLimit);
89
90         resourceConfig.setLimits(lim);
91
92         Map<String, GetInput> req = new TreeMap<>();
93
94         GetInput cpuR = new GetInput();
95         cpuR.setBpInputName(name + Constants.CPU_REQUEST);
96         req.put("cpu", cpuR);
97
98         GetInput memR = new GetInput();
99         memR.setBpInputName(name + Constants.MEMORY_REQUEST);
100         req.put("memory", memR);
101
102         inputs.put(name + Constants.CPU_REQUEST, cpuLimit);
103         inputs.put(name + Constants.MEMORY_REQUEST, memoryLimit);
104
105         resourceConfig.setRequests(req);
106
107         response.put("resourceConfig", resourceConfig);
108         response.put("inputs", inputs);
109         return response;
110     }
111 }