Service import - Import unknown interface types

Issue-ID: SDC-4186
Signed-off-by: KrupaNagabhushan <krupa.nagabhushan@est.tech>
Change-Id: Idfbce90e795136ef2ea1a96f65e458db9206339f
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarInfo.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarInfo.java
index f92c1d1..c401660 100644
--- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarInfo.java
+++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarInfo.java
@@ -183,6 +183,13 @@
 
     public abstract Map<String, Object> getArtifactTypes();
 
+    /**
+     * Get the interface types defined in the CSAR
+     *
+     * @return map with the interface type name as key and representation of the interface type definition as value
+     */
+    public abstract Map<String, Object> getInterfaceTypes();
+
     public Map<String, Object> getPolicyTypes() {
         if (policytypeDefinitions == null) {
             policytypeDefinitions = new HashMap<>();
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/OnboardedCsarInfo.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/OnboardedCsarInfo.java
index 9ab7678..7cb1180 100644
--- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/OnboardedCsarInfo.java
+++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/OnboardedCsarInfo.java
@@ -24,6 +24,7 @@
 import static org.openecomp.sdc.be.components.impl.ImportUtils.findToscaElement;
 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DATA_TYPES;
 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.GROUP_TYPES;
+import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.INTERFACE_TYPES;
 
 import fj.data.Either;
 import java.util.ArrayList;
@@ -217,6 +218,11 @@
     }
 
     @Override
+    public Map<String, Object> getInterfaceTypes() {
+        return getTypes(INTERFACE_TYPES);
+    }
+
+    @Override
     public Map<String, Object> getCapabilityTypes() {
         return getTypes(ToscaTagNamesEnum.CAPABILITY_TYPES);
     }
@@ -230,5 +236,4 @@
         types.putAll(getTypesFromTemplate(mappedToscaMainTemplate, toscaTag));
         return types;
     }
-
 }
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ServiceCsarInfo.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ServiceCsarInfo.java
index 54cd298..3e0ea35 100644
--- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ServiceCsarInfo.java
+++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ServiceCsarInfo.java
@@ -41,6 +41,7 @@
 import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
 import org.openecomp.sdc.be.components.impl.ImportUtils.ToscaElementTypeEnum;
 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
+import org.openecomp.sdc.be.model.InterfaceDefinition;
 import org.openecomp.sdc.be.model.NodeTypeDefinition;
 import org.openecomp.sdc.be.model.NodeTypeInfo;
 import org.openecomp.sdc.be.model.NodeTypeMetadata;
@@ -174,6 +175,11 @@
         return artifactsTypes;
     }
 
+    @Override
+    public Map<String, Object> getInterfaceTypes() {
+        return getTypes(ToscaTagNamesEnum.INTERFACE_TYPES);
+    }
+
     public List<NodeTypeDefinition> getNodeTypesUsed() {
         if (nodeTypeDefinitions == null) {
             nodeTypeDefinitions = new ArrayList<>();
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java
index 151717f..04798ec 100644
--- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java
+++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java
@@ -144,6 +144,7 @@
 import org.openecomp.sdc.be.model.operations.impl.ArtifactTypeOperation;
 import org.openecomp.sdc.be.model.operations.impl.CapabilityTypeOperation;
 import org.openecomp.sdc.be.model.operations.impl.GroupTypeOperation;
+import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
@@ -203,6 +204,8 @@
 
     private final GroupTypeImportManager groupTypeImportManager;
     private final GroupTypeOperation groupTypeOperation;
+    private InterfaceLifecycleOperation interfaceLifecycleTypeOperation;
+    private InterfaceLifecycleTypeImportManager interfaceLifecycleTypeImportManager;
 
     private final CapabilityTypeImportManager capabilityTypeImportManager;
     private final CapabilityTypeOperation capabilityTypeOperation;
@@ -219,6 +222,8 @@
                                       final DataTypeBusinessLogic dataTypeBusinessLogic, final ArtifactTypeOperation artifactTypeOperation,
                                       final ArtifactTypeImportManager artifactTypeImportManager, final GroupTypeImportManager groupTypeImportManager,
                                       final GroupTypeOperation groupTypeOperation,
+                                      final InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
+                                      final InterfaceLifecycleTypeImportManager interfaceLifecycleTypeImportManager,
                                       final CapabilityTypeImportManager capabilityTypeImportManager,
                                       final CapabilityTypeOperation capabilityTypeOperation) {
         this.componentsUtils = componentsUtils;
@@ -242,6 +247,8 @@
         this.artifactTypeImportManager = artifactTypeImportManager;
         this.groupTypeImportManager = groupTypeImportManager;
         this.groupTypeOperation = groupTypeOperation;
+        this.interfaceLifecycleTypeOperation = interfaceLifecycleTypeOperation;
+        this.interfaceLifecycleTypeImportManager = interfaceLifecycleTypeImportManager;
         this.capabilityTypeImportManager = capabilityTypeImportManager;
         this.capabilityTypeOperation = capabilityTypeOperation;
     }
@@ -310,6 +317,12 @@
                 groupTypeImportManager.createGroupTypes(toscaTypeImportData, service.getModel(), true);
             }
 
+            final Map<String, Object> interfaceTypesToCreate = getInterfaceTypesToCreate(service.getModel(), csarInfo);
+            if (MapUtils.isNotEmpty(interfaceTypesToCreate)) {
+                interfaceLifecycleTypeImportManager
+                    .createLifecycleTypes(new Yaml().dump(interfaceTypesToCreate), service.getModel(), true);
+            }
+
             final Map<String, Object> capabilityTypesToCreate = getCapabilityTypesToCreate(service.getModel(), csarInfo);
 
             if (MapUtils.isNotEmpty(capabilityTypesToCreate)) {
@@ -426,8 +439,21 @@
         return artifactTypesToCreate;
     }
 
-    private boolean hasNewProperties(final Either<DataTypeDefinition, JanusGraphOperationStatus> result,
-                                     final Map<String, Map<String, Object>> dataType) {
+    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();
     }
diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/csar/ServiceCsarInfoTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/csar/ServiceCsarInfoTest.java
index 80ecc21..add427d 100644
--- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/csar/ServiceCsarInfoTest.java
+++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/csar/ServiceCsarInfoTest.java
@@ -47,7 +47,7 @@
     private User user;
 
     private static final String CSAR_UUID = "csarUUID";
-    private static final String PAYLOAD_NAME = "csars/serviceWithUnknownDataTypes.csar";
+    private static final String PAYLOAD_NAME = "csars/serviceWithUnknownTypes.csar";
     private static final String SERVICE_NAME = "serviceWithDataType";
     private static final String MAIN_TEMPLATE_NAME = "Definitions/service-Servicewithdatatype-template.yml";
 
@@ -78,4 +78,16 @@
                 ((Map<String, Object>) ((Map<String, Object>) dataTypeDefinition.get("properties")).get("prop2")).get("type"));
     }
 
+    @SuppressWarnings("unchecked")
+    @Test
+    void testGetInterfaceTypes() {
+        final Map<String, Object> interfaceTypes = csarInfo.getInterfaceTypes();
+        assertEquals(9, interfaceTypes.size());
+        final Map<String, Object> interfaceTypeDefinition = (Map<String, Object>) interfaceTypes.get(
+            "tosca.interfaces.test.node.lifecycle.Reconfigure");
+        assertNotNull(interfaceTypeDefinition);
+        assertEquals("tosca.interfaces.Root", interfaceTypeDefinition.get("derived_from"));
+        assertEquals("reconfigure", ((Map<String, Object>) interfaceTypeDefinition.get("Reconfigure")).get("description"));
+    }
+
 }
diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogicTest.java
index 944f51c..faaa7e3 100644
--- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogicTest.java
+++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogicTest.java
@@ -143,6 +143,7 @@
     private final GroupTypeOperation groupTypeOperation = mock(GroupTypeOperation.class);
     private final CapabilityTypeOperation capabilityTypeOperation = mock(CapabilityTypeOperation.class);
     private final CapabilityTypeImportManager capabilityTypeImportManager = mock(CapabilityTypeImportManager.class);
+    private final InterfaceLifecycleTypeImportManager interfaceLifecycleTypeImportManager = mock(InterfaceLifecycleTypeImportManager.class);
 
     @InjectMocks
     private ServiceImportBusinessLogic sIBL;
@@ -256,6 +257,10 @@
         when(artifactTypeOperation.getArtifactTypeByUid(contains("tosca.testartifacts.Name"))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
         when(artifactTypeOperation.getArtifactTypeByUid(contains("tosca.artifacts"))).thenReturn(Either.left(null));
 
+        when(interfaceLifecycleTypeOperation.getInterface(contains("tosca.interfaces"))).thenReturn(Either.left(new InterfaceDefinition()));
+        when(interfaceLifecycleTypeOperation.getInterface(contains("tosca.interfaces.test"))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
+
+
         when(capabilityTypeOperation.getCapabilityType(anyString()))
             .thenReturn(Either.left(new CapabilityTypeDefinition()));
         when(capabilityTypeOperation.getCapabilityType(contains("tosca.testcapabilitytypes.Name")))
@@ -313,6 +318,15 @@
         Map<String, Object> nodeTypesMap = nodeTypes.getValue();
         Map<String, Object> newUpdatedNodeType = (Map<String, Object>) nodeTypesMap.get(updatedNodeType);
         assertEquals(8, ((Map<String, Object>) newUpdatedNodeType.get("properties")).size());
+
+        ArgumentCaptor<String> interfaceTypes = ArgumentCaptor.forClass(String.class);
+        verify(interfaceLifecycleTypeImportManager).createLifecycleTypes(interfaceTypes.capture(), any(), anyBoolean());
+        Map<String, Object> yamlInterfaceMap = new Yaml().load(interfaceTypes.getValue());
+        assertEquals(3, yamlInterfaceMap.size());
+        assertNotNull(yamlInterfaceMap.get("tosca.interfaces.test.node.lifecycle.Attach"));
+        assertNotNull(yamlInterfaceMap.get("tosca.interfaces.test.node.lifecycle.Detach"));
+        assertNotNull(yamlInterfaceMap.get("tosca.interfaces.test.node.lifecycle.Reconfigure"));
+
     }
 
     @Test
diff --git a/catalog-be/src/test/resources/csars/service-Ser09080002-csar.csar b/catalog-be/src/test/resources/csars/service-Ser09080002-csar.csar
index c780e34..de7d71f 100644
--- a/catalog-be/src/test/resources/csars/service-Ser09080002-csar.csar
+++ b/catalog-be/src/test/resources/csars/service-Ser09080002-csar.csar
Binary files differ
diff --git a/catalog-be/src/test/resources/csars/serviceWithUnknownDataTypes.csar b/catalog-be/src/test/resources/csars/serviceWithUnknownTypes.csar
similarity index 89%
rename from catalog-be/src/test/resources/csars/serviceWithUnknownDataTypes.csar
rename to catalog-be/src/test/resources/csars/serviceWithUnknownTypes.csar
index ffd2099..b50113c 100644
--- a/catalog-be/src/test/resources/csars/serviceWithUnknownDataTypes.csar
+++ b/catalog-be/src/test/resources/csars/serviceWithUnknownTypes.csar
Binary files differ
diff --git a/common-be/src/main/java/org/openecomp/sdc/be/utils/TypeUtils.java b/common-be/src/main/java/org/openecomp/sdc/be/utils/TypeUtils.java
index 3f6f0cd..7602509 100644
--- a/common-be/src/main/java/org/openecomp/sdc/be/utils/TypeUtils.java
+++ b/common-be/src/main/java/org/openecomp/sdc/be/utils/TypeUtils.java
@@ -78,7 +78,8 @@
         //Operations
         IMPLEMENTATION("implementation"),
         SUBSTITUTION_FILTERS("substitution_filter"),
-        DERIVED_FROM_NAME("derivedFromName");
+        DERIVED_FROM_NAME("derivedFromName"),
+        INTERFACE_TYPES("interface_types");
         // @formatter:om
 
         private final String elementName;