Merge "[AAI] Fix doc config files"
[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
22 package org.onap.aai.schemaif.oxm;
23
24 import com.google.common.base.CaseFormat;
25
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import org.eclipse.persistence.dynamic.DynamicType;
30 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
31 import org.eclipse.persistence.mappings.DatabaseMapping;
32 import org.eclipse.persistence.oxm.mappings.XMLAnyCollectionMapping;
33 import org.eclipse.persistence.oxm.mappings.XMLAnyObjectMapping;
34 import org.onap.aai.schemaif.SchemaProviderException;
35 import org.onap.aai.schemaif.definitions.PropertySchema;
36 import org.onap.aai.schemaif.definitions.VertexSchema;
37
38 public class FromOxmVertexSchema extends VertexSchema {
39     public void fromOxm(String vertexType, DynamicJAXBContext jaxbContext,
40             HashMap<String, DynamicType> xmlElementLookup) throws SchemaProviderException {
41         name = vertexType;
42         properties = new HashMap<String, PropertySchema>();
43         annotations = new HashMap<String, String>();
44
45         String javaTypeName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, vertexType);
46         DynamicType modelObjectType = jaxbContext.getDynamicType(javaTypeName);
47
48         if (modelObjectType == null) {
49             // Try to lookup by xml root element by exact match
50             modelObjectType = xmlElementLookup.get(vertexType);
51         }
52
53         if (modelObjectType == null) {
54             // Try to lookup by xml root element by lowercase
55             modelObjectType = xmlElementLookup.get(vertexType.toLowerCase());
56         }
57
58         if (modelObjectType == null) {
59             // Direct lookup as java-type name
60             modelObjectType = jaxbContext.getDynamicType(vertexType);
61         }
62
63         if (modelObjectType == null) {
64             // Vertex isn't found in the OXM
65             throw new SchemaProviderException("Vertex " + vertexType + " not found in OXM");
66         }
67
68         // Check annotations
69         Map<String, Object> oxmProps = modelObjectType.getDescriptor().getProperties();
70         for (Map.Entry<String, Object> entry : oxmProps.entrySet()) {
71             if (entry.getValue() instanceof String) {
72                 annotations.put(entry.getKey().toLowerCase(), (String) entry.getValue());
73             }
74         }
75
76         // Regular props
77         for (DatabaseMapping mapping : modelObjectType.getDescriptor().getMappings()) {
78             if (mapping instanceof XMLAnyObjectMapping)
79                 continue;
80             if (mapping instanceof XMLAnyCollectionMapping)
81                 continue;
82             FromOxmPropertySchema propSchema = new FromOxmPropertySchema();
83             propSchema.fromOxm(mapping, modelObjectType, false);
84             properties.put(propSchema.getName().toLowerCase(), propSchema);
85         }
86
87         // Reserved Props
88         final DynamicType reservedType = jaxbContext.getDynamicType("ReservedPropNames");
89         for (DatabaseMapping mapping : reservedType.getDescriptor().getMappings()) {
90             if (mapping.isAbstractDirectMapping()) {
91                 FromOxmPropertySchema propSchema = new FromOxmPropertySchema();
92                 propSchema.fromOxm(mapping, reservedType, true);
93                 properties.put(propSchema.getName().toLowerCase(), propSchema);
94             }
95         }
96     }
97 }