+ 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;
+ }
+