Re-factor matchable to reduce complexity
[policy/xacml-pdp.git] / applications / common / src / main / java / org / onap / policy / pdp / xacml / application / common / std / StdMatchableTranslator.java
index 690b710..11a45e0 100644 (file)
@@ -35,7 +35,6 @@ import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
@@ -44,9 +43,9 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import lombok.Setter;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
@@ -61,8 +60,8 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
 import org.onap.policy.pdp.xacml.application.common.OnapObligation;
 import org.onap.policy.pdp.xacml.application.common.PolicyApiCaller;
 import org.onap.policy.pdp.xacml.application.common.PolicyApiException;
@@ -70,6 +69,9 @@ import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslatorUtils;
 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
+import org.onap.policy.pdp.xacml.application.common.matchable.MatchableCallback;
+import org.onap.policy.pdp.xacml.application.common.matchable.MatchablePolicyType;
+import org.onap.policy.pdp.xacml.application.common.matchable.MatchableProperty;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -80,16 +82,14 @@ import org.slf4j.LoggerFactory;
  * @author pameladragosh
  *
  */
-public class StdMatchableTranslator  extends StdBaseTranslator {
+public class StdMatchableTranslator  extends StdBaseTranslator implements MatchableCallback {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(StdMatchableTranslator.class);
     private static final StandardYamlCoder standardYamlCoder = new StandardYamlCoder();
 
-    private static final String MSG_WEIGHT = "Weight is {}";
-    private static final String MSG_WEIGHT_LIST = "Weight list is {}";
-    private static final String MSG_WEIGHT_MAP = "Weight map is {}";
-
     private final Map<ToscaPolicyTypeIdentifier, ToscaServiceTemplate> matchablePolicyTypes = new HashMap<>();
+    private final Map<ToscaPolicyTypeIdentifier, MatchablePolicyType> matchableCache = new HashMap<>();
+
     @Setter
     private RestServerParameters apiRestParameters;
     @Setter
@@ -322,6 +322,50 @@ public class StdMatchableTranslator  extends StdBaseTranslator {
         return newPolicyType;
     }
 
+    @Override
+    public ToscaPolicyType retrievePolicyType(String derivedFrom) {
+        ToscaServiceTemplate template = this.findPolicyType(new ToscaPolicyTypeIdentifier(derivedFrom, "1.0.0"));
+        if (template == null) {
+            LOGGER.error("Could not retrieve Policy Type {}", derivedFrom);
+            return null;
+        }
+        return template.getPolicyTypes().get(derivedFrom);
+    }
+
+    @Override
+    public ToscaDataType retrieveDataType(String datatype) {
+        //
+        // Our outer class is not storing the current template being scanned
+        //
+        LOGGER.error("this retrieveDataType should not be called.");
+        return null;
+    }
+
+    private class MyMatchableCallback implements MatchableCallback {
+        private StdMatchableTranslator translator;
+        private ToscaServiceTemplate template;
+
+        public MyMatchableCallback(StdMatchableTranslator translator, ToscaServiceTemplate template) {
+            this.translator = translator;
+            this.template = template;
+        }
+
+        @Override
+        public ToscaPolicyType retrievePolicyType(String derivedFrom) {
+            ToscaPolicyType policyType = this.template.getPolicyTypes().get(derivedFrom);
+            if (policyType != null) {
+                return policyType;
+            }
+            return translator.retrievePolicyType(derivedFrom);
+        }
+
+        @Override
+        public ToscaDataType retrieveDataType(String datatype) {
+            return this.template.getDataTypes().get(datatype);
+        }
+
+    }
+
     /**
      * For generating target type, we scan for matchable properties
      * and use those to build the policy.
@@ -330,496 +374,100 @@ public class StdMatchableTranslator  extends StdBaseTranslator {
      * @param policyTypes Collection of policy Type to find matchable metadata
      * @return {@code Pair<TargetType, Integer>} Returns a TargetType and a Total Weight of matchables.
      */
-    protected Pair<TargetType, Integer> generateTargetType(ToscaPolicy policyType,
-            ToscaServiceTemplate policyTemplate) {
+    protected Pair<TargetType, Integer> generateTargetType(ToscaPolicy policy, ToscaServiceTemplate template) {
         //
         // Our return object
         //
-        TargetType targetType = new TargetType();
-        //
-        // Top-level list of properties
-        //
-        Map<String, Object> properties = policyType.getProperties();
+        TargetType target = new TargetType();
         //
-        // To start, we know these properties are for this specific Policy Type ID/Version
+        // See if we have a matchable in the cache already
         //
-        ToscaPolicyTypeIdentifier propertiesPolicyId = policyType.getTypeIdentifier();
+        MatchablePolicyType matchablePolicyType = matchableCache.get(policy.getTypeIdentifier());
         //
-        // Scan the property map for matchables
+        // If not found, create one
         //
-        int totalWeight = findMatchablesInProperties(properties, propertiesPolicyId, policyTemplate, targetType);
-        LOGGER.info("Total weight is {}", totalWeight);
-        return Pair.of(targetType, totalWeight);
-    }
-
-    protected int findMatchablesInProperties(Map<String, Object> properties,
-            ToscaPolicyTypeIdentifier propertiesPolicyId,
-            ToscaServiceTemplate policyTemplate,
-            TargetType targetType) {
-        LOGGER.info("findMatchablesInProperties from policy Type {} {}", propertiesPolicyId, properties);
-        //
-        // We better have the policy type definition available from the template
-        //
-        ToscaPolicyType policyType = getToscaPolicyTypeFromTemplate(propertiesPolicyId, policyTemplate);
-        if (policyType == null) {
-            LOGGER.error("Failed to find policy type in template {}", propertiesPolicyId);
-            return 0;
-        }
-        //
-        // Our total weight to return
-        //
-        int totalWeight = 0;
-        for (Entry<String, Object> entrySet : properties.entrySet()) {
+        if (matchablePolicyType == null) {
             //
-            // Find the property details
+            // Our callback
             //
-            Pair<ToscaProperty, ToscaServiceTemplate> property = findProperty(entrySet.getKey(),
-                    policyType, propertiesPolicyId, policyTemplate);
-            if (property == null) {
-                continue;
-            }
-            ToscaProperty toscaProperty = property.getLeft();
-            LOGGER.info("Found property {} with type {} schema {}", entrySet.getKey(), toscaProperty.getType(),
-                    (toscaProperty.getEntrySchema() == null ? "null" : toscaProperty.getEntrySchema().getType()));
+            MyMatchableCallback myCallback = new MyMatchableCallback(this, template);
             //
-            // Is it matchable?
+            // Create the matchable
             //
-            if (checkIsMatchableProperty(toscaProperty)) {
-                //
-                // This will generate the matchables for the property
-                //
-                int weight = generateMatchable(targetType, entrySet.getKey(), entrySet.getValue(),
-                        property.getLeft(), property.getRight());
-                LOGGER.info(MSG_WEIGHT, weight);
-                totalWeight += weight;
-            } else {
-                //
-                // Not matchable, but we need to check if this contains list or map of datatypes.
-                // Those will need to be searched for matchables.
-                //
-                if ("list".equals(toscaProperty.getType())) {
-                    int weight = findMatchablesInList(entrySet.getKey(), entrySet.getValue(), toscaProperty,
-                            policyTemplate, targetType);
-                    LOGGER.info(MSG_WEIGHT_LIST, weight);
-                    totalWeight += weight;
-                } else if ("map".equals(toscaProperty.getType())) {
-                    int weight = findMatchablesInMap(entrySet.getKey(), entrySet.getValue(), toscaProperty,
-                            policyTemplate, targetType);
-                    LOGGER.info(MSG_WEIGHT_MAP, weight);
-                    totalWeight += weight;
-                }
-            }
-        }
-        return totalWeight;
-    }
-
-    @SuppressWarnings("unchecked")
-    protected int findMatchablesInList(String listPropertyName, Object listValue, ToscaProperty listProperty,
-            ToscaServiceTemplate listTemplate, TargetType targetType) {
-        //
-        // Don't bother if there is no schema (which should be a problem) or
-        // its a list of primitives
-        //
-        if (listProperty.getEntrySchema() == null) {
-            LOGGER.error("No entry schema for list property {}", listPropertyName);
-            return 0;
-        }
-        //
-        // If they are primitives, then no need to go through them. ??
-        //
-        if (isYamlType(listProperty.getEntrySchema().getType())) {
-            LOGGER.info("list of primitives");
-            return 0;
-        }
-        //
-        // Find the datatype
-        //
-        ToscaDataType listDataType = listTemplate.getDataTypes().get(listProperty.getEntrySchema().getType());
-        if (listDataType == null) {
-            LOGGER.error("Unable to find datatype {}", listProperty.getEntrySchema().getType());
-            return 0;
-        }
-
-        int totalWeight = 0;
-        for (Object datatypeValue : ((Collection<Object>)listValue)) {
+            matchablePolicyType = new MatchablePolicyType(
+                    template.getPolicyTypes().get(policy.getType()), myCallback);
             //
-            // This should be a map - because this is a list of datatypes.
+            // Cache it
             //
-            if (! (datatypeValue instanceof Map)) {
-                LOGGER.error("datatype {} value is not a map {}", listDataType.getName(), datatypeValue.getClass());
-                continue;
-            }
-            for (Entry<String, Object> entrySet : ((Map<String, Object>)datatypeValue).entrySet()) {
-                ToscaProperty toscaProperty = listDataType.getProperties().get(entrySet.getKey());
-                if (toscaProperty == null) {
-                    LOGGER.error("Failed to find datatype {} property {}", listDataType.getName(), entrySet.getKey());
-                    continue;
-                }
-                LOGGER.info("Found list property {} with type {} schema {}", entrySet.getKey(), toscaProperty.getType(),
-                        (toscaProperty.getEntrySchema() == null ? "null" : toscaProperty.getEntrySchema().getType()));
-                //
-                // Is it matchable?
-                //
-                if (checkIsMatchableProperty(toscaProperty)) {
-                    //
-                    // This will generate the matchables for the property
-                    //
-                    int weight = generateMatchable(targetType, entrySet.getKey(), entrySet.getValue(),
-                            toscaProperty, listTemplate);
-                    LOGGER.info(MSG_WEIGHT, weight);
-                    totalWeight += weight;
-                } else {
-                    //
-                    // Not matchable, but we need to check if this contains list or map of datatypes.
-                    // Those will need to be searched for matchables.
-                    //
-                    if ("list".equals(toscaProperty.getType())) {
-                        int weight = findMatchablesInList(entrySet.getKey(), entrySet.getValue(), toscaProperty,
-                                listTemplate, targetType);
-                        LOGGER.info(MSG_WEIGHT_LIST, weight);
-                        totalWeight += weight;
-                    } else if ("map".equals(toscaProperty.getType())) {
-                        int weight = findMatchablesInMap(entrySet.getKey(), entrySet.getValue(), toscaProperty,
-                                listTemplate, targetType);
-                        LOGGER.info(MSG_WEIGHT_MAP, weight);
-                        totalWeight += weight;
-                    }
-                }
-            }
+            matchableCache.put(policy.getTypeIdentifier(), matchablePolicyType);
         }
-
-        return totalWeight;
-    }
-
-    @SuppressWarnings("unchecked")
-    protected int findMatchablesInMap(String mapPropertyName, Object mapValue, ToscaProperty mapProperty,
-            ToscaServiceTemplate mapTemplate, TargetType targetType) {
         //
-        // There needs to be a schema.
+        // Fill in the target type with potential matchables
         //
-        if (mapProperty.getEntrySchema() == null) {
-            LOGGER.error("No entry schema for map property {}", mapPropertyName);
-            return 0;
+        try {
+            fillTargetTypeWithMatchables(target, matchablePolicyType, policy.getProperties());
+        } catch (ToscaPolicyConversionException e) {
+            LOGGER.error("Could not generate target type", e);
         }
         //
-        // If they are primitives, then no need to go through them. ??
+        // There may be a case for default policies there is no weight - need to clean
+        // up the target then else PDP will report bad policy missing AnyOf
         //
-        if (isYamlType(mapProperty.getEntrySchema().getType())) {
-            LOGGER.debug("map property {} is primitives", mapPropertyName);
-            return 0;
-        }
+        int weight = calculateWeight(target);
+        LOGGER.debug("Weight is {} for policy {}", weight, policy.getName());
         //
-        // Find the datatype
+        // Assume the number of AllOf's is the weight for now
         //
-        ToscaDataType mapDataType = mapTemplate.getDataTypes().get(mapProperty.getEntrySchema().getType());
-        if (mapDataType == null) {
-            LOGGER.error("Unable to find datatype {}", mapProperty.getEntrySchema().getType());
-            return 0;
-        }
-
-        int totalWeight = 0;
-        for (Entry<String, Object> entrySet : ((Map<String, Object>)mapValue).entrySet()) {
-            ToscaProperty toscaProperty = mapDataType.getProperties().get(entrySet.getKey());
-            if (toscaProperty == null) {
-                LOGGER.error("Failed to find datatype {} property {}", mapDataType.getName(), entrySet.getKey());
-                continue;
-            }
-            LOGGER.info("Found map property {} with type {} schema {}", entrySet.getKey(), toscaProperty.getType(),
-                    (toscaProperty.getEntrySchema() == null ? "null" : toscaProperty.getEntrySchema().getType()));
-            //
-            // Is it matchable?
-            //
-            if (checkIsMatchableProperty(toscaProperty)) {
-                //
-                // This will generate the matchables for the property
-                //
-                int weight = generateMatchable(targetType, entrySet.getKey(), entrySet.getValue(),
-                        toscaProperty, mapTemplate);
-                LOGGER.info(MSG_WEIGHT, weight);
-                totalWeight += weight;
-            } else {
-                //
-                // Not matchable, but we need to check if this contains list or map of datatypes.
-                // Those will need to be searched for matchables.
-                //
-                if ("list".equals(toscaProperty.getType())) {
-                    int weight = findMatchablesInList(entrySet.getKey(), entrySet.getValue(), toscaProperty,
-                            mapTemplate, targetType);
-                    LOGGER.info(MSG_WEIGHT_LIST, weight);
-                    totalWeight += weight;
-                } else if ("map".equals(toscaProperty.getType())) {
-                    int weight = findMatchablesInMap(entrySet.getKey(), entrySet.getValue(), toscaProperty,
-                            mapTemplate, targetType);
-                    LOGGER.info(MSG_WEIGHT_MAP, weight);
-                    totalWeight += weight;
-                }
-            }
-        }
-
-        return totalWeight;
+        return Pair.of(target, weight);
     }
 
-    /**
-     * findMatchableProperty - Iterates through available TOSCA Policy Types and return the
-     * ToscaProperty and template for the property.
-     *
-     * @param propertyName Name of property
-     * @param policyTypes Collection of TOSCA Policy Types to scan
-     * @return ToscaProperty and ToscaServiceTemplate if matchable
-     */
-    protected Pair<ToscaProperty, ToscaServiceTemplate> findProperty(String propertyName,
-            ToscaPolicyType policyType, ToscaPolicyTypeIdentifier propertiesPolicyId,
-            ToscaServiceTemplate policyTemplate) {
-        //
-        // See if the property is defined by the policy template
-        //
-        ToscaProperty toscaProperty = policyType.getProperties().get(propertyName);
-        if (toscaProperty != null) {
-            //
-            // Does it contain the matchable property and if so its set to true?
-            //
-            return Pair.of(toscaProperty, policyTemplate);
-        }
-        LOGGER.debug("property {} is not in policy type {}", propertyName, propertiesPolicyId);
-        //
-        // Check its parent policy types
-        //
-        ToscaPolicyTypeIdentifier parentId = getParentDerivedFrom(propertiesPolicyId, policyTemplate);
-        while (parentId != null) {
-            LOGGER.debug("searching parent policy type {}", parentId);
-            //
-            // Search the existing template (should be there during runtime)
-            //
-            ToscaPolicyType parentPolicyType = getParentPolicyType(parentId, policyTemplate);
-            if (parentPolicyType != null) {
-                toscaProperty = parentPolicyType.getProperties().get(propertyName);
-                if (toscaProperty != null) {
-                    return Pair.of(toscaProperty, policyTemplate);
-                }
+    @SuppressWarnings("unchecked")
+    protected void fillTargetTypeWithMatchables(TargetType target, MatchablePolicyType matchablePolicyType,
+            Map<String, Object> properties) throws ToscaPolicyConversionException {
+        for (Entry<String, Object> entrySet : properties.entrySet()) {
+            String propertyName = entrySet.getKey();
+            Object propertyValue = entrySet.getValue();
+            MatchableProperty matchable = matchablePolicyType.get(propertyName);
+            if (matchable != null) {
                 //
-                // Move to the next parent
+                // Construct attribute id
                 //
-                parentId = getParentDerivedFrom(parentId, policyTemplate);
-            } else {
-                LOGGER.warn("Parent policy type is not found {}", parentId);
+                Identifier id = new IdentifierImpl(ToscaDictionary.ID_RESOURCE_MATCHABLE + propertyName);
                 //
-                // Find the parent policy type. During JUnit this may be in a separate
-                // file. We hope that during runtime the template is complete.
+                // Depending on what type it is, add it into the target
                 //
-                ToscaServiceTemplate parentTemplate = findPolicyType(parentId);
-                if (parentTemplate != null) {
-                    parentPolicyType = getParentPolicyType(parentId, parentTemplate);
-                    if (parentPolicyType != null) {
-                        toscaProperty = parentPolicyType.getProperties().get(propertyName);
-                        if (toscaProperty != null) {
-                            return Pair.of(toscaProperty, parentTemplate);
-                        }
-                    }
-                    //
-                    // Move to the next parent
-                    //
-                    parentId = getParentDerivedFrom(parentId, parentTemplate);
-                } else {
-                    LOGGER.error("Unable to find/pull parent policy type {}", parentId);
-                    parentId = null;
-                }
-            }
-        }
-        LOGGER.warn("Property {} is NOT found in any template", propertyName);
-        return null;
-    }
-
-    private ToscaPolicyType getToscaPolicyTypeFromTemplate(ToscaPolicyTypeIdentifier propertiesPolicyId,
-            ToscaServiceTemplate policyTemplate) {
-        for (Entry<String, ToscaPolicyType> entry : policyTemplate.getPolicyTypes().entrySet()) {
-            if (propertiesPolicyId.getName().equals(entry.getKey())
-                    && propertiesPolicyId.getVersion().equals(entry.getValue().getVersion())) {
-                return entry.getValue();
-            }
-        }
-        return null;
-    }
+                ToscaPolicyTranslatorUtils.buildAndAppendTarget(target,
+                        matchable.getType().generate(propertyValue, id));
 
-    private boolean isYamlType(String type) {
-        return "string".equalsIgnoreCase(type) || "integer".equalsIgnoreCase(type) || "float".equalsIgnoreCase(type)
-                || "boolean".equalsIgnoreCase(type) || "timestamp".equalsIgnoreCase(type);
-    }
-
-    /**
-     * checkIsMatchableProperty - checks the property metadata to see if matchable exists.
-     *
-     * @param toscaProperty ToscaProperty
-     * @return true if matchable
-     */
-    protected boolean checkIsMatchableProperty(ToscaProperty toscaProperty) {
-        if (toscaProperty.getMetadata() == null) {
-            return false;
-        }
-        for (Entry<String, String> entrySet : toscaProperty.getMetadata().entrySet()) {
-            if ("matchable".equals(entrySet.getKey()) && "true".equals(entrySet.getValue())) {
-                LOGGER.debug("found matchable of type {}", toscaProperty.getType());
-                return true;
+                continue;
             }
-        }
-        return false;
-    }
-
-    /**
-     * generateMatchable - Given the object, generates list of MatchType objects and add them
-     * to the TargetType object. Returns a weight which is the number of AnyOf's generated. The
-     * weight can be used to further filter the results for "closest match".
-     *
-     * @param targetType TargetType object to add matches to
-     * @param key Property key
-     * @param value Object is the value - which can be a Collection or single Object
-     * @param toscaProperty The property that was found
-     * @param toscaServiceTemplate The template from which the property was found
-     * @return int Weight of the match.
-     */
-    protected int generateMatchable(TargetType targetType, String key, Object value, ToscaProperty toscaProperty,
-            ToscaServiceTemplate toscaServiceTemplate) {
-        int weight = 0;
-        if (value instanceof Collection) {
             //
-            // Further determine how we treat this collection. We will need the schema
-            // if it is not available then we have to bail.
+            // Here is the special case where we look for a Collection of values that may
+            // contain potential matchables
             //
-            if (toscaProperty.getEntrySchema() == null) {
-                LOGGER.error("No schema for property {} of type {}", key, toscaProperty.getType());
-            }
-            if ("list".equals(toscaProperty.getType())) {
-                return generateMatchableList(targetType, key, value, toscaProperty, toscaServiceTemplate);
-            }
-            if ("map".equals(toscaProperty.getType())) {
-                return generateMatchableMap(targetType, key, value, toscaProperty, toscaServiceTemplate);
-            }
-        } else {
-            AnyOfType anyOf = generateMatches(Arrays.asList(value),
-                    new IdentifierImpl(ToscaDictionary.ID_RESOURCE_MATCHABLE + key));
-            if (! anyOf.getAllOf().isEmpty()) {
-                targetType.getAnyOf().add(anyOf);
-                weight = 1;
+            if (propertyValue instanceof List) {
+                for (Object listValue : ((List<?>)propertyValue)) {
+                    if (listValue instanceof Map) {
+                        fillTargetTypeWithMatchables(target, matchablePolicyType, (Map<String, Object>) listValue);
+                    }
+                }
+            } else if (propertyValue instanceof Map) {
+                fillTargetTypeWithMatchables(target, matchablePolicyType, (Map<String, Object>) propertyValue);
             }
         }
-        return weight;
     }
 
-    @SuppressWarnings("unchecked")
-    protected int generateMatchableList(TargetType targetType, String key, Object value, ToscaProperty toscaProperty,
-            ToscaServiceTemplate toscaServiceTemplate) {
+    protected int calculateWeight(TargetType target) {
         int weight = 0;
-        if (isYamlType(toscaProperty.getEntrySchema().getType())) {
-            AnyOfType anyOf = generateMatches((Collection<Object>) value,
-                    new IdentifierImpl(ToscaDictionary.ID_RESOURCE_MATCHABLE + key));
-            if (! anyOf.getAllOf().isEmpty()) {
-                targetType.getAnyOf().add(anyOf);
-                weight = 1;
+        for (AnyOfType anyOf : target.getAnyOf()) {
+            for (AllOfType allOf : anyOf.getAllOf()) {
+                weight += allOf.getMatch().size();
             }
-        } else {
-            LOGGER.debug("PLD use datatype for list?");
         }
-        return weight;
-    }
 
-    @SuppressWarnings("unchecked")
-    protected int generateMatchableMap(TargetType targetType, String key, Object value, ToscaProperty toscaProperty,
-            ToscaServiceTemplate toscaServiceTemplate) {
-        int weight = 0;
-        if (isYamlType(toscaProperty.getEntrySchema().getType())) {
-            //
-            // PLD TODO - this won't work. Right now there are no maps being used to match.
-            // need to investigate whether we really can support that situation.
-            //
-            AnyOfType anyOf = generateMatches((Collection<Object>) value,
-                    new IdentifierImpl(ToscaDictionary.ID_RESOURCE_MATCHABLE + key));
-            if (! anyOf.getAllOf().isEmpty()) {
-                targetType.getAnyOf().add(anyOf);
-                weight = 1;
-            }
-        } else {
-            LOGGER.debug("PLD use datatype for map?");
-        }
         return weight;
     }
 
-    /**
-     * generateMatches - Goes through the collection of objects, creates a MatchType object
-     * for each object and associates it with the given attribute Id. Returns the AnyOfType
-     * object that contains all the generated MatchType objects.
-     *
-     * @param matchables Collection of object to generate MatchType from
-     * @param attributeId Given attribute Id for each MatchType
-     * @return AnyOfType object
-     */
-    protected AnyOfType generateMatches(Collection<Object> matchables, Identifier attributeId) {
-        //
-        // This is our outer AnyOf - which is an OR
-        //
-        AnyOfType anyOf = new AnyOfType();
-        for (Object matchable : matchables) {
-            //
-            // Default to string
-            //
-            Identifier idFunction = XACML3.ID_FUNCTION_STRING_EQUAL;
-            Identifier idDatatype = XACML3.ID_DATATYPE_STRING;
-            //
-            // See if we are another datatype
-            //
-            // We should add datetime support. But to do that we need
-            // probably more metadata to describe how that would be translated.
-            //
-            if (matchable instanceof Integer) {
-                idFunction = XACML3.ID_FUNCTION_INTEGER_EQUAL;
-                idDatatype = XACML3.ID_DATATYPE_INTEGER;
-            } else if (matchable instanceof Double) {
-                idFunction = XACML3.ID_FUNCTION_DOUBLE_EQUAL;
-                idDatatype = XACML3.ID_DATATYPE_DOUBLE;
-            } else if (matchable instanceof Boolean) {
-                idFunction = XACML3.ID_FUNCTION_BOOLEAN_EQUAL;
-                idDatatype = XACML3.ID_DATATYPE_BOOLEAN;
-            }
-            //
-            // Create a match for this
-            //
-            MatchType match = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
-                    idFunction,
-                    matchable.toString(),
-                    idDatatype,
-                    attributeId,
-                    XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
-            //
-            // Now create an anyOf (OR)
-            //
-            anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(match));
-        }
-        return anyOf;
-    }
-
-    private ToscaPolicyTypeIdentifier getParentDerivedFrom(ToscaPolicyTypeIdentifier policyTypeId,
-            ToscaServiceTemplate template) {
-        for (Entry<String, ToscaPolicyType> entrySet : template.getPolicyTypes().entrySet()) {
-            ToscaPolicyType policyType = entrySet.getValue();
-            if (entrySet.getKey().equals(policyTypeId.getName())
-                    && policyType.getVersion().equals(policyTypeId.getVersion())
-                    && ! "tosca.policies.Root".equals(policyType.getDerivedFrom())) {
-                return new ToscaPolicyTypeIdentifier(policyType.getDerivedFrom(), "1.0.0");
-            }
-        }
-
-        return null;
-    }
-
-    private ToscaPolicyType getParentPolicyType(ToscaPolicyTypeIdentifier policyTypeId, ToscaServiceTemplate template) {
-        for (Entry<String, ToscaPolicyType> entrySet : template.getPolicyTypes().entrySet()) {
-            ToscaPolicyType policyType = entrySet.getValue();
-            if (entrySet.getKey().equals(policyTypeId.getName())
-                    && policyType.getVersion().equals(policyTypeId.getVersion())) {
-                return policyType;
-            }
-        }
-        return null;
-    }
-
     /**
      * findPolicyType - given the ToscaPolicyTypeIdentifier, finds it in memory, or
      * then tries to find it either locally on disk or pull it from the Policy
@@ -889,8 +537,17 @@ public class StdMatchableTranslator  extends StdBaseTranslator {
         //
         LOGGER.info("Read in local policy type {}", policyTypePath.toAbsolutePath());
         try {
-            return standardYamlCoder.decode(new String(bytes, StandardCharsets.UTF_8),
+            //
+            // Decode the template
+            //
+            ToscaServiceTemplate template = standardYamlCoder.decode(new String(bytes, StandardCharsets.UTF_8),
                     ToscaServiceTemplate.class);
+            //
+            // Ensure all the fields are setup correctly
+            //
+            JpaToscaServiceTemplate jtst = new JpaToscaServiceTemplate();
+            jtst.fromAuthorative(template);
+            return jtst.toAuthorative();
         } catch (CoderException e) {
             LOGGER.error("Failed to decode tosca template for {}", policyTypePath, e);
         }