3a1c55a31f31f0f8947cd35e98a2bafcf91fca12
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ImportUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.be.components.impl;
21
22 import static org.apache.commons.collections.CollectionUtils.isEmpty;
23 import static org.openecomp.sdc.be.components.impl.ResourceImportManager.PROPERTY_NAME_PATTERN_IGNORE_LENGTH;
24 import static org.openecomp.sdc.be.datatypes.elements.Annotation.setAnnotationsName;
25
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonParseException;
29 import com.google.gson.reflect.TypeToken;
30 import fj.data.Either;
31 import java.lang.reflect.Type;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.LinkedHashMap;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Map.Entry;
41 import java.util.function.Consumer;
42 import java.util.function.Function;
43 import org.apache.commons.collections.CollectionUtils;
44 import org.apache.commons.lang3.StringEscapeUtils;
45 import org.onap.sdc.tosca.datatypes.model.EntrySchema;
46 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
47 import org.openecomp.sdc.be.config.BeEcompErrorManager;
48 import org.openecomp.sdc.be.dao.api.ActionStatus;
49 import org.openecomp.sdc.be.datatypes.elements.Annotation;
50 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
51 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
52 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
53 import org.openecomp.sdc.be.impl.ComponentsUtils;
54 import org.openecomp.sdc.be.model.AnnotationTypeDefinition;
55 import org.openecomp.sdc.be.model.AttributeDefinition;
56 import org.openecomp.sdc.be.model.HeatParameterDefinition;
57 import org.openecomp.sdc.be.model.InputDefinition;
58 import org.openecomp.sdc.be.model.LifecycleStateEnum;
59 import org.openecomp.sdc.be.model.PropertyConstraint;
60 import org.openecomp.sdc.be.model.PropertyDefinition;
61 import org.openecomp.sdc.be.model.heat.HeatParameterType;
62 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
63 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation.PropertyConstraintDeserialiser;
64 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
65 import org.openecomp.sdc.be.model.tosca.constraints.ConstraintType;
66 import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint;
67 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
68 import org.openecomp.sdc.be.utils.TypeUtils;
69 import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum;
70 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
71 import org.openecomp.sdc.common.log.wrappers.Logger;
72 import org.openecomp.sdc.exception.ResponseFormat;
73 import org.springframework.beans.factory.annotation.Autowired;
74 import org.springframework.beans.factory.config.YamlProcessor;
75 import org.springframework.stereotype.Component;
76 import org.yaml.snakeyaml.DumperOptions;
77 import org.yaml.snakeyaml.Yaml;
78 import org.yaml.snakeyaml.constructor.Constructor;
79 import org.yaml.snakeyaml.nodes.Tag;
80 import org.yaml.snakeyaml.representer.Representer;
81 import org.yaml.snakeyaml.resolver.Resolver;
82
83 @Component
84 public final class ImportUtils {
85
86     private static final CustomResolver customResolver = new CustomResolver();
87     private static final Yaml strictYamlLoader = new YamlLoader().getStrictYamlLoader();
88     private static final Logger log = Logger.getLogger(ImportUtils.class);
89     private static ComponentsUtils componentsUtils;
90
91     private ImportUtils() {
92     }
93
94     @Autowired
95     public static void setComponentsUtils(ComponentsUtils componentsUtils) {
96         componentsUtils = componentsUtils;
97     }
98
99     private static void buildMap(Map<String, Object> output, Map<String, Object> map) {
100         for (Entry<String, Object> entry : map.entrySet()) {
101             String key = entry.getKey();
102             Object value = entry.getValue();
103             if (value instanceof Map) {
104                 Map<String, Object> result = new LinkedHashMap<>();
105                 buildMap(result, (Map) value);
106                 output.put(key, result);
107             } else if (value instanceof Collection) {
108                 Map<String, Object> result = new LinkedHashMap<>();
109                 int i = 0;
110                 for (Object item : (Collection<Object>) value) {
111                     buildMap(result, Collections.singletonMap("[" + (i++) + "]", item));
112                 }
113                 output.put(key, new ArrayList<>(result.values()));
114             } else {
115                 output.put(key, value);
116             }
117         }
118     }
119
120     public static Map<String, Object> loadYamlAsStrictMap(String content) {
121         Map<String, Object> result = new LinkedHashMap<>();
122         Object map = strictYamlLoader.load(content);
123         buildMap(result, (Map<String, Object>) map);
124         return result;
125     }
126
127     @SuppressWarnings("unchecked")
128     public static Either<List<HeatParameterDefinition>, ResultStatusEnum> getHeatParamsWithoutImplicitTypes(String heatDecodedPayload,
129                                                                                                             String artifactType) {
130         Map<String, Object> heatData = (Map<String, Object>) new Yaml(new Constructor(), new Representer(), new DumperOptions(), customResolver)
131             .load(heatDecodedPayload);
132         return getHeatParameters(heatData, artifactType);
133     }
134
135     @SuppressWarnings("unchecked")
136     private static void handleElementNameNotFound(String elementName, Object elementValue, ToscaElementTypeEnum elementType,
137                                                   List<Object> returnedList) {
138         if (elementValue instanceof Map) {
139             findToscaElements((Map<String, Object>) elementValue, elementName, elementType, returnedList);
140         } else if (elementValue instanceof List) {
141             findAllToscaElementsInList((List<Object>) elementValue, elementName, elementType, returnedList);
142         }
143     }
144
145     @SuppressWarnings("unchecked")
146     private static void addFoundElementAccordingToItsType(String elementName, ToscaElementTypeEnum elementType, List<Object> returnedList,
147                                                           Object elementValue) {
148         if (elementValue instanceof Boolean) {
149             if (elementType == ToscaElementTypeEnum.BOOLEAN || elementType == ToscaElementTypeEnum.ALL) {
150                 returnedList.add(elementValue);
151             }
152         } else if (elementValue instanceof String) {
153             if (elementType == ToscaElementTypeEnum.STRING || elementType == ToscaElementTypeEnum.ALL) {
154                 returnedList.add(elementValue);
155             }
156         } else if (elementValue instanceof Map) {
157             if (elementType == ToscaElementTypeEnum.MAP || elementType == ToscaElementTypeEnum.ALL) {
158                 returnedList.add(elementValue);
159             }
160             findToscaElements((Map<String, Object>) elementValue, elementName, elementType, returnedList);
161         } else if (elementValue instanceof List) {
162             if (elementType == ToscaElementTypeEnum.LIST || elementType == ToscaElementTypeEnum.ALL) {
163                 returnedList.add(elementValue);
164             }
165             findAllToscaElementsInList((List<Object>) elementValue, elementName, elementType, returnedList);
166         }
167         // For Integer, Double etc...
168         else if (elementType == ToscaElementTypeEnum.ALL && elementValue != null) {
169             returnedList.add(String.valueOf(elementValue));
170         }
171     }
172
173     private static void findAllToscaElementsInList(List<Object> list, String elementName, ToscaElementTypeEnum elementType,
174                                                    List<Object> returnedList) {
175         list.forEach(elementValue -> handleElementNameNotFound(elementName, elementValue, elementType, returnedList));
176     }
177
178     public static Either<Object, ResultStatusEnum> findToscaElement(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum elementName,
179                                                                     ToscaElementTypeEnum elementType) {
180         List<Object> foundElements = new ArrayList<>();
181         findToscaElements(toscaJson, elementName.getElementName(), elementType, foundElements);
182         if (!isEmpty(foundElements)) {
183             return Either.left(foundElements.get(0));
184         }
185         return Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
186     }
187
188     /**
189      * Recursively searches for all tosca elements with key equals to elementName and value equals to elementType. <br> Returns Either element
190      * with:<br> List with all value if values found<br> Or ELEMENT_NOT_FOUND ActionStatus
191      *
192      * @param toscaJson
193      * @return
194      */
195     public static Either<List<Object>, ResultStatusEnum> findToscaElements(Map<String, Object> toscaJson, String elementName,
196                                                                            ToscaElementTypeEnum elementType, List<Object> returnedList) {
197         Either<List<Object>, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
198         String skipKey = null;
199         if (toscaJson.containsKey(elementName)) {
200             skipKey = handleFoundElement(toscaJson, elementName, elementType, returnedList);
201         }
202         Iterator<Entry<String, Object>> keyValItr = toscaJson.entrySet().iterator();
203         while (keyValItr.hasNext()) {
204             Entry<String, Object> keyValEntry = keyValItr.next();
205             if (!String.valueOf(keyValEntry.getKey()).equals(skipKey)) {
206                 handleElementNameNotFound(elementName, keyValEntry.getValue(), elementType, returnedList);
207             }
208         }
209         if (!isEmpty(returnedList)) {
210             returnedElement = Either.left(returnedList);
211         }
212         return returnedElement;
213     }
214
215     private static String handleFoundElement(Map<String, Object> toscaJson, String elementName, ToscaElementTypeEnum elementType,
216                                              List<Object> returnedList) {
217         Object elementValue = toscaJson.get(elementName);
218         addFoundElementAccordingToItsType(elementName, elementType, returnedList, elementValue);
219         return elementName;
220     }
221
222     @SuppressWarnings("unchecked")
223     public static <T> Either<List<T>, ResultStatusEnum> findFirstToscaListElement(Map<String, Object> toscaJson,
224                                                                                   TypeUtils.ToscaTagNamesEnum toscaTagName) {
225         Either<List<T>, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
226         Either<Object, ResultStatusEnum> findFirstToscaElement = findToscaElement(toscaJson, toscaTagName, ToscaElementTypeEnum.LIST);
227         if (findFirstToscaElement.isLeft()) {
228             returnedElement = Either.left((List<T>) findFirstToscaElement.left().value());
229         }
230         return returnedElement;
231     }
232
233     @SuppressWarnings("unchecked")
234     public static <T> Either<Map<String, T>, ResultStatusEnum> findFirstToscaMapElement(Map<String, Object> toscaJson,
235                                                                                         TypeUtils.ToscaTagNamesEnum toscaTagName) {
236         Either<Map<String, T>, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
237         Either<Object, ResultStatusEnum> findFirstToscaElement = findToscaElement(toscaJson, toscaTagName, ToscaElementTypeEnum.MAP);
238         if (findFirstToscaElement.isLeft()) {
239             returnedElement = Either.left((Map<String, T>) findFirstToscaElement.left().value());
240         }
241         return returnedElement;
242     }
243
244     public static Either<String, ResultStatusEnum> findFirstToscaStringElement(Map<String, Object> toscaJson,
245                                                                                TypeUtils.ToscaTagNamesEnum toscaTagName) {
246         Either<String, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
247         Either<Object, ResultStatusEnum> findFirstToscaElements = findToscaElement(toscaJson, toscaTagName, ToscaElementTypeEnum.STRING);
248         if (findFirstToscaElements.isLeft()) {
249             returnedElement = Either.left((String) findFirstToscaElements.left().value());
250         }
251         return returnedElement;
252     }
253
254     /**
255      * searches for first Tosca in Json map (toscaJson) boolean element by name (toscaTagName) returns found element or ELEMENT_NOT_FOUND status
256      *
257      * @param toscaJson
258      * @param toscaTagName
259      * @return
260      */
261     public static Either<String, ResultStatusEnum> findFirstToscaBooleanElement(Map<String, Object> toscaJson,
262                                                                                 TypeUtils.ToscaTagNamesEnum toscaTagName) {
263         Either<String, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
264         Either<Object, ResultStatusEnum> findFirstToscaElements = findToscaElement(toscaJson, toscaTagName, ToscaElementTypeEnum.BOOLEAN);
265         if (findFirstToscaElements.isLeft()) {
266             returnedElement = Either.left(String.valueOf(findFirstToscaElements.left().value()));
267         }
268         return returnedElement;
269     }
270
271     private static void setPropertyConstraints(Map<String, Object> propertyValue, PropertyDefinition property) {
272         List<PropertyConstraint> constraints = getPropertyConstraints(propertyValue, property.getType());
273         if (CollectionUtils.isNotEmpty(constraints)) {
274             property.setConstraints(constraints);
275         }
276     }
277
278     private static List<PropertyConstraint> getPropertyConstraints(final Map<String, Object> propertyValue, final String propertyType) {
279         final List<Object> propertyFieldConstraints = findCurrentLevelConstraintsElement(propertyValue);
280         if (CollectionUtils.isEmpty(propertyFieldConstraints)) {
281             return Collections.emptyList();
282         }
283         final List<PropertyConstraint> constraintList = new ArrayList<>();
284         final Type constraintType = new TypeToken<PropertyConstraint>() {
285         }.getType();
286         final Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintDeserialiser()).create();
287         for (final Object constraintJson : propertyFieldConstraints) {
288             final PropertyConstraint propertyConstraint = validateAndGetPropertyConstraint(propertyType, constraintType, gson, constraintJson);
289             constraintList.add(propertyConstraint);
290         }
291         return constraintList;
292     }
293
294     private static List<Object> findCurrentLevelConstraintsElement(Map<String, Object> toscaJson) {
295         List<Object> constraints = null;
296         if (toscaJson.containsKey(TypeUtils.ToscaTagNamesEnum.CONSTRAINTS.getElementName())) {
297             try {
298                 constraints = (List<Object>) toscaJson.get(TypeUtils.ToscaTagNamesEnum.CONSTRAINTS.getElementName());
299             } catch (ClassCastException e) {
300                 throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS_FORMAT,
301                     toscaJson.get(TypeUtils.ToscaTagNamesEnum.CONSTRAINTS.getElementName()).toString());
302             }
303         }
304         return constraints;
305     }
306
307     private static PropertyConstraint validateAndGetPropertyConstraint(String propertyType, Type constraintType, Gson gson, Object constraintJson) {
308         PropertyConstraint propertyConstraint;
309         try {
310             propertyConstraint = gson.fromJson(gson.toJson(constraintJson), constraintType);
311         } catch (ClassCastException | JsonParseException e) {
312             throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS_FORMAT, constraintJson.toString());
313         }
314         if (propertyConstraint != null && propertyConstraint instanceof ValidValuesConstraint) {
315             try {
316                 ((ValidValuesConstraint) propertyConstraint).validateType(propertyType);
317             } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
318                 BeEcompErrorManager.getInstance()
319                     .logInternalFlowError("GetInitializedPropertyConstraint", e.getMessage(), BeEcompErrorManager.ErrorSeverity.ERROR);
320                 throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS, ConstraintType.VALID_VALUES.name(),
321                     ((ValidValuesConstraint) propertyConstraint).getValidValues().toString(), propertyType);
322             }
323         }
324         return propertyConstraint;
325     }
326
327     public static PropertyDefinition createModuleProperty(Map<String, Object> propertyValue) {
328         PropertyDefinition propertyDef = new PropertyDefinition();
329         setField(propertyValue, TypeUtils.ToscaTagNamesEnum.TYPE, propertyDef::setType);
330         setFieldBoolean(propertyValue, ToscaTagNamesEnum.REQUIRED, req -> propertyDef.setRequired(Boolean.parseBoolean(req)));
331         setField(propertyValue, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, propertyDef::setDescription);
332         setJsonStringField(propertyValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, propertyDef.getType(), propertyDef::setDefaultValue);
333         setJsonStringField(propertyValue, TypeUtils.ToscaTagNamesEnum.VALUE, propertyDef.getType(), propertyDef::setValue);
334         setFieldBoolean(propertyValue, TypeUtils.ToscaTagNamesEnum.IS_PASSWORD, pass -> propertyDef.setPassword(Boolean.parseBoolean(pass)));
335         setField(propertyValue, TypeUtils.ToscaTagNamesEnum.STATUS, propertyDef::setStatus);
336         setSchema(propertyValue, propertyDef);
337         setPropertyConstraints(propertyValue, propertyDef);
338         return propertyDef;
339     }
340
341     private static void setJsonStringField(Map<String, Object> propertyValue, ToscaTagNamesEnum elementName, String type, Consumer<String> setter) {
342         Either<Object, ResultStatusEnum> eitherValue = findToscaElement(propertyValue, elementName, ToscaElementTypeEnum.ALL);
343         if (eitherValue.isLeft()) {
344             String propertyJsonStringValue = getPropertyJsonStringValue(eitherValue.left().value(), type);
345             setter.accept(propertyJsonStringValue);
346         }
347     }
348
349     public static Annotation createModuleAnnotation(Map<String, Object> annotationMap, AnnotationTypeOperations annotationTypeOperations) {
350         String parsedAnnotationType = findFirstToscaStringElement(annotationMap, TypeUtils.ToscaTagNamesEnum.TYPE).left().value();
351         AnnotationTypeDefinition annotationTypeObject = annotationTypeOperations.getLatestType(parsedAnnotationType);
352         if (annotationTypeObject != null) {
353             Annotation annotation = new Annotation();
354             setField(annotationMap, TypeUtils.ToscaTagNamesEnum.TYPE, annotation::setType);
355             setField(annotationMap, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, annotation::setDescription);
356             Either<Map<String, PropertyDefinition>, ResultStatusEnum> properties = getProperties(annotationMap);
357             modifyPropertiesKeysToProperForm(properties, annotation);
358             return annotation;
359         }
360         return null;
361     }
362
363     private static Either<Boolean, ResponseFormat> modifyPropertiesKeysToProperForm(
364         Either<Map<String, PropertyDefinition>, ResultStatusEnum> properties, Annotation annotation) {
365         Either<Boolean, ResponseFormat> result = Either.left(true);
366         if (properties.isLeft()) {
367             List<PropertyDataDefinition> propertiesList = new ArrayList<>();
368             Map<String, PropertyDefinition> value = properties.left().value();
369             if (value != null) {
370                 for (Entry<String, PropertyDefinition> entry : value.entrySet()) {
371                     String name = entry.getKey();
372                     if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(name).matches()) {
373                         log.debug("The property with invalid name {} occurred upon import resource {}. ", name, annotation.getName());
374                         result = Either.right(componentsUtils.getResponseFormat(
375                             componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_PROPERTY_NAME, JsonPresentationFields.PROPERTY)));
376                     }
377                     PropertyDefinition propertyDefinition = entry.getValue();
378                     propertyDefinition.setValue(propertyDefinition.getName());
379                     propertyDefinition.setName(name);
380                     propertiesList.add(propertyDefinition);
381                 }
382             }
383             annotation.setProperties(propertiesList);
384         } else if (properties.right().value() != ResultStatusEnum.ELEMENT_NOT_FOUND) {
385             result = Either.right(componentsUtils
386                 .getResponseFormat(componentsUtils.convertFromResultStatusEnum(properties.right().value(), JsonPresentationFields.PROPERTY)));
387         }
388         return result;
389     }
390
391     public static InputDefinition createModuleInput(final Map<String, Object> inputValue, final AnnotationTypeOperations annotationTypeOperations) {
392         return parseAnnotationsAndAddItToInput(createModuleInput(inputValue), inputValue, annotationTypeOperations);
393     }
394
395     public static InputDefinition createModuleInput(final Map<String, Object> inputValue) {
396         final InputDefinition inputDef = new InputDefinition();
397         setField(inputValue, TypeUtils.ToscaTagNamesEnum.TYPE, inputDef::setType);
398         setFieldBoolean(inputValue, ToscaTagNamesEnum.REQUIRED, req -> inputDef.setRequired(Boolean.parseBoolean(req)));
399         setField(inputValue, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, inputDef::setDescription);
400         setJsonStringField(inputValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, inputDef.getType(), inputDef::setDefaultValue);
401         setFieldBoolean(inputValue, TypeUtils.ToscaTagNamesEnum.IS_PASSWORD, pass -> inputDef.setPassword(Boolean.parseBoolean(pass)));
402         setField(inputValue, TypeUtils.ToscaTagNamesEnum.STATUS, inputDef::setStatus);
403         setField(inputValue, TypeUtils.ToscaTagNamesEnum.LABEL, inputDef::setLabel);
404         setFieldBoolean(inputValue, TypeUtils.ToscaTagNamesEnum.HIDDEN, hidden -> inputDef.setHidden(Boolean.parseBoolean(hidden)));
405         setFieldBoolean(inputValue, TypeUtils.ToscaTagNamesEnum.IMMUTABLE, immutable -> inputDef.setImmutable(Boolean.parseBoolean(immutable)));
406         setFieldMap(inputValue, ToscaTagNamesEnum.METADATA, inputDef::setMetadata);
407         setSchema(inputValue, inputDef);
408         setPropertyConstraints(inputValue, inputDef);
409         return inputDef;
410     }
411
412     public static InputDefinition parseAnnotationsAndAddItToInput(InputDefinition inputDef, Map<String, Object> inputValue,
413                                                                   AnnotationTypeOperations annotationTypeOperations) {
414         Function<String, Annotation> elementGenByName = ImportUtils::createAnnotation;
415         Function<Map<String, Object>, Annotation> func = annotation -> createModuleAnnotation(annotation, annotationTypeOperations);
416         return getElements(inputValue, TypeUtils.ToscaTagNamesEnum.ANNOTATIONS, elementGenByName, func).left()
417             .map(annotations -> modifyInputWithAnnotations(inputDef, annotations)).left().on(err -> {
418                 log.error("Parsing annotations or adding them to the PropertyDataDefinition object failed");
419                 return inputDef;
420             });
421     }
422
423     private static InputDefinition modifyInputWithAnnotations(InputDefinition inputDef, Map<String, Annotation> annotationsMap) {
424         setAnnotationsName(annotationsMap);
425         inputDef.setAnnotationsToInput(annotationsMap.values());
426         return inputDef;
427     }
428
429     public static AttributeDefinition createModuleAttribute(Map<String, Object> attributeMap) {
430         AttributeDefinition attributeDef = new AttributeDefinition();
431         setField(attributeMap, TypeUtils.ToscaTagNamesEnum.TYPE, attributeDef::setType);
432         setField(attributeMap, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, attributeDef::setDescription);
433         setField(attributeMap, TypeUtils.ToscaTagNamesEnum.STATUS, attributeDef::setStatus);
434         setJsonStringField(attributeMap, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, attributeDef.getType(), attributeDef::set_default);
435         setEntrySchema(attributeMap, attributeDef);
436         return attributeDef;
437     }
438
439     private static void setSchema(final Map<String, Object> propertyValue, final PropertyDefinition propertyDefinition) {
440         final Either<Object, ResultStatusEnum> schemaElementRes = findEntrySchemaElement(propertyValue);
441         if (schemaElementRes.isLeft()) {
442             propertyDefinition.setSchema(getSchema(schemaElementRes.left().value()));
443         }
444     }
445
446     private static void setEntrySchema(final Map<String, Object> toscaJsonMap, final AttributeDefinition attributeDefinition) {
447         final Either<Object, ResultStatusEnum> schemaElementRes = findEntrySchemaElement(toscaJsonMap);
448         if (schemaElementRes.isLeft()) {
449             attributeDefinition.setEntry_schema(createEntrySchema(schemaElementRes.left().value()));
450         }
451     }
452
453     private static Either<Object, ResultStatusEnum> findEntrySchemaElement(final Map<String, Object> propertyValue) {
454         return findToscaElement(propertyValue, TypeUtils.ToscaTagNamesEnum.ENTRY_SCHEMA, ToscaElementTypeEnum.ALL);
455     }
456
457     private static SchemaDefinition getSchema(Object propertyFieldEntryScheme) {
458         SchemaDefinition schema = new SchemaDefinition();
459         if (propertyFieldEntryScheme instanceof String) {
460             String schemaType = (String) propertyFieldEntryScheme;
461             PropertyDefinition schemeProperty = new PropertyDefinition();
462             schemeProperty.setType(schemaType);
463             schema.setProperty(schemeProperty);
464         } else if (propertyFieldEntryScheme instanceof Map) {
465             PropertyDefinition schemeProperty = createModuleProperty((Map<String, Object>) propertyFieldEntryScheme);
466             schema.setProperty(schemeProperty);
467         }
468         return schema;
469     }
470
471     private static EntrySchema createEntrySchema(final Object toscaEntrySchemaObj) {
472         final EntrySchema entrySchema = new EntrySchema();
473         if (toscaEntrySchemaObj instanceof String) {
474             entrySchema.setType((String) toscaEntrySchemaObj);
475         } else if (toscaEntrySchemaObj instanceof Map) {
476             final PropertyDefinition schemeProperty = createModuleProperty((Map<String, Object>) toscaEntrySchemaObj);
477             entrySchema.setType(schemeProperty.getType());
478             entrySchema.setDescription(schemeProperty.getDescription());
479         }
480         return entrySchema;
481     }
482
483     private static void setField(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum tagName, Consumer<String> setter) {
484         Either<String, ResultStatusEnum> fieldStringValue = findFirstToscaStringElement(toscaJson, tagName);
485         if (fieldStringValue.isLeft()) {
486             setter.accept(fieldStringValue.left().value());
487         }
488     }
489
490     public static void setFieldBoolean(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum tagName, Consumer<String> setter) {
491         Either<String, ResultStatusEnum> fieldStringValue = findFirstToscaBooleanElement(toscaJson, tagName);
492         if (fieldStringValue.isLeft()) {
493             setter.accept(fieldStringValue.left().value());
494         }
495     }
496
497     private static void setFieldMap(final Map<String, Object> toscaJson, final ToscaTagNamesEnum tagName,
498                                     final Consumer<Map<String, String>> setter) {
499         final Either<Map<String, String>, ResultStatusEnum> toscaMapElement = findFirstToscaMapElement(toscaJson, tagName);
500         if (toscaMapElement.isLeft()) {
501             setter.accept(toscaMapElement.left().value());
502         }
503     }
504
505     public static Either<Map<String, PropertyDefinition>, ResultStatusEnum> getProperties(Map<String, Object> toscaJson) {
506         Function<String, PropertyDefinition> elementGenByName = ImportUtils::createProperties;
507         Function<Map<String, Object>, PropertyDefinition> func = ImportUtils::createModuleProperty;
508         return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.PROPERTIES, elementGenByName, func);
509     }
510
511     public static Either<Map<String, AttributeDefinition>, ResultStatusEnum> getAttributes(final Map<String, Object> toscaJson) {
512         final Function<String, AttributeDefinition> elementGenByName = ImportUtils::createAttribute;
513         final Function<Map<String, Object>, AttributeDefinition> func = ImportUtils::createModuleAttribute;
514         return getElements(toscaJson, ToscaTagNamesEnum.ATTRIBUTES, elementGenByName, func);
515     }
516
517     public static Either<Map<String, InputDefinition>, ResultStatusEnum> getInputs(Map<String, Object> toscaJson,
518                                                                                    AnnotationTypeOperations annotationTypeOperations) {
519         Function<String, InputDefinition> elementGenByName = ImportUtils::createInputs;
520         Function<Map<String, Object>, InputDefinition> func = object -> createModuleInput(object, annotationTypeOperations);
521         return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.INPUTS, elementGenByName, func);
522     }
523
524     public static Either<Map<String, InputDefinition>, ResultStatusEnum> getInputs(final Map<String, Object> toscaJson) {
525         return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.INPUTS, ImportUtils::createInputs, ImportUtils::createModuleInput);
526     }
527
528     public static <T> Either<Map<String, T>, ResultStatusEnum> getElements(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum elementTagName,
529                                                                            Function<String, T> elementGenByName,
530                                                                            Function<Map<String, Object>, T> func) {
531         Either<Map<String, T>, ResultStatusEnum> eitherResult = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
532         Either<Map<String, Object>, ResultStatusEnum> toscaAttributes = findFirstToscaMapElement(toscaJson, elementTagName);
533         if (toscaAttributes.isLeft()) {
534             Map<String, Object> jsonAttributes = toscaAttributes.left().value();
535             Map<String, T> moduleAttributes = new HashMap<>();
536             Iterator<Entry<String, Object>> propertiesNameValue = jsonAttributes.entrySet().iterator();
537             while (propertiesNameValue.hasNext()) {
538                 Entry<String, Object> attributeNameValue = propertiesNameValue.next();
539                 if (attributeNameValue.getValue() instanceof Map) {
540                     @SuppressWarnings("unchecked") T attribute = func.apply((Map<String, Object>) attributeNameValue.getValue());
541                     if (attribute != null) {
542                         moduleAttributes.put(String.valueOf(attributeNameValue.getKey()), attribute);
543                     }
544                 } else {
545                     T element = elementGenByName.apply(String.valueOf(attributeNameValue.getValue()));
546                     moduleAttributes.put(String.valueOf(attributeNameValue.getKey()), element);
547                 }
548             }
549             if (moduleAttributes.size() > 0) {
550                 eitherResult = Either.left(moduleAttributes);
551             }
552         }
553         return eitherResult;
554     }
555
556     private static AttributeDefinition createAttribute(String name) {
557         AttributeDefinition attribute = new AttributeDefinition();
558         attribute.setName(name);
559         return attribute;
560     }
561
562     private static PropertyDefinition createProperties(String name) {
563         PropertyDefinition property = new PropertyDefinition();
564         property.setDefaultValue(name);
565         property.setName(name);
566         return property;
567     }
568
569     private static InputDefinition createInputs(String name) {
570         InputDefinition input = new InputDefinition();
571         input.setName(name);
572         return input;
573     }
574
575     private static Annotation createAnnotation(String name) {
576         Annotation annotation = new Annotation();
577         annotation.setName(name);
578         return annotation;
579     }
580
581     public static Either<List<HeatParameterDefinition>, ResultStatusEnum> getHeatParameters(Map<String, Object> heatData, String artifactType) {
582         Either<List<HeatParameterDefinition>, ResultStatusEnum> eitherResult = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
583         Either<Map<String, Object>, ResultStatusEnum> toscaProperties = findFirstToscaMapElement(heatData, TypeUtils.ToscaTagNamesEnum.PARAMETERS);
584         if (toscaProperties.isLeft()) {
585             Map<String, Object> jsonProperties = toscaProperties.left().value();
586             List<HeatParameterDefinition> moduleProperties = new ArrayList<>();
587             Iterator<Entry<String, Object>> propertiesNameValue = jsonProperties.entrySet().iterator();
588             while (propertiesNameValue.hasNext()) {
589                 Entry<String, Object> propertyNameValue = propertiesNameValue.next();
590                 if (propertyNameValue.getValue() instanceof Map || propertyNameValue.getValue() instanceof List) {
591                     if (!artifactType.equals(ArtifactTypeEnum.HEAT_ENV.getType())) {
592                         @SuppressWarnings("unchecked") Either<HeatParameterDefinition, ResultStatusEnum> propertyStatus = createModuleHeatParameter(
593                             (Map<String, Object>) propertyNameValue.getValue());
594                         if (propertyStatus.isRight()) {
595                             return Either.right(propertyStatus.right().value());
596                         }
597                         HeatParameterDefinition property = propertyStatus.left().value();
598                         property.setName(String.valueOf(propertyNameValue.getKey()));
599                         moduleProperties.add(property);
600                     } else {
601                         addHeatParamDefinition(moduleProperties, propertyNameValue, true);
602                     }
603                 } else {
604                     addHeatParamDefinition(moduleProperties, propertyNameValue, false);
605                 }
606             }
607             if (!isEmpty(moduleProperties)) {
608                 eitherResult = Either.left(moduleProperties);
609             }
610         }
611         return eitherResult;
612     }
613
614     private static void addHeatParamDefinition(List<HeatParameterDefinition> moduleProperties, Entry<String, Object> propertyNameValue,
615                                                boolean isJson) {
616         HeatParameterDefinition property = new HeatParameterDefinition();
617         Object value = propertyNameValue.getValue();
618         if (value != null) {
619             property.setDefaultValue(isJson ? new Gson().toJson(value) : StringEscapeUtils.escapeJava(String.valueOf(value)));
620         }
621         property.setName(String.valueOf(propertyNameValue.getKey()));
622         moduleProperties.add(property);
623     }
624
625     private static Either<HeatParameterDefinition, ResultStatusEnum> createModuleHeatParameter(Map<String, Object> propertyValue) {
626         HeatParameterDefinition propertyDef = new HeatParameterDefinition();
627         String type;
628         Either<String, ResultStatusEnum> propertyFieldType = findFirstToscaStringElement(propertyValue, TypeUtils.ToscaTagNamesEnum.TYPE);
629         if (propertyFieldType.isLeft()) {
630             type = propertyFieldType.left().value();
631             propertyDef.setType(type);
632         } else {
633             return Either.right(ResultStatusEnum.INVALID_PROPERTY_TYPE);
634         }
635         Either<String, ResultStatusEnum> propertyFieldDescription = findFirstToscaStringElement(propertyValue,
636             TypeUtils.ToscaTagNamesEnum.DESCRIPTION);
637         if (propertyFieldDescription.isLeft()) {
638             propertyDef.setDescription(propertyFieldDescription.left().value());
639         }
640         Either<Object, ResultStatusEnum> propertyFieldDefaultVal = findToscaElement(propertyValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE,
641             ToscaElementTypeEnum.ALL);
642         if (propertyFieldDefaultVal.isLeft()) {
643             if (propertyFieldDefaultVal.left().value() == null) {
644                 return Either.right(ResultStatusEnum.INVALID_PROPERTY_VALUE);
645             }
646             Object value = propertyFieldDefaultVal.left().value();
647             String defaultValue =
648                 type.equals(HeatParameterType.JSON.getType()) ? new Gson().toJson(value) : StringEscapeUtils.escapeJava(String.valueOf(value));
649             propertyDef.setDefaultValue(defaultValue);
650             propertyDef.setCurrentValue(defaultValue);
651         }
652         return Either.left(propertyDef);
653     }
654
655     public static boolean containsGetInput(Object propValue) {
656         String value = getPropertyJsonStringValue(propValue, ToscaPropertyType.MAP.getType());
657         return value != null && value.contains(TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
658     }
659
660     public static String getPropertyJsonStringValue(Object value, String type) {
661         Gson gson = new Gson();
662         if (type == null) {
663             return null;
664         }
665         ToscaPropertyType validType = ToscaPropertyType.isValidType(type);
666         if (validType == null || validType == ToscaPropertyType.JSON || validType == ToscaPropertyType.MAP || validType == ToscaPropertyType.LIST) {
667             return gson.toJson(value);
668         }
669         return value.toString();
670     }
671
672     /**
673      * removes from Json map (toscaJson) first element found by name (elementName) note that this method could update the received argument toscaJson
674      *
675      * @param toscaJson
676      * @param elementName
677      */
678     public static void removeElementFromJsonMap(Map<String, Object> toscaJson, String elementName) {
679         for (Entry<String, Object> entry : toscaJson.entrySet()) {
680             String key = entry.getKey();
681             Object value = entry.getValue();
682             if (key.equals(elementName)) {
683                 toscaJson.remove(elementName);
684                 return;
685             } else if (value instanceof Map) {
686                 removeElementFromJsonMap((Map<String, Object>) value, elementName);
687             }
688         }
689     }
690
691     public enum ResultStatusEnum {
692         ELEMENT_NOT_FOUND, GENERAL_ERROR, OK, INVALID_PROPERTY_DEFAULT_VALUE, INVALID_PROPERTY_TYPE, INVALID_PROPERTY_VALUE, MISSING_ENTRY_SCHEMA_TYPE, INVALID_PROPERTY_NAME, INVALID_ATTRIBUTE_NAME
693     }
694
695     public enum ToscaElementTypeEnum {
696         BOOLEAN, STRING, MAP, LIST, ALL
697     }
698
699     private static class CustomResolver extends Resolver {
700
701         @Override
702         protected void addImplicitResolvers() {
703             // avoid implicit resolvers for strings that can be interpreted as boolean values
704             addImplicitResolver(Tag.STR, EMPTY, "");
705             addImplicitResolver(Tag.STR, NULL, null);
706             addImplicitResolver(Tag.NULL, NULL, "~nN\0");
707             addImplicitResolver(Tag.NULL, EMPTY, null);
708             addImplicitResolver(Tag.INT, INT, "-+0123456789");
709             addImplicitResolver(Tag.FLOAT, FLOAT, "-+0123456789.");
710             addImplicitResolver(Tag.YAML, YAML, "!&*");
711         }
712     }
713
714     private static class YamlLoader extends YamlProcessor {
715
716         public Yaml getStrictYamlLoader() {
717             return createYaml();
718         }
719     }
720
721     public static class Constants {
722
723         public static final String FIRST_NON_CERTIFIED_VERSION = "0.1";
724         public static final String VENDOR_NAME = "ONAP (Tosca)";
725         public static final String VENDOR_RELEASE = "1.0.0.wd03";
726         public static final LifecycleStateEnum NORMATIVE_TYPE_LIFE_CYCLE = LifecycleStateEnum.CERTIFIED;
727         public static final LifecycleStateEnum NORMATIVE_TYPE_LIFE_CYCLE_NOT_CERTIFIED_CHECKOUT = LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT;
728         public static final boolean NORMATIVE_TYPE_HIGHEST_VERSION = true;
729         public static final String ABSTRACT_CATEGORY_NAME = "Generic";
730         public static final String ABSTRACT_SUBCATEGORY = "Abstract";
731         public static final String DEFAULT_ICON = "defaulticon";
732         public static final String INNER_VFC_DESCRIPTION = "Not reusable inner VFC";
733         public static final String USER_DEFINED_RESOURCE_NAMESPACE_PREFIX = "org.openecomp.resource.";
734         public static final String UI_JSON_PAYLOAD_NAME = "payloadName";
735         public static final String CVFC_DESCRIPTION = "Complex node type that is used as nested type in VF";
736         public static final String ESCAPED_DOUBLE_QUOTE = "\"";
737         public static final String QUOTE = "'";
738         public static final String VF_DESCRIPTION = "Nested VF in service";
739
740         private Constants() {
741         }
742     }
743 }