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;
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;
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.text.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.OutputDefinition;
60 import org.openecomp.sdc.be.model.PropertyConstraint;
61 import org.openecomp.sdc.be.model.PropertyDefinition;
62 import org.openecomp.sdc.be.model.heat.HeatParameterType;
63 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
64 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation.PropertyConstraintDeserialiser;
65 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
66 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
67 import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint;
68 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
69 import org.openecomp.sdc.be.utils.TypeUtils;
70 import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum;
71 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
72 import org.openecomp.sdc.common.log.wrappers.Logger;
73 import org.openecomp.sdc.exception.ResponseFormat;
74 import org.springframework.beans.factory.annotation.Autowired;
75 import org.springframework.beans.factory.config.YamlProcessor;
76 import org.springframework.stereotype.Component;
77 import org.yaml.snakeyaml.DumperOptions;
78 import org.yaml.snakeyaml.Yaml;
79 import org.yaml.snakeyaml.constructor.Constructor;
80 import org.yaml.snakeyaml.nodes.Tag;
81 import org.yaml.snakeyaml.representer.Representer;
82 import org.yaml.snakeyaml.resolver.Resolver;
85 public final class ImportUtils {
87 private static final CustomResolver customResolver = new CustomResolver();
88 private static final Yaml strictYamlLoader = new YamlLoader().getStrictYamlLoader();
89 private static final Logger log = Logger.getLogger(ImportUtils.class);
90 private static ComponentsUtils componentsUtils;
92 private ImportUtils() {
96 public static void setComponentsUtils(ComponentsUtils cu) {
100 private static void buildMap(Map<String, Object> output, Map<String, Object> map) {
101 for (Entry<String, Object> entry : map.entrySet()) {
102 String key = entry.getKey();
103 Object value = entry.getValue();
104 if (value instanceof Map) {
105 Map<String, Object> result = new LinkedHashMap<>();
106 buildMap(result, (Map) value);
107 output.put(key, result);
108 } else if (value instanceof Collection) {
109 Map<String, Object> result = new LinkedHashMap<>();
111 for (Object item : (Collection<Object>) value) {
112 buildMap(result, Collections.singletonMap("[" + (i++) + "]", item));
114 output.put(key, new ArrayList<>(result.values()));
116 output.put(key, value);
121 public static Map<String, Object> loadYamlAsStrictMap(String content) {
122 Map<String, Object> result = new LinkedHashMap<>();
123 Object map = strictYamlLoader.load(content);
124 buildMap(result, (Map<String, Object>) map);
128 @SuppressWarnings("unchecked")
129 public static Either<List<HeatParameterDefinition>, ResultStatusEnum> getHeatParamsWithoutImplicitTypes(String heatDecodedPayload,
130 String artifactType) {
131 Map<String, Object> heatData = (Map<String, Object>) new Yaml(new Constructor(), new Representer(), new DumperOptions(), customResolver)
132 .load(heatDecodedPayload);
133 return getHeatParameters(heatData, artifactType);
136 @SuppressWarnings("unchecked")
137 private static void handleElementNameNotFound(String elementName, Object elementValue, ToscaElementTypeEnum elementType,
138 List<Object> returnedList) {
139 if (elementValue instanceof Map) {
140 findToscaElements((Map<String, Object>) elementValue, elementName, elementType, returnedList);
141 } else if (elementValue instanceof List) {
142 findAllToscaElementsInList((List<Object>) elementValue, elementName, elementType, returnedList);
146 @SuppressWarnings("unchecked")
147 private static void addFoundElementAccordingToItsType(String elementName, ToscaElementTypeEnum elementType, List<Object> returnedList,
148 Object elementValue) {
149 if (elementValue instanceof Boolean) {
150 if (elementType == ToscaElementTypeEnum.BOOLEAN || elementType == ToscaElementTypeEnum.ALL) {
151 returnedList.add(elementValue);
153 } else if (elementValue instanceof String) {
154 if (elementType == ToscaElementTypeEnum.STRING || elementType == ToscaElementTypeEnum.ALL) {
155 returnedList.add(elementValue);
157 } else if (elementValue instanceof Map) {
158 if (elementType == ToscaElementTypeEnum.MAP || elementType == ToscaElementTypeEnum.ALL) {
159 returnedList.add(elementValue);
161 findToscaElements((Map<String, Object>) elementValue, elementName, elementType, returnedList);
162 } else if (elementValue instanceof List) {
163 if (elementType == ToscaElementTypeEnum.LIST || elementType == ToscaElementTypeEnum.ALL) {
164 returnedList.add(elementValue);
166 findAllToscaElementsInList((List<Object>) elementValue, elementName, elementType, returnedList);
168 // For Integer, Double etc...
169 else if (elementType == ToscaElementTypeEnum.ALL && elementValue != null) {
170 returnedList.add(String.valueOf(elementValue));
174 private static void findAllToscaElementsInList(List<Object> list, String elementName, ToscaElementTypeEnum elementType,
175 List<Object> returnedList) {
176 list.forEach(elementValue -> handleElementNameNotFound(elementName, elementValue, elementType, returnedList));
179 public static Either<Object, ResultStatusEnum> findToscaElement(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum elementName,
180 ToscaElementTypeEnum elementType) {
181 final var toscaElements = findToscaElements(toscaJson, elementName.getElementName(), elementType, new ArrayList<>());
182 if (toscaElements.isLeft() && CollectionUtils.isNotEmpty(toscaElements.left().value())) {
183 return Either.left(toscaElements.left().value().get(0));
185 return Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
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
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);
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);
209 if (!isEmpty(returnedList)) {
210 returnedElement = Either.left(returnedList);
212 return returnedElement;
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);
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());
230 return returnedElement;
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());
241 return returnedElement;
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());
251 return returnedElement;
255 * searches for first Tosca in Json map (toscaJson) boolean element by name (toscaTagName) returns found element or ELEMENT_NOT_FOUND status
258 * @param toscaTagName
261 private 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()));
268 return returnedElement;
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);
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();
283 final List<PropertyConstraint> constraintList = new ArrayList<>();
284 final Type constraintType = new TypeToken<PropertyConstraint>() {
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);
291 return constraintList;
294 private static List<Object> findCurrentLevelConstraintsElement(Map<String, Object> toscaJson) {
295 List<Object> constraints = null;
296 if (toscaJson.containsKey(TypeUtils.ToscaTagNamesEnum.CONSTRAINTS.getElementName())) {
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());
307 private static PropertyConstraint validateAndGetPropertyConstraint(String propertyType, Type constraintType, Gson gson, Object constraintJson) {
308 PropertyConstraint propertyConstraint;
310 propertyConstraint = gson.fromJson(gson.toJson(constraintJson), constraintType);
311 } catch (ClassCastException | JsonParseException e) {
312 throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS_FORMAT, constraintJson.toString());
314 if (propertyConstraint instanceof ValidValuesConstraint) {
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);
324 return propertyConstraint;
327 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);
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 setter.accept(getPropertyJsonStringValue(eitherValue.left().value(), type));
348 private static Annotation createModuleAnnotation(Map<String, Object> annotationMap, AnnotationTypeOperations annotationTypeOperations) {
349 String parsedAnnotationType = findFirstToscaStringElement(annotationMap, TypeUtils.ToscaTagNamesEnum.TYPE).left().value();
350 AnnotationTypeDefinition annotationTypeObject = annotationTypeOperations.getLatestType(parsedAnnotationType);
351 if (annotationTypeObject != null) {
352 Annotation annotation = new Annotation();
353 setField(annotationMap, TypeUtils.ToscaTagNamesEnum.TYPE, annotation::setType);
354 setField(annotationMap, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, annotation::setDescription);
355 Either<Map<String, PropertyDefinition>, ResultStatusEnum> properties = getProperties(annotationMap);
356 modifyPropertiesKeysToProperForm(properties, annotation);
362 private static Either<Boolean, ResponseFormat> modifyPropertiesKeysToProperForm(
363 Either<Map<String, PropertyDefinition>, ResultStatusEnum> properties, Annotation annotation) {
364 Either<Boolean, ResponseFormat> result = Either.left(true);
365 if (properties.isLeft()) {
366 List<PropertyDataDefinition> propertiesList = new ArrayList<>();
367 Map<String, PropertyDefinition> value = properties.left().value();
369 for (Entry<String, PropertyDefinition> entry : value.entrySet()) {
370 String name = entry.getKey();
371 if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(name).matches()) {
372 log.debug("The property with invalid name {} occurred upon import resource {}. ", name, annotation.getName());
373 result = Either.right(componentsUtils.getResponseFormat(
374 componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_PROPERTY_NAME, JsonPresentationFields.PROPERTY)));
376 PropertyDefinition propertyDefinition = entry.getValue();
377 propertyDefinition.setValue(propertyDefinition.getName());
378 propertyDefinition.setName(name);
379 propertiesList.add(propertyDefinition);
382 annotation.setProperties(propertiesList);
383 } else if (properties.right().value() != ResultStatusEnum.ELEMENT_NOT_FOUND) {
384 result = Either.right(componentsUtils
385 .getResponseFormat(componentsUtils.convertFromResultStatusEnum(properties.right().value(), JsonPresentationFields.PROPERTY)));
390 private static InputDefinition createModuleInput(final Map<String, Object> inputValue, final AnnotationTypeOperations annotationTypeOperations) {
391 return parseAnnotationsAndAddItToInput(createModuleInput(inputValue), inputValue, annotationTypeOperations);
394 private static InputDefinition createModuleInput(final Map<String, Object> inputValue) {
395 final InputDefinition inputDef = new InputDefinition();
396 setField(inputValue, TypeUtils.ToscaTagNamesEnum.TYPE, inputDef::setType);
397 setFieldBoolean(inputValue, ToscaTagNamesEnum.REQUIRED, req -> inputDef.setRequired(Boolean.parseBoolean(req)));
398 setField(inputValue, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, inputDef::setDescription);
399 setJsonStringField(inputValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, inputDef.getType(), inputDef::setDefaultValue);
400 setFieldBoolean(inputValue, TypeUtils.ToscaTagNamesEnum.IS_PASSWORD, pass -> inputDef.setPassword(Boolean.parseBoolean(pass)));
401 setField(inputValue, TypeUtils.ToscaTagNamesEnum.STATUS, inputDef::setStatus);
402 setField(inputValue, TypeUtils.ToscaTagNamesEnum.LABEL, inputDef::setLabel);
403 setFieldBoolean(inputValue, TypeUtils.ToscaTagNamesEnum.HIDDEN, hidden -> inputDef.setHidden(Boolean.parseBoolean(hidden)));
404 setFieldBoolean(inputValue, TypeUtils.ToscaTagNamesEnum.IMMUTABLE, immutable -> inputDef.setImmutable(Boolean.parseBoolean(immutable)));
405 setFieldMap(inputValue, ToscaTagNamesEnum.METADATA, inputDef::setMetadata);
406 setSchema(inputValue, inputDef);
407 setPropertyConstraints(inputValue, inputDef);
411 private static OutputDefinition createModuleOutput(final Map<String, Object> outputValue) {
412 final OutputDefinition outputDef = new OutputDefinition();
413 setField(outputValue, TypeUtils.ToscaTagNamesEnum.TYPE, outputDef::setType);
414 setField(outputValue, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, outputDef::setDescription);
415 setJsonStringField(outputValue, TypeUtils.ToscaTagNamesEnum.VALUE, outputDef.getType(), outputDef::setValue);
416 setField(outputValue, TypeUtils.ToscaTagNamesEnum.STATUS, outputDef::setStatus);
420 private static InputDefinition parseAnnotationsAndAddItToInput(InputDefinition inputDef, Map<String, Object> inputValue,
421 AnnotationTypeOperations annotationTypeOperations) {
422 Function<String, Annotation> elementGenByName = ImportUtils::createAnnotation;
423 Function<Map<String, Object>, Annotation> func = annotation -> createModuleAnnotation(annotation, annotationTypeOperations);
424 return getElements(inputValue, TypeUtils.ToscaTagNamesEnum.ANNOTATIONS, elementGenByName, func).left()
425 .map(annotations -> modifyInputWithAnnotations(inputDef, annotations)).left().on(err -> {
426 log.error("Parsing annotations or adding them to the PropertyDataDefinition object failed");
431 private static InputDefinition modifyInputWithAnnotations(InputDefinition inputDef, Map<String, Annotation> annotationsMap) {
432 setAnnotationsName(annotationsMap);
433 inputDef.setAnnotationsToInput(annotationsMap.values());
437 static AttributeDefinition createModuleAttribute(Map<String, Object> attributeMap) {
438 AttributeDefinition attributeDef = new AttributeDefinition();
439 setField(attributeMap, TypeUtils.ToscaTagNamesEnum.TYPE, attributeDef::setType);
440 setField(attributeMap, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, attributeDef::setDescription);
441 setField(attributeMap, TypeUtils.ToscaTagNamesEnum.STATUS, attributeDef::setStatus);
442 setJsonStringField(attributeMap, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, attributeDef.getType(), attributeDef::set_default);
443 setEntrySchema(attributeMap, attributeDef);
447 private static void setSchema(final Map<String, Object> propertyValue, final PropertyDefinition propertyDefinition) {
448 final Either<Object, ResultStatusEnum> schemaElementRes = findEntrySchemaElement(propertyValue);
449 if (schemaElementRes.isLeft()) {
450 propertyDefinition.setSchema(getSchema(schemaElementRes.left().value()));
454 private static void setEntrySchema(final Map<String, Object> toscaJsonMap, final AttributeDefinition attributeDefinition) {
455 final Either<Object, ResultStatusEnum> schemaElementRes = findEntrySchemaElement(toscaJsonMap);
456 if (schemaElementRes.isLeft()) {
457 attributeDefinition.setEntry_schema(createEntrySchema(schemaElementRes.left().value()));
461 private static Either<Object, ResultStatusEnum> findEntrySchemaElement(final Map<String, Object> propertyValue) {
462 return findToscaElement(propertyValue, TypeUtils.ToscaTagNamesEnum.ENTRY_SCHEMA, ToscaElementTypeEnum.ALL);
465 private static SchemaDefinition getSchema(Object propertyFieldEntryScheme) {
466 SchemaDefinition schema = new SchemaDefinition();
467 if (propertyFieldEntryScheme instanceof String) {
468 String schemaType = (String) propertyFieldEntryScheme;
469 PropertyDefinition schemeProperty = new PropertyDefinition();
470 schemeProperty.setType(schemaType);
471 schema.setProperty(schemeProperty);
472 } else if (propertyFieldEntryScheme instanceof Map) {
473 PropertyDefinition schemeProperty = createModuleProperty((Map<String, Object>) propertyFieldEntryScheme);
474 schema.setProperty(schemeProperty);
479 private static EntrySchema createEntrySchema(final Object toscaEntrySchemaObj) {
480 final EntrySchema entrySchema = new EntrySchema();
481 if (toscaEntrySchemaObj instanceof String) {
482 entrySchema.setType((String) toscaEntrySchemaObj);
483 } else if (toscaEntrySchemaObj instanceof Map) {
484 final PropertyDefinition schemeProperty = createModuleProperty((Map<String, Object>) toscaEntrySchemaObj);
485 entrySchema.setType(schemeProperty.getType());
486 entrySchema.setDescription(schemeProperty.getDescription());
491 private static void setField(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum tagName, Consumer<String> setter) {
492 Either<String, ResultStatusEnum> fieldStringValue = findFirstToscaStringElement(toscaJson, tagName);
493 if (fieldStringValue.isLeft()) {
494 setter.accept(fieldStringValue.left().value());
498 private static void setFieldBoolean(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum tagName, Consumer<String> setter) {
499 Either<String, ResultStatusEnum> fieldStringValue = findFirstToscaBooleanElement(toscaJson, tagName);
500 if (fieldStringValue.isLeft()) {
501 setter.accept(fieldStringValue.left().value());
505 private static void setFieldMap(final Map<String, Object> toscaJson, final ToscaTagNamesEnum tagName,
506 final Consumer<Map<String, String>> setter) {
507 final Either<Map<String, String>, ResultStatusEnum> toscaMapElement = findFirstToscaMapElement(toscaJson, tagName);
508 if (toscaMapElement.isLeft()) {
509 setter.accept(toscaMapElement.left().value());
513 public static Either<Map<String, PropertyDefinition>, ResultStatusEnum> getProperties(Map<String, Object> toscaJson) {
514 Function<String, PropertyDefinition> elementGenByName = ImportUtils::createProperties;
515 Function<Map<String, Object>, PropertyDefinition> func = ImportUtils::createModuleProperty;
516 return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.PROPERTIES, elementGenByName, func);
519 public static Either<Map<String, AttributeDefinition>, ResultStatusEnum> getAttributes(final Map<String, Object> toscaJson) {
520 final Function<String, AttributeDefinition> elementGenByName = ImportUtils::createAttribute;
521 final Function<Map<String, Object>, AttributeDefinition> func = ImportUtils::createModuleAttribute;
522 return getElements(toscaJson, ToscaTagNamesEnum.ATTRIBUTES, elementGenByName, func);
525 public static Either<Map<String, InputDefinition>, ResultStatusEnum> getInputs(Map<String, Object> toscaJson,
526 AnnotationTypeOperations annotationTypeOperations) {
527 Function<String, InputDefinition> elementGenByName = ImportUtils::createInputs;
528 Function<Map<String, Object>, InputDefinition> func = object -> createModuleInput(object, annotationTypeOperations);
529 return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.INPUTS, elementGenByName, func);
532 public static Either<Map<String, OutputDefinition>, ResultStatusEnum> getOutputs(Map<String, Object> toscaJson) {
533 final Function<String, OutputDefinition> elementGenByName = ImportUtils::createOutputs;
534 final Function<Map<String, Object>, OutputDefinition> func = ImportUtils::createModuleOutput;
535 return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.OUTPUTS, elementGenByName, func);
538 public static Either<Map<String, InputDefinition>, ResultStatusEnum> getInputs(final Map<String, Object> toscaJson) {
539 return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.INPUTS, ImportUtils::createInputs, ImportUtils::createModuleInput);
542 private static <T> Either<Map<String, T>, ResultStatusEnum> getElements(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum elementTagName,
543 Function<String, T> elementGenByName,
544 Function<Map<String, Object>, T> func) {
545 Either<Map<String, T>, ResultStatusEnum> eitherResult = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
546 Either<Map<String, Object>, ResultStatusEnum> toscaAttributes = findFirstToscaMapElement(toscaJson, elementTagName);
547 if (toscaAttributes.isLeft()) {
548 Map<String, Object> jsonAttributes = toscaAttributes.left().value();
549 Map<String, T> moduleAttributes = new HashMap<>();
550 Iterator<Entry<String, Object>> propertiesNameValue = jsonAttributes.entrySet().iterator();
551 while (propertiesNameValue.hasNext()) {
552 Entry<String, Object> attributeNameValue = propertiesNameValue.next();
553 if (attributeNameValue.getValue() instanceof Map) {
554 @SuppressWarnings("unchecked") T attribute = func.apply((Map<String, Object>) attributeNameValue.getValue());
555 if (attribute != null) {
556 moduleAttributes.put(String.valueOf(attributeNameValue.getKey()), attribute);
559 T element = elementGenByName.apply(String.valueOf(attributeNameValue.getValue()));
560 moduleAttributes.put(String.valueOf(attributeNameValue.getKey()), element);
563 if (moduleAttributes.size() > 0) {
564 eitherResult = Either.left(moduleAttributes);
570 private static AttributeDefinition createAttribute(String name) {
571 AttributeDefinition attribute = new AttributeDefinition();
572 attribute.setName(name);
576 private static PropertyDefinition createProperties(String name) {
577 PropertyDefinition property = new PropertyDefinition();
578 property.setDefaultValue(name);
579 property.setName(name);
583 private static InputDefinition createInputs(String name) {
584 InputDefinition input = new InputDefinition();
589 private static OutputDefinition createOutputs(final String name) {
590 final OutputDefinition output = new OutputDefinition();
591 output.setName(name);
595 private static Annotation createAnnotation(String name) {
596 Annotation annotation = new Annotation();
597 annotation.setName(name);
601 static Either<List<HeatParameterDefinition>, ResultStatusEnum> getHeatParameters(Map<String, Object> heatData, String artifactType) {
602 Either<List<HeatParameterDefinition>, ResultStatusEnum> eitherResult = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND);
603 Either<Map<String, Object>, ResultStatusEnum> toscaProperties = findFirstToscaMapElement(heatData, TypeUtils.ToscaTagNamesEnum.PARAMETERS);
604 if (toscaProperties.isLeft()) {
605 Map<String, Object> jsonProperties = toscaProperties.left().value();
606 List<HeatParameterDefinition> moduleProperties = new ArrayList<>();
607 Iterator<Entry<String, Object>> propertiesNameValue = jsonProperties.entrySet().iterator();
608 while (propertiesNameValue.hasNext()) {
609 Entry<String, Object> propertyNameValue = propertiesNameValue.next();
610 if (propertyNameValue.getValue() instanceof Map || propertyNameValue.getValue() instanceof List) {
611 if (!artifactType.equals(ArtifactTypeEnum.HEAT_ENV.getType())) {
612 @SuppressWarnings("unchecked") Either<HeatParameterDefinition, ResultStatusEnum> propertyStatus = createModuleHeatParameter(
613 (Map<String, Object>) propertyNameValue.getValue());
614 if (propertyStatus.isRight()) {
615 return Either.right(propertyStatus.right().value());
617 HeatParameterDefinition property = propertyStatus.left().value();
618 property.setName(String.valueOf(propertyNameValue.getKey()));
619 moduleProperties.add(property);
621 addHeatParamDefinition(moduleProperties, propertyNameValue, true);
624 addHeatParamDefinition(moduleProperties, propertyNameValue, false);
627 if (!isEmpty(moduleProperties)) {
628 eitherResult = Either.left(moduleProperties);
634 private static void addHeatParamDefinition(List<HeatParameterDefinition> moduleProperties, Entry<String, Object> propertyNameValue,
636 HeatParameterDefinition property = new HeatParameterDefinition();
637 Object value = propertyNameValue.getValue();
639 property.setDefaultValue(isJson ? new Gson().toJson(value) : StringEscapeUtils.escapeJava(String.valueOf(value)));
641 property.setName(String.valueOf(propertyNameValue.getKey()));
642 moduleProperties.add(property);
645 private static Either<HeatParameterDefinition, ResultStatusEnum> createModuleHeatParameter(Map<String, Object> propertyValue) {
646 HeatParameterDefinition propertyDef = new HeatParameterDefinition();
648 Either<String, ResultStatusEnum> propertyFieldType = findFirstToscaStringElement(propertyValue, TypeUtils.ToscaTagNamesEnum.TYPE);
649 if (propertyFieldType.isLeft()) {
650 type = propertyFieldType.left().value();
651 propertyDef.setType(type);
653 return Either.right(ResultStatusEnum.INVALID_PROPERTY_TYPE);
655 Either<String, ResultStatusEnum> propertyFieldDescription = findFirstToscaStringElement(propertyValue,
656 TypeUtils.ToscaTagNamesEnum.DESCRIPTION);
657 if (propertyFieldDescription.isLeft()) {
658 propertyDef.setDescription(propertyFieldDescription.left().value());
660 Either<Object, ResultStatusEnum> propertyFieldDefaultVal = findToscaElement(propertyValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE,
661 ToscaElementTypeEnum.ALL);
662 if (propertyFieldDefaultVal.isLeft()) {
663 if (propertyFieldDefaultVal.left().value() == null) {
664 return Either.right(ResultStatusEnum.INVALID_PROPERTY_VALUE);
666 Object value = propertyFieldDefaultVal.left().value();
667 String defaultValue =
668 type.equals(HeatParameterType.JSON.getType()) ? new Gson().toJson(value) : StringEscapeUtils.escapeJava(String.valueOf(value));
669 propertyDef.setDefaultValue(defaultValue);
670 propertyDef.setCurrentValue(defaultValue);
672 return Either.left(propertyDef);
675 public static boolean containsGetInput(Object propValue) {
676 String value = getPropertyJsonStringValue(propValue, ToscaPropertyType.MAP.getType());
677 return value != null && value.contains(TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
680 public static String getPropertyJsonStringValue(Object value, String type) {
684 ToscaPropertyType validType = ToscaPropertyType.isValidType(type);
685 if (validType == null
686 || validType == ToscaPropertyType.JSON
687 || validType == ToscaPropertyType.MAP
688 || validType == ToscaPropertyType.LIST
689 || value instanceof Map) {
690 return (new Gson()).toJson(value);
692 return value.toString();
696 * removes from Json map (toscaJson) first element found by name (elementName) note that this method could update the received argument toscaJson
701 public static void removeElementFromJsonMap(Map<String, Object> toscaJson, String elementName) {
702 for (Entry<String, Object> entry : toscaJson.entrySet()) {
703 String key = entry.getKey();
704 Object value = entry.getValue();
705 if (key.equals(elementName)) {
706 toscaJson.remove(elementName);
708 } else if (value instanceof Map) {
709 removeElementFromJsonMap((Map<String, Object>) value, elementName);
714 public enum ResultStatusEnum {
715 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
718 public enum ToscaElementTypeEnum {
719 BOOLEAN, STRING, MAP, LIST, ALL
722 private static class CustomResolver extends Resolver {
725 protected void addImplicitResolvers() {
726 // avoid implicit resolvers for strings that can be interpreted as boolean values
727 addImplicitResolver(Tag.STR, EMPTY, "");
728 addImplicitResolver(Tag.STR, NULL, null);
729 addImplicitResolver(Tag.NULL, NULL, "~nN\0");
730 addImplicitResolver(Tag.NULL, EMPTY, null);
731 addImplicitResolver(Tag.INT, INT, "-+0123456789");
732 addImplicitResolver(Tag.FLOAT, FLOAT, "-+0123456789.");
733 addImplicitResolver(Tag.YAML, YAML, "!&*");
737 private static class YamlLoader extends YamlProcessor {
739 public Yaml getStrictYamlLoader() {
744 public static class Constants {
746 public static final String FIRST_NON_CERTIFIED_VERSION = "0.1";
747 public static final String VENDOR_NAME = "ONAP (Tosca)";
748 public static final String VENDOR_RELEASE = "1.0.0.wd03";
749 public static final LifecycleStateEnum NORMATIVE_TYPE_LIFE_CYCLE = LifecycleStateEnum.CERTIFIED;
750 public static final LifecycleStateEnum NORMATIVE_TYPE_LIFE_CYCLE_NOT_CERTIFIED_CHECKOUT = LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT;
751 public static final boolean NORMATIVE_TYPE_HIGHEST_VERSION = true;
752 public static final String ABSTRACT_CATEGORY_NAME = "Generic";
753 public static final String ABSTRACT_SUBCATEGORY = "Abstract";
754 public static final String DEFAULT_ICON = "defaulticon";
755 public static final String INNER_VFC_DESCRIPTION = "Not reusable inner VFC";
756 public static final String USER_DEFINED_RESOURCE_NAMESPACE_PREFIX = "org.openecomp.resource.";
757 public static final String UI_JSON_PAYLOAD_NAME = "payloadName";
758 public static final String CVFC_DESCRIPTION = "Complex node type that is used as nested type in VF";
759 public static final String ESCAPED_DOUBLE_QUOTE = "\"";
760 public static final String QUOTE = "'";
761 public static final String VF_DESCRIPTION = "Nested VF in service";
763 private Constants() {