Search in sources :

Example 1 with SchemaProviderException

use of org.onap.aai.schemaif.SchemaProviderException in project aai-aai-common by onap.

the class FromOxmVertexSchema method fromOxm.

public void fromOxm(String vertexType, DynamicJAXBContext jaxbContext, HashMap<String, DynamicType> xmlElementLookup) throws SchemaProviderException {
    name = vertexType;
    properties = new HashMap<String, PropertySchema>();
    annotations = new HashMap<String, String>();
    String javaTypeName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, vertexType);
    DynamicType modelObjectType = jaxbContext.getDynamicType(javaTypeName);
    if (modelObjectType == null) {
        // Try to lookup by xml root element by exact match
        modelObjectType = xmlElementLookup.get(vertexType);
    }
    if (modelObjectType == null) {
        // Try to lookup by xml root element by lowercase
        modelObjectType = xmlElementLookup.get(vertexType.toLowerCase());
    }
    if (modelObjectType == null) {
        // Direct lookup as java-type name
        modelObjectType = jaxbContext.getDynamicType(vertexType);
    }
    if (modelObjectType == null) {
        // Vertex isn't found in the OXM
        throw new SchemaProviderException("Vertex " + vertexType + " not found in OXM");
    }
    // Check annotations
    Map<String, Object> oxmProps = modelObjectType.getDescriptor().getProperties();
    for (Map.Entry<String, Object> entry : oxmProps.entrySet()) {
        if (entry.getValue() instanceof String) {
            annotations.put(entry.getKey().toLowerCase(), (String) entry.getValue());
        }
    }
    // Regular props
    for (DatabaseMapping mapping : modelObjectType.getDescriptor().getMappings()) {
        if (mapping instanceof XMLAnyObjectMapping)
            continue;
        if (mapping instanceof XMLAnyCollectionMapping)
            continue;
        FromOxmPropertySchema propSchema = new FromOxmPropertySchema();
        propSchema.fromOxm(mapping, modelObjectType, false);
        properties.put(propSchema.getName().toLowerCase(), propSchema);
    }
    // Reserved Props
    final DynamicType reservedType = jaxbContext.getDynamicType("ReservedPropNames");
    for (DatabaseMapping mapping : reservedType.getDescriptor().getMappings()) {
        if (mapping.isAbstractDirectMapping()) {
            FromOxmPropertySchema propSchema = new FromOxmPropertySchema();
            propSchema.fromOxm(mapping, reservedType, true);
            properties.put(propSchema.getName().toLowerCase(), propSchema);
        }
    }
}
Also used : PropertySchema(org.onap.aai.schemaif.definitions.PropertySchema) XMLAnyCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLAnyCollectionMapping) DynamicType(org.eclipse.persistence.dynamic.DynamicType) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) SchemaProviderException(org.onap.aai.schemaif.SchemaProviderException) Map(java.util.Map) HashMap(java.util.HashMap) XMLAnyObjectMapping(org.eclipse.persistence.oxm.mappings.XMLAnyObjectMapping)

Example 2 with SchemaProviderException

use of org.onap.aai.schemaif.SchemaProviderException in project aai-aai-common by onap.

the class OxmEdgeRulesLoader method loadModels.

/**
 * Finds all DB Edge Rules and Edge Properties files for all OXM models.
 *
 * @throws SchemaProviderException
 * @throws SchemaProviderException
 */
public static synchronized void loadModels() throws SchemaProviderException {
    Map<String, File> propFiles = edgePropertyFiles(edgePropsConfiguration);
    if (logger.isDebugEnabled()) {
        logger.debug("Loading DB Edge Rules");
    }
    for (String version : OxmSchemaLoader.getLoadedOXMVersions()) {
        try {
            SchemaVersion schemaVersion = translator.getSchemaVersions().getVersions().stream().filter(s -> s.toString().equalsIgnoreCase(version)).findAny().orElse(null);
            loadModel(schemaVersion, edgeIngestor, propFiles);
        } catch (IOException | EdgeRuleNotFoundException e) {
            throw new SchemaProviderException(e.getMessage(), e);
        }
    }
}
Also used : EdgeRuleNotFoundException(org.onap.aai.edges.exceptions.EdgeRuleNotFoundException) SchemaVersion(org.onap.aai.setup.SchemaVersion) IOException(java.io.IOException) SchemaProviderException(org.onap.aai.schemaif.SchemaProviderException) File(java.io.File)

Example 3 with SchemaProviderException

use of org.onap.aai.schemaif.SchemaProviderException in project aai-aai-common by onap.

the class OxmEdgeRulesLoader method loadModels.

/**
 * Loads DB Edge Rules and Edge Properties for a given version.
 *
 * @throws SchemaProviderException
 */
public static synchronized void loadModels(String v) throws SchemaProviderException {
    Map<String, File> propFiles = edgePropertyFiles(edgePropsConfiguration);
    if (logger.isDebugEnabled()) {
        logger.debug("Loading DB Edge Rules ");
    }
    try {
        SchemaVersion schemaVersion = translator.getSchemaVersions().getVersions().stream().filter(s -> s.toString().equalsIgnoreCase(v)).findAny().orElse(null);
        loadModel(schemaVersion, edgeIngestor, propFiles);
    } catch (IOException | EdgeRuleNotFoundException e) {
        throw new SchemaProviderException(e.getMessage());
    }
}
Also used : EdgeRuleNotFoundException(org.onap.aai.edges.exceptions.EdgeRuleNotFoundException) SchemaVersion(org.onap.aai.setup.SchemaVersion) IOException(java.io.IOException) SchemaProviderException(org.onap.aai.schemaif.SchemaProviderException) File(java.io.File)

Example 4 with SchemaProviderException

use of org.onap.aai.schemaif.SchemaProviderException in project aai-aai-common by onap.

the class OxmSchemaLoader method getLatestVersion.

public static String getLatestVersion() throws SchemaProviderException {
    // If we haven't already loaded in the available OXM models, then do so now.
    if (versionContextMap == null || versionContextMap.isEmpty()) {
        loadModels();
    }
    // If there are still no models available, then there's not much we can do...
    if (versionContextMap.isEmpty()) {
        throw new SchemaProviderException("No available OXM schemas to get latest version for.");
    }
    // Iterate over the available model versions to determine which is the most
    // recent.
    Integer latestVersion = null;
    String latestVersionStr = null;
    for (String versionKey : versionContextMap.keySet()) {
        Matcher matcher = versionPattern.matcher(versionKey);
        if (matcher.find()) {
            int currentVersion = Integer.valueOf(matcher.group(1));
            if ((latestVersion == null) || (currentVersion > latestVersion)) {
                latestVersion = currentVersion;
                latestVersionStr = versionKey;
            }
        }
    }
    return latestVersionStr;
}
Also used : Matcher(java.util.regex.Matcher) SchemaProviderException(org.onap.aai.schemaif.SchemaProviderException)

Example 5 with SchemaProviderException

use of org.onap.aai.schemaif.SchemaProviderException in project aai-aai-common by onap.

the class OxmSchemaProviderTest method testInvalidVertexOrEdge.

@Test
public void testInvalidVertexOrEdge() throws SchemaProviderException {
    try {
        OxmSchemaProvider schemaProvider = new OxmSchemaProvider();
        schemaProvider.loadSchema();
        VertexSchema vertSchema = schemaProvider.getVertexSchema("bad-node", schemaProvider.getLatestSchemaVersion());
        assertTrue(vertSchema == null);
        EdgeSchema edgeSchema = schemaProvider.getEdgeSchema("org.onap.relationships.inventory.LocatedIn", "cloud-region", "bad-node", schemaProvider.getLatestSchemaVersion());
        assertTrue(edgeSchema == null);
        Set<EdgeSchema> edgeSchemaList = schemaProvider.getAdjacentEdgeSchema("org.onap.nodes.bad-node", schemaProvider.getLatestSchemaVersion());
        assertTrue(edgeSchemaList.isEmpty());
    } catch (Exception ex) {
        StringWriter writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        ex.printStackTrace(printWriter);
        System.out.println(writer.toString());
        assertTrue(false);
    }
}
Also used : VertexSchema(org.onap.aai.schemaif.definitions.VertexSchema) StringWriter(java.io.StringWriter) EdgeSchema(org.onap.aai.schemaif.definitions.EdgeSchema) SchemaProviderException(org.onap.aai.schemaif.SchemaProviderException) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

SchemaProviderException (org.onap.aai.schemaif.SchemaProviderException)16 PrintWriter (java.io.PrintWriter)5 StringWriter (java.io.StringWriter)5 Test (org.junit.Test)4 EdgeSchema (org.onap.aai.schemaif.definitions.EdgeSchema)4 File (java.io.File)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Matcher (java.util.regex.Matcher)3 EdgeRuleNotFoundException (org.onap.aai.edges.exceptions.EdgeRuleNotFoundException)3 PropertySchema (org.onap.aai.schemaif.definitions.PropertySchema)3 VertexSchema (org.onap.aai.schemaif.definitions.VertexSchema)3 SchemaVersion (org.onap.aai.setup.SchemaVersion)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 BooleanDataType (org.onap.aai.schemaif.definitions.types.BooleanDataType)2 FloatDataType (org.onap.aai.schemaif.definitions.types.FloatDataType)2 IntDataType (org.onap.aai.schemaif.definitions.types.IntDataType)2 LongDataType (org.onap.aai.schemaif.definitions.types.LongDataType)2 StringDataType (org.onap.aai.schemaif.definitions.types.StringDataType)2