2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.be.components.impl;
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 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27 import com.google.gson.JsonParseException;
28 import com.google.gson.reflect.TypeToken;
29 import fj.data.Either;
30 import java.lang.reflect.Type;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.LinkedHashMap;
37 import java.util.List;
39 import java.util.Map.Entry;
40 import java.util.function.Consumer;
41 import java.util.function.Function;
42 import org.apache.commons.collections.CollectionUtils;
43 import org.apache.commons.text.StringEscapeUtils;
44 import org.onap.sdc.tosca.datatypes.model.EntrySchema;
45 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
46 import org.openecomp.sdc.be.config.BeEcompErrorManager;
47 import org.openecomp.sdc.be.dao.api.ActionStatus;
48 import org.openecomp.sdc.be.datatypes.elements.Annotation;
49 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
50 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
51 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
52 import org.openecomp.sdc.be.impl.ComponentsUtils;
53 import org.openecomp.sdc.be.model.AnnotationTypeDefinition;
54 import org.openecomp.sdc.be.model.AttributeDefinition;
55 import org.openecomp.sdc.be.model.HeatParameterDefinition;
56 import org.openecomp.sdc.be.model.InputDefinition;
57 import org.openecomp.sdc.be.model.LifecycleStateEnum;
58 import org.openecomp.sdc.be.model.OutputDefinition;
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.datatypes.enums.ConstraintType;
66 import org.openecomp.sdc.be.model.tosca.constraints.AbstractComparablePropertyConstraint;
67 import org.openecomp.sdc.be.model.tosca.constraints.EqualConstraint;
68 import org.openecomp.sdc.be.model.tosca.constraints.InRangeConstraint;
69 import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint;
70 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
71 import org.openecomp.sdc.be.utils.TypeUtils;
72 import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum;
73 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
74 import org.openecomp.sdc.common.log.wrappers.Logger;
75 import org.openecomp.sdc.exception.ResponseFormat;
76 import org.springframework.beans.factory.annotation.Autowired;
77 import org.springframework.beans.factory.config.YamlProcessor;
78 import org.springframework.stereotype.Component;
79 import org.yaml.snakeyaml.DumperOptions;
80 import org.yaml.snakeyaml.Yaml;
81 import org.yaml.snakeyaml.constructor.Constructor;
82 import org.yaml.snakeyaml.nodes.Tag;
83 import org.yaml.snakeyaml.representer.Representer;
84 import org.yaml.snakeyaml.resolver.Resolver;
87 public final class ImportUtils {
89 private static final CustomResolver customResolver = new CustomResolver();
90 private static final Yaml strictYamlLoader = new YamlLoader().getStrictYamlLoader();
91 private static final Logger log = Logger.getLogger(ImportUtils.class);
92 private static ComponentsUtils componentsUtils;
94 private ImportUtils() {
98 public static void setComponentsUtils(ComponentsUtils cu) {
102 private static void buildMap(Map<String, Object> output, Map<String, Object> map) {
103 for (Entry<String, Object> entry : map.entrySet()) {
104 String key = entry.getKey();
105 Object value = entry.getValue();
106 if (value instanceof Map) {
107 Map<String, Object> result = new LinkedHashMap<>();
108 buildMap(result, (Map) value);
109 output.put(key, result);
110 } else if (value instanceof Collection) {
111 Map<String, Object> result = new LinkedHashMap<>();
113 for (Object item : (Collection<Object>) value) {
114 buildMap(result, Collections.singletonMap("[" + (i++) + "]", item));
116 output.put(key, new ArrayList<>(result.values()));
118 output.put(key, value);
123 public static Map<String, Object> loadYamlAsStrictMap(String content) {
124 Map<String, Object> result = new LinkedHashMap<>();
125 Object map = strictYamlLoader.load(content);
126 buildMap(result, (Map<String, Object>) map);
130 @SuppressWarnings("unchecked")
131 public static Either<List<HeatParameterDefinition>, ResultStatusEnum> getHeatParamsWithoutImplicitTypes(String heatDecodedPayload,
132 String artifactType) {
133 Map<String, Object> heatData = (Map<String, Object>) new Yaml(new Constructor(), new Representer(), new DumperOptions(), customResolver)
134 .load(heatDecodedPayload);
135 return getHeatParameters(heatData, artifactType);
138 @SuppressWarnings("unchecked")
139 private static void handleElementNameNotFound(String elementName, Object elementValue, ToscaElementTypeEnum elementType,
140 List<Object> returnedList) {
141 if (elementValue instanceof Map) {
142 findToscaElements((Map<String, Object>) elementValue, elementName, elementType, returnedList);
143 } else if (elementValue instanceof List) {
144 findAllToscaElementsInList((List<Object>) elementValue, elementName, elementType, returnedList);
148 @SuppressWarnings("unchecked")
149 private static void addFoundElementAccordingToItsType(String elementName, ToscaElementTypeEnum elementType, List<Object> returnedList,
150 Object elementValue) {
151 if (elementValue instanceof Boolean) {
152 if (elementType == ToscaElementTypeEnum.BOOLEAN || elementType == ToscaElementTypeEnum.ALL) {
153 returnedList.add(elementValue);
155 } else if (elementValue instanceof String) {
156 if (elementType == ToscaElementTypeEnum.STRING || elementType == ToscaElementTypeEnum.ALL) {
157 returnedList.add(elementValue);
159 } else if (elementValue instanceof Map) {
160 if (elementType == ToscaElementTypeEnum.MAP || elementType == ToscaElementTypeEnum.ALL) {
161 returnedList.add(elementValue);
163 findToscaElements((Map<String, Object>) elementValue, elementName, elementType, returnedList);
164 } else if (elementValue instanceof List) {
165 if (elementType == ToscaElementTypeEnum.LIST || elementType == ToscaElementTypeEnum.ALL) {
166 returnedList.add(elementValue);
168 findAllToscaElementsInList((List<Object>) elementValue, elementName, elementType, returnedList);
170 // For Integer, Double etc...
171 else if (elementType == ToscaElementTypeEnum.ALL && elementValue != null) {
172 returnedList.add(String.valueOf(elementValue));
176 private static void findAllToscaElementsInList(List<Object> list, String elementName, ToscaElementTypeEnum elementType,
177 List<Object> returnedList) {
178 list.forEach(elementValue -> handleElementNameNotFound(elementName, elementValue, elementType, returnedList));
181 public static Either<Object, ResultStatusEnum> findToscaElement(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum elementName,
182 ToscaElementTypeEnum elementType) {
183 final var toscaElements = findToscaElements(toscaJson, elementName.getElementName(), elementType, new ArrayList<>());
184 if (toscaElements.isLeft() && CollectionUtils.isNotEmpty(toscaElements.left().value())) {
185 return Either.left(toscaElements.left().value().get(0));
187 return Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
191 * Recursively searches for all tosca elements with key equals to elementName and value equals to elementType. <br> Returns Either element
192 * with:<br> List with all value if values found<br> Or ELEMENT_NOT_FOUND ActionStatus
197 public static Either<List<Object>, ResultStatusEnum> findToscaElements(Map<String, Object> toscaJson, String elementName,
198 ToscaElementTypeEnum elementType, List<Object> returnedList) {
199 Either<List<Object>, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
200 String skipKey = null;
201 if (toscaJson.containsKey(elementName)) {
202 skipKey = handleFoundElement(toscaJson, elementName, elementType, returnedList);
204 Iterator<Entry<String, Object>> keyValItr = toscaJson.entrySet().iterator();
205 while (keyValItr.hasNext()) {
206 Entry<String, Object> keyValEntry = keyValItr.next();
207 if (!String.valueOf(keyValEntry.getKey()).equals(skipKey)) {
208 handleElementNameNotFound(elementName, keyValEntry.getValue(), elementType, returnedList);
211 if (!isEmpty(returnedList)) {
212 returnedElement = Either.left(returnedList);
214 return returnedElement;
217 private static String handleFoundElement(Map<String, Object> toscaJson, String elementName, ToscaElementTypeEnum elementType,
218 List<Object> returnedList) {
219 Object elementValue = toscaJson.get(elementName);
220 addFoundElementAccordingToItsType(elementName, elementType, returnedList, elementValue);
224 @SuppressWarnings("unchecked")
225 public static <T> Either<List<T>, ResultStatusEnum> findFirstToscaListElement(Map<String, Object> toscaJson,
226 TypeUtils.ToscaTagNamesEnum toscaTagName) {
227 Either<List<T>, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
228 Either<Object, ResultStatusEnum> findFirstToscaElement = findToscaElement(toscaJson, toscaTagName, ToscaElementTypeEnum.LIST);
229 if (findFirstToscaElement.isLeft()) {
230 returnedElement = Either.left((List<T>) findFirstToscaElement.left().value());
232 return returnedElement;
235 @SuppressWarnings("unchecked")
236 public static <T> Either<Map<String, T>, ResultStatusEnum> findFirstToscaMapElement(Map<String, Object> toscaJson,
237 TypeUtils.ToscaTagNamesEnum toscaTagName) {
238 Either<Map<String, T>, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
239 Either<Object, ResultStatusEnum> findFirstToscaElement = findToscaElement(toscaJson, toscaTagName, ToscaElementTypeEnum.MAP);
240 if (findFirstToscaElement.isLeft()) {
241 returnedElement = Either.left((Map<String, T>) findFirstToscaElement.left().value());
243 return returnedElement;
246 public static Either<String, ResultStatusEnum> findFirstToscaStringElement(Map<String, Object> toscaJson,
247 TypeUtils.ToscaTagNamesEnum toscaTagName) {
248 Either<String, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
249 Either<Object, ResultStatusEnum> findFirstToscaElements = findToscaElement(toscaJson, toscaTagName, ToscaElementTypeEnum.STRING);
250 if (findFirstToscaElements.isLeft()) {
251 returnedElement = Either.left((String) findFirstToscaElements.left().value());
253 return returnedElement;
257 * searches for first Tosca in Json map (toscaJson) boolean element by name (toscaTagName) returns found element or ELEMENT_NOT_FOUND status
260 * @param toscaTagName
263 private static Either<String, ResultStatusEnum> findFirstToscaBooleanElement(Map<String, Object> toscaJson,
264 TypeUtils.ToscaTagNamesEnum toscaTagName) {
265 Either<String, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
266 Either<Object, ResultStatusEnum> findFirstToscaElements = findToscaElement(toscaJson, toscaTagName, ToscaElementTypeEnum.BOOLEAN);
267 if (findFirstToscaElements.isLeft()) {
268 returnedElement = Either.left(String.valueOf(findFirstToscaElements.left().value()));
270 return returnedElement;
273 private static void setPropertyConstraints(Map<String, Object> propertyValue, PropertyDefinition property) {
274 List<PropertyConstraint> constraints = getPropertyConstraints(propertyValue, property.getType());
275 if (CollectionUtils.isNotEmpty(constraints)) {
276 property.setConstraints(constraints);
280 private static List<PropertyConstraint> getPropertyConstraints(final Map<String, Object> propertyValue, final String propertyType) {
281 final List<Object> propertyFieldConstraints = findCurrentLevelConstraintsElement(propertyValue);
282 if (CollectionUtils.isEmpty(propertyFieldConstraints)) {
283 return Collections.emptyList();
285 final List<PropertyConstraint> constraintList = new ArrayList<>();
286 final Type constraintType = new TypeToken<PropertyConstraint>() {
288 final Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintDeserialiser()).create();
289 for (final Object constraintJson : propertyFieldConstraints) {
290 final PropertyConstraint propertyConstraint = validateAndGetPropertyConstraint(propertyType, constraintType, gson, constraintJson);
291 if (propertyConstraint != null) {
292 constraintList.add(propertyConstraint);
295 return constraintList;
298 private static List<Object> findCurrentLevelConstraintsElement(Map<String, Object> toscaJson) {
299 List<Object> constraints = null;
300 if (toscaJson.containsKey(TypeUtils.ToscaTagNamesEnum.CONSTRAINTS.getElementName())) {
302 constraints = (List<Object>) toscaJson.get(TypeUtils.ToscaTagNamesEnum.CONSTRAINTS.getElementName());
303 } catch (ClassCastException e) {
304 throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS_FORMAT,
305 toscaJson.get(TypeUtils.ToscaTagNamesEnum.CONSTRAINTS.getElementName()).toString());
311 private static PropertyConstraint validateAndGetPropertyConstraint(String propertyType, Type constraintType, Gson gson, Object constraintJson) {
312 PropertyConstraint propertyConstraint;
314 propertyConstraint = gson.fromJson(gson.toJson(constraintJson), constraintType);
315 } catch (ClassCastException | JsonParseException e) {
316 throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS_FORMAT, constraintJson.toString());
318 if (propertyConstraint instanceof ValidValuesConstraint) {
320 ((ValidValuesConstraint) propertyConstraint).validateType(propertyType);
321 boolean valid = ((ValidValuesConstraint) propertyConstraint).validateValueType(propertyType);
323 ((ValidValuesConstraint) propertyConstraint).changeConstraintValueTypeTo(propertyType);
325 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
326 BeEcompErrorManager.getInstance()
327 .logInternalFlowError("GetInitializedPropertyConstraint", e.getMessage(), BeEcompErrorManager.ErrorSeverity.ERROR);
328 throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS, ConstraintType.VALID_VALUES.name(),
329 ((ValidValuesConstraint) propertyConstraint).getValidValues().toString(), propertyType);
331 } else if (propertyConstraint instanceof AbstractComparablePropertyConstraint) {
333 boolean valid = ((AbstractComparablePropertyConstraint) propertyConstraint).validateValueType(propertyType);
335 ((AbstractComparablePropertyConstraint) propertyConstraint).changeConstraintValueTypeTo(propertyType);
337 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
338 throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS, propertyConstraint.getConstraintType().name(),
339 ((AbstractComparablePropertyConstraint) propertyConstraint).getConstraintValueAsString(), propertyType);
341 } else if (propertyConstraint instanceof EqualConstraint) {
343 boolean valid = ((EqualConstraint) propertyConstraint).validateValueType(propertyType);
345 ((EqualConstraint) propertyConstraint).changeConstraintValueTypeTo(propertyType);
347 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
348 throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS, ConstraintType.EQUAL.name(),
349 String.valueOf(((EqualConstraint) propertyConstraint).getEqual()), propertyType);
351 } else if (propertyConstraint instanceof InRangeConstraint) {
353 boolean valid = ((InRangeConstraint) propertyConstraint).validateValueType(propertyType);
355 ((InRangeConstraint) propertyConstraint).changeConstraintValueTypeTo(propertyType);
357 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
358 throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS, ConstraintType.IN_RANGE.name(),
359 String.valueOf(((InRangeConstraint) propertyConstraint).getInRange()), propertyType);
362 return propertyConstraint;
365 static PropertyDefinition createModuleProperty(Map<String, Object> propertyValue) {
366 PropertyDefinition propertyDef = new PropertyDefinition();
367 setField(propertyValue, TypeUtils.ToscaTagNamesEnum.TYPE, propertyDef::setType);
368 setFieldBoolean(propertyValue, ToscaTagNamesEnum.REQUIRED, req -> propertyDef.setRequired(Boolean.parseBoolean(req)));
369 setField(propertyValue, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, propertyDef::setDescription);
370 setJsonStringField(propertyValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, propertyDef.getType(), propertyDef::setDefaultValue);
371 setJsonStringField(propertyValue, TypeUtils.ToscaTagNamesEnum.VALUE, propertyDef.getType(), propertyDef::setValue);
372 setFieldBoolean(propertyValue, TypeUtils.ToscaTagNamesEnum.IS_PASSWORD, pass -> propertyDef.setPassword(Boolean.parseBoolean(pass)));
373 setField(propertyValue, TypeUtils.ToscaTagNamesEnum.STATUS, propertyDef::setStatus);
374 setSchema(propertyValue, propertyDef);
375 setPropertyConstraints(propertyValue, propertyDef);
379 private static void setJsonStringField(Map<String, Object> propertyValue, ToscaTagNamesEnum elementName, String type, Consumer<String> setter) {
380 Either<Object, ResultStatusEnum> eitherValue = findToscaElement(propertyValue, elementName, ToscaElementTypeEnum.ALL);
381 if (eitherValue.isLeft()) {
382 setter.accept(getPropertyJsonStringValue(eitherValue.left().value(), type));
386 private static Annotation createModuleAnnotation(Map<String, Object> annotationMap, AnnotationTypeOperations annotationTypeOperations) {
387 String parsedAnnotationType = findFirstToscaStringElement(annotationMap, TypeUtils.ToscaTagNamesEnum.TYPE).left().value();
388 AnnotationTypeDefinition annotationTypeObject = annotationTypeOperations.getLatestType(parsedAnnotationType);
389 if (annotationTypeObject != null) {
390 Annotation annotation = new Annotation();
391 setField(annotationMap, TypeUtils.ToscaTagNamesEnum.TYPE, annotation::setType);
392 setField(annotationMap, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, annotation::setDescription);
393 Either<Map<String, PropertyDefinition>, ResultStatusEnum> properties = getProperties(annotationMap);
394 modifyPropertiesKeysToProperForm(properties, annotation);
400 private static Either<Boolean, ResponseFormat> modifyPropertiesKeysToProperForm(
401 Either<Map<String, PropertyDefinition>, ResultStatusEnum> properties, Annotation annotation) {
402 Either<Boolean, ResponseFormat> result = Either.left(true);
403 if (properties.isLeft()) {
404 List<PropertyDataDefinition> propertiesList = new ArrayList<>();
405 Map<String, PropertyDefinition> value = properties.left().value();
407 for (Entry<String, PropertyDefinition> entry : value.entrySet()) {
408 String name = entry.getKey();
409 if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(name).matches()) {
410 log.debug("The property with invalid name {} occurred upon import resource {}. ", name, annotation.getName());
411 result = Either.right(componentsUtils.getResponseFormat(
412 componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_PROPERTY_NAME, JsonPresentationFields.PROPERTY)));
414 PropertyDefinition propertyDefinition = entry.getValue();
415 propertyDefinition.setValue(propertyDefinition.getName());
416 propertyDefinition.setName(name);
417 propertiesList.add(propertyDefinition);
420 annotation.setProperties(propertiesList);
421 } else if (properties.right().value() != ResultStatusEnum.ELEMENT_NOT_FOUND) {
422 result = Either.right(componentsUtils
423 .getResponseFormat(componentsUtils.convertFromResultStatusEnum(properties.right().value(), JsonPresentationFields.PROPERTY)));
428 private static InputDefinition createModuleInput(final Map<String, Object> inputValue, final AnnotationTypeOperations annotationTypeOperations) {
429 return parseAnnotationsAndAddItToInput(createModuleInput(inputValue), inputValue, annotationTypeOperations);
432 private static InputDefinition createModuleInput(final Map<String, Object> inputValue) {
433 final InputDefinition inputDef = new InputDefinition();
434 setField(inputValue, TypeUtils.ToscaTagNamesEnum.TYPE, inputDef::setType);
435 setFieldBoolean(inputValue, ToscaTagNamesEnum.REQUIRED, req -> inputDef.setRequired(Boolean.parseBoolean(req)));
436 setField(inputValue, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, inputDef::setDescription);
437 setJsonStringField(inputValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, inputDef.getType(), inputDef::setDefaultValue);
438 setFieldBoolean(inputValue, TypeUtils.ToscaTagNamesEnum.IS_PASSWORD, pass -> inputDef.setPassword(Boolean.parseBoolean(pass)));
439 setField(inputValue, TypeUtils.ToscaTagNamesEnum.STATUS, inputDef::setStatus);
440 setField(inputValue, TypeUtils.ToscaTagNamesEnum.LABEL, inputDef::setLabel);
441 setFieldBoolean(inputValue, TypeUtils.ToscaTagNamesEnum.HIDDEN, hidden -> inputDef.setHidden(Boolean.parseBoolean(hidden)));
442 setFieldBoolean(inputValue, TypeUtils.ToscaTagNamesEnum.IMMUTABLE, immutable -> inputDef.setImmutable(Boolean.parseBoolean(immutable)));
443 setFieldMap(inputValue, ToscaTagNamesEnum.METADATA, inputDef::setMetadata);
444 setSchema(inputValue, inputDef);
445 setPropertyConstraints(inputValue, inputDef);
449 private static OutputDefinition createModuleOutput(final Map<String, Object> outputValue) {
450 final OutputDefinition outputDef = new OutputDefinition();
451 setField(outputValue, TypeUtils.ToscaTagNamesEnum.TYPE, outputDef::setType);
452 setField(outputValue, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, outputDef::setDescription);
453 setJsonStringField(outputValue, TypeUtils.ToscaTagNamesEnum.VALUE, outputDef.getType(), outputDef::setValue);
454 setField(outputValue, TypeUtils.ToscaTagNamesEnum.STATUS, outputDef::setStatus);
458 private static InputDefinition parseAnnotationsAndAddItToInput(InputDefinition inputDef, Map<String, Object> inputValue,
459 AnnotationTypeOperations annotationTypeOperations) {
460 Function<String, Annotation> elementGenByName = ImportUtils::createAnnotation;
461 Function<Map<String, Object>, Annotation> func = annotation -> createModuleAnnotation(annotation, annotationTypeOperations);
462 return getElements(inputValue, TypeUtils.ToscaTagNamesEnum.ANNOTATIONS, elementGenByName, func).left()
463 .map(annotations -> modifyInputWithAnnotations(inputDef, annotations)).left().on(err -> {
464 log.error("Parsing annotations or adding them to the PropertyDataDefinition object failed");
469 private static InputDefinition modifyInputWithAnnotations(InputDefinition inputDef, Map<String, Annotation> annotationsMap) {
470 setAnnotationsName(annotationsMap);
471 inputDef.setAnnotationsToInput(annotationsMap.values());
475 static AttributeDefinition createModuleAttribute(Map<String, Object> attributeMap) {
476 AttributeDefinition attributeDef = new AttributeDefinition();
477 setField(attributeMap, TypeUtils.ToscaTagNamesEnum.TYPE, attributeDef::setType);
478 setField(attributeMap, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, attributeDef::setDescription);
479 setField(attributeMap, TypeUtils.ToscaTagNamesEnum.STATUS, attributeDef::setStatus);
480 setJsonStringField(attributeMap, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, attributeDef.getType(), attributeDef::set_default);
481 setEntrySchema(attributeMap, attributeDef);
485 private static void setSchema(final Map<String, Object> propertyValue, final PropertyDefinition propertyDefinition) {
486 final Either<Object, ResultStatusEnum> schemaElementRes = findEntrySchemaElement(propertyValue);
487 if (schemaElementRes.isLeft()) {
488 propertyDefinition.setSchema(getSchema(schemaElementRes.left().value()));
492 private static void setEntrySchema(final Map<String, Object> toscaJsonMap, final AttributeDefinition attributeDefinition) {
493 final Either<Object, ResultStatusEnum> schemaElementRes = findEntrySchemaElement(toscaJsonMap);
494 if (schemaElementRes.isLeft()) {
495 attributeDefinition.setEntry_schema(createEntrySchema(schemaElementRes.left().value()));
499 private static Either<Object, ResultStatusEnum> findEntrySchemaElement(final Map<String, Object> propertyValue) {
500 return findToscaElement(propertyValue, TypeUtils.ToscaTagNamesEnum.ENTRY_SCHEMA, ToscaElementTypeEnum.ALL);
503 private static SchemaDefinition getSchema(Object propertyFieldEntryScheme) {
504 SchemaDefinition schema = new SchemaDefinition();
505 if (propertyFieldEntryScheme instanceof String) {
506 String schemaType = (String) propertyFieldEntryScheme;
507 PropertyDefinition schemeProperty = new PropertyDefinition();
508 schemeProperty.setType(schemaType);
509 schema.setProperty(schemeProperty);
510 } else if (propertyFieldEntryScheme instanceof Map) {
511 PropertyDefinition schemeProperty = createModuleProperty((Map<String, Object>) propertyFieldEntryScheme);
512 schema.setProperty(schemeProperty);
517 private static EntrySchema createEntrySchema(final Object toscaEntrySchemaObj) {
518 final EntrySchema entrySchema = new EntrySchema();
519 if (toscaEntrySchemaObj instanceof String) {
520 entrySchema.setType((String) toscaEntrySchemaObj);
521 } else if (toscaEntrySchemaObj instanceof Map) {
522 final PropertyDefinition schemeProperty = createModuleProperty((Map<String, Object>) toscaEntrySchemaObj);
523 entrySchema.setType(schemeProperty.getType());
524 entrySchema.setDescription(schemeProperty.getDescription());
529 private static void setField(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum tagName, Consumer<String> setter) {
530 Either<String, ResultStatusEnum> fieldStringValue = findFirstToscaStringElement(toscaJson, tagName);
531 if (fieldStringValue.isLeft()) {
532 setter.accept(fieldStringValue.left().value());
536 private static void setFieldBoolean(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum tagName, Consumer<String> setter) {
537 Either<String, ResultStatusEnum> fieldStringValue = findFirstToscaBooleanElement(toscaJson, tagName);
538 if (fieldStringValue.isLeft()) {
539 setter.accept(fieldStringValue.left().value());
543 private static void setFieldMap(final Map<String, Object> toscaJson, final ToscaTagNamesEnum tagName,
544 final Consumer<Map<String, String>> setter) {
545 final Either<Map<String, String>, ResultStatusEnum> toscaMapElement = findFirstToscaMapElement(toscaJson, tagName);
546 if (toscaMapElement.isLeft()) {
547 setter.accept(toscaMapElement.left().value());
551 public static Either<Map<String, PropertyDefinition>, ResultStatusEnum> getProperties(Map<String, Object> toscaJson) {
552 Function<String, PropertyDefinition> elementGenByName = ImportUtils::createProperties;
553 Function<Map<String, Object>, PropertyDefinition> func = ImportUtils::createModuleProperty;
554 return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.PROPERTIES, elementGenByName, func);
557 public static Either<Map<String, AttributeDefinition>, ResultStatusEnum> getAttributes(final Map<String, Object> toscaJson) {
558 final Function<String, AttributeDefinition> elementGenByName = ImportUtils::createAttribute;
559 final Function<Map<String, Object>, AttributeDefinition> func = ImportUtils::createModuleAttribute;
560 return getElements(toscaJson, ToscaTagNamesEnum.ATTRIBUTES, elementGenByName, func);
563 public static Either<Map<String, InputDefinition>, ResultStatusEnum> getInputs(Map<String, Object> toscaJson,
564 AnnotationTypeOperations annotationTypeOperations) {
565 Function<String, InputDefinition> elementGenByName = ImportUtils::createInputs;
566 Function<Map<String, Object>, InputDefinition> func = object -> createModuleInput(object, annotationTypeOperations);
567 return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.INPUTS, elementGenByName, func);
570 public static Either<Map<String, OutputDefinition>, ResultStatusEnum> getOutputs(Map<String, Object> toscaJson) {
571 final Function<String, OutputDefinition> elementGenByName = ImportUtils::createOutputs;
572 final Function<Map<String, Object>, OutputDefinition> func = ImportUtils::createModuleOutput;
573 return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.OUTPUTS, elementGenByName, func);
576 public static Either<Map<String, InputDefinition>, ResultStatusEnum> getInputs(final Map<String, Object> toscaJson) {
577 return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.INPUTS, ImportUtils::createInputs, ImportUtils::createModuleInput);
580 private static <T> Either<Map<String, T>, ResultStatusEnum> getElements(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum elementTagName,
581 Function<String, T> elementGenByName,
582 Function<Map<String, Object>, T> func) {
583 Either<Map<String, T>, ResultStatusEnum> eitherResult = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
584 Either<Map<String, Object>, ResultStatusEnum> toscaAttributes = findFirstToscaMapElement(toscaJson, elementTagName);
585 if (toscaAttributes.isLeft()) {
586 Map<String, Object> jsonAttributes = toscaAttributes.left().value();
587 Map<String, T> moduleAttributes = new HashMap<>();
588 Iterator<Entry<String, Object>> propertiesNameValue = jsonAttributes.entrySet().iterator();
589 while (propertiesNameValue.hasNext()) {
590 Entry<String, Object> attributeNameValue = propertiesNameValue.next();
591 if (attributeNameValue.getValue() instanceof Map) {
592 @SuppressWarnings("unchecked") T attribute = func.apply((Map<String, Object>) attributeNameValue.getValue());
593 if (attribute != null) {
594 moduleAttributes.put(String.valueOf(attributeNameValue.getKey()), attribute);
597 T element = elementGenByName.apply(String.valueOf(attributeNameValue.getValue()));
598 moduleAttributes.put(String.valueOf(attributeNameValue.getKey()), element);
601 if (moduleAttributes.size() > 0) {
602 eitherResult = Either.left(moduleAttributes);
608 private static AttributeDefinition createAttribute(String name) {
609 AttributeDefinition attribute = new AttributeDefinition();
610 attribute.setName(name);
614 private static PropertyDefinition createProperties(String name) {
615 PropertyDefinition property = new PropertyDefinition();
616 property.setDefaultValue(name);
617 property.setName(name);
621 private static InputDefinition createInputs(String name) {
622 InputDefinition input = new InputDefinition();
627 private static OutputDefinition createOutputs(final String name) {
628 final OutputDefinition output = new OutputDefinition();
629 output.setName(name);
633 private static Annotation createAnnotation(String name) {
634 Annotation annotation = new Annotation();
635 annotation.setName(name);
639 static Either<List<HeatParameterDefinition>, ResultStatusEnum> getHeatParameters(Map<String, Object> heatData, String artifactType) {
640 Either<List<HeatParameterDefinition>, ResultStatusEnum> eitherResult = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
641 Either<Map<String, Object>, ResultStatusEnum> toscaProperties = findFirstToscaMapElement(heatData, TypeUtils.ToscaTagNamesEnum.PARAMETERS);
642 if (toscaProperties.isLeft()) {
643 Map<String, Object> jsonProperties = toscaProperties.left().value();
644 List<HeatParameterDefinition> moduleProperties = new ArrayList<>();
645 Iterator<Entry<String, Object>> propertiesNameValue = jsonProperties.entrySet().iterator();
646 while (propertiesNameValue.hasNext()) {
647 Entry<String, Object> propertyNameValue = propertiesNameValue.next();
648 if (propertyNameValue.getValue() instanceof Map || propertyNameValue.getValue() instanceof List) {
649 if (!artifactType.equals(ArtifactTypeEnum.HEAT_ENV.getType())) {
650 @SuppressWarnings("unchecked") Either<HeatParameterDefinition, ResultStatusEnum> propertyStatus = createModuleHeatParameter(
651 (Map<String, Object>) propertyNameValue.getValue());
652 if (propertyStatus.isRight()) {
653 return Either.right(propertyStatus.right().value());
655 HeatParameterDefinition property = propertyStatus.left().value();
656 property.setName(String.valueOf(propertyNameValue.getKey()));
657 moduleProperties.add(property);
659 addHeatParamDefinition(moduleProperties, propertyNameValue, true);
662 addHeatParamDefinition(moduleProperties, propertyNameValue, false);
665 if (!isEmpty(moduleProperties)) {
666 eitherResult = Either.left(moduleProperties);
672 private static void addHeatParamDefinition(List<HeatParameterDefinition> moduleProperties, Entry<String, Object> propertyNameValue,
674 HeatParameterDefinition property = new HeatParameterDefinition();
675 Object value = propertyNameValue.getValue();
677 property.setDefaultValue(isJson ? new Gson().toJson(value) : StringEscapeUtils.escapeJava(String.valueOf(value)));
679 property.setName(String.valueOf(propertyNameValue.getKey()));
680 moduleProperties.add(property);
683 private static Either<HeatParameterDefinition, ResultStatusEnum> createModuleHeatParameter(Map<String, Object> propertyValue) {
684 HeatParameterDefinition propertyDef = new HeatParameterDefinition();
686 Either<String, ResultStatusEnum> propertyFieldType = findFirstToscaStringElement(propertyValue, TypeUtils.ToscaTagNamesEnum.TYPE);
687 if (propertyFieldType.isLeft()) {
688 type = propertyFieldType.left().value();
689 propertyDef.setType(type);
691 return Either.right(ResultStatusEnum.INVALID_PROPERTY_TYPE);
693 Either<String, ResultStatusEnum> propertyFieldDescription = findFirstToscaStringElement(propertyValue,
694 TypeUtils.ToscaTagNamesEnum.DESCRIPTION);
695 if (propertyFieldDescription.isLeft()) {
696 propertyDef.setDescription(propertyFieldDescription.left().value());
698 Either<Object, ResultStatusEnum> propertyFieldDefaultVal = findToscaElement(propertyValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE,
699 ToscaElementTypeEnum.ALL);
700 if (propertyFieldDefaultVal.isLeft()) {
701 if (propertyFieldDefaultVal.left().value() == null) {
702 return Either.right(ResultStatusEnum.INVALID_PROPERTY_VALUE);
704 Object value = propertyFieldDefaultVal.left().value();
705 String defaultValue =
706 type.equals(HeatParameterType.JSON.getType()) ? new Gson().toJson(value) : StringEscapeUtils.escapeJava(String.valueOf(value));
707 propertyDef.setDefaultValue(defaultValue);
708 propertyDef.setCurrentValue(defaultValue);
710 return Either.left(propertyDef);
713 public static boolean containsGetInput(Object propValue) {
714 String value = getPropertyJsonStringValue(propValue, ToscaPropertyType.MAP.getType());
715 return value != null && value.contains(TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName()) && !value.contains(TypeUtils.ToscaTagNamesEnum.CONCAT.getElementName());
718 public static String getPropertyJsonStringValue(Object value, String type) {
722 ToscaPropertyType validType = ToscaPropertyType.isValidType(type);
723 if (validType == null
724 || validType == ToscaPropertyType.JSON
725 || validType == ToscaPropertyType.MAP
726 || validType == ToscaPropertyType.LIST
727 || value instanceof Map) {
728 return (new Gson()).toJson(value);
730 return value.toString();
734 * removes from Json map (toscaJson) first element found by name (elementName) note that this method could update the received argument toscaJson
739 public static void removeElementFromJsonMap(Map<String, Object> toscaJson, String elementName) {
740 for (Entry<String, Object> entry : toscaJson.entrySet()) {
741 String key = entry.getKey();
742 Object value = entry.getValue();
743 if (key.equals(elementName)) {
744 toscaJson.remove(elementName);
746 } else if (value instanceof Map) {
747 removeElementFromJsonMap((Map<String, Object>) value, elementName);
752 public enum ResultStatusEnum {
753 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
756 public enum ToscaElementTypeEnum {
757 BOOLEAN, STRING, MAP, LIST, ALL
760 private static class CustomResolver extends Resolver {
763 protected void addImplicitResolvers() {
764 // avoid implicit resolvers for strings that can be interpreted as boolean values
765 addImplicitResolver(Tag.STR, EMPTY, "");
766 addImplicitResolver(Tag.STR, NULL, null);
767 addImplicitResolver(Tag.NULL, NULL, "~nN\0");
768 addImplicitResolver(Tag.NULL, EMPTY, null);
769 addImplicitResolver(Tag.INT, INT, "-+0123456789");
770 addImplicitResolver(Tag.FLOAT, FLOAT, "-+0123456789.");
771 addImplicitResolver(Tag.YAML, YAML, "!&*");
775 private static class YamlLoader extends YamlProcessor {
777 public Yaml getStrictYamlLoader() {
782 public static class Constants {
784 public static final String FIRST_NON_CERTIFIED_VERSION = "0.1";
785 public static final String VENDOR_NAME = "ONAP (Tosca)";
786 public static final String VENDOR_RELEASE = "1.0.0.wd03";
787 public static final LifecycleStateEnum NORMATIVE_TYPE_LIFE_CYCLE = LifecycleStateEnum.CERTIFIED;
788 public static final LifecycleStateEnum NORMATIVE_TYPE_LIFE_CYCLE_NOT_CERTIFIED_CHECKOUT = LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT;
789 public static final boolean NORMATIVE_TYPE_HIGHEST_VERSION = true;
790 public static final String ABSTRACT_CATEGORY_NAME = "Generic";
791 public static final String ABSTRACT_SUBCATEGORY = "Abstract";
792 public static final String DEFAULT_ICON = "defaulticon";
793 public static final String INNER_VFC_DESCRIPTION = "Not reusable inner VFC";
794 public static final String USER_DEFINED_RESOURCE_NAMESPACE_PREFIX = "org.openecomp.resource.";
795 public static final String UI_JSON_PAYLOAD_NAME = "payloadName";
796 public static final String CVFC_DESCRIPTION = "Complex node type that is used as nested type in VF";
797 public static final String ESCAPED_DOUBLE_QUOTE = "\"";
798 public static final String QUOTE = "'";
799 public static final String VF_DESCRIPTION = "Nested VF in service";
801 private Constants() {