Merge "Fix the tosca converter"
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / update / elements / Constraint.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP CLAMP\r
4  * ================================================================================\r
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights\r
6  *                             reserved.\r
7  * ================================================================================\r
8  * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * you may not use this file except in compliance with the License.\r
10  * You may obtain a copy of the License at\r
11  *\r
12  * http://www.apache.org/licenses/LICENSE-2.0\r
13  *\r
14  * Unless required by applicable law or agreed to in writing, software\r
15  * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * See the License for the specific language governing permissions and\r
18  * limitations under the License.\r
19  * ============LICENSE_END============================================\r
20  * ===================================================================\r
21  *\r
22  */\r
23 \r
24 package org.onap.clamp.clds.tosca.update.elements;\r
25 \r
26 import com.google.gson.JsonArray;\r
27 import com.google.gson.JsonObject;\r
28 import java.util.ArrayList;\r
29 import java.util.LinkedHashMap;\r
30 import java.util.Map.Entry;\r
31 import org.onap.clamp.clds.tosca.update.templates.JsonTemplate;\r
32 \r
33 public class Constraint {\r
34 \r
35     private LinkedHashMap<String, Object> constraints;\r
36     private JsonTemplate jsonTemplate;\r
37 \r
38     public Constraint(LinkedHashMap<String, Object> constraints, JsonTemplate jsonTemplate) {\r
39         this.jsonTemplate = jsonTemplate;\r
40         this.constraints = constraints;\r
41     }\r
42 \r
43     /**\r
44      * Deploy the linkedhashmap which contains the constraints, to extract them one to one.\r
45      *\r
46      * @param jsonSchema   The json Schema\r
47      * @param typeProperty The ype property\r
48      * @return the json object\r
49      */\r
50     public JsonObject deployConstraints(JsonObject jsonSchema, String typeProperty) {\r
51         for (Entry<String, Object> constraint : constraints.entrySet()) {\r
52             this.parseConstraint(jsonSchema, constraint.getKey(), constraint.getValue(), typeProperty);\r
53         }\r
54         return jsonSchema;\r
55     }\r
56 \r
57     /**\r
58      * Each case of Tosca constraints below parse specifically the field in the JsonObject.\r
59      *\r
60      * @param jsonSchema      Json Schema\r
61      * @param nameConstraint  Name constraint\r
62      * @param valueConstraint value constraint\r
63      * @param typeProperty    Type Property\r
64      */\r
65     @SuppressWarnings("unchecked")\r
66     public void parseConstraint(JsonObject jsonSchema, String nameConstraint, Object valueConstraint,\r
67                                 String typeProperty) {\r
68         switch (nameConstraint) {\r
69             case "equal":\r
70                 checkTemplateField("const", jsonSchema, valueConstraint);\r
71                 break;\r
72             case "greater_than":\r
73                 checkTemplateField("exclusiveMinimum", jsonSchema, valueConstraint);\r
74                 break;\r
75             case "greater_or_equal":\r
76                 checkTemplateField("minimum", jsonSchema, valueConstraint);\r
77                 break;\r
78             case "less_than":\r
79                 checkTemplateField("exclusiveMaximum", jsonSchema, valueConstraint);\r
80                 break;\r
81             case "less_or_equal":\r
82                 checkTemplateField("maximum", jsonSchema, valueConstraint);\r
83                 break;\r
84             case "in_range":\r
85                 ArrayList<Integer> limitValues = (ArrayList<Integer>) valueConstraint;\r
86                 checkTemplateField("minimum", jsonSchema, limitValues.get(0));\r
87                 checkTemplateField("maximum", jsonSchema, limitValues.get(1));\r
88                 break;\r
89             case "pattern":\r
90                 jsonSchema.addProperty(nameConstraint, (String) valueConstraint);\r
91                 break;\r
92             case "length":\r
93                 this.getSpecificLength(jsonSchema, valueConstraint, typeProperty);\r
94                 break;\r
95             case "min_length":\r
96                 String[] prefixValues = nameConstraint.split("_");\r
97                 this.getLimitValue(jsonSchema, valueConstraint, typeProperty, prefixValues[0]);\r
98                 break;\r
99             case "max_length":\r
100                 String[] maxtab = nameConstraint.split("_");\r
101                 this.getLimitValue(jsonSchema, valueConstraint, typeProperty, maxtab[0]);\r
102                 break;\r
103             default://valid_value\r
104                 this.getValueArray(jsonSchema, valueConstraint, typeProperty);\r
105                 break;\r
106         }\r
107 \r
108     }\r
109 \r
110     /**\r
111      * To be done.\r
112      *\r
113      * @param jsonSchema   json schema\r
114      * @param fieldValue   field value\r
115      * @param typeProperty For the complex components, get a specific number of items/properties\r
116      */\r
117     public void getSpecificLength(JsonObject jsonSchema, Object fieldValue, String typeProperty) {\r
118         switch (typeProperty.toLowerCase()) {\r
119             case "string":\r
120                 checkTemplateField("minLength", jsonSchema, fieldValue);\r
121                 checkTemplateField("maxLength", jsonSchema, fieldValue);\r
122                 break;\r
123             case "array":\r
124                 if (fieldValue.equals(1) && jsonTemplate.hasFields("uniqueItems")) {\r
125                     jsonSchema.addProperty("uniqueItems", true);\r
126                 } else {\r
127                     checkTemplateField("minItems", jsonSchema, fieldValue);\r
128                     checkTemplateField("maxItems", jsonSchema, fieldValue);\r
129                 }\r
130                 break;\r
131             default:// Map && List\r
132                 checkTemplateField("minProperties", jsonSchema, fieldValue);\r
133                 checkTemplateField("maxProperties", jsonSchema, fieldValue);\r
134                 break;\r
135         }\r
136 \r
137     }\r
138 \r
139     /**\r
140      * To be done.\r
141      *\r
142      * @param jsonSchema   json schema\r
143      * @param fieldValue   field value\r
144      * @param typeProperty type property\r
145      * @param side         Get the limits fieldValue for the properties : depend of the type of the component\r
146      */\r
147     public void getLimitValue(JsonObject jsonSchema, Object fieldValue, String typeProperty, String side) {\r
148         switch (typeProperty) {\r
149             case "string":\r
150                 if (side.equals("min")) {\r
151                     checkTemplateField("minLength", jsonSchema, fieldValue);\r
152                 } else {\r
153                     checkTemplateField("maxLength", jsonSchema, fieldValue);\r
154                 }\r
155                 break;\r
156             default:// Array\r
157                 if (side.equals("min")) {\r
158                     checkTemplateField("minItems", jsonSchema, fieldValue);\r
159                 } else {\r
160                     checkTemplateField("maxItems", jsonSchema, fieldValue);\r
161                 }\r
162                 break;\r
163         }\r
164 \r
165     }\r
166 \r
167     /**\r
168      * To be done.\r
169      *\r
170      * @param jsonSchema   Json schema\r
171      * @param fieldValue   field value\r
172      * @param typeProperty Get as Enum the valid values for the property\r
173      */\r
174     public void getValueArray(JsonObject jsonSchema, Object fieldValue, String typeProperty) {\r
175         if (jsonTemplate.hasFields("enum")) {\r
176             JsonArray enumeration = new JsonArray();\r
177             if (typeProperty.equals("string") || typeProperty.equals("String")) {\r
178                 ArrayList<String> arrayValues = (ArrayList<String>) fieldValue;\r
179                 for (String arrayItem : arrayValues) {\r
180                     enumeration.add(arrayItem);\r
181                 }\r
182                 jsonSchema.add("enum", enumeration);\r
183             } else {\r
184                 ArrayList<Number> arrayValues = (ArrayList<Number>) fieldValue;\r
185                 for (Number arrayItem : arrayValues) {\r
186                     enumeration.add(arrayItem);\r
187                 }\r
188                 jsonSchema.add("enum", enumeration);\r
189             }\r
190         }\r
191     }\r
192 \r
193     /**\r
194      * To be done.\r
195      *\r
196      * @param field      Field\r
197      * @param jsonSchema Json schema\r
198      * @param fieldValue Simple way to avoid code duplication\r
199      */\r
200     public void checkTemplateField(String field, JsonObject jsonSchema, Object fieldValue) {\r
201         if (jsonTemplate.hasFields(field)) {\r
202             String typeField = fieldValue.getClass().getSimpleName();\r
203             switch (typeField) {\r
204                 case "String":\r
205                     jsonSchema.addProperty(field, (String) fieldValue);\r
206                     break;\r
207                 case "Integer":\r
208                     jsonSchema.addProperty(field, (Integer) fieldValue);\r
209                     break;\r
210                 case "Number":\r
211                     jsonSchema.addProperty(field, (Number) fieldValue);\r
212                     break;\r
213                 case "Boolean":\r
214                     jsonSchema.addProperty(field, (Boolean) fieldValue);\r
215                     break;\r
216                 default:\r
217                     break;\r
218             }\r
219         }\r
220     }\r
221 \r
222 }