f53726743c28240b72cd227b0c93187d4564d484
[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         setSchema(inputValue, inputDef);
407         setPropertyConstraints(inputValue, inputDef);
408         return inputDef;
409     }
410
411     public static InputDefinition parseAnnotationsAndAddItToInput(InputDefinition inputDef, Map<String, Object> inputValue,
412                                                                   AnnotationTypeOperations annotationTypeOperations) {
413         Function<String, Annotation> elementGenByName = ImportUtils::createAnnotation;
414         Function<Map<String, Object>, Annotation> func = annotation -> createModuleAnnotation(annotation, annotationTypeOperations);
415         return getElements(inputValue, TypeUtils.ToscaTagNamesEnum.ANNOTATIONS, elementGenByName, func).left()
416             .map(annotations -> modifyInputWithAnnotations(inputDef, annotations)).left().on(err -> {
417                 log.error("Parsing annotations or adding them to the PropertyDataDefinition object failed");
418                 return inputDef;
419             });
420     }
421
422     private static InputDefinition modifyInputWithAnnotations(InputDefinition inputDef, Map<String, Annotation> annotationsMap) {
423         setAnnotationsName(annotationsMap);
424         inputDef.setAnnotationsToInput(annotationsMap.values());
425         return inputDef;
426     }
427
428     public static AttributeDefinition createModuleAttribute(Map<String, Object> attributeMap) {
429         AttributeDefinition attributeDef = new AttributeDefinition();
430         setField(attributeMap, TypeUtils.ToscaTagNamesEnum.TYPE, attributeDef::setType);
431         setField(attributeMap, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, attributeDef::setDescription);
432         setField(attributeMap, TypeUtils.ToscaTagNamesEnum.STATUS, attributeDef::setStatus);
433         setJsonStringField(attributeMap, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, attributeDef.getType(), attributeDef::set_default);
434         setEntrySchema(attributeMap, attributeDef);
435         return attributeDef;
436     }
437
438     private static void setSchema(final Map<String, Object> propertyValue, final PropertyDefinition propertyDefinition) {
439         final Either<Object, ResultStatusEnum> schemaElementRes = findEntrySchemaElement(propertyValue);
440         if (schemaElementRes.isLeft()) {
441             propertyDefinition.setSchema(getSchema(schemaElementRes.left().value()));
442         }
443     }
444
445     private static void setEntrySchema(final Map<String, Object> toscaJsonMap, final AttributeDefinition attributeDefinition) {
446         final Either<Object, ResultStatusEnum> schemaElementRes = findEntrySchemaElement(toscaJsonMap);
447         if (schemaElementRes.isLeft()) {
448             attributeDefinition.setEntry_schema(createEntrySchema(schemaElementRes.left().value()));
449         }
450     }
451
452     private static Either<Object, ResultStatusEnum> findEntrySchemaElement(final Map<String, Object> propertyValue) {
453         return findToscaElement(propertyValue, TypeUtils.ToscaTagNamesEnum.ENTRY_SCHEMA, ToscaElementTypeEnum.ALL);
454     }
455
456     private static SchemaDefinition getSchema(Object propertyFieldEntryScheme) {
457         SchemaDefinition schema = new SchemaDefinition();
458         if (propertyFieldEntryScheme instanceof String) {
459             String schemaType = (String) propertyFieldEntryScheme;
460             PropertyDefinition schemeProperty = new PropertyDefinition();
461             schemeProperty.setType(schemaType);
462             schema.setProperty(schemeProperty);
463         } else if (propertyFieldEntryScheme instanceof Map) {
464             PropertyDefinition schemeProperty = createModuleProperty((Map<String, Object>) propertyFieldEntryScheme);
465             schema.setProperty(schemeProperty);
466         }
467         return schema;
468     }
469
470     private static EntrySchema createEntrySchema(final Object toscaEntrySchemaObj) {
471         final EntrySchema entrySchema = new EntrySchema();
472         if (toscaEntrySchemaObj instanceof String) {
473             entrySchema.setType((String) toscaEntrySchemaObj);
474         } else if (toscaEntrySchemaObj instanceof Map) {
475             final PropertyDefinition schemeProperty = createModuleProperty((Map<String, Object>) toscaEntrySchemaObj);
476             entrySchema.setType(schemeProperty.getType());
477             entrySchema.setDescription(schemeProperty.getDescription());
478         }
479         return entrySchema;
480     }
481
482     private static void setField(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum tagName, Consumer<String> setter) {
483         Either<String, ResultStatusEnum> fieldStringValue = findFirstToscaStringElement(toscaJson, tagName);
484         if (fieldStringValue.isLeft()) {
485             setter.accept(fieldStringValue.left().value());
486         }
487     }
488
489     public static void setFieldBoolean(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum tagName, Consumer<String> setter) {
490         Either<String, ResultStatusEnum> fieldStringValue = findFirstToscaBooleanElement(toscaJson, tagName);
491         if (fieldStringValue.isLeft()) {
492             setter.accept(fieldStringValue.left().value());
493         }
494     }
495
496     public static Either<Map<String, PropertyDefinition>, ResultStatusEnum> getProperties(Map<String, Object> toscaJson) {
497         Function<String, PropertyDefinition> elementGenByName = ImportUtils::createProperties;
498         Function<Map<String, Object>, PropertyDefinition> func = ImportUtils::createModuleProperty;
499         return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.PROPERTIES, elementGenByName, func);
500     }
501
502     public static Either<Map<String, AttributeDefinition>, ResultStatusEnum> getAttributes(final Map<String, Object> toscaJson) {
503         final Function<String, AttributeDefinition> elementGenByName = ImportUtils::createAttribute;
504         final Function<Map<String, Object>, AttributeDefinition> func = ImportUtils::createModuleAttribute;
505         return getElements(toscaJson, ToscaTagNamesEnum.ATTRIBUTES, elementGenByName, func);
506     }
507
508     public static Either<Map<String, InputDefinition>, ResultStatusEnum> getInputs(Map<String, Object> toscaJson,
509                                                                                    AnnotationTypeOperations annotationTypeOperations) {
510         Function<String, InputDefinition> elementGenByName = ImportUtils::createInputs;
511         Function<Map<String, Object>, InputDefinition> func = object -> createModuleInput(object, annotationTypeOperations);
512         return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.INPUTS, elementGenByName, func);
513     }
514
515     public static Either<Map<String, InputDefinition>, ResultStatusEnum> getInputs(final Map<String, Object> toscaJson) {
516         return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.INPUTS, ImportUtils::createInputs, ImportUtils::createModuleInput);
517     }
518
519     public static <T> Either<Map<String, T>, ResultStatusEnum> getElements(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum elementTagName,
520                                                                            Function<String, T> elementGenByName,
521                                                                            Function<Map<String, Object>, T> func) {
522         Either<Map<String, T>, ResultStatusEnum> eitherResult = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
523         Either<Map<String, Object>, ResultStatusEnum> toscaAttributes = findFirstToscaMapElement(toscaJson, elementTagName);
524         if (toscaAttributes.isLeft()) {
525             Map<String, Object> jsonAttributes = toscaAttributes.left().value();
526             Map<String, T> moduleAttributes = new HashMap<>();
527             Iterator<Entry<String, Object>> propertiesNameValue = jsonAttributes.entrySet().iterator();
528             while (propertiesNameValue.hasNext()) {
529                 Entry<String, Object> attributeNameValue = propertiesNameValue.next();
530                 if (attributeNameValue.getValue() instanceof Map) {
531                     @SuppressWarnings("unchecked") T attribute = func.apply((Map<String, Object>) attributeNameValue.getValue());
532                     if (attribute != null) {
533                         moduleAttributes.put(String.valueOf(attributeNameValue.getKey()), attribute);
534                     }
535                 } else {
536                     T element = elementGenByName.apply(String.valueOf(attributeNameValue.getValue()));
537                     moduleAttributes.put(String.valueOf(attributeNameValue.getKey()), element);
538                 }
539             }
540             if (moduleAttributes.size() > 0) {
541                 eitherResult = Either.left(moduleAttributes);
542             }
543         }
544         return eitherResult;
545     }
546
547     private static AttributeDefinition createAttribute(String name) {
548         AttributeDefinition attribute = new AttributeDefinition();
549         attribute.setName(name);
550         return attribute;
551     }
552
553     private static PropertyDefinition createProperties(String name) {
554         PropertyDefinition property = new PropertyDefinition();
555         property.setDefaultValue(name);
556         property.setName(name);
557         return property;
558     }
559
560     private static InputDefinition createInputs(String name) {
561         InputDefinition input = new InputDefinition();
562         input.setName(name);
563         return input;
564     }
565
566     private static Annotation createAnnotation(String name) {
567         Annotation annotation = new Annotation();
568         annotation.setName(name);
569         return annotation;
570     }
571
572     public static Either<List<HeatParameterDefinition>, ResultStatusEnum> getHeatParameters(Map<String, Object> heatData, String artifactType) {
573         Either<List<HeatParameterDefinition>, ResultStatusEnum> eitherResult = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
574         Either<Map<String, Object>, ResultStatusEnum> toscaProperties = findFirstToscaMapElement(heatData, TypeUtils.ToscaTagNamesEnum.PARAMETERS);
575         if (toscaProperties.isLeft()) {
576             Map<String, Object> jsonProperties = toscaProperties.left().value();
577             List<HeatParameterDefinition> moduleProperties = new ArrayList<>();
578             Iterator<Entry<String, Object>> propertiesNameValue = jsonProperties.entrySet().iterator();
579             while (propertiesNameValue.hasNext()) {
580                 Entry<String, Object> propertyNameValue = propertiesNameValue.next();
581                 if (propertyNameValue.getValue() instanceof Map || propertyNameValue.getValue() instanceof List) {
582                     if (!artifactType.equals(ArtifactTypeEnum.HEAT_ENV.getType())) {
583                         @SuppressWarnings("unchecked") Either<HeatParameterDefinition, ResultStatusEnum> propertyStatus = createModuleHeatParameter(
584                             (Map<String, Object>) propertyNameValue.getValue());
585                         if (propertyStatus.isRight()) {
586                             return Either.right(propertyStatus.right().value());
587                         }
588                         HeatParameterDefinition property = propertyStatus.left().value();
589                         property.setName(String.valueOf(propertyNameValue.getKey()));
590                         moduleProperties.add(property);
591                     } else {
592                         addHeatParamDefinition(moduleProperties, propertyNameValue, true);
593                     }
594                 } else {
595                     addHeatParamDefinition(moduleProperties, propertyNameValue, false);
596                 }
597             }
598             if (!isEmpty(moduleProperties)) {
599                 eitherResult = Either.left(moduleProperties);
600             }
601         }
602         return eitherResult;
603     }
604
605     private static void addHeatParamDefinition(List<HeatParameterDefinition> moduleProperties, Entry<String, Object> propertyNameValue,
606                                                boolean isJson) {
607         HeatParameterDefinition property = new HeatParameterDefinition();
608         Object value = propertyNameValue.getValue();
609         if (value != null) {
610             property.setDefaultValue(isJson ? new Gson().toJson(value) : StringEscapeUtils.escapeJava(String.valueOf(value)));
611         }
612         property.setName(String.valueOf(propertyNameValue.getKey()));
613         moduleProperties.add(property);
614     }
615
616     private static Either<HeatParameterDefinition, ResultStatusEnum> createModuleHeatParameter(Map<String, Object> propertyValue) {
617         HeatParameterDefinition propertyDef = new HeatParameterDefinition();
618         String type;
619         Either<String, ResultStatusEnum> propertyFieldType = findFirstToscaStringElement(propertyValue, TypeUtils.ToscaTagNamesEnum.TYPE);
620         if (propertyFieldType.isLeft()) {
621             type = propertyFieldType.left().value();
622             propertyDef.setType(type);
623         } else {
624             return Either.right(ResultStatusEnum.INVALID_PROPERTY_TYPE);
625         }
626         Either<String, ResultStatusEnum> propertyFieldDescription = findFirstToscaStringElement(propertyValue,
627             TypeUtils.ToscaTagNamesEnum.DESCRIPTION);
628         if (propertyFieldDescription.isLeft()) {
629             propertyDef.setDescription(propertyFieldDescription.left().value());
630         }
631         Either<Object, ResultStatusEnum> propertyFieldDefaultVal = findToscaElement(propertyValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE,
632             ToscaElementTypeEnum.ALL);
633         if (propertyFieldDefaultVal.isLeft()) {
634             if (propertyFieldDefaultVal.left().value() == null) {
635                 return Either.right(ResultStatusEnum.INVALID_PROPERTY_VALUE);
636             }
637             Object value = propertyFieldDefaultVal.left().value();
638             String defaultValue =
639                 type.equals(HeatParameterType.JSON.getType()) ? new Gson().toJson(value) : StringEscapeUtils.escapeJava(String.valueOf(value));
640             propertyDef.setDefaultValue(defaultValue);
641             propertyDef.setCurrentValue(defaultValue);
642         }
643         return Either.left(propertyDef);
644     }
645
646     public static boolean containsGetInput(Object propValue) {
647         String value = getPropertyJsonStringValue(propValue, ToscaPropertyType.MAP.getType());
648         return value != null && value.contains(TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
649     }
650
651     public static String getPropertyJsonStringValue(Object value, String type) {
652         Gson gson = new Gson();
653         if (type == null) {
654             return null;
655         }
656         ToscaPropertyType validType = ToscaPropertyType.isValidType(type);
657         if (validType == null || validType == ToscaPropertyType.JSON || validType == ToscaPropertyType.MAP || validType == ToscaPropertyType.LIST) {
658             return gson.toJson(value);
659         }
660         return value.toString();
661     }
662
663     /**
664      * removes from Json map (toscaJson) first element found by name (elementName) note that this method could update the received argument toscaJson
665      *
666      * @param toscaJson
667      * @param elementName
668      */
669     public static void removeElementFromJsonMap(Map<String, Object> toscaJson, String elementName) {
670         for (Entry<String, Object> entry : toscaJson.entrySet()) {
671             String key = entry.getKey();
672             Object value = entry.getValue();
673             if (key.equals(elementName)) {
674                 toscaJson.remove(elementName);
675                 return;
676             } else if (value instanceof Map) {
677                 removeElementFromJsonMap((Map<String, Object>) value, elementName);
678             }
679         }
680     }
681
682     public enum ResultStatusEnum {
683         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
684     }
685
686     public enum ToscaElementTypeEnum {
687         BOOLEAN, STRING, MAP, LIST, ALL
688     }
689
690     private static class CustomResolver extends Resolver {
691
692         @Override
693         protected void addImplicitResolvers() {
694             // avoid implicit resolvers for strings that can be interpreted as boolean values
695             addImplicitResolver(Tag.STR, EMPTY, "");
696             addImplicitResolver(Tag.STR, NULL, null);
697             addImplicitResolver(Tag.NULL, NULL, "~nN\0");
698             addImplicitResolver(Tag.NULL, EMPTY, null);
699             addImplicitResolver(Tag.INT, INT, "-+0123456789");
700             addImplicitResolver(Tag.FLOAT, FLOAT, "-+0123456789.");
701             addImplicitResolver(Tag.YAML, YAML, "!&*");
702         }
703     }
704
705     private static class YamlLoader extends YamlProcessor {
706
707         public Yaml getStrictYamlLoader() {
708             return createYaml();
709         }
710     }
711
712     public static class Constants {
713
714         public static final String FIRST_NON_CERTIFIED_VERSION = "0.1";
715         public static final String VENDOR_NAME = "ONAP (Tosca)";
716         public static final String VENDOR_RELEASE = "1.0.0.wd03";
717         public static final LifecycleStateEnum NORMATIVE_TYPE_LIFE_CYCLE = LifecycleStateEnum.CERTIFIED;
718         public static final LifecycleStateEnum NORMATIVE_TYPE_LIFE_CYCLE_NOT_CERTIFIED_CHECKOUT = LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT;
719         public static final boolean NORMATIVE_TYPE_HIGHEST_VERSION = true;
720         public static final String ABSTRACT_CATEGORY_NAME = "Generic";
721         public static final String ABSTRACT_SUBCATEGORY = "Abstract";
722         public static final String DEFAULT_ICON = "defaulticon";
723         public static final String INNER_VFC_DESCRIPTION = "Not reusable inner VFC";
724         public static final String USER_DEFINED_RESOURCE_NAMESPACE_PREFIX = "org.openecomp.resource.";
725         public static final String UI_JSON_PAYLOAD_NAME = "payloadName";
726         public static final String CVFC_DESCRIPTION = "Complex node type that is used as nested type in VF";
727         public static final String ESCAPED_DOUBLE_QUOTE = "\"";
728         public static final String QUOTE = "'";
729         public static final String VF_DESCRIPTION = "Nested VF in service";
730
731         private Constants() {
732         }
733     }
734 }