BpGen refactor Code Quality Issue-ID: DCAEGEN2-2502
[dcaegen2/platform.git] / mod / bpgenerator / onap / src / main / java / org / onap / blueprintgenerator / service / OnapBlueprintService.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
5  *  *  ================================================================================
6  *  *  Copyright (c) 2020  AT&T Intellectual Property. All rights reserved.
7  *  *  ================================================================================
8  *  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  *  you may not use this file except in compliance with the License.
10  *  *  You may obtain a copy of the License at
11  *  *
12  *  *       http://www.apache.org/licenses/LICENSE-2.0
13  *  *
14  *  *  Unless required by applicable law or agreed to in writing, software
15  *  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  *  See the License for the specific language governing permissions and
18  *  *  limitations under the License.
19  *  *  ============LICENSE_END=========================================================
20  *
21  *
22  */
23
24 package org.onap.blueprintgenerator.service;
25
26 import org.onap.blueprintgenerator.constants.Constants;
27 import org.onap.blueprintgenerator.exception.BlueprintException;
28 import org.onap.blueprintgenerator.model.common.Input;
29 import org.onap.blueprintgenerator.model.common.Node;
30 import org.onap.blueprintgenerator.model.common.OnapBlueprint;
31 import org.onap.blueprintgenerator.model.componentspec.OnapComponentSpec;
32 import org.onap.blueprintgenerator.service.base.BlueprintService;
33 import org.onap.blueprintgenerator.service.common.ImportsService;
34 import org.onap.blueprintgenerator.service.common.NodeService;
35 import org.onap.blueprintgenerator.service.common.PgaasNodeService;
36 import org.onap.blueprintgenerator.service.common.PolicyNodeService;
37 import org.onap.blueprintgenerator.service.common.QuotationService;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Service;
40 import org.springframework.util.StringUtils;
41
42 import java.util.LinkedHashMap;
43 import java.util.Map;
44 import java.util.TreeMap;
45
46 /**
47  * @author : Ravi Mantena
48  * @date 10/16/2020 Application: ONAP - Blueprint Generator Service to create ONAP Blueprint
49  */
50 @Service
51 public class OnapBlueprintService extends BlueprintService {
52
53     @Autowired
54     private NodeService nodeService;
55
56     @Autowired
57     private PolicyNodeService policyNodeService;
58
59     @Autowired
60     private PgaasNodeService pgaasNodeService;
61
62     @Autowired
63     private QuotationService quotationService;
64
65     @Autowired
66     protected ImportsService importsService;
67
68     /**
69      * Creates Onap Blueprint
70      *
71      * @param onapComponentSpec OnapComponentSpec
72      * @param input Inputs
73      * @return
74      */
75     public OnapBlueprint createBlueprint(OnapComponentSpec onapComponentSpec, Input input) {
76         try {
77             OnapBlueprint blueprint = new OnapBlueprint();
78             blueprint.setTosca_definitions_version(Constants.TOSCA_DEF_VERSION);
79
80             // if (!"".equals(input.getImportPath()))
81             if (!StringUtils.isEmpty(input.getImportPath())) {
82                 blueprint.setImports(importsService.createImportsFromFile(input.getImportPath()));
83             } else {
84                 blueprint.setImports(importsService.createImports(input.getBpType()));
85             }
86
87             Map<String, Node> nodeTemplate = new TreeMap<>();
88             String nodeName = onapComponentSpec.getSelf().getName();
89             Map<String, LinkedHashMap<String, Object>> inputs = new TreeMap<>();
90
91             Map<String, Object> onapNodeResponse =
92                 nodeService
93                     .createOnapNode(inputs, onapComponentSpec, input.getServiceNameOverride());
94             inputs = (Map<String, LinkedHashMap<String, Object>>) onapNodeResponse.get("inputs");
95             nodeTemplate.put(nodeName, (Node) onapNodeResponse.get("onapNode"));
96             blueprint.setNode_templates(nodeTemplate);
97
98             if (onapComponentSpec.getPolicyInfo() != null) {
99                 policyNodeService.addPolicyNodesAndInputs(onapComponentSpec, nodeTemplate, inputs);
100             }
101
102             if (onapComponentSpec.getAuxilary() != null
103                 && onapComponentSpec.getAuxilary().getDatabases() != null) {
104                 pgaasNodeService.addPgaasNodesAndInputs(onapComponentSpec, nodeTemplate, inputs);
105             }
106
107             blueprint.setInputs(inputs);
108
109             return quotationService.setQuotations(blueprint);
110         } catch (Exception ex) {
111             throw new BlueprintException(
112                 "Unable to create ONAP Blueprint Object from given input parameters", ex);
113         }
114     }
115 }