Search in sources :

Example 1 with PropertySchema

use of org.onap.aai.schemaif.definitions.PropertySchema 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 PropertySchema

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

the class JsonSchemaProviderTest method testJsonSchemaComplexAttribute.

@Test
public void testJsonSchemaComplexAttribute() {
    try {
        String testSchema = readFile("src/test/resources/json/jsonSchema.json");
        JsonSchemaProvider schemaProvider = new JsonSchemaProvider(config);
        schemaProvider.loadSchema(testSchema, schemaProvider.getLatestSchemaVersion());
        VertexSchema vertSchema = schemaProvider.getVertexSchema("org.onap.resource.extContrailCP", schemaProvider.getLatestSchemaVersion());
        System.out.println(vertSchema.toString());
        System.out.println("\n\nSize: " + vertSchema.getPropertySchemaList().size());
        System.out.println(vertSchema.getPropertySchemaList());
        assertEquals(22, vertSchema.getPropertySchemaList().size());
        // Validate property schema
        PropertySchema propSchema = vertSchema.getPropertySchema("exCP_naming");
        assertEquals(0, propSchema.getDataType().getType().compareTo(Type.COMPLEX));
        ComplexDataType complexType = (ComplexDataType) propSchema.getDataType();
        List<PropertySchema> complexProps = complexType.getSubProperties();
        assertEquals(4, complexProps.size());
        PropertySchema subProp = null;
        for (PropertySchema p : complexProps) {
            if (p.getName().equals("naming_policy")) {
                subProp = p;
            }
        }
        assertNotNull(subProp);
        assertFalse(subProp.isRequired());
        assertEquals(0, subProp.getDataType().getType().compareTo(Type.STRING));
    } catch (Exception ex) {
        StringWriter writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        ex.printStackTrace(printWriter);
        System.out.println(writer);
        fail();
    }
}
Also used : VertexSchema(org.onap.aai.schemaif.definitions.VertexSchema) JsonVertexSchema(org.onap.aai.schemaif.json.definitions.JsonVertexSchema) StringWriter(java.io.StringWriter) PropertySchema(org.onap.aai.schemaif.definitions.PropertySchema) JsonPropertySchema(org.onap.aai.schemaif.json.definitions.JsonPropertySchema) ComplexDataType(org.onap.aai.schemaif.definitions.types.ComplexDataType) SchemaProviderException(org.onap.aai.schemaif.SchemaProviderException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 3 with PropertySchema

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

the class JsonSchemaProviderTest method testJsonSchemaMapAttribute.

@Test
public void testJsonSchemaMapAttribute() {
    try {
        String testSchema = readFile("src/test/resources/json/jsonSchema.json");
        JsonSchemaProvider schemaProvider = new JsonSchemaProvider(config);
        schemaProvider.loadSchema(testSchema, schemaProvider.getLatestSchemaVersion());
        VertexSchema vertSchema = schemaProvider.getVertexSchema("onap.nodes.sdwan.ManagementDomain", schemaProvider.getLatestSchemaVersion());
        System.out.println(vertSchema.toString());
        // Validate schema
        PropertySchema propSchema = vertSchema.getPropertySchema("analyticClusters");
        assertEquals(0, propSchema.getDataType().getType().compareTo(Type.MAP));
        MapDataType mapType = (MapDataType) propSchema.getDataType();
        assertEquals(0, mapType.getMapType().getType().compareTo(Type.STRING));
    } catch (Exception ex) {
        StringWriter writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        ex.printStackTrace(printWriter);
        System.out.println(writer);
        fail();
    }
}
Also used : VertexSchema(org.onap.aai.schemaif.definitions.VertexSchema) JsonVertexSchema(org.onap.aai.schemaif.json.definitions.JsonVertexSchema) StringWriter(java.io.StringWriter) PropertySchema(org.onap.aai.schemaif.definitions.PropertySchema) JsonPropertySchema(org.onap.aai.schemaif.json.definitions.JsonPropertySchema) MapDataType(org.onap.aai.schemaif.definitions.types.MapDataType) SchemaProviderException(org.onap.aai.schemaif.SchemaProviderException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 4 with PropertySchema

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

the class FromJsonPropertySchema method resolveComplexDataType.

private DataType resolveComplexDataType(String typeString, List<DataTypeDefinition> dataTypes) throws SchemaProviderException {
    // It must be a custom/complex type.
    DataTypeDefinition dType = null;
    for (DataTypeDefinition d : dataTypes) {
        if ((d.getName().equals(typeString))) {
            dType = d;
            break;
        }
    }
    if (dType == null) {
        throw new SchemaProviderException("Invalid data type: " + typeString);
    }
    List<PropertySchema> propList = new ArrayList<PropertySchema>();
    for (JsonPropertySchema p : dType.getProperties()) {
        FromJsonPropertySchema pSchema = new FromJsonPropertySchema();
        pSchema.fromJson(p, false, dataTypes);
        propList.add(pSchema);
    }
    return new ComplexDataType(propList);
}
Also used : PropertySchema(org.onap.aai.schemaif.definitions.PropertySchema) JsonPropertySchema(org.onap.aai.schemaif.json.definitions.JsonPropertySchema) ArrayList(java.util.ArrayList) SchemaProviderException(org.onap.aai.schemaif.SchemaProviderException) ComplexDataType(org.onap.aai.schemaif.definitions.types.ComplexDataType) DataTypeDefinition(org.onap.aai.schemaif.json.definitions.DataTypeDefinition) JsonPropertySchema(org.onap.aai.schemaif.json.definitions.JsonPropertySchema)

Example 5 with PropertySchema

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

the class FromJsonVertexSchema method fromJson.

public void fromJson(JsonVertexSchema jsonVertex, List<DataTypeDefinition> dataTypes, List<JsonPropertySchema> commonProps) throws SchemaProviderException {
    name = jsonVertex.getName();
    properties = new HashMap<String, PropertySchema>();
    annotations = new HashMap<String, String>();
    // Populate property schema
    if (jsonVertex.getProperties() != null) {
        for (JsonPropertySchema pSchema : jsonVertex.getProperties()) {
            FromJsonPropertySchema propSchema = new FromJsonPropertySchema();
            propSchema.fromJson(pSchema, false, dataTypes);
            properties.put(propSchema.getName().toLowerCase(), propSchema);
        }
    }
    // Add common properties
    if (commonProps != null) {
        for (JsonPropertySchema pSchema : commonProps) {
            FromJsonPropertySchema propSchema = new FromJsonPropertySchema();
            propSchema.fromJson(pSchema, true, dataTypes);
            properties.put(propSchema.getName().toLowerCase(), propSchema);
        }
    } else {
        // TODO:  This is a hack until the schema-service return the list of common props
        addCommonProps();
    }
    // Populate annotation schema
    if (jsonVertex.getAnnotations() != null) {
        for (Map.Entry<String, String> entry : jsonVertex.getAnnotations().entrySet()) {
            annotations.put(entry.getKey().toLowerCase(), entry.getValue());
        }
    }
    // The searchable and indexed annotations, need to grab these from the property annotations
    // and store them at the vertex level as well (backwards compatibility with OXM)
    StringBuilder searchableVal = new StringBuilder();
    StringBuilder indexedVal = new StringBuilder();
    for (PropertySchema pSchema : properties.values()) {
        if ((pSchema.getAnnotationValue("searchable") != null) && (pSchema.getAnnotationValue("searchable").equalsIgnoreCase("true"))) {
            if (searchableVal.length() > 0) {
                searchableVal.append(",");
            }
            searchableVal.append(pSchema.getName());
        }
        if ((pSchema.getAnnotationValue("indexed") != null) && (pSchema.getAnnotationValue("indexed").equalsIgnoreCase("true"))) {
            if (indexedVal.length() > 0) {
                indexedVal.append(",");
            }
            indexedVal.append(pSchema.getName());
        }
    }
    if (searchableVal.length() > 0) {
        annotations.put("searchable", searchableVal.toString());
    }
    if (indexedVal.length() > 0) {
        annotations.put("indexedprops", indexedVal.toString());
    }
}
Also used : PropertySchema(org.onap.aai.schemaif.definitions.PropertySchema) JsonPropertySchema(org.onap.aai.schemaif.json.definitions.JsonPropertySchema) Map(java.util.Map) HashMap(java.util.HashMap) JsonPropertySchema(org.onap.aai.schemaif.json.definitions.JsonPropertySchema)

Aggregations

PropertySchema (org.onap.aai.schemaif.definitions.PropertySchema)9 SchemaProviderException (org.onap.aai.schemaif.SchemaProviderException)7 JsonPropertySchema (org.onap.aai.schemaif.json.definitions.JsonPropertySchema)6 PrintWriter (java.io.PrintWriter)5 StringWriter (java.io.StringWriter)5 Test (org.junit.Test)5 VertexSchema (org.onap.aai.schemaif.definitions.VertexSchema)5 IOException (java.io.IOException)4 JsonVertexSchema (org.onap.aai.schemaif.json.definitions.JsonVertexSchema)4 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ComplexDataType (org.onap.aai.schemaif.definitions.types.ComplexDataType)2 ArrayList (java.util.ArrayList)1 DynamicType (org.eclipse.persistence.dynamic.DynamicType)1 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)1 XMLAnyCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLAnyCollectionMapping)1 XMLAnyObjectMapping (org.eclipse.persistence.oxm.mappings.XMLAnyObjectMapping)1 EdgeSchema (org.onap.aai.schemaif.definitions.EdgeSchema)1 ListDataType (org.onap.aai.schemaif.definitions.types.ListDataType)1 MapDataType (org.onap.aai.schemaif.definitions.types.MapDataType)1