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