Rework of the csarinstaller

Csarinstaller rework to save Template and Model to database

Issue-ID: CLAMP-81
Change-Id: I5edf9716146975ed3751a93b78f5c9b0b9ff2af9
Signed-off-by: Determe, Sebastien (sd378r) <sd378r@intl.att.com>
diff --git a/src/main/java/org/onap/clamp/clds/config/sdc/BlueprintParserMappingConfiguration.java b/src/main/java/org/onap/clamp/clds/config/sdc/BlueprintParserMappingConfiguration.java
index b263104..a78e895 100644
--- a/src/main/java/org/onap/clamp/clds/config/sdc/BlueprintParserMappingConfiguration.java
+++ b/src/main/java/org/onap/clamp/clds/config/sdc/BlueprintParserMappingConfiguration.java
@@ -39,6 +39,7 @@
 public class BlueprintParserMappingConfiguration {
 
     private String blueprintKey;
+    private boolean dcaeDeployable;
     private BlueprintParserFilesConfiguration files;
 
     public String getBlueprintKey() {
@@ -57,6 +58,10 @@
         this.files = filesConfig;
     }
 
+    public boolean isDcaeDeployable() {
+        return dcaeDeployable;
+    }
+
     public static List<BlueprintParserMappingConfiguration> createFromJson(InputStream json) throws IOException {
         TypeReference<List<BlueprintParserMappingConfiguration>> mapType = new TypeReference<List<BlueprintParserMappingConfiguration>>() {
         };
diff --git a/src/main/java/org/onap/clamp/clds/model/CldsModel.java b/src/main/java/org/onap/clamp/clds/model/CldsModel.java
index 02c70ca..34876bb 100644
--- a/src/main/java/org/onap/clamp/clds/model/CldsModel.java
+++ b/src/main/java/org/onap/clamp/clds/model/CldsModel.java
@@ -43,33 +43,36 @@
  */
 public class CldsModel {
 
-    private static final EELFLogger logger             = EELFManager.getInstance().getLogger(CldsModel.class);
-    private static final int        UUID_LENGTH        = 36;
-    private static final String     STATUS_DESIGN      = "DESIGN";
-    private static final String     STATUS_DISTRIBUTED = "DISTRIBUTED";
-    private static final String     STATUS_ACTIVE      = "ACTIVE";
-    private static final String     STATUS_STOPPED     = "STOPPED";
-    private static final String     STATUS_DELETING    = "DELETING";
-    private static final String     STATUS_ERROR       = "ERROR";
-    private static final String     STATUS_UNKNOWN     = "UNKNOWN";
-    private String                  id;
-    private String                  templateId;
-    private String                  templateName;
-    private String                  name;
-    private String                  controlNamePrefix;
-    private String                  controlNameUuid;
-    private String                  bpmnText;
-    private String                  propText;
-    private String                  imageText;
-    private String                  docText;
-    private String                  blueprintText;
-    private CldsEvent               event;
-    private String                  status;
-    private List<String>            permittedActionCd;
+    private static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsModel.class);
+    private static final int UUID_LENGTH = 36;
+    private static final String STATUS_DESIGN = "DESIGN";
+    private static final String STATUS_DISTRIBUTED = "DISTRIBUTED";
+    private static final String STATUS_ACTIVE = "ACTIVE";
+    private static final String STATUS_STOPPED = "STOPPED";
+    private static final String STATUS_DELETING = "DELETING";
+    private static final String STATUS_ERROR = "ERROR";
+    private static final String STATUS_UNKNOWN = "UNKNOWN";
+    private String id;
+    private String templateId;
+    private String templateName;
+    private String name;
+    private String controlNamePrefix;
+    private String controlNameUuid;
+    private String bpmnText;
+    private String propText;
+    private String imageText;
+    private String docText;
+    private String blueprintText;
+    private CldsEvent event;
+    private String status;
+    private List<String> permittedActionCd;
     private List<CldsModelInstance> cldsModelInstanceList;
-    private String                  typeId;
-    private String                  typeName;
-    private String                  deploymentId;
+    /**
+     * The service type Id received from DCAE by querying it
+     */
+    private String typeId;
+    private String typeName;
+    private String deploymentId;
 
     /**
      * Construct empty model.
@@ -95,7 +98,6 @@
     public boolean canInventoryCall() {
         boolean canCall = false;
         /* Below checks the clds event is submit/resubmit */
-
         if ((event.isActionCd(CldsEvent.ACTION_SUBMIT) || event.isActionCd(CldsEvent.ACTION_RESUBMIT)
                 || event.isActionCd(CldsEvent.ACTION_SUBMITDCAE))) {
             canCall = true;
@@ -116,7 +118,6 @@
      * set the status in the model
      */
     private void determineStatus() {
-
         status = STATUS_UNKNOWN;
         if (event == null || event.getActionCd() == null) {
             status = STATUS_DESIGN;
@@ -141,7 +142,6 @@
         } else if (event.isActionAndStateCd(CldsEvent.ACTION_STOP, CldsEvent.ACTION_STATE_ANY)) {
             status = STATUS_STOPPED;
         }
-
     }
 
     /**
@@ -491,5 +491,4 @@
     public List<String> getPermittedActionCd() {
         return permittedActionCd;
     }
-
 }
diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarHandler.java b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarHandler.java
index 940b7cf..b113852 100644
--- a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarHandler.java
+++ b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarHandler.java
@@ -43,6 +43,7 @@
 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
 import org.openecomp.sdc.api.notification.IArtifactInfo;
 import org.openecomp.sdc.api.notification.INotificationData;
+import org.openecomp.sdc.api.notification.IResourceInstance;
 import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;
 import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper;
 import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException;
@@ -61,9 +62,14 @@
     private SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
     private ISdcCsarHelper sdcCsarHelper;
     private String dcaeBlueprint;
+    private String blueprintArtifactName;
+    private String blueprintInvariantResourceUuid;
+    private String blueprintInvariantServiceUuid;
     public static final String CSAR_TYPE = "TOSCA_CSAR";
+    private INotificationData sdcNotification;
 
     public CsarHandler(INotificationData iNotif, String controller, String clampCsarPath) throws CsarHandlerException {
+        this.sdcNotification = iNotif;
         this.controllerName = controller;
         this.artifactElement = searchForUniqueCsar(iNotif);
         this.csarFilePath = buildFilePathForCsar(artifactElement, clampCsarPath);
@@ -96,12 +102,27 @@
             }
             sdcCsarHelper = factory.getSdcCsarHelper(csarFilePath);
             this.loadDcaeBlueprint();
+            this.loadBlueprintArtifactDetails();
         } catch (IOException e) {
             throw new SdcArtifactInstallerException(
                     "Exception caught when trying to write the CSAR on the file system to " + csarFilePath, e);
         }
     }
 
+    private void loadBlueprintArtifactDetails() {
+        blueprintInvariantServiceUuid = this.getSdcNotification().getServiceInvariantUUID();
+        for (IResourceInstance resource : this.getSdcNotification().getResources()) {
+            if ("VF".equals(resource.getResourceType())) {
+                for (IArtifactInfo artifact : resource.getArtifacts()) {
+                    if ("DCAE_INVENTORY_BLUEPRINT".equals(artifact.getArtifactType())) {
+                        blueprintArtifactName = artifact.getArtifactName();
+                        blueprintInvariantResourceUuid = resource.getResourceInvariantUUID();
+                    }
+                }
+            }
+        }
+    }
+
     private void loadDcaeBlueprint() throws IOException, SdcArtifactInstallerException {
         List<ZipEntry> listEntries = new ArrayList<>();
         try (ZipFile zipFile = new ZipFile(csarFilePath)) {
@@ -136,4 +157,20 @@
     public synchronized String getDcaeBlueprint() {
         return dcaeBlueprint;
     }
+
+    public INotificationData getSdcNotification() {
+        return sdcNotification;
+    }
+
+    public String getBlueprintArtifactName() {
+        return blueprintArtifactName;
+    }
+
+    public String getBlueprintInvariantResourceUuid() {
+        return blueprintInvariantResourceUuid;
+    }
+
+    public String getBlueprintInvariantServiceUuid() {
+        return blueprintInvariantServiceUuid;
+    }
 }
diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java
index c56eed5..3a6bef8 100644
--- a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java
+++ b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java
@@ -35,7 +35,8 @@
 
 import javax.annotation.PostConstruct;
 
-import org.onap.clamp.clds.config.ClampProperties;
+import org.json.simple.parser.ParseException;
+import org.onap.clamp.clds.client.DcaeInventoryServices;
 import org.onap.clamp.clds.config.sdc.BlueprintParserFilesConfiguration;
 import org.onap.clamp.clds.config.sdc.BlueprintParserMappingConfiguration;
 import org.onap.clamp.clds.dao.CldsDao;
@@ -55,8 +56,8 @@
 
     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarInstallerImpl.class);
     private Map<String, BlueprintParserFilesConfiguration> bpmnMapping = new HashMap<>();
-    public static final String TEMPLATE_NAME_SUFFIX = "-template-dcae-designer";
-    public static final String MODEL_NAME_SUFFIX = "-model-dcae-designer";
+    public static final String TEMPLATE_NAME_PREFIX = "DCAE-Designer-ClosedLoopTemplate-";
+    public static final String MODEL_NAME_PREFIX = "DCAE-Designer-ClosedLoopInstance-";
     /**
      * The file name that will be loaded by Spring.
      */
@@ -67,11 +68,11 @@
     @Autowired
     private CldsDao cldsDao;
     @Autowired
-    private ClampProperties refProp;
-    @Autowired
     CldsTemplateService cldsTemplateService;
     @Autowired
     CldsService cldsService;
+    @Autowired
+    DcaeInventoryServices dcaeInventoryService;
 
     @PostConstruct
     public void loadConfiguration() throws IOException {
@@ -88,10 +89,15 @@
     @Override
     public void installTheCsar(CsarHandler csar) throws SdcArtifactInstallerException {
         try {
-            BlueprintParserFilesConfiguration configFiles = this.searchForRightMapping(csar);
-            createFakeCldsModel(csar, configFiles, createFakeCldsTemplate(csar, configFiles));
+            String serviceTypeId = queryDcaeToGetServiceTypeId(csar);
+            String policyName = searchForPolicyName(csar);
+            if (policyName.contains("*")) {
+            }
+            createFakeCldsModel(csar, createFakeCldsTemplate(csar, this.searchForRightMapping(csar)), serviceTypeId);
         } catch (IOException e) {
             throw new SdcArtifactInstallerException("Exception caught during the Csar installation in database", e);
+        } catch (ParseException e) {
+            throw new SdcArtifactInstallerException("Exception caught during the Dcae query to get ServiceTypeId", e);
         }
     }
 
@@ -115,12 +121,38 @@
         return listConfig.get(0);
     }
 
-    private String createTemplateName(CsarHandler csar) {
-        return csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + TEMPLATE_NAME_SUFFIX;
+    private String searchForPolicyName(CsarHandler csar) throws SdcArtifactInstallerException {
+        String policyName = null;
+        Yaml yaml = new Yaml();
+        List<String> policyNameList = new ArrayList<>();
+        Map<String, Object> templateNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
+                .load(csar.getDcaeBlueprint())).get("node_templates"));
+        templateNodes.entrySet().stream().filter(e -> e.getKey().contains("policy_")).forEach(ef -> {
+            String filteredPolicyName = (String) ((Map<String, Object>) ((Map<String, Object>) ef.getValue())
+                    .get("properties")).get("policy_filter");
+            if (policyName != null) {
+                policyNameList.add(filteredPolicyName);
+            } else {
+                String inputPolicyName = (String) ((Map<String, Object>) ((Map<String, Object>) ((Map<String, Object>) ef
+                        .getValue()).get("properties")).get("policy_id")).get("get_input");
+                if (inputPolicyName != null) {
+                    policyNameList.add("get_input");
+                }
+            }
+        });
+        if (policyNameList.size() > 1) {
+            throw new SdcArtifactInstallerException(
+                    "The code does not currently support multiple Policy MicroServices in the blueprint");
+        } else if (policyNameList.isEmpty()) {
+            throw new SdcArtifactInstallerException(
+                    "There is no recognized Policy MicroService found in the blueprint");
+        }
+        return policyNameList.get(0);
     }
 
-    private String createModelName(CsarHandler csar) {
-        return csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + MODEL_NAME_SUFFIX;
+    private String queryDcaeToGetServiceTypeId(CsarHandler csar) throws IOException, ParseException {
+        return dcaeInventoryService.getDcaeInformation(csar.getBlueprintArtifactName(),
+                csar.getBlueprintInvariantServiceUuid(), csar.getBlueprintInvariantResourceUuid());
     }
 
     private CldsTemplate createFakeCldsTemplate(CsarHandler csar, BlueprintParserFilesConfiguration configFiles)
@@ -129,19 +161,26 @@
         template.setBpmnId("Sdc-Generated");
         template.setBpmnText(
                 IOUtils.toString(appContext.getResource(configFiles.getBpmnXmlFilePath()).getInputStream()));
-        // ((ObjectNode)refProp.getJsonTemplate(CldsService.GLOBAL_PROPERTIES_KEY));
         template.setPropText(csar.getDcaeBlueprint());
         template.setImageText(
                 IOUtils.toString(appContext.getResource(configFiles.getSvgXmlFilePath()).getInputStream()));
-        return cldsTemplateService.putTemplate(createTemplateName(csar), template);
+        template.setName(TEMPLATE_NAME_PREFIX + csar.getSdcCsarHelper().getServiceMetadata().getValue("name"));
+        template.save(cldsDao, null);
+        return template;
     }
 
-    private CldsModel createFakeCldsModel(CsarHandler csar, BlueprintParserFilesConfiguration configFiles,
-            CldsTemplate cldsTemplate) {
+    private CldsModel createFakeCldsModel(CsarHandler csar, CldsTemplate cldsTemplate, String serviceTypeId) {
         CldsModel cldsModel = new CldsModel();
+        cldsModel.setControlNamePrefix(MODEL_NAME_PREFIX);
+        cldsModel.setName(csar.getSdcCsarHelper().getServiceMetadata().getValue("name"));
         cldsModel.setBlueprintText(csar.getDcaeBlueprint());
         cldsModel.setTemplateName(cldsTemplate.getName());
-        // cldsModel.set
-        return cldsService.putModel(createModelName(csar), cldsModel);
+        cldsModel.setTemplateId(cldsTemplate.getId());
+        cldsModel.setDocText(cldsTemplate.getPropText());
+        cldsModel.setPropText("{}");
+        cldsModel.setBpmnText(cldsTemplate.getBpmnText());
+        cldsModel.setTypeId(serviceTypeId);
+        cldsModel.save(cldsDao, null);
+        return cldsModel;
     }
 }
diff --git a/src/main/resources/clds/camel/flexible-flow.xml b/src/main/resources/clds/camel/flexible-flow.xml
index 7a3ebdb..b2a728d 100644
--- a/src/main/resources/clds/camel/flexible-flow.xml
+++ b/src/main/resources/clds/camel/flexible-flow.xml
@@ -4,7 +4,6 @@
 				<choice>
 						<when>
 								<simple> ${exchangeProperty.actionCd} == 'SUBMIT' || ${exchangeProperty.actionCd} == 'RESUBMIT'</simple>
-								<to uri="bean:org.onap.clamp.clds.client.SdcSendReqDelegate" />	
 								<to uri="bean:org.onap.clamp.clds.client.TcaPolicyDelegate" />
 								<to uri="bean:org.onap.clamp.clds.client.HolmesPolicyDelegate" />
 								<to uri="bean:org.onap.clamp.clds.client.OperationalPolicyDelegate" />
diff --git a/src/test/java/org/onap/clamp/clds/it/sdc/controller/installer/CsarInstallerItCase.java b/src/test/java/org/onap/clamp/clds/it/sdc/controller/installer/CsarInstallerItCase.java
index 94adfaf..6ae64ef 100644
--- a/src/test/java/org/onap/clamp/clds/it/sdc/controller/installer/CsarInstallerItCase.java
+++ b/src/test/java/org/onap/clamp/clds/it/sdc/controller/installer/CsarInstallerItCase.java
@@ -38,6 +38,7 @@
 import org.onap.clamp.clds.dao.CldsDao;
 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
+import org.onap.clamp.clds.model.CldsModel;
 import org.onap.clamp.clds.model.CldsTemplate;
 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
 import org.onap.clamp.clds.sdc.controller.installer.CsarInstaller;
@@ -90,7 +91,7 @@
         Mockito.when(csarHelper.getServiceMetadata()).thenReturn(data);
         Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(csarHelper);
         csarInstaller.installTheCsar(csarHandler);
-        // Get it back from DB
+        // Get the template back from DB
         CldsTemplate templateFromDB = CldsTemplate.retrieve(cldsDao,
                 generatedName + CsarInstallerImpl.TEMPLATE_NAME_SUFFIX, false);
         assertNotNull(templateFromDB);
@@ -98,5 +99,12 @@
         assertNotNull(templateFromDB.getImageText());
         assertNotNull(templateFromDB.getPropText());
         assertEquals(templateFromDB.getName(), generatedName + CsarInstallerImpl.TEMPLATE_NAME_SUFFIX);
+        // Get the Model back from DB
+        CldsModel modelFromDB = CldsModel.retrieve(cldsDao, generatedName + CsarInstallerImpl.MODEL_NAME_SUFFIX, false);
+        assertNotNull(modelFromDB);
+        assertNotNull(modelFromDB.getBpmnText());
+        assertNotNull(modelFromDB.getImageText());
+        assertNotNull(modelFromDB.getPropText());
+        assertEquals(modelFromDB.getName(), generatedName + CsarInstallerImpl.MODEL_NAME_SUFFIX);
     }
 }
diff --git a/src/test/resources/clds/blueprint-parser-mapping.json b/src/test/resources/clds/blueprint-parser-mapping.json
index a16ef0c..a22e9fc 100644
--- a/src/test/resources/clds/blueprint-parser-mapping.json
+++ b/src/test/resources/clds/blueprint-parser-mapping.json
@@ -1,6 +1,7 @@
 [
 	{
 		"blueprintKey": "tca_",
+		"dcaeDeployable":"true",
 		"files": {
 			"bpmnXmlFilePath": "classpath:/clds/templates/bpmn/tca-template.xml",
 			"svgXmlFilePath": "classpath:/clds/templates/bpmn/tca-img.xml"
@@ -8,6 +9,7 @@
 	},
 	{
 		"blueprintKey": "holmes_",
+		"dcaeDeployable":"false",
 		"files": {
 			"bpmnXmlFilePath": "classpath:/clds/templates/bpmn/holmes-template.xml",
 			"svgXmlFilePath": "classpath:/clds/templates/bpmn/holmes-img.xml"
diff --git a/src/test/resources/example/sdc/blueprint-dcae/holmes.yaml b/src/test/resources/example/sdc/blueprint-dcae/holmes.yaml
index 1277a02..f180a7d 100644
--- a/src/test/resources/example/sdc/blueprint-dcae/holmes.yaml
+++ b/src/test/resources/example/sdc/blueprint-dcae/holmes.yaml
@@ -22,6 +22,11 @@
   topic1_client_role:
     type: string
 node_templates:
+  policy_0:
+    type: dcae.nodes.policy
+    properties:
+      policy_model: policy.nodes.holmes
+      policy_filter: "DCAE.Config_Holmes.*"
   docker_host_host:
     type: dcae.nodes.SelectedDockerHost
     properties:
@@ -157,6 +162,8 @@
       type: dcae.relationships.subscribe_to_events
     - target: topic1
       type: dcae.relationships.subscribe_to_events
+    - target: policy_0
+      type: dcae.relationships.depends_on
   topic0:
     type: dcae.nodes.Topic
     properties:
diff --git a/src/test/resources/example/sdc/blueprint-dcae/tca.yaml b/src/test/resources/example/sdc/blueprint-dcae/tca.yaml
index 101dc2c..1a756ee 100644
--- a/src/test/resources/example/sdc/blueprint-dcae/tca.yaml
+++ b/src/test/resources/example/sdc/blueprint-dcae/tca.yaml
@@ -9,7 +9,14 @@
     type: string
   service_id:
     type: string
+  policy_id:
+    type: string
 node_templates:
+  policy_0:
+    type: dcae.nodes.policy
+    properties:
+      policy_id: 
+        get_input: policy_id
   cdap_host_host:
     type: dcae.nodes.StreamingAnalytics.SelectedCDAPInfrastructure
     properties:
@@ -78,5 +85,20 @@
         get_input: service_id
       streamname: TCASubscriberOutputStream
     relationships:
+    - target: topic0
+      type: dcae.relationships.subscribe_to_events
+    - target: topic1
+      type: dcae.relationships.publish_events
     - target: cdap_host_host
-      type: dcae.relationships.component_contained_in
\ No newline at end of file
+      type: dcae.relationships.component_contained_in
+    - target: policy_0
+      type: dcae.relationships.depends_on
+  topic0:
+    type: dcae.nodes.Topic
+    properties:
+      topic_name: ''
+  topic1:
+    type: dcae.nodes.Topic
+    properties:
+      topic_name: ''
+      
\ No newline at end of file