4f6b27a6ae076da2f0405fff17d1c85f63bdc8f7
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / update / 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;\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 \r
32 public class Constraint {\r
33 \r
34     private LinkedHashMap<String, Object> constraints;\r
35     private Template template;\r
36 \r
37     public Constraint(LinkedHashMap<String, Object> constraints, Template template) {\r
38         this.template = template;\r
39         this.constraints = constraints;\r
40     }\r
41 \r
42     /**\r
43      * Deploy the linkedhashmap which contains the constraints, to extract them one to one.\r
44      *\r
45      * @param jsonSchema   The json Schema\r
46      * @param typeProperty The ype property\r
47      * @return the json object\r
48      */\r
49     public JsonObject deployConstraints(JsonObject jsonSchema, String typeProperty) {\r
50         for (Entry<String, Object> constraint : constraints.entrySet()) {\r
51             this.parseConstraint(jsonSchema, constraint.getKey(), constraint.getValue(), typeProperty);\r
52         }\r
53         return jsonSchema;\r
54     }\r
55 \r
56     /**\r
57      * Each case of Tosca constraints below parse specifically the field in the JsonObject.\r
58      *\r
59      * @param jsonSchema      Json Schema\r
60      * @param nameConstraint  Name constraint\r
61      * @param valueConstraint value constraint\r
62      * @param typeProperty    Type Property\r
63      */\r
64     @SuppressWarnings("unchecked")\r
65     public void parseConstraint(JsonObject jsonSchema, String nameConstraint, Object valueConstraint,\r
66                                 String typeProperty) {\r
67         switch (nameConstraint) {\r
68             case "equal":\r
69                 checkTemplateField("const", jsonSchema, valueConstraint);\r
70                 break;\r
71             case "greater_than":\r
72                 checkTemplateField("exclusiveMinimum", jsonSchema, valueConstraint);\r
73                 break;\r
74             case "greater_or_equal":\r
75                 checkTemplateField("minimum", jsonSchema, valueConstraint);\r
76                 break;\r
77             case "less_than":\r
78                 checkTemplateField("exclusiveMaximum", jsonSchema, valueConstraint);\r
79                 break;\r
80             case "less_or_equal":\r
81                 checkTemplateField("maximum", jsonSchema, valueConstraint);\r
82                 break;\r
83             case "in_range":\r
84                 ArrayList<Integer> limitValues = (ArrayList<Integer>) valueConstraint;\r
85                 checkTemplateField("minimum", jsonSchema, limitValues.get(0));\r
86                 checkTemplateField("maximum", jsonSchema, limitValues.get(1));\r
87                 break;\r
88             case "pattern":\r
89                 jsonSchema.addProperty(nameConstraint, (String) valueConstraint);\r
90                 break;\r
91             case "length":\r
92                 this.getSpecificLength(jsonSchema, valueConstraint, typeProperty);\r
93                 break;\r
94             case "min_length":\r
95                 String[] prefixValues = nameConstraint.split("_");\r
96                 this.getLimitValue(jsonSchema, valueConstraint, typeProperty, prefixValues[0]);\r
97                 break;\r
98             case "max_length":\r
99                 String[] maxtab = nameConstraint.split("_");\r
100                 this.getLimitValue(jsonSchema, valueConstraint, typeProperty, maxtab[0]);\r
101                 break;\r
102             default://valid_value\r
103                 this.getValueArray(jsonSchema, valueConstraint, typeProperty);\r
104                 break;\r
105         }\r
106 \r
107     }\r
108 \r
109     /**\r
110      * To be done.\r
111      *\r
112      * @param jsonSchema   json schema\r
113      * @param fieldValue   field value\r
114      * @param typeProperty For the complex components, get a specific number of items/properties\r
115      */\r
116     public void getSpecificLength(JsonObject jsonSchema, Object fieldValue, String typeProperty) {\r
117         switch (typeProperty.toLowerCase()) {\r
118             case "string":\r
119                 checkTemplateField("minLength", jsonSchema, fieldValue);\r
120                 checkTemplateField("maxLength", jsonSchema, fieldValue);\r
121                 break;\r
122             case "array":\r
123                 if (fieldValue.equals(1) && template.hasFields("uniqueItems")) {\r
124                     jsonSchema.addProperty("uniqueItems", true);\r
125                 } else {\r
126                     checkTemplateField("minItems", jsonSchema, fieldValue);\r
127                     checkTemplateField("maxItems", jsonSchema, fieldValue);\r
128                 }\r
129                 break;\r
130             default:// Map && List\r
131                 checkTemplateField("minProperties", jsonSchema, fieldValue);\r
132                 checkTemplateField("maxProperties", jsonSchema, fieldValue);\r
133                 break;\r
134         }\r
135 \r
136     }\r
137 \r
138     /**\r
139      * To be done.\r
140      *\r
141      * @param jsonSchema   json schema\r
142      * @param fieldValue   field value\r
143      * @param typeProperty type property\r
144      * @param side         Get the limits fieldValue for the properties : depend of the type of the component\r
145      */\r
146     public void getLimitValue(JsonObject jsonSchema, Object fieldValue, String typeProperty, String side) {\r
147         switch (typeProperty) {\r
148             case "string":\r
149                 if (side.equals("min")) {\r
150                     checkTemplateField("minLength", jsonSchema, fieldValue);\r
151                 } else {\r
152                     checkTemplateField("maxLength", jsonSchema, fieldValue);\r
153                 }\r
154                 break;\r
155             default:// Array\r
156                 if (side.equals("min")) {\r
157                     checkTemplateField("minItems", jsonSchema, fieldValue);\r
158                 } else {\r
159                     checkTemplateField("maxItems", jsonSchema, fieldValue);\r
160                 }\r
161                 break;\r
162         }\r
163 \r
164     }\r
165 \r
166     /**\r
167      * To be done.\r
168      *\r
169      * @param jsonSchema   Json schema\r
170      * @param fieldValue   field value\r
171      * @param typeProperty Get as Enum the valid values for the property\r
172      */\r
173     public void getValueArray(JsonObject jsonSchema, Object fieldValue, String typeProperty) {\r
174         if (template.hasFields("enum")) {\r
175             JsonArray enumeration = new JsonArray();\r
176             if (typeProperty.equals("string") || typeProperty.equals("String")) {\r
177                 ArrayList<String> arrayValues = (ArrayList<String>) fieldValue;\r
178                 for (String arrayItem : arrayValues) {\r
179                     enumeration.add(arrayItem);\r
180                 }\r
181                 jsonSchema.add("enum", enumeration);\r
182             } else {\r
183                 ArrayList<Number> arrayValues = (ArrayList<Number>) fieldValue;\r
184                 for (Number arrayItem : arrayValues) {\r
185                     enumeration.add(arrayItem);\r
186                 }\r
187                 jsonSchema.add("enum", enumeration);\r
188             }\r
189         }\r
190     }\r
191 \r
192     /**\r
193      * To be done.\r
194      *\r
195      * @param field      Field\r
196      * @param jsonSchema Json schema\r
197      * @param fieldValue Simple way to avoid code duplication\r
198      */\r
199     public void checkTemplateField(String field, JsonObject jsonSchema, Object fieldValue) {\r
200         if (template.hasFields(field)) {\r
201             String typeField = fieldValue.getClass().getSimpleName();\r
202             switch (typeField) {\r
203                 case "String":\r
204                     jsonSchema.addProperty(field, (String) fieldValue);\r
205                     break;\r
206                 case "Integer":\r
207                     jsonSchema.addProperty(field, (Integer) fieldValue);\r
208                     break;\r
209                 case "Number":\r
210                     jsonSchema.addProperty(field, (Number) fieldValue);\r
211                     break;\r
212                 case "Boolean":\r
213                     jsonSchema.addProperty(field, (Boolean) fieldValue);\r
214                     break;\r
215                 default:\r
216                     break;\r
217             }\r
218         }\r
219     }\r
220 \r
221 }