* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* limitations under the License.
* ============LICENSE_END=========================================================
*/
-
package org.openecomp.sdc.be.components.impl;
-import java.util.ArrayList;
+import fj.data.Either;
+import java.util.Collections;
import java.util.List;
import java.util.Map;
-import java.util.Set;
-import java.util.function.Consumer;
+import java.util.Optional;
import java.util.stream.Collectors;
-
-import javax.annotation.Resource;
-
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
-import org.openecomp.sdc.be.components.impl.CommonImportManager.ElementTypeEnum;
-import org.openecomp.sdc.be.components.impl.ImportUtils.ToscaTagNamesEnum;
+import org.openecomp.sdc.be.components.impl.model.ToscaTypeImportData;
import org.openecomp.sdc.be.config.BeEcompErrorManager;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.impl.ComponentsUtils;
+import org.openecomp.sdc.be.model.CapabilityDefinition;
+import org.openecomp.sdc.be.model.ComponentInstanceProperty;
import org.openecomp.sdc.be.model.GroupTypeDefinition;
+import org.openecomp.sdc.be.model.Model;
import org.openecomp.sdc.be.model.PropertyDefinition;
-import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
-import org.openecomp.sdc.be.model.operations.api.IResourceOperation;
+import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
+import org.openecomp.sdc.be.model.normatives.ElementTypeEnum;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
-import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
+import org.openecomp.sdc.be.model.operations.impl.GroupTypeOperation;
+import org.openecomp.sdc.be.model.operations.impl.ModelOperation;
+import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
+import org.openecomp.sdc.be.model.utils.TypeCompareUtils;
+import org.openecomp.sdc.be.utils.TypeUtils;
+import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum;
+import org.openecomp.sdc.common.log.wrappers.Logger;
import org.openecomp.sdc.exception.ResponseFormat;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
-import fj.data.Either;
-
@Component("groupTypeImportManager")
public class GroupTypeImportManager {
- public static void main(String[] args) {
-
- List<PropertyDefinition> properties = new ArrayList<>();
- PropertyDefinition propertyDefintion = new PropertyDefinition();
- propertyDefintion.setName("aaa");
- properties.add(propertyDefintion);
-
- List<String> allParentsProps = new ArrayList<>();
- allParentsProps.add("aaa");
- allParentsProps.add("bbb");
-
- Set<String> alreadyExistPropsCollection = properties.stream().filter(p -> allParentsProps.contains(p.getName())).map(p -> p.getName()).collect(Collectors.toSet());
- System.out.println(alreadyExistPropsCollection);
-
- }
-
- private static Logger log = LoggerFactory.getLogger(GroupTypeImportManager.class.getName());
- @Resource
- private PropertyOperation propertyOperation;
- @Resource
- private IGroupTypeOperation groupTypeOperation;
- @Resource
- private ComponentsUtils componentsUtils;
- @Resource
- private IResourceOperation resourceOperation;
-
- @Resource
- private CommonImportManager commonImportManager;
-
- public Either<List<ImmutablePair<GroupTypeDefinition, Boolean>>, ResponseFormat> createGroupTypes(String groupTypesYml) {
- return commonImportManager.createElementTypes(groupTypesYml, elementTypeYml -> createGroupTypesFromYml(elementTypeYml), groupTypesList -> createGroupTypesByDao(groupTypesList), ElementTypeEnum.GroupType);
- }
-
- private Either<List<GroupTypeDefinition>, ActionStatus> createGroupTypesFromYml(String groupTypesYml) {
-
- return commonImportManager.createElementTypesFromYml(groupTypesYml, (groupTypeName, groupTypeJsonData) -> createGroupType(groupTypeName, groupTypeJsonData));
- }
-
- private Either<List<ImmutablePair<GroupTypeDefinition, Boolean>>, ResponseFormat> createGroupTypesByDao(List<GroupTypeDefinition> groupTypesToCreate) {
- return commonImportManager.createElementTypesByDao(groupTypesToCreate, groupType -> validateGroupType(groupType), groupType -> new ImmutablePair<>(ElementTypeEnum.GroupType, groupType.getType()),
- groupTypeName -> groupTypeOperation.getLatestGroupTypeByType(groupTypeName), groupType -> groupTypeOperation.addGroupType(groupType), null);
- }
-
- private Either<ActionStatus, ResponseFormat> validateGroupType(GroupTypeDefinition groupType) {
- Either<ActionStatus, ResponseFormat> result = Either.left(ActionStatus.OK);
- if (groupType.getMembers() != null) {
- if (groupType.getMembers().isEmpty()) {
- ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GROUP_MEMBER_EMPTY, groupType.getType());
- result = Either.right(responseFormat);
- } else {
- for (String member : groupType.getMembers()) {
- // Verify that such Resource exist
- Either<org.openecomp.sdc.be.model.Resource, StorageOperationStatus> eitherMemberExist = resourceOperation.getLatestByToscaResourceName(member, false);
- if (eitherMemberExist.isRight()) {
- StorageOperationStatus operationStatus = eitherMemberExist.right().value();
- log.debug("Error when fetching parent resource {}, error: {}", member, operationStatus);
- ActionStatus convertFromStorageResponse = componentsUtils.convertFromStorageResponse(operationStatus);
- BeEcompErrorManager.getInstance().logBeComponentMissingError("Import GroupType", "resource", member);
- result = Either.right(componentsUtils.getResponseFormat(convertFromStorageResponse, member));
- break;
- }
- }
-
- }
- }
- return result;
- }
-
- private GroupTypeDefinition createGroupType(String groupTypeName, Map<String, Object> toscaJson) {
-
- GroupTypeDefinition groupType = new GroupTypeDefinition();
-
- if (toscaJson != null) {
- // Description
- final Consumer<String> descriptionSetter = description -> groupType.setDescription(description);
- commonImportManager.setField(toscaJson, ToscaTagNamesEnum.DESCRIPTION.getElementName(), descriptionSetter);
- // Derived From
- final Consumer<String> derivedFromSetter = derivedFrom -> groupType.setDerivedFrom(derivedFrom);
- commonImportManager.setField(toscaJson, ToscaTagNamesEnum.DERIVED_FROM.getElementName(), derivedFromSetter);
- // Properties
- commonImportManager.setProperties(toscaJson, (values) -> groupType.setProperties(values));
- // Metadata
- final Consumer<Map<String, String>> metadataSetter = metadata -> groupType.setMetadata(metadata);
- commonImportManager.setField(toscaJson, ToscaTagNamesEnum.METADATA.getElementName(), metadataSetter);
- // Members
- final Consumer<List<String>> membersSetter = members -> groupType.setMembers(members);
- commonImportManager.setField(toscaJson, ToscaTagNamesEnum.MEMBERS.getElementName(), membersSetter);
-
- groupType.setType(groupTypeName);
-
- groupType.setHighestVersion(true);
-
- groupType.setVersion(ImportUtils.Constants.FIRST_CERTIFIED_VERSION_VERSION);
- }
- return groupType;
- }
-
+ private static final Logger log = Logger.getLogger(GroupTypeImportManager.class);
+ private final GroupTypeOperation groupTypeOperation;
+ private final ComponentsUtils componentsUtils;
+ private final ToscaOperationFacade toscaOperationFacade;
+ private final CommonImportManager commonImportManager;
+ private final ModelOperation modelOperation;
+
+ public GroupTypeImportManager(GroupTypeOperation groupTypeOperation, ComponentsUtils componentsUtils, ToscaOperationFacade toscaOperationFacade,
+ CommonImportManager commonImportManager, ModelOperation modelOperation) {
+ this.groupTypeOperation = groupTypeOperation;
+ this.componentsUtils = componentsUtils;
+ this.toscaOperationFacade = toscaOperationFacade;
+ this.commonImportManager = commonImportManager;
+ this.modelOperation = modelOperation;
+ }
+
+ public Either<List<ImmutablePair<GroupTypeDefinition, Boolean>>, ResponseFormat> createGroupTypes(ToscaTypeImportData toscaTypeImportData,
+ String modelName,
+ final boolean includeToModelDefaultImports) {
+ final Either<List<ImmutablePair<GroupTypeDefinition, Boolean>>, ResponseFormat> elementTypes = commonImportManager.createElementTypes(
+ toscaTypeImportData, this::createGroupTypesFromYml, this::upsertGroupTypesByDao, modelName);
+ if (elementTypes.isLeft() && includeToModelDefaultImports && StringUtils.isNotEmpty(modelName)) {
+ commonImportManager.addTypesToDefaultImports(ElementTypeEnum.GROUP_TYPE, toscaTypeImportData.getToscaTypesYml(), modelName);
+ }
+ return elementTypes;
+ }
+
+ private Either<List<GroupTypeDefinition>, ActionStatus> createGroupTypesFromYml(String groupTypesYml, String modelName) {
+ Either<List<GroupTypeDefinition>, ActionStatus> groupTypes = commonImportManager.createElementTypesFromYml(groupTypesYml,
+ this::createGroupType);
+ if (groupTypes.isLeft() && StringUtils.isNotEmpty(modelName)) {
+ final Optional<Model> modelOptional = modelOperation.findModelByName(modelName);
+ if (modelOptional.isPresent()) {
+ groupTypes.left().value().forEach(groupType -> groupType.setModel(modelName));
+ return groupTypes;
+ }
+ return Either.right(ActionStatus.INVALID_MODEL);
+ }
+ return groupTypes;
+ }
+
+ private Either<List<ImmutablePair<GroupTypeDefinition, Boolean>>, ResponseFormat> upsertGroupTypesByDao(
+ List<GroupTypeDefinition> groupTypesToCreate, String modelName) {
+ return commonImportManager.createElementTypesWithVersionByDao(groupTypesToCreate, this::validateGroupType,
+ groupType -> new ImmutablePair<>(ElementTypeEnum.GROUP_TYPE, UniqueIdBuilder.buildGroupTypeUid(groupType.getModel(),
+ groupType.getType(), groupType.getVersion())),
+ groupTypeOperation::getLatestGroupTypeByType,
+ groupTypeOperation::addGroupType, this::updateGroupType, modelName);
+ }
+
+ private Either<GroupTypeDefinition, StorageOperationStatus> updateGroupType(GroupTypeDefinition newGroupType, GroupTypeDefinition oldGroupType) {
+ Either<GroupTypeDefinition, StorageOperationStatus> validationRes = groupTypeOperation.validateUpdateProperties(newGroupType);
+ if (validationRes.isRight()) {
+ log.error("#updateGroupType - One or all properties of group type {} not valid. status is {}", newGroupType,
+ validationRes.right().value());
+ return validationRes;
+ }
+ if (TypeCompareUtils.isGroupTypesEquals(newGroupType, oldGroupType)) {
+ return TypeCompareUtils.typeAlreadyExists();
+ }
+ return groupTypeOperation.updateGroupType(newGroupType, oldGroupType);
+ }
+
+ private Either<ActionStatus, ResponseFormat> validateGroupType(GroupTypeDefinition groupType) {
+ if (groupType.getMembers() != null) {
+ if (groupType.getMembers().isEmpty()) {
+ return Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_MEMBER_EMPTY, groupType.getType()));
+ } else {
+ for (String member : groupType.getMembers()) {
+ // Verify that such Resource exist
+ Either<org.openecomp.sdc.be.model.Resource, StorageOperationStatus> eitherMemberExist = toscaOperationFacade
+ .getLatestByToscaResourceName(member, groupType.getModel());
+ if (eitherMemberExist.isRight()) {
+ StorageOperationStatus operationStatus = eitherMemberExist.right().value();
+ log.debug("Error when fetching parent resource {}, error: {}", member, operationStatus);
+ ActionStatus convertFromStorageResponse = componentsUtils.convertFromStorageResponse(operationStatus);
+ BeEcompErrorManager.getInstance().logBeComponentMissingError("Import GroupType", "resource", member);
+ return Either.right(componentsUtils.getResponseFormat(convertFromStorageResponse, member));
+ }
+ }
+ }
+ }
+ return Either.left(ActionStatus.OK);
+ }
+
+ private GroupTypeDefinition createGroupType(String groupTypeName, Map<String, Object> toscaJson) {
+ GroupTypeDefinition groupType = new GroupTypeDefinition();
+ if (toscaJson != null) {
+ // Description
+ commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName(), groupType::setDescription);
+ // Derived From
+ commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DERIVED_FROM.getElementName(), groupType::setDerivedFrom);
+ // Properties
+ CommonImportManager.setProperties(toscaJson, groupType::setProperties);
+ // Metadata
+ commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.METADATA.getElementName(), groupType::setMetadata);
+ // Capabilities
+ groupType.setCapabilities(createCapabilities(toscaJson));
+ // Members
+ commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.MEMBERS.getElementName(), groupType::setMembers);
+ groupType.setType(groupTypeName);
+ groupType.setHighestVersion(true);
+ groupType.setVersion(TypeUtils.getFirstCertifiedVersionVersion());
+ }
+ return groupType;
+ }
+
+ /**
+ * @param toscaJson
+ * @return
+ */
+ private Map<String, CapabilityDefinition> createCapabilities(Map<String, Object> toscaJson) {
+ CapabilityTypeToscaJsonHolder capabilityTypeToscaJsonHolder = new CapabilityTypeToscaJsonHolder();
+ commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.CAPABILITIES.getElementName(),
+ capabilityTypeToscaJsonHolder::setCapabilityTypeToscaJson);
+ Map<String, CapabilityDefinition> capabilities;
+ if (capabilityTypeToscaJsonHolder.isEmpty()) {
+ capabilities = Collections.emptyMap();
+ } else {
+ capabilities = commonImportManager
+ .createElementTypesMapFromToscaJsonMap(this::createCapability, capabilityTypeToscaJsonHolder.getCapabilityTypeToscaJson());
+ }
+ return capabilities;
+ }
+
+ private CapabilityDefinition createCapability(String capabilityName, Map<String, Object> toscaJson) {
+ CapabilityDefinition capability = new CapabilityDefinition();
+ capability.setName(capabilityName);
+ commonImportManager.setField(toscaJson, ToscaTagNamesEnum.TYPE.getElementName(), capability::setType);
+ // Properties
+ CommonImportManager.setProperties(toscaJson, pl -> capability.setProperties(map(pl)));
+ return capability;
+ }
+
+ /**
+ * @param pl
+ * @return
+ */
+ private List<ComponentInstanceProperty> map(List<PropertyDefinition> pl) {
+ return pl.stream().map(ComponentInstanceProperty::new).collect(Collectors.toList());
+ }
+
+ @NoArgsConstructor
+ @Getter
+ @Setter
+ private class CapabilityTypeToscaJsonHolder {
+
+ private Map<String, Object> capabilityTypeToscaJson;
+
+ public boolean isEmpty() {
+ return capabilityTypeToscaJson == null;
+ }
+ }
}