Adjust BP-gen to correctly support DFC component spec - types
[dcaegen2/platform.git] / mod / bpgenerator / src / main / java / org / onap / blueprintgenerator / models / policymodel / PolicyModelNode.java
1 /*============LICENSE_START=======================================================
2  org.onap.dcae 
3  ================================================================================ 
4  Copyright (c) 2019 AT&T Intellectual Property. All rights reserved. 
5  ================================================================================
6  Modifications Copyright (c) 2020 Nokia. 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 package org.onap.blueprintgenerator.models.policymodel;
24
25 import static org.onap.blueprintgenerator.models.blueprint.BpConstants.TOSCA_DATATYPES_ROOT;
26 import static org.onap.blueprintgenerator.models.blueprint.BpConstants.TOSCA_NODES_ROOT;
27
28 import com.fasterxml.jackson.annotation.JsonInclude;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.TreeMap;
32 import lombok.Getter;
33 import lombok.Setter;
34 import org.onap.blueprintgenerator.models.componentspec.EntrySchemaObj;
35 import org.onap.blueprintgenerator.models.componentspec.Parameters;
36 import org.onap.blueprintgenerator.models.componentspec.PolicySchemaObj;
37
38 @Getter
39 @Setter
40 @JsonInclude(JsonInclude.Include.NON_NULL)
41 public class PolicyModelNode {
42
43     private String derived_from;
44     private TreeMap<String, PolicyProperties> properties;
45
46     public String createNodeType(String policyName, Parameters[] params) {
47         String hasEntrySchema = "";
48
49         TreeMap<String, PolicyProperties> props = new TreeMap();
50         for (Parameters p : params) {
51             if (p.getPolicy_group() != null) {
52                 if (p.getPolicy_group().equals(policyName)) {
53                     String name = p.getName();
54                     String type = p.getType();
55                     PolicyProperties polProps = new PolicyProperties();
56                     if (p.getPolicy_schema() != null) {
57                         polProps.setType("map");
58                         HashMap<String, String> entrySchema = new HashMap();
59                         entrySchema.put("type", "onap.datatypes." + name);
60                         //ArrayList<String> entrySchema = new ArrayList<String>();
61                         //entrySchema.add("type: onap.data." + name);
62                         polProps.setEntry_schema(entrySchema);
63                         hasEntrySchema = name;
64                         props.put(name, polProps);
65                     } else {
66                         polProps.setType(type);
67                         props.put(name, polProps);
68                     }
69                 }
70             }
71         }
72
73         this.setDerived_from(TOSCA_DATATYPES_ROOT);
74         this.setProperties(props);
75         return hasEntrySchema;
76     }
77
78     public TreeMap<String, PolicyModelNode> createDataTypes(String param, Parameters[] parameters) {
79         TreeMap<String, PolicyModelNode> dataType = new TreeMap<String, PolicyModelNode>();
80
81         PolicyModelNode node = new PolicyModelNode();
82         node.setDerived_from(TOSCA_DATATYPES_ROOT);
83
84         TreeMap<String, PolicyProperties> properties = new TreeMap();
85
86         Parameters par = new Parameters();
87         for (Parameters p : parameters) {
88             if (p.getName().equals(param)) {
89                 par = p;
90                 break;
91             }
92         }
93
94         for (PolicySchemaObj pol : par.getPolicy_schema()) {
95             if (pol.getEntry_schema() != null) {
96                 PolicyProperties prop = new PolicyProperties();
97                 prop.setType("map");
98                 HashMap<String, String> schema = new HashMap();
99                 schema.put("type", "onap.datatypes." + pol.getName());
100 //                              prop.setType("list");
101 //                              ArrayList<String> schema = new ArrayList();
102 //                              schema.add("type: onap.data." + pol.getName());
103                 prop.setEntry_schema(schema);
104                 properties.put(pol.getName(), prop);
105                 dataType = translateEntrySchema(dataType, pol.getEntry_schema(), pol.getName());
106             } else {
107                 PolicyProperties prop = new PolicyProperties();
108                 prop.setType(pol.getType());
109                 properties.put(pol.getName(), prop);
110             }
111         }
112
113         node.setProperties(properties);
114         dataType.put("onap.datatypes." + param, node);
115         return dataType;
116     }
117
118     private TreeMap<String, PolicyModelNode> translateEntrySchema(TreeMap<String, PolicyModelNode> dataType,
119         EntrySchemaObj[] entry, String name) {
120         TreeMap<String, PolicyModelNode> data = dataType;
121         PolicyModelNode node = new PolicyModelNode();
122         node.setDerived_from(TOSCA_NODES_ROOT);
123         TreeMap<String, PolicyProperties> properties = new TreeMap<String, PolicyProperties>();
124
125         for (EntrySchemaObj e : entry) {
126             if (e.getEntry_schema() != null) {
127                 PolicyProperties prop = new PolicyProperties();
128                 prop.setType("list");
129                 ArrayList<String> schema = new ArrayList<String>();
130                 schema.add("type: onap.datatypes." + e.getName());
131                 prop.setEntry_schema(schema);
132                 properties.put(e.getName(), prop);
133                 data = translateEntrySchema(data, e.getEntry_schema(), e.getName());
134                 node.setProperties(properties);
135             } else {
136                 PolicyProperties prop = new PolicyProperties();
137                 prop.setType(e.getType());
138                 properties.put(e.getName(), prop);
139                 node.setProperties(properties);
140             }
141         }
142
143         dataType.put("onap.datatypes." + name, node);
144         return data;
145     }
146
147 }