[AAI-2404]add aai-schema-abstraction library
[aai/aai-common.git] / aai-schema-abstraction / src / main / java / org / onap / aai / schemaif / oxm / FromOxmVertexSchema.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2019 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.schemaif.oxm;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.eclipse.persistence.dynamic.DynamicType;
27 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
28 import org.eclipse.persistence.mappings.DatabaseMapping;
29 import org.eclipse.persistence.oxm.mappings.XMLAnyCollectionMapping;
30 import org.eclipse.persistence.oxm.mappings.XMLAnyObjectMapping;
31 import org.onap.aai.schemaif.SchemaProviderException;
32 import org.onap.aai.schemaif.definitions.PropertySchema;
33 import org.onap.aai.schemaif.definitions.VertexSchema;
34
35 import com.google.common.base.CaseFormat;
36
37 public class FromOxmVertexSchema extends VertexSchema {
38     public void fromOxm(String vertexType, DynamicJAXBContext jaxbContext, HashMap<String, DynamicType> xmlElementLookup) throws SchemaProviderException {
39         name = vertexType;
40         properties = new HashMap<String,PropertySchema>();
41         annotations = new HashMap<String,String>();
42
43         String javaTypeName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, vertexType);
44         DynamicType modelObjectType = jaxbContext.getDynamicType(javaTypeName);
45
46         if (modelObjectType == null) {
47             // Try to lookup by xml root element by exact match
48             modelObjectType = xmlElementLookup.get(vertexType);
49         }
50
51         if (modelObjectType == null) {
52             // Try to lookup by xml root element by lowercase
53             modelObjectType = xmlElementLookup.get(vertexType.toLowerCase());
54         }
55
56         if (modelObjectType == null) {
57             // Direct lookup as java-type name
58             modelObjectType = jaxbContext.getDynamicType(vertexType);
59         }
60
61         if (modelObjectType == null) {
62             // Vertex isn't found in the OXM
63             throw new SchemaProviderException("Vertex " + vertexType + " not found in OXM");
64         }
65         
66         // Check annotations
67         Map<String, Object> oxmProps = modelObjectType.getDescriptor().getProperties();
68         for (Map.Entry<String, Object> entry : oxmProps.entrySet()) {
69             if (entry.getValue() instanceof String) {
70                 annotations.put(entry.getKey().toLowerCase(), (String)entry.getValue());
71             }
72         }
73
74         // Regular props
75         for (DatabaseMapping mapping : modelObjectType.getDescriptor().getMappings()) {
76             if (mapping instanceof XMLAnyObjectMapping)
77               continue;
78             if(mapping instanceof XMLAnyCollectionMapping)
79               continue;
80             FromOxmPropertySchema propSchema = new FromOxmPropertySchema();
81             propSchema.fromOxm(mapping, modelObjectType, false);
82             properties.put(propSchema.getName().toLowerCase(), propSchema);
83         }
84
85         // Reserved Props
86         final DynamicType reservedType = jaxbContext.getDynamicType("ReservedPropNames");
87         for (DatabaseMapping mapping : reservedType.getDescriptor().getMappings()) {
88             if (mapping.isAbstractDirectMapping()) {
89                 FromOxmPropertySchema propSchema = new FromOxmPropertySchema();
90                 propSchema.fromOxm(mapping, reservedType, true);
91                 properties.put(propSchema.getName().toLowerCase(), propSchema);
92             }
93         } 
94     }
95 }