Unable to import service template with interface
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ServiceImportBusinessLogic.java
index 316c940..8671d9e 100644 (file)
@@ -221,10 +221,10 @@ public class ServiceImportBusinessLogic {
     private final GroupTypeOperation groupTypeOperation;
     private final CapabilityTypeImportManager capabilityTypeImportManager;
     private final CapabilityTypeOperation capabilityTypeOperation;
-    private ApplicationDataTypeCache applicationDataTypeCache;
     private final InterfaceLifecycleOperation interfaceLifecycleTypeOperation;
     private final InterfaceLifecycleTypeImportManager interfaceLifecycleTypeImportManager;
     private final ModelOperation modelOperation;
+    private ApplicationDataTypeCache applicationDataTypeCache;
 
     public ServiceImportBusinessLogic(final GroupBusinessLogic groupBusinessLogic, final ArtifactsBusinessLogic artifactsBusinessLogic,
                                       final ComponentsUtils componentsUtils, final ToscaOperationFacade toscaOperationFacade,
@@ -2157,6 +2157,7 @@ public class ServiceImportBusinessLogic {
         if (originResource.getAttributes() != null && !originResource.getAttributes().isEmpty()) {
             instAttributes.put(resourceInstanceId, originResource.getAttributes());
             addAttributeValueToResourceInstance(instAttributes, uploadComponentInstanceInfo.getAttributes());
+            instAttributes.get(resourceInstanceId).addAll(addImplicitAttributeValues(originResource, uploadComponentInstanceInfo));
         }
         if (uploadComponentInstanceInfo.getUploadNodeFilterInfo() == null) {
             instNodeFilter.put(resourceInstanceId, new UploadNodeFilterInfo());
@@ -2187,6 +2188,44 @@ public class ServiceImportBusinessLogic {
         }
     }
 
+    private List<AttributeDefinition> addImplicitAttributeValues(Resource originResource, UploadComponentInstanceInfo uploadComponentInstanceInfo) {
+        if (uploadComponentInstanceInfo.getAttributes() == null) {
+            return Collections.emptyList();
+        }
+        List<String> origAttributes = originResource.getAttributes().stream().map(AttributeDefinition::getName).collect(toList());
+        Map<String, UploadAttributeInfo> uploadAttributes = uploadComponentInstanceInfo.getAttributes();
+        List<String> newAttributesToAdd =
+            uploadAttributes.keySet().stream().filter(newAttribute -> !origAttributes.contains(newAttribute))
+                .collect(toList());
+        List<PropertyDefinition> propsToAddAsAttributes =
+            originResource.getProperties().stream().filter(prop -> newAttributesToAdd.contains(prop.getName())).collect(toList());
+        propsToAddAsAttributes.stream().forEach(prop -> {
+            Object value = uploadAttributes.get(prop.getName()).getValue();
+            if (value instanceof Collection<?> || value instanceof Map<?, ?>) {
+                Gson gson = new Gson();
+                String json = gson.toJson(value);
+                prop.setValue(json);
+            } else {
+                prop.setValue(String.valueOf(value));
+            }
+        });
+        List<AttributeDefinition> attributesToAdd = new ArrayList<>();
+        for (PropertyDefinition prop : propsToAddAsAttributes) {
+            attributesToAdd.add(getPropertyAsAttribute(prop));
+        }
+        return attributesToAdd;
+    }
+
+    private AttributeDefinition getPropertyAsAttribute(PropertyDefinition property) {
+        AttributeDefinition attribute = new AttributeDefinition();
+        attribute.setName(property.getName());
+        attribute.setType(property.getType());
+        attribute.setSchema(property.getSchema());
+        attribute.setValue(property.getValue());
+        attribute.setDefaultValue(property.getDefaultValue());
+        return attribute;
+    }
+
     protected void addInputsValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, Component component, Resource originResource,
                                        ComponentInstance currentCompInstance, Map<String, List<ComponentInstanceInput>> instInputs,
                                        Map<String, DataTypeDefinition> allDataTypes) {
@@ -2424,13 +2463,13 @@ public class ServiceImportBusinessLogic {
         Map<String, UploadInterfaceInfo> instanceInterfacesMap = uploadComponentInstanceInfo.getInterfaces();
         Map<String, InterfaceDefinition> currInterfacesMap = new HashMap<>();
         Map<String, InterfaceDefinition> interfacesFromNodeType = originResource.getInterfaces();
-        if ((MapUtils.isNotEmpty(instanceInterfacesMap)) && (MapUtils.isEmpty(interfacesFromNodeType))) {
+        if (interfacesFromNodeType == null) {
+            interfacesFromNodeType = new HashMap<>();
+        }
+        if (MapUtils.isEmpty(instanceInterfacesMap) && MapUtils.isEmpty(instanceInterfacesMap)) {
             log.debug("failed to find interfaces ");
             return componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT);
         }
-        if (interfacesFromNodeType == null || interfacesFromNodeType.isEmpty()) {
-            return componentsUtils.getResponseFormat(ActionStatus.OK);
-        }
         for (Map.Entry<String, InterfaceDefinition> entryInstances : interfacesFromNodeType.entrySet()) {
             String interfaceName = entryInstances.getKey().substring(entryInstances.getKey().lastIndexOf(".") + 1);
             if (!currInterfacesMap.containsKey(interfaceName)) {
@@ -2442,11 +2481,16 @@ public class ServiceImportBusinessLogic {
         if (MapUtils.isNotEmpty(instanceInterfacesMap)) {
             for (UploadInterfaceInfo uploadInterfaceInfo : instanceInterfacesMap.values()) {
                 String interfaceName = uploadInterfaceInfo.getName();
+                InterfaceDefinition currentInterfaceDef;
                 if (!currInterfacesMap.containsKey(interfaceName)) {
-                    log.debug("failed to find interface {} ", interfaceName);
-                    return componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT, interfaceName);
+                    currentInterfaceDef = getInterfaceDef(interfaceName, component.getModel());
+                    if (currentInterfaceDef == null) {
+                        log.debug("failed to find interface {} ", interfaceName);
+                        return componentsUtils.getResponseFormat(ActionStatus.INTERFACE_NOT_FOUND_IN_COMPONENT, interfaceName);
+                    }
+                } else {
+                    currentInterfaceDef = currInterfacesMap.get(interfaceName);
                 }
-                InterfaceDefinition currentInterfaceDef = currInterfacesMap.get(interfaceName);
                 Map<String, OperationDataDefinition> operationsToAdd = new HashMap<>();
 
                 Map<String, OperationDataDefinition> operations = uploadInterfaceInfo.getOperations();
@@ -2472,6 +2516,7 @@ public class ServiceImportBusinessLogic {
                     templateOperation.setImplementation(instanceOperation.getImplementation());
                     //Description
                     templateOperation.setDescription(instanceOperation.getDescription());
+                    templateOperation.setMilestones(instanceOperation.getMilestones());
                     operationsToAdd.put(operation.getKey(), templateOperation);
                 }
                 InterfaceDefinition interfaceDef = new InterfaceDefinition();
@@ -2493,6 +2538,21 @@ public class ServiceImportBusinessLogic {
         return componentsUtils.getResponseFormat(ActionStatus.OK);
     }
 
+    private InterfaceDefinition getInterfaceDef(String interfaceName, String model) {
+        Either<Map<String, InterfaceDefinition>, StorageOperationStatus> interfaceLifecycleTypesEither =
+            interfaceLifecycleTypeOperation.getAllInterfaceLifecycleTypes(model);
+        if (interfaceLifecycleTypesEither.isRight()) {
+            return null;
+        }
+        Map<String, InterfaceDefinition> interfaceLifecycleTypes = interfaceLifecycleTypesEither.left().value();
+        Optional<InterfaceDefinition> interfaceType =
+            interfaceLifecycleTypes.values().stream().filter(interfaceDef -> interfaceDef.getUniqueId().contains(interfaceName)).findFirst();
+        if (interfaceType.isEmpty()) {
+            return null;
+        }
+        return interfaceType.get();
+    }
+
     private void mergeOperationInputDefinitions(ListDataDefinition<OperationInputDefinition> inputsFromNodeType,
                                                 ListDataDefinition<OperationInputDefinition> instanceInputs) {
         if (inputsFromNodeType == null || CollectionUtils.isEmpty(inputsFromNodeType.getListToscaDataDefinition()) || instanceInputs == null