import static org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementOperation.createDataType;
import static org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementOperation.createDataTypeDefinitionWithName;
+import fj.data.Either;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
-
import javax.servlet.ServletContext;
-
import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.openecomp.sdc.be.config.BeEcompErrorManager;
import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
import org.openecomp.sdc.be.dao.api.ActionStatus;
+import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
+import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
+import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
+import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
+import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
+import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
+import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
import org.openecomp.sdc.be.model.CapabilityDefinition;
import org.openecomp.sdc.be.model.ComponentInstanceProperty;
import org.openecomp.sdc.be.model.DataTypeDefinition;
+import org.openecomp.sdc.be.model.DefaultUploadResourceInfo;
import org.openecomp.sdc.be.model.InterfaceDefinition;
import org.openecomp.sdc.be.model.LifecycleStateEnum;
+import org.openecomp.sdc.be.model.NodeTypesMetadataList;
import org.openecomp.sdc.be.model.PropertyDefinition;
import org.openecomp.sdc.be.model.RequirementDefinition;
import org.openecomp.sdc.be.model.Resource;
+import org.openecomp.sdc.be.model.UploadComponentInstanceInfo;
+import org.openecomp.sdc.be.model.UploadInterfaceInfo;
import org.openecomp.sdc.be.model.UploadResourceInfo;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.category.CategoryDefinition;
import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
+import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
+import org.openecomp.sdc.be.model.mapper.NodeTypeMetadataMapper;
import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.model.operations.impl.CapabilityTypeOperation;
import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo;
import org.openecomp.sdc.be.utils.TypeUtils;
import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum;
+import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.openecomp.sdc.common.util.ThreadLocalsHolder;
import org.openecomp.sdc.common.util.ValidationUtils;
import org.openecomp.sdc.exception.ResponseFormat;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.yaml.snakeyaml.Yaml;
-import fj.data.Either;
-
-@Component("resourceImportManager")
+@org.springframework.stereotype.Component("resourceImportManager")
public class ResourceImportManager {
- static final Pattern PROPERTY_NAME_PATTERN_IGNORE_LENGTH = Pattern.compile("[\\w\\-\\_\\d\\:]+");
+ static final Pattern PROPERTY_NAME_PATTERN_IGNORE_LENGTH = Pattern.compile("['\\w\\s\\-\\.\\:]+");
private static final Logger log = Logger.getLogger(ResourceImportManager.class);
private final InterfaceDefinitionHandler interfaceDefinitionHandler;
private final ComponentsUtils componentsUtils;
private final CapabilityTypeOperation capabilityTypeOperation;
+ private final JanusGraphDao janusGraphDao;
private ServletContext servletContext;
private AuditingManager auditingManager;
private ResourceBusinessLogic resourceBusinessLogic;
private ResponseFormatManager responseFormatManager;
@Autowired
- public ResourceImportManager(ComponentsUtils componentsUtils, CapabilityTypeOperation capabilityTypeOperation,
- final InterfaceDefinitionHandler interfaceDefinitionHandler) {
+ public ResourceImportManager(final ComponentsUtils componentsUtils, final CapabilityTypeOperation capabilityTypeOperation,
+ final InterfaceDefinitionHandler interfaceDefinitionHandler, final JanusGraphDao janusGraphDao) {
this.componentsUtils = componentsUtils;
this.capabilityTypeOperation = capabilityTypeOperation;
this.interfaceDefinitionHandler = interfaceDefinitionHandler;
+ this.janusGraphDao = janusGraphDao;
}
public ServiceBusinessLogic getServiceBusinessLogic() {
this.toscaOperationFacade = toscaOperationFacade;
}
- public ImmutablePair<Resource, ActionStatus> importNormativeResource(String resourceYml, UploadResourceInfo resourceMetaData, User creator,
- boolean createNewVersion, boolean needLock) {
+ public ImmutablePair<Resource, ActionStatus> importNormativeResource(final String resourceYml, final UploadResourceInfo resourceMetaData,
+ final Map<String, UploadComponentInstanceInfo> instancesFromCsar,
+ final User creator, final boolean createNewVersion, final boolean needLock,
+ final boolean isInTransaction) {
LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction();
lifecycleChangeInfo.setUserRemarks("certification on import");
Function<Resource, Boolean> validator = resource -> resourceBusinessLogic.validatePropertiesDefaultValues(resource);
- return importCertifiedResource(resourceYml, resourceMetaData, creator, validator, lifecycleChangeInfo, false, createNewVersion, needLock,
- null, null, false, null, null, false);
+ return importCertifiedResource(resourceYml, resourceMetaData, creator, validator, lifecycleChangeInfo, isInTransaction, createNewVersion,
+ needLock, null, null, false, null, null, false, instancesFromCsar);
+ }
+
+ public void importAllNormativeResource(final String resourcesYaml, final NodeTypesMetadataList nodeTypesMetadataList, final User user,
+ final boolean createNewVersion, final boolean needLock) {
+ final Map<String, Object> nodeTypesYamlMap;
+ try {
+ nodeTypesYamlMap = new Yaml().load(resourcesYaml);
+ } catch (final Exception e) {
+ log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ResourceImportManager.class.getName(), "Could not parse node types YAML", e);
+ throw new ByActionStatusComponentException(ActionStatus.INVALID_NODE_TYPES_YAML);
+ }
+ if (!nodeTypesYamlMap.containsKey(ToscaTagNamesEnum.NODE_TYPES.getElementName())) {
+ return;
+ }
+ final Map<String, Object> nodeTypesMap = (Map<String, Object>) nodeTypesYamlMap.get(ToscaTagNamesEnum.NODE_TYPES.getElementName());
+ importAllNormativeResource(nodeTypesMap, nodeTypesMetadataList, null, user, "", createNewVersion,needLock);
+ }
+
+ public void importAllNormativeResource(final Map<String, Object> nodeTypesMap, final NodeTypesMetadataList nodeTypesMetadataList,
+ Map<String, UploadComponentInstanceInfo> instancesFromCsar, final User user, String model, final boolean createNewVersion, final boolean needLock) {
+ try {
+ nodeTypesMetadataList.getNodeMetadataList().forEach(nodeTypeMetadata -> {
+ final String nodeTypeToscaName = nodeTypeMetadata.getToscaName();
+ final Map<String, Object> nodeTypeMap = (Map<String, Object>) nodeTypesMap.get(nodeTypeToscaName);
+ if (nodeTypeMap == null) {
+ log.warn(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ResourceImportManager.class.getName(),
+ "Could not find given node type '{}'. The node will not be created.", nodeTypeToscaName);
+ } else {
+ final Map<String, Map<String, Map<String, Object>>> nodeTypeDefinitionMap =
+ Map.of(ToscaTagNamesEnum.NODE_TYPES.getElementName(),
+ Map.of(nodeTypeToscaName, nodeTypeMap)
+ );
+ final String nodeTypeYaml = new Yaml().dump(nodeTypeDefinitionMap);
+ UploadResourceInfo uploadResourceInfo = NodeTypeMetadataMapper.mapTo(nodeTypeMetadata);
+ if (uploadResourceInfo instanceof DefaultUploadResourceInfo) {
+ uploadResourceInfo.setModel(model);
+ uploadResourceInfo.setContactId(user.getUserId());
+ }
+ importNormativeResource(nodeTypeYaml, uploadResourceInfo, instancesFromCsar, user, createNewVersion, needLock, true);
+ }
+ });
+ janusGraphDao.commit();
+ } catch (final Exception e) {
+ janusGraphDao.rollback();
+ throw e;
+ }
}
public ImmutablePair<Resource, ActionStatus> importNormativeResourceFromCsar(String resourceYml, UploadResourceInfo resourceMetaData,
lifecycleChangeInfo.setUserRemarks("certification on import");
Function<Resource, Boolean> validator = resource -> resourceBusinessLogic.validatePropertiesDefaultValues(resource);
return importCertifiedResource(resourceYml, resourceMetaData, creator, validator, lifecycleChangeInfo, false, createNewVersion, needLock,
- null, null, false, null, null, false);
+ null, null, false, null, null, false, null);
}
public ImmutablePair<Resource, ActionStatus> importCertifiedResource(String resourceYml, UploadResourceInfo resourceMetaData, User creator,
Map<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle,
List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
boolean forceCertificationAllowed, CsarInfo csarInfo, String nodeName,
- boolean isNested) {
+ boolean isNested, Map<String, UploadComponentInstanceInfo> instancesFromCsar) {
Resource resource = new Resource();
ImmutablePair<Resource, ActionStatus> responsePair = new ImmutablePair<>(resource, ActionStatus.CREATED);
Either<ImmutablePair<Resource, ActionStatus>, ResponseFormat> response = Either.left(responsePair);
try {
boolean shouldBeCertified = nodeTypeArtifactsToHandle == null || nodeTypeArtifactsToHandle.isEmpty();
setConstantMetaData(resource, shouldBeCertified);
- setMetaDataFromJson(resourceMetaData, resource);
- populateResourceFromYaml(resourceYml, resource);
+ setResourceMetaData(resource, resourceYml, resourceMetaData);
+ populateResourceFromYaml(resourceYml, resource, instancesFromCsar);
validationFunction.apply(resource);
+ resource.getComponentMetadataDefinition().getMetadataDataDefinition().setNormative(resourceMetaData.isNormative());
checkResourceExists(createNewVersion, csarInfo, resource);
resource = resourceBusinessLogic
.createOrUpdateResourceByImport(resource, creator, true, isInTransaction, needLock, csarInfo, nodeName, isNested).left;
}
final Either<Resource, StorageOperationStatus> resourceEither =
toscaOperationFacade.getComponentByNameAndVendorRelease(resource.getComponentType(), resource.getName(),
- resource.getVendorRelease(), JsonParseFlagEnum.ParseAll);
+ resource.getVendorRelease(), JsonParseFlagEnum.ParseAll, resource.getModel());
if (resourceEither.isLeft() && toscaOperationFacade.isNodeAssociatedToModel(resource.getModel(), resource)) {
if (resource.getModel() == null) {
throw new ByActionStatusComponentException(ActionStatus.COMPONENT_WITH_VENDOR_RELEASE_ALREADY_EXISTS,
final Either<Resource, StorageOperationStatus> latestByName = toscaOperationFacade.getLatestByName(resource.getName(), model);
if (latestByName.isLeft() && toscaOperationFacade.isNodeAssociatedToModel(model, resource)) {
if (model == null) {
- throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, resource.getName());
+ throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NAME_ALREADY_EXIST,
+ resource.getResourceType().name(), resource.getName());
}
throw new ByActionStatusComponentException(ActionStatus.COMPONENT_WITH_MODEL_ALREADY_EXIST, resource.getName(), model);
}
resource.setIcon(resourceMetaData.getResourceIconPath());
resource.setResourceVendorModelNumber(resourceMetaData.getResourceVendorModelNumber());
resource.setResourceType(ResourceTypeEnum.valueOf(resourceMetaData.getResourceType()));
+ resource.setTenant(resourceMetaData.getTenant());
if (resourceMetaData.getVendorName() != null) {
resource.setVendorName(resourceMetaData.getVendorName());
}
ImmutablePair<Resource, ActionStatus> responsePair = new ImmutablePair<>(resource, ActionStatus.CREATED);
try {
setMetaDataFromJson(resourceMetaData, resource);
- populateResourceFromYaml(resourceYml, resource);
- // currently import VF isn't supported. In future will be supported
-
- // import VF only with CSAR file!!
+ populateResourceFromYaml(resourceYml, resource, null);
+ // currently import VF isn't supported. In future will be supported import VF only with CSAR file!!
if (ResourceTypeEnum.VF == resource.getResourceType()) {
log.debug("Now import VF isn't supported. It will be supported in future with CSAR file only");
throw new ByActionStatusComponentException(ActionStatus.RESTRICTED_OPERATION);
}
resourceBusinessLogic.validateDerivedFromNotEmpty(creator, resource, AuditingActionEnum.CREATE_RESOURCE);
- Boolean validatePropertiesTypes = resourceBusinessLogic.validatePropertiesDefaultValues(resource);
+ resourceBusinessLogic.validatePropertiesDefaultValues(resource);
responsePair = resourceBusinessLogic.createOrUpdateResourceByImport(resource, creator, false, isInTransaction, true, null, null, false);
} catch (RuntimeException e) {
handleImportResourceException(resourceMetaData, creator, false, e);
return responsePair;
}
- private void populateResourceFromYaml(String resourceYml, Resource resource) {
+ private void populateResourceFromYaml(final String resourceYml, Resource resource, Map<String, UploadComponentInstanceInfo> instancesFromCsar) {
@SuppressWarnings("unchecked") Object ymlObj = new Yaml().load(resourceYml);
if (ymlObj instanceof Map) {
- Map<String, Object> toscaJsonAll = (Map<String, Object>) ymlObj;
+ final Either<Resource, StorageOperationStatus> existingResource = getExistingResource(resource);
+ final Map<String, Object> toscaJsonAll = (Map<String, Object>) ymlObj;
Map<String, Object> toscaJson = toscaJsonAll;
- // Checks if exist and builds the node_types map
- if (toscaJsonAll.containsKey(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName())
- && resource.getResourceType() != ResourceTypeEnum.CVFC) {
+ if (toscaJsonAll.containsKey(ToscaTagNamesEnum.NODE_TYPES.getElementName()) && resource.getResourceType() != ResourceTypeEnum.CVFC) {
toscaJson = new HashMap<>();
- toscaJson.put(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName(),
- toscaJsonAll.get(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName()));
+ toscaJson.put(ToscaTagNamesEnum.NODE_TYPES.getElementName(), toscaJsonAll.get(ToscaTagNamesEnum.NODE_TYPES.getElementName()));
}
final List<Object> foundElements = new ArrayList<>();
final Either<List<Object>, ResultStatusEnum> toscaElements = ImportUtils
if (toscaElements.isLeft()) {
final Map<String, Object> toscaAttributes = (Map<String, Object>) foundElements.get(0);
if (MapUtils.isNotEmpty(toscaAttributes)) {
- resource.setDataTypes(extractDataTypeFromJson(resourceBusinessLogic, toscaAttributes));
+ resource.setDataTypes(extractDataTypeFromJson(resourceBusinessLogic, toscaAttributes, resource.getModel()));
}
}
- // Derived From
- Resource parentResource = setDerivedFrom(toscaJson, resource);
+ final Resource parentResource = setDerivedFrom(toscaJson, resource);
if (StringUtils.isEmpty(resource.getToscaResourceName())) {
setToscaResourceName(toscaJson, resource);
}
setCapabilities(toscaJson, resource, parentResource);
- setProperties(toscaJson, resource);
+ setProperties(toscaJson, resource, existingResource);
setAttributes(toscaJson, resource);
setRequirements(toscaJson, resource, parentResource);
- setInterfaceLifecycle(toscaJson, resource);
+ setInterfaceLifecycle(toscaJson, resource, existingResource, instancesFromCsar);
} else {
throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
}
}
+ private Either<Resource, StorageOperationStatus> getExistingResource(final Resource resource) {
+ final Either<List<GraphVertex>, JanusGraphOperationStatus> byCriteria = janusGraphDao.getByCriteria(
+ getVertexTypeEnum(resource.getResourceType()), propertiesToMatch(resource), propertiesToNotMatch(),
+ JsonParseFlagEnum.ParseAll, resource.getModel(), false);
+ if (byCriteria.isLeft() && CollectionUtils.isNotEmpty(byCriteria.left().value())) {
+ final List<GraphVertex> graphVertexList = byCriteria.left().value();
+ if (graphVertexList.size() == 1) {
+ return toscaOperationFacade.getToscaElement(graphVertexList.get(0).getUniqueId());
+ } else {
+ final Optional<GraphVertex> vertex = graphVertexList.stream()
+ .max(Comparator.comparing(graphVertex -> (String) graphVertex.getMetadataProperties().get(GraphPropertyEnum.VERSION)));
+ if (vertex.isPresent()) {
+ return toscaOperationFacade.getToscaElement(vertex.get().getUniqueId());
+ }
+ }
+ }
+ return Either.right(StorageOperationStatus.NOT_FOUND);
+ }
+
+ private VertexTypeEnum getVertexTypeEnum(final ResourceTypeEnum resourceType) {
+ return ModelConverter.isAtomicComponent(resourceType) ? VertexTypeEnum.NODE_TYPE : VertexTypeEnum.TOPOLOGY_TEMPLATE;
+ }
+
+ private Map<GraphPropertyEnum, Object> propertiesToMatch(final Resource resource) {
+ final Map<GraphPropertyEnum, Object> graphProperties = new EnumMap<>(GraphPropertyEnum.class);
+ graphProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normaliseComponentName(resource.getName()));
+ graphProperties.put(GraphPropertyEnum.COMPONENT_TYPE, resource.getComponentType().name());
+ graphProperties.put(GraphPropertyEnum.RESOURCE_TYPE, resource.getResourceType().name());
+ graphProperties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
+ return graphProperties;
+ }
+
+ private Map<GraphPropertyEnum, Object> propertiesToNotMatch() {
+ final Map<GraphPropertyEnum, Object> graphProperties = new EnumMap<>(GraphPropertyEnum.class);
+ graphProperties.put(GraphPropertyEnum.IS_DELETED, true);
+ graphProperties.put(GraphPropertyEnum.IS_ARCHIVED, true);
+ return graphProperties;
+ }
+
private void setToscaResourceName(Map<String, Object> toscaJson, Resource resource) {
+ resource.setToscaResourceName(getToscaResourceName(toscaJson));
+ }
+
+ private String getToscaResourceName(Map<String, Object> toscaJson) {
Either<Map<String, Object>, ResultStatusEnum> toscaElement = ImportUtils
- .findFirstToscaMapElement(toscaJson, TypeUtils.ToscaTagNamesEnum.NODE_TYPES);
- if (toscaElement.isLeft() || toscaElement.left().value().size() == 1) {
- String toscaResourceName = toscaElement.left().value().keySet().iterator().next();
- resource.setToscaResourceName(toscaResourceName);
+ .findFirstToscaMapElement(toscaJson, ToscaTagNamesEnum.NODE_TYPES);
+ if (toscaElement.isLeft() && toscaElement.left().value().size() == 1) {
+ return toscaElement.left().value().keySet().iterator().next();
}
+ return null;
}
- private void setInterfaceLifecycle(Map<String, Object> toscaJson, Resource resource) {
- Either<Map<String, Object>, ResultStatusEnum> toscaInterfaces = ImportUtils
- .findFirstToscaMapElement(toscaJson, TypeUtils.ToscaTagNamesEnum.INTERFACES);
+ private void setInterfaceLifecycle(Map<String, Object> toscaJson, Resource resource, Either<Resource, StorageOperationStatus> existingResource,
+ Map<String, UploadComponentInstanceInfo> instancesFromCsar) {
+ final Either<Map<String, Object>, ResultStatusEnum> toscaInterfaces = ImportUtils
+ .findFirstToscaMapElement(toscaJson, ToscaTagNamesEnum.INTERFACES);
+ final Map<String, InterfaceDefinition> moduleInterfaces = new HashMap<>();
+ final Map<String, Object> map;
+ List<UploadInterfaceInfo> interfaceInfoList = null;
+ if (MapUtils.isNotEmpty(instancesFromCsar)) {
+ interfaceInfoList = instancesFromCsar.values().stream().filter(i -> MapUtils.isNotEmpty(i.getInterfaces()))
+ .flatMap(i -> i.getInterfaces().values().stream()).collect(Collectors.toList());
+ }
if (toscaInterfaces.isLeft()) {
- Map<String, Object> jsonInterfaces = toscaInterfaces.left().value();
- Map<String, InterfaceDefinition> moduleInterfaces = new HashMap<>();
- for (final Entry<String, Object> interfaceNameValue : jsonInterfaces.entrySet()) {
- final Either<InterfaceDefinition, ResultStatusEnum> eitherInterface = createModuleInterface(interfaceNameValue.getValue(), resource.getModel());
+ map = toscaInterfaces.left().value();
+ for (final Entry<String, Object> interfaceNameValue : map.entrySet()) {
+ final Either<InterfaceDefinition, ResultStatusEnum> eitherInterface =
+ createModuleInterface(interfaceNameValue.getValue(), resource.getModel());
if (eitherInterface.isRight()) {
log.info("error when creating interface:{}, for resource:{}", interfaceNameValue.getKey(), resource.getName());
} else {
final InterfaceDefinition interfaceDefinition = eitherInterface.left().value();
- moduleInterfaces.put(interfaceNameValue.getKey(), interfaceDefinition);
+ if (CollectionUtils.isNotEmpty(interfaceInfoList)) {
+ updateInterfaceDefinition(interfaceDefinition, interfaceInfoList);
+ }
+ moduleInterfaces.put(interfaceDefinition.getType(), interfaceDefinition);
}
}
- if (!moduleInterfaces.isEmpty()) {
- resource.setInterfaces(moduleInterfaces);
+ } else {
+ map = Collections.emptyMap();
+ }
+ if (existingResource.isLeft()) {
+ final Map<String, InterfaceDefinition> interfaces = existingResource.left().value().getInterfaces();
+ if (MapUtils.isNotEmpty(interfaces)) {
+ final Map<String, InterfaceDefinition> userCreatedInterfaceDefinitions =
+ interfaces.entrySet().stream()
+ .filter(i -> i.getValue().isUserCreated())
+ .filter(i -> !map.containsKey(i.getValue().getType()))
+ .collect(Collectors.toMap(Entry::getKey, Entry::getValue));
+ if (MapUtils.isNotEmpty(userCreatedInterfaceDefinitions)) {
+ moduleInterfaces.putAll(userCreatedInterfaceDefinitions);
+ }
}
}
+
+ if (MapUtils.isNotEmpty(moduleInterfaces)) {
+ resource.setInterfaces(moduleInterfaces);
+ }
+ }
+
+ private void updateInterfaceDefinition(InterfaceDefinition interfaceDefinition, List<UploadInterfaceInfo> interfaceInfoList) {
+ Map<String, OperationDataDefinition> operations = new HashMap<>();
+ interfaceInfoList.stream().filter(i -> interfaceDefinition.getType().endsWith(i.getKey())).forEach(i -> {
+ i.getOperations().values().forEach(o -> {
+ o.setImplementation(null);
+ });
+ operations.putAll(i.getOperations());
+ });
+ interfaceDefinition.setOperations(operations);
}
private Either<InterfaceDefinition, ResultStatusEnum> createModuleInterface(final Object interfaceJson, final String model) {
private void setRequirements(Map<String, Object> toscaJson, Resource resource,
Resource parentResource) {// Note that parentResource can be null
Either<List<Object>, ResultStatusEnum> toscaRequirements = ImportUtils
- .findFirstToscaListElement(toscaJson, TypeUtils.ToscaTagNamesEnum.REQUIREMENTS);
+ .findFirstToscaListElement(toscaJson, ToscaTagNamesEnum.REQUIREMENTS);
if (toscaRequirements.isLeft()) {
List<Object> jsonRequirements = toscaRequirements.left().value();
Map<String, List<RequirementDefinition>> moduleRequirements = new HashMap<>();
// Checking for name duplication
Set<String> reqNames = new HashSet<>();
- // Getting flattened list of capabilities of parent node - cap name
-
- // to cap type
+ // Getting flattened list of capabilities of parent node - cap name to cap type
Map<String, String> reqName2TypeMap = getReqName2Type(parentResource);
for (Object jsonRequirementObj : jsonRequirements) {
// Requirement
requirement.setCapability(requirementJsonString);
} else if (requirementJson instanceof Map) {
Map<String, Object> requirementJsonMap = (Map<String, Object>) requirementJson;
- if (requirementJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.CAPABILITY.getElementName())) {
- requirement.setCapability((String) requirementJsonMap.get(TypeUtils.ToscaTagNamesEnum.CAPABILITY.getElementName()));
+ if (requirementJsonMap.containsKey(ToscaTagNamesEnum.CAPABILITY.getElementName())) {
+ requirement.setCapability((String) requirementJsonMap.get(ToscaTagNamesEnum.CAPABILITY.getElementName()));
}
- if (requirementJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.NODE.getElementName())) {
- requirement.setNode((String) requirementJsonMap.get(TypeUtils.ToscaTagNamesEnum.NODE.getElementName()));
+ if (requirementJsonMap.containsKey(ToscaTagNamesEnum.NODE.getElementName())) {
+ requirement.setNode((String) requirementJsonMap.get(ToscaTagNamesEnum.NODE.getElementName()));
}
- if (requirementJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.RELATIONSHIP.getElementName())) {
- requirement.setRelationship((String) requirementJsonMap.get(TypeUtils.ToscaTagNamesEnum.RELATIONSHIP.getElementName()));
+ if (requirementJsonMap.containsKey(ToscaTagNamesEnum.RELATIONSHIP.getElementName())) {
+ requirement.setRelationship((String) requirementJsonMap.get(ToscaTagNamesEnum.RELATIONSHIP.getElementName()));
}
- if (requirementJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName())) {
- List<Object> occurrencesList = (List) requirementJsonMap.get(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName());
+ if (requirementJsonMap.containsKey(ToscaTagNamesEnum.OCCURRENCES.getElementName())) {
+ List<Object> occurrencesList = (List) requirementJsonMap.get(ToscaTagNamesEnum.OCCURRENCES.getElementName());
validateOccurrences(occurrencesList);
requirement.setMinOccurrences(occurrencesList.get(0).toString());
requirement.setMaxOccurrences(occurrencesList.get(1).toString());
return requirement;
}
- private void setProperties(Map<String, Object> toscaJson, Resource resource) {
- Map<String, Object> reducedToscaJson = new HashMap<>(toscaJson);
+ private void setProperties(final Map<String, Object> toscaJson, final Resource resource,
+ final Either<Resource, StorageOperationStatus> existingResource) {
+ final Map<String, Object> reducedToscaJson = new HashMap<>(toscaJson);
ImportUtils.removeElementFromJsonMap(reducedToscaJson, "capabilities");
- Either<Map<String, PropertyDefinition>, ResultStatusEnum> properties = ImportUtils.getProperties(reducedToscaJson);
+ final Either<Map<String, PropertyDefinition>, ResultStatusEnum> properties = ImportUtils.getProperties(reducedToscaJson);
if (properties.isLeft()) {
- List<PropertyDefinition> propertiesList = new ArrayList<>();
- Map<String, PropertyDefinition> value = properties.left().value();
- if (value != null) {
- 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 {} occured upon import resource {}. ", name, resource.getName());
- throw new ByActionStatusComponentException(
- componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_PROPERTY_NAME, JsonPresentationFields.PROPERTY));
+ final Map<String, PropertyDefinition> propertyDefinitionMap = properties.left().value();
+ if (MapUtils.isNotEmpty(propertyDefinitionMap)) {
+ final List<PropertyDefinition> propertiesList = new ArrayList<>();
+ for (final Entry<String, PropertyDefinition> entry : propertyDefinitionMap.entrySet()) {
+ addPropertyToList(resource.getName(), propertiesList, entry);
+ }
+ if (existingResource.isLeft() && CollectionUtils.isNotEmpty(existingResource.left().value().getProperties())) {
+ final List<PropertyDefinition> userCreatedResourceProperties =
+ existingResource.left().value().getProperties().stream()
+ .filter(PropertyDataDefinition::isUserCreated)
+ .filter(propertyDefinition -> !propertyDefinitionMap.containsKey(propertyDefinition.getName()))
+ .collect(Collectors.toList());
+ if (CollectionUtils.isNotEmpty(userCreatedResourceProperties)) {
+ propertiesList.addAll(userCreatedResourceProperties);
}
- PropertyDefinition propertyDefinition = entry.getValue();
- propertyDefinition.setName(name);
- propertiesList.add(propertyDefinition);
}
+
+ resource.setProperties(propertiesList);
}
- resource.setProperties(propertiesList);
} else if (properties.right().value() != ResultStatusEnum.ELEMENT_NOT_FOUND) {
throw new ByActionStatusComponentException(
componentsUtils.convertFromResultStatusEnum(properties.right().value(), JsonPresentationFields.PROPERTY));
}
}
+ private void addPropertyToList(final String resourceName,
+ final List<PropertyDefinition> propertiesList,
+ final Entry<String, PropertyDefinition> entry) {
+ final String propertyName = entry.getKey();
+ if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(propertyName).matches()) {
+ log.debug("The property with invalid name {} occured upon import resource {}. ", propertyName, resourceName);
+ throw new ByActionStatusComponentException(
+ componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_PROPERTY_NAME, JsonPresentationFields.PROPERTY));
+ }
+ final PropertyDefinition propertyDefinition = entry.getValue();
+ propertyDefinition.setName(propertyName);
+ propertiesList.add(propertyDefinition);
+ }
+
private void setAttributes(final Map<String, Object> originalToscaJsonMap, final Resource resource) {
final Map<String, Object> toscaJsonMap = new HashMap<>(originalToscaJsonMap);
ImportUtils.removeElementFromJsonMap(toscaJsonMap, "capabilities");
}
final AttributeDefinition attributeDefinition = entry.getValue();
attributeDefinition.setName(name);
+ if (attributeDefinition.getEntry_schema() != null && attributeDefinition.getEntry_schema().getType() != null) {
+ attributeDefinition.setSchema(new SchemaDefinition());
+ attributeDefinition.getSchema().setProperty(new PropertyDataDefinition());
+ attributeDefinition.getSchema().getProperty().setType(entry.getValue().getEntry_schema().getType());
+ }
attributeDefinitionList.add(attributeDefinition);
}
resource.setAttributes(attributeDefinitionList);
private Resource setDerivedFrom(Map<String, Object> toscaJson, Resource resource) {
Either<String, ResultStatusEnum> toscaDerivedFromElement = ImportUtils
- .findFirstToscaStringElement(toscaJson, TypeUtils.ToscaTagNamesEnum.DERIVED_FROM);
+ .findFirstToscaStringElement(toscaJson, ToscaTagNamesEnum.DERIVED_FROM);
Resource derivedFromResource = null;
if (toscaDerivedFromElement.isLeft()) {
String derivedFrom = toscaDerivedFromElement.left().value();
log.debug("Derived from TOSCA name is {}", derivedFrom);
resource.setDerivedFrom(Arrays.asList(new String[]{derivedFrom}));
- Either<Resource, StorageOperationStatus> latestByToscaResourceName = toscaOperationFacade.getLatestByToscaResourceName(derivedFrom);
+ Either<Resource, StorageOperationStatus> latestByToscaResourceName = toscaOperationFacade.getLatestByToscaResourceName(derivedFrom,
+ resource.getModel());
if (latestByToscaResourceName.isRight()) {
StorageOperationStatus operationStatus = latestByToscaResourceName.right().value();
if (operationStatus == StorageOperationStatus.NOT_FOUND) {
private void setCapabilities(Map<String, Object> toscaJson, Resource resource,
Resource parentResource) {// Note that parentResource can be null
Either<Map<String, Object>, ResultStatusEnum> toscaCapabilities = ImportUtils
- .findFirstToscaMapElement(toscaJson, TypeUtils.ToscaTagNamesEnum.CAPABILITIES);
+ .findFirstToscaMapElement(toscaJson, ToscaTagNamesEnum.CAPABILITIES);
if (toscaCapabilities.isLeft()) {
Map<String, Object> jsonCapabilities = toscaCapabilities.left().value();
Map<String, List<CapabilityDefinition>> moduleCapabilities = new HashMap<>();
} else if (capabilityJson instanceof Map) {
Map<String, Object> capabilityJsonMap = (Map<String, Object>) capabilityJson;
// Type
- if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName())) {
- capabilityDefinition.setType((String) capabilityJsonMap.get(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName()));
+ if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.TYPE.getElementName())) {
+ capabilityDefinition.setType((String) capabilityJsonMap.get(ToscaTagNamesEnum.TYPE.getElementName()));
}
// ValidSourceTypes
- if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.VALID_SOURCE_TYPES.getElementName())) {
+ if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.VALID_SOURCE_TYPES.getElementName())) {
capabilityDefinition
- .setValidSourceTypes((List<String>) capabilityJsonMap.get(TypeUtils.ToscaTagNamesEnum.VALID_SOURCE_TYPES.getElementName()));
+ .setValidSourceTypes((List<String>) capabilityJsonMap.get(ToscaTagNamesEnum.VALID_SOURCE_TYPES.getElementName()));
}
// ValidSourceTypes
- if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName())) {
- capabilityDefinition.setDescription((String) capabilityJsonMap.get(TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName()));
+ if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.DESCRIPTION.getElementName())) {
+ capabilityDefinition.setDescription((String) capabilityJsonMap.get(ToscaTagNamesEnum.DESCRIPTION.getElementName()));
}
- if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName())) {
- List<Object> occurrencesList = (List) capabilityJsonMap.get(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName());
+ if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.OCCURRENCES.getElementName())) {
+ List<Object> occurrencesList = (List) capabilityJsonMap.get(ToscaTagNamesEnum.OCCURRENCES.getElementName());
validateOccurrences(occurrencesList);
capabilityDefinition.setMinOccurrences(occurrencesList.get(0).toString());
capabilityDefinition.setMaxOccurrences(occurrencesList.get(1).toString());
}
- if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.PROPERTIES.getElementName())) {
+ if (capabilityJsonMap.containsKey(ToscaTagNamesEnum.PROPERTIES.getElementName())) {
Either<Map<String, PropertyDefinition>, ResultStatusEnum> propertiesRes = ImportUtils.getProperties(capabilityJsonMap);
if (propertiesRes.isRight()) {
throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND);
getAuditingManager().auditEvent(factory);
}
+ private void setResourceMetaData(Resource resource, String resourceYml, UploadResourceInfo resourceMetaData) {
+ Map<String, Object> ymlObj = new Yaml().load(resourceYml);
+ String toscaName = getToscaResourceName(ymlObj);
+ final Either<Resource, StorageOperationStatus> latestByToscaName = toscaOperationFacade
+ .getLatestByToscaResourceName(toscaName, resourceMetaData.getModel());
+ if (latestByToscaName.isLeft() && resourceMetaData instanceof DefaultUploadResourceInfo) {
+ setMetaDataFromLatestResource(resource, latestByToscaName.left().value());
+ } else {
+ setMetaDataFromJson(resourceMetaData, resource);
+ }
+ }
+
private void setMetaDataFromJson(final UploadResourceInfo resourceMetaData, final Resource resource) {
this.populateResourceMetadata(resourceMetaData, resource);
resource.setCreatorUserId(resourceMetaData.getContactId());
calculateResourceIsAbstract(resource, categories);
}
+ private void setMetaDataFromLatestResource(Resource resource, Resource latestResource) {
+ if (resource != null && latestResource != null) {
+ resource.setCreatorUserId(latestResource.getContactId());
+ resource.setDescription(latestResource.getDescription());
+ resource.setTags(latestResource.getTags());
+ resource.setCategories(latestResource.getCategories());
+ resource.setContactId(latestResource.getContactId());
+ resource.setName(latestResource.getName());
+ resource.setIcon(latestResource.getIcon());
+ resource.setResourceVendorModelNumber(latestResource.getResourceVendorModelNumber());
+ resource.setResourceType(latestResource.getResourceType());
+ if (latestResource.getVendorName() != null) {
+ resource.setVendorName(latestResource.getVendorName());
+ }
+ if (latestResource.getVendorRelease() != null) {
+ resource.setVendorRelease(latestResource.getVendorRelease());
+ }
+ if (latestResource.getModel() != null) {
+ resource.setModel(latestResource.getModel());
+ }
+ if (latestResource.getToscaVersion() != null) {
+ resource.setToscaVersion(latestResource.getToscaVersion());
+ }
+ final List<CategoryDefinition> categories = latestResource.getCategories();
+ calculateResourceIsAbstract(resource, categories);
+ }
+ }
+
private Map<String, Object> decodePayload(final String payloadData) {
final String decodedPayload = new String(Base64.decodeBase64(payloadData));
return (Map<String, Object>) new Yaml().load(decodedPayload);
private String getToscaVersion(final String payloadData) {
final Map<String, Object> mappedToscaTemplate = decodePayload(payloadData);
final Either<String, ResultStatusEnum> findFirstToscaStringElement = ImportUtils
- .findFirstToscaStringElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION);
+ .findFirstToscaStringElement(mappedToscaTemplate, ToscaTagNamesEnum.TOSCA_VERSION);
if (findFirstToscaStringElement.isLeft()) {
return findFirstToscaStringElement.left().value();
} else {
log.debug("Invalid occurrenses format. Max occurrence is {}", maxObj);
throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
}
- if (maxOccurrences <= 0 || maxOccurrences < minOccurrences) {
+ if (maxOccurrences < 0 || maxOccurrences < minOccurrences) {
log.debug("Invalid occurrenses format. min occurrence is {}, Max occurrence is {}", minOccurrences, maxOccurrences);
throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
}
}
private List<DataTypeDefinition> extractDataTypeFromJson(final ResourceBusinessLogic resourceBusinessLogic,
- final Map<String, Object> foundElements) {
+ final Map<String, Object> foundElements,
+ final String model) {
final List<DataTypeDefinition> dataTypeDefinitionList = new ArrayList<>();
if (MapUtils.isNotEmpty(foundElements)) {
- final Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypeCacheAll = resourceBusinessLogic.dataTypeCache.getAll();
+ final Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypeCacheAll =
+ resourceBusinessLogic.applicationDataTypeCache.getAll(model);
if (dataTypeCacheAll.isLeft()) {
for (final Entry<String, Object> attributeNameValue : foundElements.entrySet()) {
final Object value = attributeNameValue.getValue();