+ private Map<String, ToscaTypeMetadata> fillToscaTypeMetadata(final Map<String, Object> groupTypesToCreate) {
+ final Map<String, ToscaTypeMetadata> toscaTypeMetadata = new HashMap<>();
+ groupTypesToCreate.entrySet().forEach(entry -> {
+ final ToscaTypeMetadata metadata = new ToscaTypeMetadata();
+ metadata.setIcon(getIconFromGroupType(entry.getValue()));
+ metadata.setDisplayName(extractDisplayName(entry.getKey()));
+ toscaTypeMetadata.put(entry.getKey(), metadata);
+ });
+ return toscaTypeMetadata;
+ }
+
+ private String extractDisplayName(final String key) {
+ final String[] split = key.split("\\.");
+ return split[split.length - 1];
+ }
+
+ private String getIconFromGroupType(final Object value) {
+ final Either<GroupTypeDefinition, StorageOperationStatus> groupType = groupTypeOperation.getLatestGroupTypeByType(
+ (String) ((LinkedHashMap) value).get(ToscaTagNamesEnum.DERIVED_FROM.getElementName()), null);
+ if (groupType.isLeft()) {
+ return groupType.left().value().getIcon();
+ }
+ return null;
+ }
+
+ private Map<String, Object> getGroupTypesToCreate(final String model, final CsarInfo csarInfo) {
+ final Map<String, Object> groupTypesToCreate = new HashMap<>();
+ final Map<String, Object> groupTypes = csarInfo.getGroupTypes();
+ if (MapUtils.isNotEmpty(groupTypes)) {
+ for (final Entry<String, Object> entry : groupTypes.entrySet()) {
+ final Either<GroupTypeDefinition, StorageOperationStatus> result
+ = groupTypeOperation.getGroupTypeByUid(UniqueIdBuilder.buildGroupTypeUid(model, entry.getKey(), "1.0"));
+ if (result.isRight() && result.right().value().equals(StorageOperationStatus.NOT_FOUND)) {
+ groupTypesToCreate.put(entry.getKey(), entry.getValue());
+ log.info("Deploying new group type {} to model {} from package {}", entry.getKey(), model, csarInfo.getCsarUUID());
+ }
+ }
+ }
+ return groupTypesToCreate;
+ }
+
+ private Map<String, Object> getCapabilityTypesToCreate(final String model, final CsarInfo csarInfo) {
+ final Map<String, Object> capabilityTypesToCreate = new HashMap<>();
+ final Map<String, Object> capabilityTypes = csarInfo.getCapabilityTypes();
+ if (MapUtils.isNotEmpty(capabilityTypes)) {
+ for (final Entry<String, Object> entry : capabilityTypes.entrySet()) {
+ final Either<CapabilityTypeDefinition, StorageOperationStatus> result
+ = capabilityTypeOperation.getCapabilityType(UniqueIdBuilder.buildCapabilityTypeUid(model, entry.getKey()));
+ if (result.isRight() && result.right().value().equals(StorageOperationStatus.NOT_FOUND)) {
+ capabilityTypesToCreate.put(entry.getKey(), entry.getValue());
+ log.info("Deploying new capability type {} to model {} from package {}", entry.getKey(), model, csarInfo.getCsarUUID());
+ }
+ }
+ }
+ return capabilityTypesToCreate;
+ }
+
+ private Map<String, Object> getDatatypesToCreate(final String model, final CsarInfo csarInfo) {
+ final Map<String, Object> dataTypesToCreate = new HashMap<>();
+
+ for (final Entry<String, Object> dataTypeEntry : csarInfo.getDataTypes().entrySet()) {
+ final Either<DataTypeDefinition, JanusGraphOperationStatus> result = applicationDataTypeCache.get(model,
+ UniqueIdBuilder.buildDataTypeUid(model, dataTypeEntry.getKey()));
+ if (result.isRight() && result.right().value().equals(JanusGraphOperationStatus.NOT_FOUND)) {
+ dataTypesToCreate.put(dataTypeEntry.getKey(), dataTypeEntry.getValue());
+ log.info("Deploying unknown type {} to model {} from package {}", dataTypeEntry.getKey(), model, csarInfo.getCsarUUID());
+ }
+ if (hasNewProperties(result, (Map<String, Map<String, Object>>) dataTypeEntry.getValue())) {
+ dataTypesToCreate.put(dataTypeEntry.getKey(), dataTypeEntry.getValue());
+ log.info("Deploying new version of type {} to model {} from package {}", dataTypeEntry.getKey(), model, csarInfo.getCsarUUID());
+ }
+ }
+ return dataTypesToCreate;
+ }
+
+ private Map<String, Object> getArtifactTypesToCreate(final String model, final CsarInfo csarInfo) {
+ final Map<String, Object> artifactTypesToCreate = new HashMap<>();
+ final Map<String, Object> artifactTypesMap = csarInfo.getArtifactTypes();
+ if (MapUtils.isNotEmpty(artifactTypesMap)) {
+ for (final Entry<String, Object> artifactTypeEntry : artifactTypesMap.entrySet()) {
+ final Either<ArtifactTypeDefinition, StorageOperationStatus> result =
+ artifactTypeOperation.getArtifactTypeByUid(UniqueIdBuilder.buildArtifactTypeUid(model, artifactTypeEntry.getKey()));
+ if (result.isRight() && StorageOperationStatus.NOT_FOUND.equals(result.right().value())) {
+ artifactTypesToCreate.put(artifactTypeEntry.getKey(), artifactTypeEntry.getValue());
+ log.info("Deploying new artifact type={}, to model={}, from package={}",
+ artifactTypeEntry.getKey(), model, csarInfo.getCsarUUID());
+ }
+ }
+ }
+ return artifactTypesToCreate;
+ }
+
+ private Map<String, Object> getInterfaceTypesToCreate(final String model, final CsarInfo csarInfo) {
+ final Map<String, Object> interfaceTypesToCreate = new HashMap<>();
+ Map<String, Object> interfacetypeMap = csarInfo.getInterfaceTypes();
+
+ interfacetypeMap.entrySet().forEach(interfacetypeDef -> {
+ Either<InterfaceDefinition, StorageOperationStatus> interfaceDefinition =
+ interfaceLifecycleTypeOperation.getInterface(UniqueIdBuilder.buildInterfaceTypeUid(model, interfacetypeDef.getKey()));
+ if (interfaceDefinition.isRight() && interfaceDefinition.right().value().equals(StorageOperationStatus.NOT_FOUND)) {
+ interfaceTypesToCreate.put(interfacetypeDef.getKey(), interfacetypeDef.getValue());
+ }
+ });
+ return interfaceTypesToCreate;
+ }
+
+ private boolean hasNewProperties(final Either<DataTypeDefinition, JanusGraphOperationStatus> result,
+ final Map<String, Map<String, Object>> dataType) {
+ return result.isLeft() && dataType.containsKey("properties") && result.left().value().getProperties() != null
+ && result.left().value().getProperties().size() != dataType.get("properties").size();
+ }
+
+ private void createNodeTypes(List<NodeTypeDefinition> nodeTypesToCreate, String model, User user) {
+ NodeTypesMetadataList nodeTypesMetadataList = new NodeTypesMetadataList();
+ List<NodeTypeMetadata> nodeTypeMetadataList = new ArrayList<>();
+ final Map<String, Object> allTypesToCreate = new HashMap<>();
+ nodeTypesToCreate.forEach(nodeType -> {
+ allTypesToCreate.put(nodeType.getMappedNodeType().getKey(), nodeType.getMappedNodeType().getValue());
+ nodeTypeMetadataList.add(nodeType.getNodeTypeMetadata());
+ });
+ nodeTypesMetadataList.setNodeMetadataList(nodeTypeMetadataList);
+ resourceImportManager.importAllNormativeResource(allTypesToCreate, nodeTypesMetadataList, user, model, true, false);
+ }
+
+ private List<NodeTypeDefinition> getNodeTypesToCreate(final String model, final ServiceCsarInfo csarInfo) {
+ List<NodeTypeDefinition> namesOfNodeTypesToCreate = new ArrayList<>();
+
+ for (final NodeTypeDefinition nodeTypeDefinition : csarInfo.getNodeTypesUsed()) {
+ Either<Component, StorageOperationStatus> result = toscaOperationFacade
+ .getLatestByToscaResourceName(nodeTypeDefinition.getMappedNodeType().getKey(), model);
+ if (result.isRight() && result.right().value().equals(StorageOperationStatus.NOT_FOUND)) {
+ namesOfNodeTypesToCreate.add(nodeTypeDefinition);
+ } else if (result.isLeft()) {
+ Resource latestResource = (Resource) result.left().value();
+ Entry<String, Object> existingMappedToscaTemplate = getResourceToscaTemplate(latestResource.getUniqueId(),
+ latestResource.getToscaArtifacts().get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE), csarInfo.getModifier().getUserId());
+ Map<String, Object> newMappedToscaTemplate = (Map<String, Object>) nodeTypeDefinition.getMappedNodeType().getValue();
+ Map<String, Object> combinedMappedToscaTemplate =
+ getNewChangesToToscaTemplate(newMappedToscaTemplate, (Map<String, Object>) existingMappedToscaTemplate.getValue());
+ if (!combinedMappedToscaTemplate.equals(existingMappedToscaTemplate.getValue())) {
+ if (latestResource.getComponentMetadataDefinition().getMetadataDataDefinition().isNormative()) {
+ nodeTypeDefinition.getNodeTypeMetadata().setNormative(true);
+ }
+ existingMappedToscaTemplate.setValue(combinedMappedToscaTemplate);
+ nodeTypeDefinition.setMappedNodeType(existingMappedToscaTemplate);
+ namesOfNodeTypesToCreate.add(nodeTypeDefinition);
+ }
+ }
+ }
+ return namesOfNodeTypesToCreate;
+ }
+
+ private Entry<String, Object> getResourceToscaTemplate(String uniqueId, ArtifactDefinition assetToscaTemplate, String userId) {
+ String assetToToscaTemplate = assetToscaTemplate.getUniqueId();
+ ImmutablePair<String, byte[]> toscaTemplate = artifactsBusinessLogic.
+ handleDownloadRequestById(uniqueId, assetToToscaTemplate, userId, ComponentTypeEnum.RESOURCE, null, null);
+ Map<String, Object> mappedToscaTemplate = new Yaml().load(new String(toscaTemplate.right));
+ Either<Map<String, Object>, ImportUtils.ResultStatusEnum> eitherNodeTypes =
+ findFirstToscaMapElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES);
+ if (eitherNodeTypes.isRight()) {
+ throw new ComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE);
+ }
+ return eitherNodeTypes.left().value().entrySet().iterator().next();
+ }
+
+ private Map<String, Object> getNewChangesToToscaTemplate(Map<String, Object> newMappedToscaTemplate,
+ Map<String, Object> existingMappedToscaTemplate) {
+ Map<String, Object> combinedMappedToscaTemplate = new HashMap<>(existingMappedToscaTemplate);
+ combinePropertiesIntoToscaTemplate((Map<String, Object>) newMappedToscaTemplate.get("properties"),
+ (Map<String, Object>) existingMappedToscaTemplate.get("properties"), combinedMappedToscaTemplate);
+ combineAttributesIntoToscaTemplate((Map<String, Object>) newMappedToscaTemplate.get("attributes"),
+ (Map<String, Object>) existingMappedToscaTemplate.get("attributes"), combinedMappedToscaTemplate);
+ combineRequirementsIntoToscaTemplate((List<Map<String, Object>>) newMappedToscaTemplate.get("requirements"),
+ (List<Map<String, Object>>) existingMappedToscaTemplate.get("requirements"), combinedMappedToscaTemplate);
+ combineCapabilitiesIntoToscaTemplate((Map<String, Object>) newMappedToscaTemplate.get("capabilities"),
+ (Map<String, Object>) existingMappedToscaTemplate.get("capabilities"), combinedMappedToscaTemplate);
+ combineInterfacesIntoToscaTemplate((Map<String, Map<String, Object>>) newMappedToscaTemplate.get("interfaces"),
+ (Map<String, Map<String, Object>>) existingMappedToscaTemplate.get("interfaces"), combinedMappedToscaTemplate);
+ return combinedMappedToscaTemplate;
+ }
+
+ private void combineInterfacesIntoToscaTemplate(Map<String, Map<String, Object>> newInterfaces,
+ Map<String, Map<String, Object>> existingInterfaces,
+ Map<String, Object> combinedMappedToscaTemplate) {
+ Map<String, Map<String, Object>> combinedInterfaces = combineAdditionalInterfaces(existingInterfaces, newInterfaces);
+ if ((MapUtils.isEmpty(existingInterfaces) && MapUtils.isNotEmpty(combinedInterfaces))
+ || (MapUtils.isNotEmpty(existingInterfaces) && !existingInterfaces.equals(combinedInterfaces))) {
+ combinedMappedToscaTemplate.put("interfaces", combinedInterfaces);
+ }
+ }
+
+ private void combineCapabilitiesIntoToscaTemplate(Map<String, Object> newCapabilities, Map<String, Object> existingCapabilities,
+ Map<String, Object> combinedMappedToscaTemplate) {
+ Map<String, Object> combinedCapabilities = combineEntries(newCapabilities, existingCapabilities);
+ if ((MapUtils.isEmpty(existingCapabilities) && MapUtils.isNotEmpty(combinedCapabilities)) ||
+ (MapUtils.isNotEmpty(existingCapabilities) && !combinedCapabilities.equals(existingCapabilities))) {
+ combinedMappedToscaTemplate.put("capabilities", combinedCapabilities);
+ }
+ }
+
+ private void combineRequirementsIntoToscaTemplate(List<Map<String, Object>> newRequirements, List<Map<String, Object>> existingRequirements,
+ Map<String, Object> combinedMappedToscaTemplate) {
+ List<Map<String, Object>> combinedRequirements = combineAdditionalRequirements(newRequirements, existingRequirements);
+ if ((CollectionUtils.isEmpty(existingRequirements) && CollectionUtils.isNotEmpty(combinedRequirements))
+ || (CollectionUtils.isNotEmpty(existingRequirements) && !combinedRequirements.equals(existingRequirements))) {
+ combinedMappedToscaTemplate.put("requirements", combinedRequirements);
+ }
+ }
+
+ private void combineAttributesIntoToscaTemplate(Map<String, Object> newAttributes, Map<String, Object> existingAttributes,
+ Map<String, Object> combinedMappedToscaTemplate) {
+ Map<String, Object> combinedAttributes = combineEntries(newAttributes, existingAttributes);
+ if ((MapUtils.isEmpty(existingAttributes) && MapUtils.isNotEmpty(combinedAttributes)) ||
+ (MapUtils.isNotEmpty(existingAttributes) && !combinedAttributes.equals(existingAttributes))) {
+ combinedMappedToscaTemplate.put("attributes", combinedAttributes);
+ }
+ }
+
+ private void combinePropertiesIntoToscaTemplate(Map<String, Object> newProperties, Map<String, Object> existingProperties,
+ Map<String, Object> combinedMappedToscaTemplate) {
+ Map<String, Object> combinedProperties = combineEntries(newProperties, existingProperties);
+ if ((MapUtils.isEmpty(existingProperties) && MapUtils.isNotEmpty(combinedProperties)) ||
+ (MapUtils.isNotEmpty(existingProperties) && !combinedProperties.equals(existingProperties))) {
+ combinedMappedToscaTemplate.put("properties", combinedProperties);
+ }
+ }
+
+ private Map<String, Map<String, Object>> combineAdditionalInterfaces(Map<String, Map<String, Object>> existingInterfaces,
+ Map<String, Map<String, Object>> newInterfaces) {
+ if (MapUtils.isEmpty(newInterfaces)) {
+ newInterfaces = new HashMap<>();
+ }
+ Map<String, Map<String, Object>> combinedEntries = new HashMap<>(newInterfaces);
+ if (MapUtils.isEmpty(existingInterfaces)) {
+ return combinedEntries;
+ }
+ existingInterfaces.entrySet().forEach(interfaceDef -> {
+ combinedEntries.entrySet().stream().filter((interFace) -> interFace.getValue().get("type").equals((interfaceDef.getValue()).get("type")))
+ .findFirst().ifPresentOrElse((interFace) -> {
+ interFace.getValue().putAll(interfaceDef.getValue());
+ }, () -> {
+ combinedEntries.put(interfaceDef.getKey(), interfaceDef.getValue());
+ });
+ });
+ return combinedEntries;
+ }
+
+ private List<Map<String, Object>> combineAdditionalRequirements(List<Map<String, Object>> newReqs,
+ List<Map<String, Object>> existingResourceReqs) {
+ if (CollectionUtils.isEmpty(existingResourceReqs)) {
+ existingResourceReqs = new ArrayList<>();
+ }
+ Set<Map<String, Object>> combinedReqs = new TreeSet<>((map1, map2) ->
+ map1.keySet().equals(map2.keySet()) ? 0 : map1.keySet().iterator().next().compareTo(map2.keySet().iterator().next()));
+ combinedReqs.addAll(existingResourceReqs);
+ if (CollectionUtils.isEmpty(newReqs)) {
+ return new ArrayList<>(combinedReqs);
+ }
+ combinedReqs.addAll(newReqs);
+ return new ArrayList<>(combinedReqs);
+ }
+
+ private Map<String, Object> combineEntries(Map<String, Object> newMap, Map<String, Object> existingMap) {
+ if (MapUtils.isEmpty(newMap)) {
+ newMap = new HashMap<>();
+ }
+ Map<String, Object> combinedEntries = new HashMap<>(newMap);
+ if (MapUtils.isEmpty(existingMap)) {
+ return combinedEntries;
+ }
+ combinedEntries.putAll(existingMap);
+ return combinedEntries;
+ }
+