Fix sonar issues
[dcaegen2/platform.git] / mod / bpgenerator / onap / src / main / java / org / onap / blueprintgenerator / service / common / PolicyNodeService.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.Node;
30 import org.onap.blueprintgenerator.model.common.PolicyNode;
31 import org.onap.blueprintgenerator.model.common.PolicyNodeProperties;
32 import org.onap.blueprintgenerator.model.componentspec.OnapComponentSpec;
33 import org.onap.blueprintgenerator.model.componentspec.TypePolicy;
34 import org.onap.blueprintgenerator.service.base.BlueprintHelperService;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Service;
37
38 import java.util.ArrayList;
39 import java.util.LinkedHashMap;
40 import java.util.List;
41 import java.util.Map;
42
43 /**
44  * @author : Ravi Mantena
45  * @date 10/16/2020 Application: ONAP - Blueprint Generator Common ONAP Service to add Policy Node
46  */
47 @Service("onapPolicyNodeService")
48 public class PolicyNodeService {
49
50     @Autowired
51     private BlueprintHelperService blueprintHelperService;
52
53     /**
54      * Creates Policy Nodes and Inputs
55      *
56      * @param onapComponentSpec OnapComponentSpec
57      * @param nodeTemplate Node Template
58      * @param inputs Inputs
59      * @return
60      */
61     public void addPolicyNodesAndInputs(
62         OnapComponentSpec onapComponentSpec,
63         Map<String, Node> nodeTemplate,
64         Map<String, Map<String, Object>> inputs) {
65         List<TypePolicy> policyList = onapComponentSpec.getPolicyInfo().getTypePolicyList();
66         for (TypePolicy policy : policyList) {
67             addPolicyNodesToNodeTemplate(policy, nodeTemplate);
68             addPolicyInputs(policy, inputs);
69         }
70     }
71
72     private void addPolicyInputs(
73         TypePolicy policy, Map<String, Map<String, Object>> inputs) {
74         String defaultValue = policy.getPolicy_id();
75         defaultValue = defaultValue != null ? defaultValue : "";
76         inputs.put(
77             policy.getNode_label() + "_policy_id",
78             blueprintHelperService.createStringInput("policy_id", defaultValue));
79     }
80
81     private void addPolicyNodesToNodeTemplate(TypePolicy policy, Map<String, Node> nodeTemplate) {
82         PolicyNode policyNode = new PolicyNode();
83         policyNode.setType(Constants.POLICY_NODE_TYPE);
84         policyNode.setPolicyNodeProperties(getPolicyNodeProperties(policy));
85         nodeTemplate.put(policy.getNode_label(), policyNode);
86     }
87
88     private PolicyNodeProperties getPolicyNodeProperties(TypePolicy policy) {
89         PolicyNodeProperties policyNodeProperties = new PolicyNodeProperties();
90         GetInput policyIdGetInput = new GetInput();
91         policyIdGetInput.setBpInputName(policy.getNode_label() + "_policy_id");
92         policyNodeProperties.setPolicyId(policyIdGetInput);
93         policyNodeProperties.setPolicyModelId(policy.getPolicy_model_id());
94         return policyNodeProperties;
95     }
96
97     /**
98      * Creates Policy Relationships
99      *
100      * @param onapComponentSpec OnapComponentSpec
101      * @return
102      */
103     public List<Map<String, String>> getPolicyRelationships(OnapComponentSpec onapComponentSpec) {
104         List<Map<String, String>> relationships = new ArrayList<>();
105         List<TypePolicy> policyList = onapComponentSpec.getPolicyInfo().getTypePolicyList();
106         for (TypePolicy policy : policyList) {
107             Map<String, String> relationship = new LinkedHashMap<>();
108             relationship.put("type", Constants.POLICY_RELATIONSHIP_TYPE);
109             relationship.put("target", policy.getNode_label());
110             relationships.add(relationship);
111         }
112         return relationships;
113     }
114 }