Fix model type searching

Change the way we get the model type in the dcaeblueprint

Issue-ID: CLAMP-370
Change-Id: I961a20e5d993049c73acd67b6cbfe245e1676dda
Signed-off-by: sebdet <sebastien.determe@intl.att.com>
diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java
index aca2ed0..3792c17 100644
--- a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java
+++ b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java
@@ -53,21 +53,23 @@
     private static final String TYPE = "type";
     private static final String PROPERTIES = "properties";
     private static final String NAME = "name";
-    private static final String POLICYID = "policy_id";
-    private static final String POLICY_TYPEID = "policy_type_id";
+    private static final String INPUT = "inputs";
+    private static final String GET_INPUT = "get_input";
+    private static final String POLICY_MODELID = "policy_model_id";
     private static final String RELATIONSHIPS = "relationships";
     private static final String CLAMP_NODE_RELATIONSHIPS_GETS_INPUT_FROM = "clamp_node.relationships.gets_input_from";
     private static final String TARGET = "target";
 
     public Set<MicroService> getMicroServices(String blueprintString) {
         Set<MicroService> microServices = new HashSet<>();
-        JsonObject jsonObject = BlueprintParser.convertToJson(blueprintString);
-        JsonObject nodeTemplateList = jsonObject.get(NODE_TEMPLATES).getAsJsonObject();
+        JsonObject blueprintJson = BlueprintParser.convertToJson(blueprintString);
+        JsonObject nodeTemplateList = blueprintJson.get(NODE_TEMPLATES).getAsJsonObject();
+        JsonObject inputList = blueprintJson.get(INPUT).getAsJsonObject();
 
         for (Entry<String, JsonElement> entry : nodeTemplateList.entrySet()) {
             JsonObject nodeTemplate = entry.getValue().getAsJsonObject();
             if (nodeTemplate.get(TYPE).getAsString().contains(DCAE_NODES)) {
-                MicroService microService = getNodeRepresentation(entry, nodeTemplateList);
+                MicroService microService = getNodeRepresentation(entry, nodeTemplateList, inputList);
                 microServices.add(microService);
             }
         }
@@ -119,12 +121,12 @@
         return "";
     }
 
-    String findModelTypeInTargetArray(JsonArray jsonArray, JsonObject nodeTemplateList) {
+    String findModelTypeInTargetArray(JsonArray jsonArray, JsonObject nodeTemplateList, JsonObject inputList) {
         for (JsonElement elem : jsonArray) {
             String modelType = getModelType(
                 new AbstractMap.SimpleEntry<String, JsonElement>(elem.getAsJsonObject().get(TARGET).getAsString(),
                     nodeTemplateList.get(elem.getAsJsonObject().get(TARGET).getAsString()).getAsJsonObject()),
-                nodeTemplateList);
+                nodeTemplateList, inputList);
             if (!modelType.isEmpty()) {
                 return modelType;
             }
@@ -132,29 +134,34 @@
         return "";
     }
 
-    String getModelType(Entry<String, JsonElement> entry, JsonObject nodeTemplateList) {
+    String getModelType(Entry<String, JsonElement> entry, JsonObject nodeTemplateList, JsonObject inputList) {
         JsonObject ob = entry.getValue().getAsJsonObject();
         // Search first in this node template
         if (ob.has(PROPERTIES)) {
             JsonObject properties = ob.get(PROPERTIES).getAsJsonObject();
-            if (properties.has(POLICYID)) {
-                JsonObject policyIdObj = properties.get(POLICYID).getAsJsonObject();
-                if (policyIdObj.has(POLICY_TYPEID)) {
-                    return policyIdObj.get(POLICY_TYPEID).getAsString();
+            if (properties.has(POLICY_MODELID)) {
+                if (properties.get(POLICY_MODELID).isJsonObject()) {
+                    // it's a blueprint parameter
+                    return inputList.get(properties.get(POLICY_MODELID).getAsJsonObject().get(GET_INPUT).getAsString())
+                        .getAsJsonObject().get("default").getAsString();
+                } else {
+                    // It's a direct value
+                    return properties.get(POLICY_MODELID).getAsString();
                 }
             }
         }
-        // Then it's may be a relationship
+        // Or it's may be defined in a relationship
         if (ob.has(RELATIONSHIPS)) {
-            return findModelTypeInTargetArray(ob.get(RELATIONSHIPS).getAsJsonArray(), nodeTemplateList);
+            return findModelTypeInTargetArray(ob.get(RELATIONSHIPS).getAsJsonArray(), nodeTemplateList, inputList);
         }
         return "";
     }
 
-    MicroService getNodeRepresentation(Entry<String, JsonElement> entry, JsonObject nodeTemplateList) {
+    MicroService getNodeRepresentation(Entry<String, JsonElement> entry, JsonObject nodeTemplateList,
+        JsonObject inputList) {
         String name = getName(entry);
         String getInputFrom = getInput(entry);
-        String modelType = getModelType(entry, nodeTemplateList);
+        String modelType = getModelType(entry, nodeTemplateList, inputList);
         return new MicroService(name, modelType, getInputFrom, "");
     }
 
diff --git a/src/test/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParserTest.java b/src/test/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParserTest.java
index 7a1f9f0..dec6397 100644
--- a/src/test/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParserTest.java
+++ b/src/test/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParserTest.java
@@ -144,7 +144,7 @@
 
         MicroService expected = new MicroService(SECOND_APPP, MODEL_TYPE1, FIRST_APPP, "");
         Entry<String, JsonElement> entry = jsonObject.entrySet().iterator().next();
-        MicroService actual = new BlueprintParser().getNodeRepresentation(entry, jsonObject);
+        MicroService actual = new BlueprintParser().getNodeRepresentation(entry, jsonObject, null);
 
         Assert.assertEquals(expected, actual);
     }
diff --git a/src/test/resources/clds/blueprint-with-microservice-chain.yaml b/src/test/resources/clds/blueprint-with-microservice-chain.yaml
index 4a7e5d7..fa2d720 100644
--- a/src/test/resources/clds/blueprint-with-microservice-chain.yaml
+++ b/src/test/resources/clds/blueprint-with-microservice-chain.yaml
@@ -31,7 +31,7 @@
       service_component_name_override: second_app
       image: { get_input: second_app_docker_image }
       policy_id:
-        policy_type_id: type2
+      policy_model_id: "type2"
     interfaces:
       cloudify.interfaces.lifecycle:
         start:
@@ -56,7 +56,7 @@
       image: { get_input: first_app_docker_image }
       container_port: 6565
       policy_id:
-        policy_type_id: type1
+      policy_model_id: "type1"
     interfaces:
       cloudify.interfaces.lifecycle:
         start:
@@ -81,7 +81,7 @@
       image: { get_input: third_app_docker_image }
       container_port: 443
       policy_id:
-        policy_type_id: type3
+      policy_model_id: "type3"
     interfaces:
       cloudify.interfaces.lifecycle:
         start:
diff --git a/src/test/resources/clds/single-microservice-fragment-valid.yaml b/src/test/resources/clds/single-microservice-fragment-valid.yaml
index 269ee50..2c16807 100644
--- a/src/test/resources/clds/single-microservice-fragment-valid.yaml
+++ b/src/test/resources/clds/single-microservice-fragment-valid.yaml
@@ -6,7 +6,7 @@
     image: { get_input: second_app_docker_image }
     name: second_app
     policy_id:
-        policy_type_id: type1
+    policy_model_id: "type1"
   interfaces:
     cloudify.interfaces.lifecycle:
       start:
diff --git a/src/test/resources/example/sdc/blueprint-dcae/prop-text-for-tca-3.json b/src/test/resources/example/sdc/blueprint-dcae/prop-text-for-tca-3.json
index f8f5316..012c46e 100644
--- a/src/test/resources/example/sdc/blueprint-dcae/prop-text-for-tca-3.json
+++ b/src/test/resources/example/sdc/blueprint-dcae/prop-text-for-tca-3.json
@@ -40,7 +40,8 @@
 				"cbs_host": "config-binding-service.dcae.svc.cluster.local",
 				"cbs_port": "10000",
 				"external_port": "32012",
-				"policy_id": "AUTO_GENERATED_POLICY_ID_AT_SUBMIT"
+				"policy_id": "AUTO_GENERATED_POLICY_ID_AT_SUBMIT",
+				"policy_model_id": "onap.policies.monitoring.cdap.tca.hi.lo.app"
 			}
 		}
 	]
diff --git a/src/test/resources/example/sdc/blueprint-dcae/tca.yaml b/src/test/resources/example/sdc/blueprint-dcae/tca.yaml
index edaa0be..0cb9cdb 100644
--- a/src/test/resources/example/sdc/blueprint-dcae/tca.yaml
+++ b/src/test/resources/example/sdc/blueprint-dcae/tca.yaml
@@ -17,7 +17,7 @@
     properties:
       policy_id: 
         get_input: policy_id
-        policy_type_id: onap.policies.monitoring.cdap.tca.hi.lo.app
+      policy_model_id: "onap.policies.monitoring.cdap.tca.hi.lo.app"
   cdap_host_host:
     type: dcae.nodes.StreamingAnalytics.SelectedCDAPInfrastructure
     properties:
diff --git a/src/test/resources/example/sdc/blueprint-dcae/tca_2.yaml b/src/test/resources/example/sdc/blueprint-dcae/tca_2.yaml
index 56ae32a..00ebfe7 100644
--- a/src/test/resources/example/sdc/blueprint-dcae/tca_2.yaml
+++ b/src/test/resources/example/sdc/blueprint-dcae/tca_2.yaml
@@ -171,4 +171,4 @@
     properties:
       policy_id:
            get_input: policy_id
-           policy_type_id: onap.policies.monitoring.cdap.tca.hi.lo.app
+      policy_model_id: "onap.policies.monitoring.cdap.tca.hi.lo.app"
diff --git a/src/test/resources/example/sdc/blueprint-dcae/tca_3.yaml b/src/test/resources/example/sdc/blueprint-dcae/tca_3.yaml
index 53cfc4f..6fab504 100644
--- a/src/test/resources/example/sdc/blueprint-dcae/tca_3.yaml
+++ b/src/test/resources/example/sdc/blueprint-dcae/tca_3.yaml
@@ -51,6 +51,9 @@
     type: string
     description: Kubernetes node port on which CDAPgui is exposed
     default: "32012"
+  policy_model_id:
+    type: string
+    default: "onap.policies.monitoring.cdap.tca.hi.lo.app"
 
 node_templates:
   tca_k8s:
@@ -150,4 +153,5 @@
     properties:
       policy_id:
            get_input: policy_id
-           policy_type_id: onap.policies.monitoring.cdap.tca.hi.lo.app
+      policy_model_id: 
+           get_input: policy_model_id