diff options
Diffstat (limited to 'catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java')
-rw-r--r-- | catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java | 362 |
1 files changed, 134 insertions, 228 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java index ed26eade32..f53726743c 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java @@ -17,7 +17,6 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.be.components.impl; import static org.apache.commons.collections.CollectionUtils.isEmpty; @@ -86,10 +85,8 @@ public final class ImportUtils { private static final CustomResolver customResolver = new CustomResolver(); private static final Yaml strictYamlLoader = new YamlLoader().getStrictYamlLoader(); - - private static ComponentsUtils componentsUtils; - private static final Logger log = Logger.getLogger(ImportUtils.class); + private static ComponentsUtils componentsUtils; private ImportUtils() { } @@ -99,21 +96,6 @@ public final class ImportUtils { componentsUtils = componentsUtils; } - private static class CustomResolver extends Resolver { - - @Override - protected void addImplicitResolvers() { - // avoid implicit resolvers for strings that can be interpreted as boolean values - addImplicitResolver(Tag.STR, EMPTY, ""); - addImplicitResolver(Tag.STR, NULL, null); - addImplicitResolver(Tag.NULL, NULL, "~nN\0"); - addImplicitResolver(Tag.NULL, EMPTY, null); - addImplicitResolver(Tag.INT, INT, "-+0123456789"); - addImplicitResolver(Tag.FLOAT, FLOAT, "-+0123456789."); - addImplicitResolver(Tag.YAML, YAML, "!&*"); - } - } - private static void buildMap(Map<String, Object> output, Map<String, Object> map) { for (Entry<String, Object> entry : map.entrySet()) { String key = entry.getKey(); @@ -142,56 +124,17 @@ public final class ImportUtils { return result; } - private static class YamlLoader extends YamlProcessor { - - public Yaml getStrictYamlLoader() { - return createYaml(); - } - } - @SuppressWarnings("unchecked") - public static Either<List<HeatParameterDefinition>, ResultStatusEnum> getHeatParamsWithoutImplicitTypes( - String heatDecodedPayload, String artifactType) { - Map<String, Object> heatData = (Map<String, Object>) new Yaml(new Constructor(), new Representer(), - new DumperOptions(), customResolver).load(heatDecodedPayload); + public static Either<List<HeatParameterDefinition>, ResultStatusEnum> getHeatParamsWithoutImplicitTypes(String heatDecodedPayload, + String artifactType) { + Map<String, Object> heatData = (Map<String, Object>) new Yaml(new Constructor(), new Representer(), new DumperOptions(), customResolver) + .load(heatDecodedPayload); return getHeatParameters(heatData, artifactType); } - public static class Constants { - - public static final String FIRST_NON_CERTIFIED_VERSION = "0.1"; - public static final String VENDOR_NAME = "ONAP (Tosca)"; - public static final String VENDOR_RELEASE = "1.0.0.wd03"; - public static final LifecycleStateEnum NORMATIVE_TYPE_LIFE_CYCLE = LifecycleStateEnum.CERTIFIED; - public static final LifecycleStateEnum NORMATIVE_TYPE_LIFE_CYCLE_NOT_CERTIFIED_CHECKOUT = LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT; - public static final boolean NORMATIVE_TYPE_HIGHEST_VERSION = true; - public static final String ABSTRACT_CATEGORY_NAME = "Generic"; - public static final String ABSTRACT_SUBCATEGORY = "Abstract"; - public static final String DEFAULT_ICON = "defaulticon"; - public static final String INNER_VFC_DESCRIPTION = "Not reusable inner VFC"; - public static final String USER_DEFINED_RESOURCE_NAMESPACE_PREFIX = "org.openecomp.resource."; - public static final String UI_JSON_PAYLOAD_NAME = "payloadName"; - public static final String CVFC_DESCRIPTION = "Complex node type that is used as nested type in VF"; - public static final String ESCAPED_DOUBLE_QUOTE = "\""; - public static final String QUOTE = "'"; - public static final String VF_DESCRIPTION = "Nested VF in service"; - - private Constants() { - } - } - - public enum ResultStatusEnum { - 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 - } - - public enum ToscaElementTypeEnum { - BOOLEAN, STRING, MAP, LIST, ALL - } - @SuppressWarnings("unchecked") - private static void handleElementNameNotFound(String elementName, Object elementValue, - ToscaElementTypeEnum elementType, List<Object> returnedList) { + private static void handleElementNameNotFound(String elementName, Object elementValue, ToscaElementTypeEnum elementType, + List<Object> returnedList) { if (elementValue instanceof Map) { findToscaElements((Map<String, Object>) elementValue, elementName, elementType, returnedList); } else if (elementValue instanceof List) { @@ -200,9 +143,8 @@ public final class ImportUtils { } @SuppressWarnings("unchecked") - private static void addFoundElementAccordingToItsType(String elementName, ToscaElementTypeEnum elementType, - List<Object> returnedList, Object elementValue) { - + private static void addFoundElementAccordingToItsType(String elementName, ToscaElementTypeEnum elementType, List<Object> returnedList, + Object elementValue) { if (elementValue instanceof Boolean) { if (elementType == ToscaElementTypeEnum.BOOLEAN || elementType == ToscaElementTypeEnum.ALL) { returnedList.add(elementValue); @@ -216,13 +158,11 @@ public final class ImportUtils { returnedList.add(elementValue); } findToscaElements((Map<String, Object>) elementValue, elementName, elementType, returnedList); - } else if (elementValue instanceof List) { if (elementType == ToscaElementTypeEnum.LIST || elementType == ToscaElementTypeEnum.ALL) { returnedList.add(elementValue); } findAllToscaElementsInList((List<Object>) elementValue, elementName, elementType, returnedList); - } // For Integer, Double etc... else if (elementType == ToscaElementTypeEnum.ALL && elementValue != null) { @@ -230,13 +170,12 @@ public final class ImportUtils { } } - private static void findAllToscaElementsInList(List<Object> list, String elementName, - ToscaElementTypeEnum elementType, List<Object> returnedList) { + private static void findAllToscaElementsInList(List<Object> list, String elementName, ToscaElementTypeEnum elementType, + List<Object> returnedList) { list.forEach(elementValue -> handleElementNameNotFound(elementName, elementValue, elementType, returnedList)); } - public static Either<Object, ResultStatusEnum> findToscaElement(Map<String, Object> toscaJson, - TypeUtils.ToscaTagNamesEnum elementName, + public static Either<Object, ResultStatusEnum> findToscaElement(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum elementName, ToscaElementTypeEnum elementType) { List<Object> foundElements = new ArrayList<>(); findToscaElements(toscaJson, elementName.getElementName(), elementType, foundElements); @@ -247,22 +186,19 @@ public final class ImportUtils { } /** - * Recursively searches for all tosca elements with key equals to elementName and value equals to elementType. <br> - * Returns Either element with:<br> List with all value if values found<br> Or ELEMENT_NOT_FOUND ActionStatus + * Recursively searches for all tosca elements with key equals to elementName and value equals to elementType. <br> Returns Either element + * with:<br> List with all value if values found<br> Or ELEMENT_NOT_FOUND ActionStatus * * @param toscaJson * @return */ - public static Either<List<Object>, ResultStatusEnum> findToscaElements(Map<String, Object> toscaJson, - String elementName, - ToscaElementTypeEnum elementType, - List<Object> returnedList) { + public static Either<List<Object>, ResultStatusEnum> findToscaElements(Map<String, Object> toscaJson, String elementName, + ToscaElementTypeEnum elementType, List<Object> returnedList) { Either<List<Object>, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); String skipKey = null; if (toscaJson.containsKey(elementName)) { skipKey = handleFoundElement(toscaJson, elementName, elementType, returnedList); } - Iterator<Entry<String, Object>> keyValItr = toscaJson.entrySet().iterator(); while (keyValItr.hasNext()) { Entry<String, Object> keyValEntry = keyValItr.next(); @@ -270,53 +206,45 @@ public final class ImportUtils { handleElementNameNotFound(elementName, keyValEntry.getValue(), elementType, returnedList); } } - if (!isEmpty(returnedList)) { returnedElement = Either.left(returnedList); } - return returnedElement; } - private static String handleFoundElement(Map<String, Object> toscaJson, String elementName, - ToscaElementTypeEnum elementType, List<Object> returnedList) { + private static String handleFoundElement(Map<String, Object> toscaJson, String elementName, ToscaElementTypeEnum elementType, + List<Object> returnedList) { Object elementValue = toscaJson.get(elementName); addFoundElementAccordingToItsType(elementName, elementType, returnedList, elementValue); return elementName; - } @SuppressWarnings("unchecked") public static <T> Either<List<T>, ResultStatusEnum> findFirstToscaListElement(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum toscaTagName) { Either<List<T>, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); - Either<Object, ResultStatusEnum> findFirstToscaElement = findToscaElement(toscaJson, toscaTagName, - ToscaElementTypeEnum.LIST); + Either<Object, ResultStatusEnum> findFirstToscaElement = findToscaElement(toscaJson, toscaTagName, ToscaElementTypeEnum.LIST); if (findFirstToscaElement.isLeft()) { returnedElement = Either.left((List<T>) findFirstToscaElement.left().value()); } return returnedElement; - } @SuppressWarnings("unchecked") public static <T> Either<Map<String, T>, ResultStatusEnum> findFirstToscaMapElement(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum toscaTagName) { Either<Map<String, T>, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); - Either<Object, ResultStatusEnum> findFirstToscaElement = findToscaElement(toscaJson, toscaTagName, - ToscaElementTypeEnum.MAP); + Either<Object, ResultStatusEnum> findFirstToscaElement = findToscaElement(toscaJson, toscaTagName, ToscaElementTypeEnum.MAP); if (findFirstToscaElement.isLeft()) { returnedElement = Either.left((Map<String, T>) findFirstToscaElement.left().value()); } return returnedElement; - } public static Either<String, ResultStatusEnum> findFirstToscaStringElement(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum toscaTagName) { Either<String, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); - Either<Object, ResultStatusEnum> findFirstToscaElements = findToscaElement(toscaJson, toscaTagName, - ToscaElementTypeEnum.STRING); + Either<Object, ResultStatusEnum> findFirstToscaElements = findToscaElement(toscaJson, toscaTagName, ToscaElementTypeEnum.STRING); if (findFirstToscaElements.isLeft()) { returnedElement = Either.left((String) findFirstToscaElements.left().value()); } @@ -324,8 +252,7 @@ public final class ImportUtils { } /** - * searches for first Tosca in Json map (toscaJson) boolean element by name (toscaTagName) returns found element or - * ELEMENT_NOT_FOUND status + * searches for first Tosca in Json map (toscaJson) boolean element by name (toscaTagName) returns found element or ELEMENT_NOT_FOUND status * * @param toscaJson * @param toscaTagName @@ -334,8 +261,7 @@ public final class ImportUtils { public static Either<String, ResultStatusEnum> findFirstToscaBooleanElement(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum toscaTagName) { Either<String, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); - Either<Object, ResultStatusEnum> findFirstToscaElements = findToscaElement(toscaJson, toscaTagName, - ToscaElementTypeEnum.BOOLEAN); + Either<Object, ResultStatusEnum> findFirstToscaElements = findToscaElement(toscaJson, toscaTagName, ToscaElementTypeEnum.BOOLEAN); if (findFirstToscaElements.isLeft()) { returnedElement = Either.left(String.valueOf(findFirstToscaElements.left().value())); } @@ -349,8 +275,7 @@ public final class ImportUtils { } } - private static List<PropertyConstraint> getPropertyConstraints(final Map<String, Object> propertyValue, - final String propertyType) { + private static List<PropertyConstraint> getPropertyConstraints(final Map<String, Object> propertyValue, final String propertyType) { final List<Object> propertyFieldConstraints = findCurrentLevelConstraintsElement(propertyValue); if (CollectionUtils.isEmpty(propertyFieldConstraints)) { return Collections.emptyList(); @@ -358,12 +283,9 @@ public final class ImportUtils { final List<PropertyConstraint> constraintList = new ArrayList<>(); final Type constraintType = new TypeToken<PropertyConstraint>() { }.getType(); - final Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintDeserialiser()) - .create(); - + final Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintDeserialiser()).create(); for (final Object constraintJson : propertyFieldConstraints) { - final PropertyConstraint propertyConstraint = validateAndGetPropertyConstraint(propertyType, constraintType, - gson, constraintJson); + final PropertyConstraint propertyConstraint = validateAndGetPropertyConstraint(propertyType, constraintType, gson, constraintJson); constraintList.add(propertyConstraint); } return constraintList; @@ -380,26 +302,22 @@ public final class ImportUtils { } } return constraints; - } - private static PropertyConstraint validateAndGetPropertyConstraint(String propertyType, Type constraintType, - Gson gson, Object constraintJson) { + private static PropertyConstraint validateAndGetPropertyConstraint(String propertyType, Type constraintType, Gson gson, Object constraintJson) { PropertyConstraint propertyConstraint; try { propertyConstraint = gson.fromJson(gson.toJson(constraintJson), constraintType); } catch (ClassCastException | JsonParseException e) { - throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS_FORMAT, - constraintJson.toString()); + throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS_FORMAT, constraintJson.toString()); } if (propertyConstraint != null && propertyConstraint instanceof ValidValuesConstraint) { try { ((ValidValuesConstraint) propertyConstraint).validateType(propertyType); } catch (ConstraintValueDoNotMatchPropertyTypeException e) { - BeEcompErrorManager.getInstance().logInternalFlowError("GetInitializedPropertyConstraint", - e.getMessage(), BeEcompErrorManager.ErrorSeverity.ERROR); - throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS, - ConstraintType.VALID_VALUES.name(), + BeEcompErrorManager.getInstance() + .logInternalFlowError("GetInitializedPropertyConstraint", e.getMessage(), BeEcompErrorManager.ErrorSeverity.ERROR); + throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS, ConstraintType.VALID_VALUES.name(), ((ValidValuesConstraint) propertyConstraint).getValidValues().toString(), propertyType); } } @@ -407,41 +325,29 @@ public final class ImportUtils { } public static PropertyDefinition createModuleProperty(Map<String, Object> propertyValue) { - PropertyDefinition propertyDef = new PropertyDefinition(); setField(propertyValue, TypeUtils.ToscaTagNamesEnum.TYPE, propertyDef::setType); - setFieldBoolean(propertyValue, ToscaTagNamesEnum.REQUIRED, - req -> propertyDef.setRequired(Boolean.parseBoolean(req))); + setFieldBoolean(propertyValue, ToscaTagNamesEnum.REQUIRED, req -> propertyDef.setRequired(Boolean.parseBoolean(req))); setField(propertyValue, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, propertyDef::setDescription); - - setJsonStringField(propertyValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, propertyDef.getType(), - propertyDef::setDefaultValue); - setJsonStringField(propertyValue, TypeUtils.ToscaTagNamesEnum.VALUE, propertyDef.getType(), - propertyDef::setValue); - - setFieldBoolean(propertyValue, TypeUtils.ToscaTagNamesEnum.IS_PASSWORD, - pass -> propertyDef.setPassword(Boolean.parseBoolean(pass))); + setJsonStringField(propertyValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, propertyDef.getType(), propertyDef::setDefaultValue); + setJsonStringField(propertyValue, TypeUtils.ToscaTagNamesEnum.VALUE, propertyDef.getType(), propertyDef::setValue); + setFieldBoolean(propertyValue, TypeUtils.ToscaTagNamesEnum.IS_PASSWORD, pass -> propertyDef.setPassword(Boolean.parseBoolean(pass))); setField(propertyValue, TypeUtils.ToscaTagNamesEnum.STATUS, propertyDef::setStatus); setSchema(propertyValue, propertyDef); setPropertyConstraints(propertyValue, propertyDef); - return propertyDef; } - private static void setJsonStringField(Map<String, Object> propertyValue, ToscaTagNamesEnum elementName, - String type, Consumer<String> setter) { - Either<Object, ResultStatusEnum> eitherValue = findToscaElement(propertyValue, elementName, - ToscaElementTypeEnum.ALL); + private static void setJsonStringField(Map<String, Object> propertyValue, ToscaTagNamesEnum elementName, String type, Consumer<String> setter) { + Either<Object, ResultStatusEnum> eitherValue = findToscaElement(propertyValue, elementName, ToscaElementTypeEnum.ALL); if (eitherValue.isLeft()) { String propertyJsonStringValue = getPropertyJsonStringValue(eitherValue.left().value(), type); setter.accept(propertyJsonStringValue); } } - public static Annotation createModuleAnnotation(Map<String, Object> annotationMap, - AnnotationTypeOperations annotationTypeOperations) { - String parsedAnnotationType = findFirstToscaStringElement(annotationMap, TypeUtils.ToscaTagNamesEnum.TYPE) - .left().value(); + public static Annotation createModuleAnnotation(Map<String, Object> annotationMap, AnnotationTypeOperations annotationTypeOperations) { + String parsedAnnotationType = findFirstToscaStringElement(annotationMap, TypeUtils.ToscaTagNamesEnum.TYPE).left().value(); AnnotationTypeDefinition annotationTypeObject = annotationTypeOperations.getLatestType(parsedAnnotationType); if (annotationTypeObject != null) { Annotation annotation = new Annotation(); @@ -464,11 +370,9 @@ public final class ImportUtils { for (Entry<String, PropertyDefinition> entry : value.entrySet()) { String name = entry.getKey(); if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(name).matches()) { - log.debug("The property with invalid name {} occurred upon import resource {}. ", name, - annotation.getName()); - result = Either.right(componentsUtils.getResponseFormat(componentsUtils - .convertFromResultStatusEnum(ResultStatusEnum.INVALID_PROPERTY_NAME, - JsonPresentationFields.PROPERTY))); + log.debug("The property with invalid name {} occurred upon import resource {}. ", name, annotation.getName()); + result = Either.right(componentsUtils.getResponseFormat( + componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_PROPERTY_NAME, JsonPresentationFields.PROPERTY))); } PropertyDefinition propertyDefinition = entry.getValue(); propertyDefinition.setValue(propertyDefinition.getName()); @@ -478,16 +382,13 @@ public final class ImportUtils { } annotation.setProperties(propertiesList); } else if (properties.right().value() != ResultStatusEnum.ELEMENT_NOT_FOUND) { - result = Either - .right(componentsUtils.getResponseFormat(componentsUtils.convertFromResultStatusEnum(properties - .right() - .value(), JsonPresentationFields.PROPERTY))); + result = Either.right(componentsUtils + .getResponseFormat(componentsUtils.convertFromResultStatusEnum(properties.right().value(), JsonPresentationFields.PROPERTY))); } return result; } - public static InputDefinition createModuleInput(final Map<String, Object> inputValue, - final AnnotationTypeOperations annotationTypeOperations) { + public static InputDefinition createModuleInput(final Map<String, Object> inputValue, final AnnotationTypeOperations annotationTypeOperations) { return parseAnnotationsAndAddItToInput(createModuleInput(inputValue), inputValue, annotationTypeOperations); } @@ -496,66 +397,52 @@ public final class ImportUtils { setField(inputValue, TypeUtils.ToscaTagNamesEnum.TYPE, inputDef::setType); setFieldBoolean(inputValue, ToscaTagNamesEnum.REQUIRED, req -> inputDef.setRequired(Boolean.parseBoolean(req))); setField(inputValue, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, inputDef::setDescription); - - setJsonStringField(inputValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, inputDef.getType(), - inputDef::setDefaultValue); - + setJsonStringField(inputValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, inputDef.getType(), inputDef::setDefaultValue); setFieldBoolean(inputValue, TypeUtils.ToscaTagNamesEnum.IS_PASSWORD, pass -> inputDef.setPassword(Boolean.parseBoolean(pass))); setField(inputValue, TypeUtils.ToscaTagNamesEnum.STATUS, inputDef::setStatus); setField(inputValue, TypeUtils.ToscaTagNamesEnum.LABEL, inputDef::setLabel); setFieldBoolean(inputValue, TypeUtils.ToscaTagNamesEnum.HIDDEN, hidden -> inputDef.setHidden(Boolean.parseBoolean(hidden))); setFieldBoolean(inputValue, TypeUtils.ToscaTagNamesEnum.IMMUTABLE, immutable -> inputDef.setImmutable(Boolean.parseBoolean(immutable))); - setSchema(inputValue, inputDef); setPropertyConstraints(inputValue, inputDef); return inputDef; } - public static InputDefinition parseAnnotationsAndAddItToInput(InputDefinition inputDef, - Map<String, Object> inputValue, + public static InputDefinition parseAnnotationsAndAddItToInput(InputDefinition inputDef, Map<String, Object> inputValue, AnnotationTypeOperations annotationTypeOperations) { Function<String, Annotation> elementGenByName = ImportUtils::createAnnotation; - Function<Map<String, Object>, Annotation> func = annotation -> createModuleAnnotation(annotation, - annotationTypeOperations); - return getElements(inputValue, TypeUtils.ToscaTagNamesEnum.ANNOTATIONS, elementGenByName, func). - left().map(annotations -> modifyInputWithAnnotations(inputDef, annotations)). - left().on(err -> { - log.error("Parsing annotations or adding them to the PropertyDataDefinition object failed"); - return inputDef; - }); - } - - private static InputDefinition modifyInputWithAnnotations(InputDefinition inputDef, - Map<String, Annotation> annotationsMap) { + Function<Map<String, Object>, Annotation> func = annotation -> createModuleAnnotation(annotation, annotationTypeOperations); + return getElements(inputValue, TypeUtils.ToscaTagNamesEnum.ANNOTATIONS, elementGenByName, func).left() + .map(annotations -> modifyInputWithAnnotations(inputDef, annotations)).left().on(err -> { + log.error("Parsing annotations or adding them to the PropertyDataDefinition object failed"); + return inputDef; + }); + } + + private static InputDefinition modifyInputWithAnnotations(InputDefinition inputDef, Map<String, Annotation> annotationsMap) { setAnnotationsName(annotationsMap); inputDef.setAnnotationsToInput(annotationsMap.values()); return inputDef; } public static AttributeDefinition createModuleAttribute(Map<String, Object> attributeMap) { - AttributeDefinition attributeDef = new AttributeDefinition(); setField(attributeMap, TypeUtils.ToscaTagNamesEnum.TYPE, attributeDef::setType); setField(attributeMap, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, attributeDef::setDescription); setField(attributeMap, TypeUtils.ToscaTagNamesEnum.STATUS, attributeDef::setStatus); - - setJsonStringField(attributeMap, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, attributeDef.getType(), - attributeDef::set_default); - + setJsonStringField(attributeMap, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, attributeDef.getType(), attributeDef::set_default); setEntrySchema(attributeMap, attributeDef); return attributeDef; } - private static void setSchema(final Map<String, Object> propertyValue, - final PropertyDefinition propertyDefinition) { + private static void setSchema(final Map<String, Object> propertyValue, final PropertyDefinition propertyDefinition) { final Either<Object, ResultStatusEnum> schemaElementRes = findEntrySchemaElement(propertyValue); if (schemaElementRes.isLeft()) { propertyDefinition.setSchema(getSchema(schemaElementRes.left().value())); } } - private static void setEntrySchema(final Map<String, Object> toscaJsonMap, - final AttributeDefinition attributeDefinition) { + private static void setEntrySchema(final Map<String, Object> toscaJsonMap, final AttributeDefinition attributeDefinition) { final Either<Object, ResultStatusEnum> schemaElementRes = findEntrySchemaElement(toscaJsonMap); if (schemaElementRes.isLeft()) { attributeDefinition.setEntry_schema(createEntrySchema(schemaElementRes.left().value())); @@ -592,62 +479,48 @@ public final class ImportUtils { return entrySchema; } - private static void setField(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum tagName, - Consumer<String> setter) { + private static void setField(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum tagName, Consumer<String> setter) { Either<String, ResultStatusEnum> fieldStringValue = findFirstToscaStringElement(toscaJson, tagName); if (fieldStringValue.isLeft()) { setter.accept(fieldStringValue.left().value()); } - } - public static void setFieldBoolean(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum tagName, - Consumer<String> setter) { + public static void setFieldBoolean(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum tagName, Consumer<String> setter) { Either<String, ResultStatusEnum> fieldStringValue = findFirstToscaBooleanElement(toscaJson, tagName); if (fieldStringValue.isLeft()) { setter.accept(fieldStringValue.left().value()); } - } - public static Either<Map<String, PropertyDefinition>, ResultStatusEnum> getProperties( - Map<String, Object> toscaJson) { + public static Either<Map<String, PropertyDefinition>, ResultStatusEnum> getProperties(Map<String, Object> toscaJson) { Function<String, PropertyDefinition> elementGenByName = ImportUtils::createProperties; Function<Map<String, Object>, PropertyDefinition> func = ImportUtils::createModuleProperty; - return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.PROPERTIES, elementGenByName, func); - } public static Either<Map<String, AttributeDefinition>, ResultStatusEnum> getAttributes(final Map<String, Object> toscaJson) { final Function<String, AttributeDefinition> elementGenByName = ImportUtils::createAttribute; final Function<Map<String, Object>, AttributeDefinition> func = ImportUtils::createModuleAttribute; - return getElements(toscaJson, ToscaTagNamesEnum.ATTRIBUTES, elementGenByName, func); } public static Either<Map<String, InputDefinition>, ResultStatusEnum> getInputs(Map<String, Object> toscaJson, AnnotationTypeOperations annotationTypeOperations) { Function<String, InputDefinition> elementGenByName = ImportUtils::createInputs; - Function<Map<String, Object>, InputDefinition> func = object -> createModuleInput(object, - annotationTypeOperations); - + Function<Map<String, Object>, InputDefinition> func = object -> createModuleInput(object, annotationTypeOperations); return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.INPUTS, elementGenByName, func); - } public static Either<Map<String, InputDefinition>, ResultStatusEnum> getInputs(final Map<String, Object> toscaJson) { - return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.INPUTS, ImportUtils::createInputs, - ImportUtils::createModuleInput); + return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.INPUTS, ImportUtils::createInputs, ImportUtils::createModuleInput); } - public static <T> Either<Map<String, T>, ResultStatusEnum> getElements(Map<String, Object> toscaJson, - TypeUtils.ToscaTagNamesEnum elementTagName, + public static <T> Either<Map<String, T>, ResultStatusEnum> getElements(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum elementTagName, Function<String, T> elementGenByName, Function<Map<String, Object>, T> func) { Either<Map<String, T>, ResultStatusEnum> eitherResult = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); - Either<Map<String, Object>, ResultStatusEnum> toscaAttributes = findFirstToscaMapElement(toscaJson, - elementTagName); + Either<Map<String, Object>, ResultStatusEnum> toscaAttributes = findFirstToscaMapElement(toscaJson, elementTagName); if (toscaAttributes.isLeft()) { Map<String, Object> jsonAttributes = toscaAttributes.left().value(); Map<String, T> moduleAttributes = new HashMap<>(); @@ -655,8 +528,7 @@ public final class ImportUtils { while (propertiesNameValue.hasNext()) { Entry<String, Object> attributeNameValue = propertiesNameValue.next(); if (attributeNameValue.getValue() instanceof Map) { - @SuppressWarnings("unchecked") - T attribute = func.apply((Map<String, Object>) attributeNameValue.getValue()); + @SuppressWarnings("unchecked") T attribute = func.apply((Map<String, Object>) attributeNameValue.getValue()); if (attribute != null) { moduleAttributes.put(String.valueOf(attributeNameValue.getKey()), attribute); } @@ -668,15 +540,12 @@ public final class ImportUtils { if (moduleAttributes.size() > 0) { eitherResult = Either.left(moduleAttributes); } - } return eitherResult; - } private static AttributeDefinition createAttribute(String name) { AttributeDefinition attribute = new AttributeDefinition(); - attribute.setName(name); return attribute; } @@ -690,7 +559,6 @@ public final class ImportUtils { private static InputDefinition createInputs(String name) { InputDefinition input = new InputDefinition(); - input.setName(name); return input; } @@ -702,11 +570,8 @@ public final class ImportUtils { } public static Either<List<HeatParameterDefinition>, ResultStatusEnum> getHeatParameters(Map<String, Object> heatData, String artifactType) { - - Either<List<HeatParameterDefinition>, ResultStatusEnum> eitherResult = Either - .right(ResultStatusEnum.ELEMENT_NOT_FOUND); - Either<Map<String, Object>, ResultStatusEnum> toscaProperties = findFirstToscaMapElement(heatData, - TypeUtils.ToscaTagNamesEnum.PARAMETERS); + Either<List<HeatParameterDefinition>, ResultStatusEnum> eitherResult = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); + Either<Map<String, Object>, ResultStatusEnum> toscaProperties = findFirstToscaMapElement(heatData, TypeUtils.ToscaTagNamesEnum.PARAMETERS); if (toscaProperties.isLeft()) { Map<String, Object> jsonProperties = toscaProperties.left().value(); List<HeatParameterDefinition> moduleProperties = new ArrayList<>(); @@ -715,8 +580,7 @@ public final class ImportUtils { Entry<String, Object> propertyNameValue = propertiesNameValue.next(); if (propertyNameValue.getValue() instanceof Map || propertyNameValue.getValue() instanceof List) { if (!artifactType.equals(ArtifactTypeEnum.HEAT_ENV.getType())) { - @SuppressWarnings("unchecked") - Either<HeatParameterDefinition, ResultStatusEnum> propertyStatus = createModuleHeatParameter( + @SuppressWarnings("unchecked") Either<HeatParameterDefinition, ResultStatusEnum> propertyStatus = createModuleHeatParameter( (Map<String, Object>) propertyNameValue.getValue()); if (propertyStatus.isRight()) { return Either.right(propertyStatus.right().value()); @@ -730,36 +594,29 @@ public final class ImportUtils { } else { addHeatParamDefinition(moduleProperties, propertyNameValue, false); } - } - if (!isEmpty(moduleProperties)) { eitherResult = Either.left(moduleProperties); } - } return eitherResult; - } - private static void addHeatParamDefinition(List<HeatParameterDefinition> moduleProperties, - Entry<String, Object> propertyNameValue, boolean isJson) { + private static void addHeatParamDefinition(List<HeatParameterDefinition> moduleProperties, Entry<String, Object> propertyNameValue, + boolean isJson) { HeatParameterDefinition property = new HeatParameterDefinition(); Object value = propertyNameValue.getValue(); if (value != null) { - property.setDefaultValue( - isJson ? new Gson().toJson(value) : StringEscapeUtils.escapeJava(String.valueOf(value))); + property.setDefaultValue(isJson ? new Gson().toJson(value) : StringEscapeUtils.escapeJava(String.valueOf(value))); } property.setName(String.valueOf(propertyNameValue.getKey())); moduleProperties.add(property); } - private static Either<HeatParameterDefinition, ResultStatusEnum> createModuleHeatParameter( - Map<String, Object> propertyValue) { + private static Either<HeatParameterDefinition, ResultStatusEnum> createModuleHeatParameter(Map<String, Object> propertyValue) { HeatParameterDefinition propertyDef = new HeatParameterDefinition(); String type; - Either<String, ResultStatusEnum> propertyFieldType = findFirstToscaStringElement(propertyValue, - TypeUtils.ToscaTagNamesEnum.TYPE); + Either<String, ResultStatusEnum> propertyFieldType = findFirstToscaStringElement(propertyValue, TypeUtils.ToscaTagNamesEnum.TYPE); if (propertyFieldType.isLeft()) { type = propertyFieldType.left().value(); propertyDef.setType(type); @@ -771,20 +628,18 @@ public final class ImportUtils { if (propertyFieldDescription.isLeft()) { propertyDef.setDescription(propertyFieldDescription.left().value()); } - - Either<Object, ResultStatusEnum> propertyFieldDefaultVal = findToscaElement(propertyValue, - TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, ToscaElementTypeEnum.ALL); + Either<Object, ResultStatusEnum> propertyFieldDefaultVal = findToscaElement(propertyValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, + ToscaElementTypeEnum.ALL); if (propertyFieldDefaultVal.isLeft()) { if (propertyFieldDefaultVal.left().value() == null) { return Either.right(ResultStatusEnum.INVALID_PROPERTY_VALUE); } Object value = propertyFieldDefaultVal.left().value(); - String defaultValue = type.equals(HeatParameterType.JSON.getType()) ? new Gson().toJson(value) - : StringEscapeUtils.escapeJava(String.valueOf(value)); + String defaultValue = + type.equals(HeatParameterType.JSON.getType()) ? new Gson().toJson(value) : StringEscapeUtils.escapeJava(String.valueOf(value)); propertyDef.setDefaultValue(defaultValue); propertyDef.setCurrentValue(defaultValue); } - return Either.left(propertyDef); } @@ -799,16 +654,14 @@ public final class ImportUtils { return null; } ToscaPropertyType validType = ToscaPropertyType.isValidType(type); - if (validType == null || validType == ToscaPropertyType.JSON || validType == ToscaPropertyType.MAP - || validType == ToscaPropertyType.LIST) { + if (validType == null || validType == ToscaPropertyType.JSON || validType == ToscaPropertyType.MAP || validType == ToscaPropertyType.LIST) { return gson.toJson(value); } return value.toString(); } /** - * removes from Json map (toscaJson) first element found by name (elementName) note that this method could update - * the received argument toscaJson + * removes from Json map (toscaJson) first element found by name (elementName) note that this method could update the received argument toscaJson * * @param toscaJson * @param elementName @@ -825,4 +678,57 @@ public final class ImportUtils { } } } + + public enum ResultStatusEnum { + 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 + } + + public enum ToscaElementTypeEnum { + BOOLEAN, STRING, MAP, LIST, ALL + } + + private static class CustomResolver extends Resolver { + + @Override + protected void addImplicitResolvers() { + // avoid implicit resolvers for strings that can be interpreted as boolean values + addImplicitResolver(Tag.STR, EMPTY, ""); + addImplicitResolver(Tag.STR, NULL, null); + addImplicitResolver(Tag.NULL, NULL, "~nN\0"); + addImplicitResolver(Tag.NULL, EMPTY, null); + addImplicitResolver(Tag.INT, INT, "-+0123456789"); + addImplicitResolver(Tag.FLOAT, FLOAT, "-+0123456789."); + addImplicitResolver(Tag.YAML, YAML, "!&*"); + } + } + + private static class YamlLoader extends YamlProcessor { + + public Yaml getStrictYamlLoader() { + return createYaml(); + } + } + + public static class Constants { + + public static final String FIRST_NON_CERTIFIED_VERSION = "0.1"; + public static final String VENDOR_NAME = "ONAP (Tosca)"; + public static final String VENDOR_RELEASE = "1.0.0.wd03"; + public static final LifecycleStateEnum NORMATIVE_TYPE_LIFE_CYCLE = LifecycleStateEnum.CERTIFIED; + public static final LifecycleStateEnum NORMATIVE_TYPE_LIFE_CYCLE_NOT_CERTIFIED_CHECKOUT = LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT; + public static final boolean NORMATIVE_TYPE_HIGHEST_VERSION = true; + public static final String ABSTRACT_CATEGORY_NAME = "Generic"; + public static final String ABSTRACT_SUBCATEGORY = "Abstract"; + public static final String DEFAULT_ICON = "defaulticon"; + public static final String INNER_VFC_DESCRIPTION = "Not reusable inner VFC"; + public static final String USER_DEFINED_RESOURCE_NAMESPACE_PREFIX = "org.openecomp.resource."; + public static final String UI_JSON_PAYLOAD_NAME = "payloadName"; + public static final String CVFC_DESCRIPTION = "Complex node type that is used as nested type in VF"; + public static final String ESCAPED_DOUBLE_QUOTE = "\""; + public static final String QUOTE = "'"; + public static final String VF_DESCRIPTION = "Nested VF in service"; + + private Constants() { + } + } } |