forwarder healer

add healer for forwarder capability

Issue-Id : SDC-653

Change-Id: Ic653cf22b4d7c4e22d34b15cac56d91f55ecd6c4
Signed-off-by: talio <tali.orenbach@amdocs.com>
diff --git a/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-core/src/main/resources/healingConfiguration.json b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-core/src/main/resources/healingConfiguration.json
index 9f83296..4e1b0df 100644
--- a/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-core/src/main/resources/healingConfiguration.json
+++ b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-core/src/main/resources/healingConfiguration.json
@@ -8,5 +8,6 @@
   "COMPONENT_QUESTIONNAIRE_HEALER" : "org.openecomp.sdc.healing.healers.ComponentQuestionnaireHealer",
   "HEAT_TOSCA_TRANSLATION_HEALER" : "org.openecomp.sdc.healing.healers.HeatToToscaTranslationHealer",
   "VLM_VERSION_HEALER" : "org.openecomp.sdc.healing.healers.VlmVersionHealer",
-  "VALIDATION_STRUCTURE_HEALER" : "org.openecomp.sdc.healing.healers.ValidationStructureHealer"
+  "VALIDATION_STRUCTURE_HEALER" : "org.openecomp.sdc.healing.healers.ValidationStructureHealer",
+  "FORWARDER_CAPABILITY_HEALER" : "org.openecomp.sdc.healing.healers.ForwarderCapabilityHealer"
 }
\ No newline at end of file
diff --git a/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/ForwarderCapabilityHealer.java b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/ForwarderCapabilityHealer.java
new file mode 100644
index 0000000..038a0d8
--- /dev/null
+++ b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/ForwarderCapabilityHealer.java
@@ -0,0 +1,121 @@
+package org.openecomp.sdc.healing.healers;
+
+import org.apache.commons.collections.MapUtils;
+import org.openecomp.core.model.dao.ServiceModelDao;
+import org.openecomp.core.model.dao.ServiceModelDaoFactory;
+import org.openecomp.core.model.types.ServiceElement;
+import org.openecomp.sdc.common.togglz.ToggleableFeature;
+import org.openecomp.sdc.common.utils.SdcCommon;
+import org.openecomp.sdc.healing.interfaces.Healer;
+import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
+import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
+import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
+import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
+import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
+import org.openecomp.sdc.tosca.services.DataModelUtil;
+import org.openecomp.sdc.tosca.services.ToscaAnalyzerService;
+import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl;
+import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesServiceTemplates;
+import org.openecomp.sdc.versioning.dao.types.Version;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+
+public class ForwarderCapabilityHealer implements Healer {
+
+  private MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
+
+  private final ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao =
+      ServiceModelDaoFactory.getInstance().createInterface();
+  private static ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
+  private static final String FORWARDER_CAPABILITY_ID = "Forwarder";
+  private static final String UNDERSCORE = "_";
+
+  @Override
+  public Object heal(Map<String, Object> healingParams) throws Exception {
+    String vspId = (String) healingParams.get(SdcCommon.VSP_ID);
+    Version version = (Version) healingParams.get(SdcCommon.VERSION);
+
+    if(!ToggleableFeature.FORWARDER_CAPABILITY.isActive()) {
+      return Optional.empty();
+    }
+
+    ToscaServiceModel serviceModel =
+        serviceModelDao.getServiceModel(vspId, version);
+
+    if (Objects.isNull(serviceModel)
+        || MapUtils.isEmpty(serviceModel.getServiceTemplates())) {
+      return Optional.empty();
+    }
+
+    addForwarderCapabilityToServiceModel(serviceModel);
+    serviceModelDao.deleteAll(vspId, version);
+    serviceModelDao.storeServiceModel(vspId, version, serviceModel);
+
+    return Optional.of(serviceModel);
+  }
+
+  private void addForwarderCapabilityToServiceModel(ToscaServiceModel serviceModel) {
+    serviceModel.getServiceTemplates().entrySet().stream().filter(serviceTemplateEntry -> Objects
+        .nonNull(serviceTemplateEntry.getValue()))
+        .forEach(serviceTemplateEntry -> handleServiceTemplate(serviceTemplateEntry.getValue(),
+            serviceModel));
+
+    handleGlobalTypes(serviceModel);
+  }
+
+  private void handleGlobalTypes(ToscaServiceModel serviceModel) {
+    Map<String, ServiceTemplate> globalTypesServiceTemplates =
+        GlobalTypesServiceTemplates.getGlobalTypesServiceTemplates();
+
+    if (MapUtils.isEmpty(globalTypesServiceTemplates)) {
+      return;
+    }
+
+    globalTypesServiceTemplates.entrySet()
+        .stream()
+        .filter(globalTypesServiceTemplateEntry -> Objects.nonNull
+            (globalTypesServiceTemplateEntry.getValue()))
+        .forEach(globalTypesServiceTemplateEntry -> serviceModel.addServiceTemplate
+            (globalTypesServiceTemplateEntry.getKey(), globalTypesServiceTemplateEntry.getValue()));
+  }
+
+  private void handleServiceTemplate(ServiceTemplate serviceTemplate,
+                                     ToscaServiceModel toscaServiceModel) {
+    if (Objects.isNull(serviceTemplate.getTopology_template())
+        || MapUtils.isEmpty(serviceTemplate.getTopology_template().getNode_templates())) {
+      return;
+    }
+
+    Map<String, NodeTemplate> nodeTemplates =
+        serviceTemplate.getTopology_template().getNode_templates();
+
+    nodeTemplates.entrySet().stream()
+        .filter(nodeTemplateEntry ->
+            toscaAnalyzerService.isTypeOf(nodeTemplateEntry.getValue(),
+                ToscaNodeType.NATIVE_NETWORK_PORT, serviceTemplate, toscaServiceModel))
+        .forEach(nodeTemplateEntry ->
+            addForwarderToSubstitutionMappings(nodeTemplateEntry.getKey(), serviceTemplate)
+        );
+  }
+
+  private void addForwarderToSubstitutionMappings(String portNodeTemplateId,
+                                                  ServiceTemplate serviceTemplate) {
+    if (Objects.isNull(serviceTemplate.getTopology_template())
+        || Objects.isNull(serviceTemplate.getTopology_template().getSubstitution_mappings())) {
+      return;
+    }
+
+    List<String> substitutionMappingCapabilityList =
+        Arrays.asList(portNodeTemplateId, FORWARDER_CAPABILITY_ID);
+
+    DataModelUtil.addSubstitutionMappingCapability(
+        serviceTemplate,
+        FORWARDER_CAPABILITY_ID + UNDERSCORE + portNodeTemplateId,
+        substitutionMappingCapabilityList);
+
+  }
+}
diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/datatypes/ToscaServiceModel.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/datatypes/ToscaServiceModel.java
index 722c286..4512fce 100644
--- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/datatypes/ToscaServiceModel.java
+++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/datatypes/ToscaServiceModel.java
@@ -20,12 +20,14 @@
 
 package org.openecomp.sdc.tosca.datatypes;
 
+import org.apache.commons.collections.MapUtils;
 import org.openecomp.core.utilities.file.FileContentHandler;
 import org.openecomp.sdc.datatypes.model.AsdcModel;
 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
 import org.openecomp.sdc.tosca.services.DataModelUtil;
 
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.Map;
 
 /**
@@ -87,6 +89,15 @@
     return Collections.unmodifiableMap(serviceTemplates);
   }
 
+  public void addServiceTemplate(String serviceTemplateName,
+                                 ServiceTemplate serviceTemplate) {
+    if(MapUtils.isEmpty(serviceTemplates)){
+      serviceTemplates = new HashMap<>();
+    }
+
+    serviceTemplates.put(serviceTemplateName, serviceTemplate);
+  }
+
   /**
    * Sets service templates.
    *
@@ -123,12 +134,4 @@
   public static ToscaServiceModel getClonedServiceModel(ToscaServiceModel toscaServiceModel) {
     return ToscaServiceModel.class.cast(DataModelUtil.getClonedObject(toscaServiceModel));
   }
-
-  public FileContentHandler getExternalFiles() {
-    return externalFiles;
-  }
-
-  public void setExternalFiles(FileContentHandler externalFiles) {
-    this.externalFiles = externalFiles;
-  }
 }