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