Use getEntity to populate network_role

Changes made:
Update sdc-tosca version to 1.5.0.
Replace getCpListByVf with equivalent getEntity call in SdncVFModel.java.
Replace policy.getMetaData with getMetaDataObj to work around parser issue.

Change-Id: I1d972f75cc5ca1c8dc0e5d58880a3bb306608ca2
Issue-ID: CCSDK-1089
Signed-off-by: Ubuntu <lalena.aria@att.com>
diff --git a/ueb-listener/pom.xml b/ueb-listener/pom.xml
index 87a9dfe..df20a01 100755
--- a/ueb-listener/pom.xml
+++ b/ueb-listener/pom.xml
@@ -18,7 +18,7 @@
 
     <properties>
         <sdc.client.version>1.3.0</sdc.client.version>
-        <sdc.tosca.version>1.4.7</sdc.tosca.version>
+        <sdc.tosca.version>1.5.0</sdc.tosca.version>
         <fasterxml.jackson.version>2.9.4</fasterxml.jackson.version>
         <skip.SWM>true</skip.SWM>
         <ueb.listener.base>/opt/app/ueb-listener</ueb.listener.base>
diff --git a/ueb-listener/src/main/java/org/onap/ccsdk/sli/northbound/uebclient/SdncBaseModel.java b/ueb-listener/src/main/java/org/onap/ccsdk/sli/northbound/uebclient/SdncBaseModel.java
index 0d5e9d6..37b100c 100644
--- a/ueb-listener/src/main/java/org/onap/ccsdk/sli/northbound/uebclient/SdncBaseModel.java
+++ b/ueb-listener/src/main/java/org/onap/ccsdk/sli/northbound/uebclient/SdncBaseModel.java
@@ -278,9 +278,9 @@
 		for (Policy policy : policyList) {
 			
 			// extract policy metadata
-			String policyUuid = policy.getMetaData().getOrDefault("UUID", "").toString(); 
-			String policyInvariantUuid = policy.getMetaData().getOrDefault("invariantUUID", "").toString();
-			String policyCustomizationUuid = policy.getMetaData().getOrDefault("customizationUUID", "").toString();
+			String policyUuid = policy.getMetaDataObj().getAllProperties().getOrDefault("UUID", "").toString();
+			String policyInvariantUuid = policy.getMetaDataObj().getAllProperties().getOrDefault("invariantUUID", "").toString();
+			String policyCustomizationUuid = policy.getMetaDataObj().getAllProperties().getOrDefault("customizationUUID", "").toString();
 			
 			// cleanup existing RESOURCE_POLICY data
 			Map<String, String> cleanupParams = new HashMap<String, String>();
@@ -293,9 +293,9 @@
 			addParameter("policy_uuid", policyUuid, policyParams);
 			addParameter("policy_customization_uuid", policyCustomizationUuid, policyParams);
 			addParameter("policy_invariant_uuid", policyInvariantUuid, policyParams);
-			addParameter("policy_name", policy.getMetaData().getOrDefault(PARAM_NAME_KEY, "").toString(), policyParams);
-			addParameter(PARAM_VERSION_KEY, policy.getMetaData().getOrDefault(PARAM_VERSION_KEY, "").toString(), policyParams);
-			addParameter("policy_type", policy.getMetaData().getOrDefault(PARAM_TYPE_KEY, "").toString(), policyParams);
+			addParameter("policy_name", policy.getMetaDataObj().getAllProperties().getOrDefault(PARAM_NAME_KEY, "").toString(), policyParams);
+			addParameter(PARAM_VERSION_KEY, policy.getMetaDataObj().getAllProperties().getOrDefault(PARAM_VERSION_KEY, "").toString(), policyParams);
+			addParameter("policy_type", policy.getType(), policyParams);
 			
 			// extract properties
 			addParameter("property_type", extractValue(policy, PARAM_TYPE_KEY), policyParams);
@@ -350,8 +350,8 @@
 		for (Policy policy : policyList) {
 			
 			// extract policy metadata
-			String policyUuid = policy.getMetaData().getOrDefault("UUID", "").toString();
-			String policyInvariantUuid = policy.getMetaData().getOrDefault("invariantUUID", "").toString();
+			String policyUuid = policy.getMetaDataObj().getAllProperties().getOrDefault("UUID", "").toString();
+			String policyInvariantUuid = policy.getMetaDataObj().getAllProperties().getOrDefault("invariantUUID", "").toString();
 			
 			// cleanup existing RESOURCE_POLICY data
 			Map<String, String> cleanupParams = new HashMap<String, String>();
@@ -363,8 +363,8 @@
 			Map<String, String> policyParams = new HashMap<String, String>();
 			addParameter("policy_uuid", policyUuid, policyParams);
 			addParameter("policy_invariant_uuid", policyInvariantUuid, policyParams);
-			addParameter("policy_name", policy.getMetaData().getOrDefault("name", "").toString(), policyParams);
-			addParameter("version", policy.getMetaData().getOrDefault("version", "").toString(), policyParams);
+			addParameter("policy_name", policy.getMetaDataObj().getAllProperties().getOrDefault("name", "").toString(), policyParams);
+			addParameter("version", policy.getMetaDataObj().getAllProperties().getOrDefault("version", "").toString(), policyParams);
 			addParameter("policy_type", policy.getType(), policyParams);
 			
 			// extract properties
@@ -386,6 +386,10 @@
 			
 			// insert into RESOURCE_POLICY_TO_TARGET_NODE_MAPPING
 			List<String> policyTargetNameList = policy.getTargets();
+			if (policyTargetNameList == null) {
+				continue;
+			}
+
 			for (String targetName : policyTargetNameList) {
 				NodeTemplate targetNode = sdcCsarHelper.getNodeTemplateByName(targetName);
 				
@@ -423,8 +427,8 @@
 		for (Policy policy : policyList) {
 			
 			// extract policy metadata
-			String policyUuid = policy.getMetaData().getOrDefault("UUID", "").toString();
-			String policyInvariantUuid = policy.getMetaData().getOrDefault("invariantUUID", "").toString();
+			String policyUuid = policy.getMetaDataObj().getAllProperties().getOrDefault("UUID", "").toString();
+			String policyInvariantUuid = policy.getMetaDataObj().getAllProperties().getOrDefault("invariantUUID", "").toString();
 			
 			// cleanup existing RESOURCE_POLICY data
 			Map<String, String> cleanupParams = new HashMap<String, String>();
@@ -436,9 +440,9 @@
 			Map<String, String> policyParams = new HashMap<String, String>();
 			addParameter("policy_uuid", policyUuid, policyParams);
 			addParameter("policy_invariant_uuid", policyInvariantUuid, policyParams);
-			String policyName = policy.getMetaData().getOrDefault(PARAM_NAME_KEY, "").toString();
+			String policyName = policy.getMetaDataObj().getAllProperties().getOrDefault(PARAM_NAME_KEY, "").toString();
 			addParameter("policy_name", policyName, policyParams);
-			addParameter(PARAM_VERSION_KEY, policy.getMetaData().getOrDefault(PARAM_VERSION_KEY, "").toString(), policyParams);
+			addParameter(PARAM_VERSION_KEY, policy.getMetaDataObj().getAllProperties().getOrDefault(PARAM_VERSION_KEY, "").toString(), policyParams);
 			addParameter("policy_type", policy.getType(), policyParams);
 			
 			// extract properties
@@ -460,6 +464,10 @@
 			
 			// insert into RESOURCE_POLICY_TO_TARGET_NODE_MAPPING
 			List<NodeTemplate> targetNodeList = sdcCsarHelper.getPolicyTargetsFromOrigin(nodeTemplate, policyName);
+			if (targetNodeList == null) {
+				continue;
+			}
+
 			for (NodeTemplate targetNode : targetNodeList) {
 				//NodeTemplate targetNode = sdcCsarHelper.getNodeTemplateByName(targetName);
 				if (targetNode == null) {					
diff --git a/ueb-listener/src/main/java/org/onap/ccsdk/sli/northbound/uebclient/SdncVFModel.java b/ueb-listener/src/main/java/org/onap/ccsdk/sli/northbound/uebclient/SdncVFModel.java
index 850a34f..a42f03d 100644
--- a/ueb-listener/src/main/java/org/onap/ccsdk/sli/northbound/uebclient/SdncVFModel.java
+++ b/ueb-listener/src/main/java/org/onap/ccsdk/sli/northbound/uebclient/SdncVFModel.java
@@ -27,10 +27,14 @@
 import java.util.Map;
 
 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
+import org.onap.sdc.tosca.parser.enums.SdcTypes;
 import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
+import org.onap.sdc.tosca.parser.api.IEntityDetails;
+import org.onap.sdc.tosca.parser.elements.queries.EntityQuery;
+import org.onap.sdc.tosca.parser.elements.queries.TopologyTemplateQuery;
 import org.onap.sdc.toscaparser.api.Group;
 import org.onap.sdc.toscaparser.api.NodeTemplate;
-import org.onap.sdc.toscaparser.api.Policy;
+import org.onap.sdc.toscaparser.api.Property;
 import org.onap.sdc.toscaparser.api.elements.Metadata;
 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
 import org.slf4j.Logger;
@@ -162,21 +166,48 @@
 	
 	private void insertVFtoNetworkRoleMappingData () throws IOException {
 		
-		// For each VF, insert VF_TO_NETWORK_ROLE_MAPPING data
-		List<NodeTemplate> cpNodes = sdcCsarHelper.getCpListByVf(getCustomizationUUIDNoQuotes());
-		for (NodeTemplate cpNode : cpNodes){
+		// Cleanup existing VF_TO_NETWORK_ROLE_MAPPING for this VF
+		try {
+			cleanUpExistingToscaData("VF_TO_NETWORK_ROLE_MAPPING", "vf_customization_uuid", getCustomizationUUID());
+		} catch (IOException e) {
+			LOG.error("Could not cleanup Tosca CSAR data into the VF_TO_NETWORK_ROLE_MAPPING table");
+			throw new IOException (e);
+		}	
+		
+		// For this VF, insert VF_TO_NETWORK_ROLE_MAPPING data
+		EntityQuery entityQueryCP = EntityQuery.newBuilder(SdcTypes.CP).build();
+	    TopologyTemplateQuery topologyTemplateQueryVF = TopologyTemplateQuery.newBuilder(SdcTypes.VF).customizationUUID(getCustomizationUUIDNoQuotes()).build();
+	    List<IEntityDetails> cpEntities = sdcCsarHelper.getEntity(entityQueryCP, topologyTemplateQueryVF, true);
+	    
+		for (IEntityDetails entity: cpEntities ) {		
+			
+			Map<String, Property> properties = entity.getProperties();
+			if (properties.containsKey("network_role")) {
 
-			// Insert into VF_TO_NETWORK_ROLE_MAPPING vf_customization_uuid and network_role
-			String cpNetworkRole = sdcCsarHelper.getNodeTemplatePropertyLeafValue(cpNode, "network_role");
-
-			try {
-				cleanUpExistingToscaData("VF_TO_NETWORK_ROLE_MAPPING", "vf_customization_uuid", getCustomizationUUID());
-				LOG.info("Call insertToscaData for VF_TO_NETWORK_ROLE_MAPPING where vf_customization_uuid = " + getCustomizationUUID());
-				insertToscaData("insert into VF_TO_NETWORK_ROLE_MAPPING (vf_customization_uuid, network_role) values (" +
-				getCustomizationUUID() + ", \"" + cpNetworkRole + "\")", null);
-			} catch (IOException e) {
-				LOG.error("Could not insert Tosca CSAR data into the VF_TO_NETWORK_ROLE_MAPPING table");
-				throw new IOException (e);
+				Property networkRoleProperty = properties.get("network_role");
+				if (networkRoleProperty != null && networkRoleProperty.getValue() != null) {
+					String cpNetworkRole = networkRoleProperty.getValue().toString();
+					LOG.debug("insertVFtoNetworkRoleMappingData: " + "VF: "  + getCustomizationUUID() + ", networkRole = " + cpNetworkRole);
+					
+					// Only insert unique network_role values for this VF
+					boolean networkRoleExists = false;
+					Map<String, String> networkRoleyKeys = new HashMap<String, String>();
+					networkRoleyKeys.put("vf_customization_uuid", getCustomizationUUID());
+					networkRoleyKeys.put("network_role", "\"" + cpNetworkRole + "\"");			
+					networkRoleExists = checkForExistingToscaData("VF_TO_NETWORK_ROLE_MAPPING", networkRoleyKeys);
+					
+					if (networkRoleExists == false) {
+						try {
+							//cleanUpExistingToscaData("VF_TO_NETWORK_ROLE_MAPPING", "vf_customization_uuid", getCustomizationUUID());
+							LOG.info("Call insertToscaData for VF_TO_NETWORK_ROLE_MAPPING where vf_customization_uuid = " + getCustomizationUUID());
+							insertToscaData("insert into VF_TO_NETWORK_ROLE_MAPPING (vf_customization_uuid, network_role) values (" +
+							getCustomizationUUID() + ", \"" + cpNetworkRole + "\")", null);
+						} catch (IOException e) {
+							LOG.error("Could not insert Tosca CSAR data into the VF_TO_NETWORK_ROLE_MAPPING table");
+							throw new IOException (e);
+						}							
+					}
+				}
 			}
 
 		} // CP loop