Service import issues
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ServiceImportBusinessLogic.java
index ca8a13d..fff1ac5 100644 (file)
@@ -13,6 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 package org.openecomp.sdc.be.components.impl;
 
 import static java.util.stream.Collectors.joining;
@@ -86,6 +87,7 @@ import org.openecomp.sdc.be.datatypes.elements.PolicyDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.SubPropertyToscaFunction;
 import org.openecomp.sdc.be.datatypes.elements.SubstitutionFilterPropertyDataDefinition;
+import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
@@ -201,16 +203,14 @@ public class ServiceImportBusinessLogic {
     private final IGraphLockOperation graphLockOperation;
     private final ToscaFunctionService toscaFunctionService;
     private final DataTypeBusinessLogic dataTypeBusinessLogic;
-    private ApplicationDataTypeCache applicationDataTypeCache;
     private final ArtifactTypeOperation artifactTypeOperation;
-
     private final GroupTypeImportManager groupTypeImportManager;
     private final GroupTypeOperation groupTypeOperation;
-    private InterfaceLifecycleOperation interfaceLifecycleTypeOperation;
-    private InterfaceLifecycleTypeImportManager interfaceLifecycleTypeImportManager;
-
     private final CapabilityTypeImportManager capabilityTypeImportManager;
     private final CapabilityTypeOperation capabilityTypeOperation;
+    private ApplicationDataTypeCache applicationDataTypeCache;
+    private InterfaceLifecycleOperation interfaceLifecycleTypeOperation;
+    private InterfaceLifecycleTypeImportManager interfaceLifecycleTypeImportManager;
 
     public ServiceImportBusinessLogic(final GroupBusinessLogic groupBusinessLogic, final ArtifactsBusinessLogic artifactsBusinessLogic,
                                       final ComponentsUtils componentsUtils, final ToscaOperationFacade toscaOperationFacade,
@@ -487,6 +487,9 @@ public class ServiceImportBusinessLogic {
                 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);
@@ -688,7 +691,8 @@ public class ServiceImportBusinessLogic {
             Map<String, InputDefinition> inputs = parsedToscaYamlInfo.getInputs();
             service = serviceImportParseLogic.createInputsOnService(service, inputs);
             log.trace("************* Finished to add inputs from yaml {}", yamlName);
-            ListDataDefinition<SubstitutionFilterPropertyDataDefinition> substitutionFilterProperties = parsedToscaYamlInfo.getSubstitutionFilterProperties();
+            ListDataDefinition<SubstitutionFilterPropertyDataDefinition> substitutionFilterProperties =
+                parsedToscaYamlInfo.getSubstitutionFilterProperties();
             service = serviceImportParseLogic.createSubstitutionFilterOnService(service, substitutionFilterProperties);
             log.trace("************* Added Substitution filter from interface yaml {}", yamlName);
             Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap = parsedToscaYamlInfo.getInstances();
@@ -753,7 +757,7 @@ public class ServiceImportBusinessLogic {
                 }
             }
             if (shouldLock) {
-                graphLockOperation.unlockComponentByName(service.getSystemName(), service.getUniqueId(), NodeTypeEnum.Resource);
+                graphLockOperation.unlockComponentByName(service.getSystemName(), service.getUniqueId(), NodeTypeEnum.Service);
             }
         }
     }
@@ -764,13 +768,17 @@ public class ServiceImportBusinessLogic {
             final List<ComponentInstance> componentInstances = component.getComponentInstances();
             final String componentUniqueId = component.getUniqueId();
             for (final InputDefinition input : inputs) {
-                if (isInputFromComponentInstanceProperty(input.getName(), componentInstances)) {
+                boolean isSubMapProp = false;
+                if (substitutionMappingProperties != null && !substitutionMappingProperties.isEmpty()) {
+                    isSubMapProp = substitutionMappingProperties.entrySet().stream()
+                        .anyMatch(stringEntry -> stringEntry.getValue().get(0).equals(input.getName()));
+                }
+                if (!isSubMapProp && isInputFromComponentInstanceProperty(input.getName(), componentInstances)) {
                     associateInputToComponentInstanceProperty(userId, input, componentInstances, componentUniqueId);
                 } else {
                     associateInputToServiceProperty(userId, input, component, substitutionMappingProperties);
                 }
             }
-
             Either<List<InputDefinition>, StorageOperationStatus> either = toscaOperationFacade.updateInputsToComponent(inputs, componentUniqueId);
             if (either.isRight()) {
                 throw new ComponentException(ActionStatus.GENERAL_ERROR);
@@ -784,7 +792,8 @@ public class ServiceImportBusinessLogic {
 
         AtomicBoolean isInputFromCIProp = new AtomicBoolean(false);
         if (CollectionUtils.isNotEmpty(componentInstances)) {
-            outer: for (ComponentInstance instance : componentInstances) {
+            outer:
+            for (ComponentInstance instance : componentInstances) {
                 for (PropertyDefinition instanceProperty : instance.getProperties()) {
                     if (CollectionUtils.isNotEmpty(instanceProperty.getGetInputValues())) {
                         for (GetInputValueDataDefinition getInputValueDataDefinition : instanceProperty.getGetInputValues()) {
@@ -807,7 +816,8 @@ public class ServiceImportBusinessLogic {
         String componentInstanceId = null;
         ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
 
-        outer: for (ComponentInstance instance : componentInstances) {
+        outer:
+        for (ComponentInstance instance : componentInstances) {
             for (PropertyDefinition instanceProperty : instance.getProperties()) {
                 if (CollectionUtils.isNotEmpty(instanceProperty.getGetInputValues())) {
                     for (GetInputValueDataDefinition getInputValueDataDefinition : instanceProperty.getGetInputValues()) {
@@ -847,8 +857,9 @@ public class ServiceImportBusinessLogic {
                 }
             });
 
-            final Optional<PropertyDefinition> propDefOptional = properties.stream().filter(prop -> prop.getName().equals(propertyNameFromInput.get()))
-                .findFirst();
+            final Optional<PropertyDefinition> propDefOptional =
+                properties.stream().filter(prop -> prop.getName().equals(propertyNameFromInput.get()))
+                    .findFirst();
             if (propDefOptional.isPresent()) {
                 // From SELF
                 final String componentUniqueId = component.getUniqueId();
@@ -864,6 +875,8 @@ public class ServiceImportBusinessLogic {
                 if (either.isRight()) {
                     throw new ComponentException(ActionStatus.GENERAL_ERROR);
                 }
+            } else {
+                input.setMappedToComponentProperty(false);
             }
         }
     }
@@ -893,8 +906,9 @@ public class ServiceImportBusinessLogic {
                                                                        boolean inTransaction, boolean shouldLock) {
         String nodeName = nodeTypeInfoToUpdateArtifacts.getNodeName();
         Resource resource = preparedResource;
-        Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = nodeTypeInfoToUpdateArtifacts
-            .getNodeTypesArtifactsToHandle();
+        Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle =
+            nodeTypeInfoToUpdateArtifacts
+                .getNodeTypesArtifactsToHandle();
         if (preparedResource.getResourceType() == ResourceTypeEnum.VF) {
             if (nodeName != null && nodeTypesArtifactsToHandle.get(nodeName) != null && !nodeTypesArtifactsToHandle.get(nodeName).isEmpty()) {
                 Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifactsRes = handleNodeTypeArtifacts(preparedResource,
@@ -1021,7 +1035,8 @@ public class ServiceImportBusinessLogic {
                 vfCsarArtifactsToHandle = new EnumMap<>(ArtifactsBusinessLogic.ArtifactOperationEnum.class);
                 vfCsarArtifactsToHandle.put(artifactOperation.getArtifactOperationEnum(), artifactPathAndNameList.left().value());
             } else {
-                Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> findVfCsarArtifactsToHandleRes = findVfCsarArtifactsToHandle(
+                Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat>
+                    findVfCsarArtifactsToHandleRes = findVfCsarArtifactsToHandle(
                     component, artifactPathAndNameList.left().value(), csarInfo.getModifier());
                 if (findVfCsarArtifactsToHandleRes.isRight()) {
                     resStatus = Either.right(findVfCsarArtifactsToHandleRes.right().value());
@@ -1312,7 +1327,8 @@ public class ServiceImportBusinessLogic {
         EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>> nodeTypeArtifactsToHandle = new EnumMap<>(
             ArtifactsBusinessLogic.ArtifactOperationEnum.class);
         Wrapper<ResponseFormat> responseWrapper = new Wrapper<>();
-        Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat> nodeTypeArtifactsToHandleRes = Either
+        Either<EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<CsarUtils.NonMetaArtifactInfo>>, ResponseFormat>
+            nodeTypeArtifactsToHandleRes = Either
             .left(nodeTypeArtifactsToHandle);
         try {
             List<CsarUtils.NonMetaArtifactInfo> artifactsToUpload = new ArrayList<>(artifactPathAndNameList);
@@ -1847,11 +1863,12 @@ public class ServiceImportBusinessLogic {
         log.debug("************* Going to create all nodes {}", yamlName);
         handleServiceNodeTypes(yamlName, service, topologyTemplateYaml, false, nodeTypesArtifactsToCreate, nodeTypesNewCreatedArtifacts,
             nodeTypesInfo, csarInfo, nodeName);
+        List<PropertyDefinition> serviceProperties = null != service ? service.getProperties() : Collections.emptyList();
         if (MapUtils.isNotEmpty(uploadComponentInstanceInfoMap)) {
             log.debug("************* Going to create all resource instances {}", yamlName);
             service = createServiceInstances(yamlName, service, uploadComponentInstanceInfoMap, csarInfo.getCreatedNodes());
             log.debug("************* Going to create all relations {}", yamlName);
-            service = createServiceInstancesRelations(csarInfo.getModifier(), yamlName, service, uploadComponentInstanceInfoMap);
+            service = createServiceInstancesRelations(csarInfo.getModifier(), yamlName, service, uploadComponentInstanceInfoMap, serviceProperties);
             log.debug("************* Going to create positions {}", yamlName);
             compositionBusinessLogic.setPositionsForComponentInstances(service, csarInfo.getModifier().getUserId());
             log.debug("************* Finished to set positions {}", yamlName);
@@ -1860,7 +1877,8 @@ public class ServiceImportBusinessLogic {
     }
 
     protected Service createServiceInstancesRelations(User user, String yamlName, Service service,
-                                                      Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) {
+                                                      Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
+                                                      List<PropertyDefinition> serviceProperties) {
         log.debug("#createResourceInstancesRelations - Going to create relations ");
         List<ComponentInstance> componentInstancesList = service.getComponentInstances();
         if (MapUtils.isEmpty(uploadResInstancesMap) || CollectionUtils.isEmpty(componentInstancesList)) { // PNF can have no resource instances
@@ -1888,6 +1906,7 @@ public class ServiceImportBusinessLogic {
             final Map<String, DataTypeDefinition> allDataTypesMap =
                 componentsUtils.getAllDataTypes(applicationDataTypeCache, service.getModel());
             final Service service1 = service;
+            service1.setProperties(serviceProperties);
             uploadResInstancesMap.values().forEach(
                 i -> processComponentInstance(yamlName, service1, componentInstancesList,
                     allDataTypesMap, instProperties,
@@ -2289,6 +2308,17 @@ public class ServiceImportBusinessLogic {
                     //Inputs
                     ListDataDefinition<OperationInputDefinition> instanceInputs = instanceOperation.getInputs();
                     mergeOperationInputDefinitions(templateOperation.getInputs(), instanceInputs);
+                    if (null != instanceInputs) {
+                        component.getProperties()
+                            .forEach(property -> instanceInputs.getListToscaDataDefinition().stream()
+                                .filter(instanceInput ->
+                                    instanceInput.getToscaFunction() instanceof ToscaGetFunctionDataDefinition &&
+                                        property.getName().equals(instanceInput.getToscaFunction() != null ?
+                                            ((ToscaGetFunctionDataDefinition) instanceInput.getToscaFunction()).getPropertyName() :
+                                            null))
+                                .forEach(oldInput -> oldInput.setType(property.getType()))
+                            );
+                    }
                     templateOperation.setInputs(instanceInputs);
                     //Implementation
                     templateOperation.setImplementation(instanceOperation.getImplementation());
@@ -2703,6 +2733,9 @@ public class ServiceImportBusinessLogic {
             Resource origResource = existingnodeTypeMap.get(uploadComponentInstanceInfo.getType());
             componentInstance.setName(uploadComponentInstanceInfo.getName());
             componentInstance.setIcon(origResource.getIcon());
+            componentInstance.setMinOccurrences(uploadComponentInstanceInfo.getMinOccurrences());
+            componentInstance.setMaxOccurrences(uploadComponentInstanceInfo.getMaxOccurrences());
+            componentInstance.setInstanceCount(uploadComponentInstanceInfo.getInstanceCount());
             resourcesInstancesMap.put(componentInstance, origResource);
         } catch (final ComponentException e) {
             throw e;