Merge "[AAI] Fix doc config files"
[aai/aai-common.git] / aai-schema-abstraction / src / main / java / org / onap / aai / schemaif / oxm / OxmSchemaProvider.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 java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
30 import org.onap.aai.edges.EdgeRule;
31 import org.onap.aai.schemaif.SchemaProvider;
32 import org.onap.aai.schemaif.SchemaProviderException;
33 import org.onap.aai.schemaif.definitions.EdgeSchema;
34 import org.onap.aai.schemaif.definitions.VertexSchema;
35
36 public class OxmSchemaProvider implements SchemaProvider {
37
38     @Override
39     public void loadSchema() throws SchemaProviderException {
40         OxmEdgeRulesLoader.loadModels();
41         OxmSchemaLoader.loadModels();
42     }
43
44     @Override
45     public String getLatestSchemaVersion() throws SchemaProviderException {
46         return OxmSchemaLoader.getLatestVersion();
47     }
48
49     @Override
50     public VertexSchema getVertexSchema(String vertexName, String schemaVersion) throws SchemaProviderException {
51         DynamicJAXBContext jaxbContext = OxmSchemaLoader.getContextForVersion(schemaVersion);
52         FromOxmVertexSchema vs = new FromOxmVertexSchema();
53
54         try {
55             vs.fromOxm(vertexName, jaxbContext, OxmSchemaLoader.getXmlLookupMap(schemaVersion));
56         } catch (SchemaProviderException ex) {
57             // Node doesn't exist in schema. Return null.
58             return null;
59         }
60
61         return vs;
62     }
63
64     @Override
65     public EdgeSchema getEdgeSchema(String edgeType, String sourceVertex, String targetVertex, String version)
66             throws SchemaProviderException {
67         RelationshipSchema relSchema = OxmEdgeRulesLoader.getSchemaForVersion(version);
68         String key = sourceVertex + ":" + targetVertex + ":" + edgeType;
69
70         EdgeRule edgeRule = relSchema.lookupEdgeRule(key);
71         if (edgeRule == null) {
72             return null;
73         }
74
75         FromOxmEdgeSchema es = new FromOxmEdgeSchema();
76         es.fromEdgeRule(edgeRule);
77
78         return es;
79     }
80
81     @Override
82     public Set<EdgeSchema> getAdjacentEdgeSchema(String vertexType, String version) throws SchemaProviderException {
83         RelationshipSchema relSchema = OxmEdgeRulesLoader.getSchemaForVersion(version);
84         Set<EdgeSchema> edges = new HashSet<>();
85         List<EdgeRule> rules = relSchema.lookupAdjacentEdges(vertexType);
86
87         for (EdgeRule rule : rules) {
88             FromOxmEdgeSchema es = new FromOxmEdgeSchema();
89             es.fromEdgeRule(rule);
90             edges.add(es);
91         }
92
93         return edges;
94     }
95
96     @Override
97     public Set<EdgeSchema> getEdgeSchemaForSourceTarget(String sourceType, String targetType, String version)
98             throws SchemaProviderException {
99         RelationshipSchema relSchema = OxmEdgeRulesLoader.getSchemaForVersion(version);
100         Set<EdgeSchema> edges = new HashSet<>();
101         Set<String> relTypes = relSchema.getValidRelationTypes(sourceType, targetType);
102
103         for (String type : relTypes) {
104             EdgeSchema edgeSchema = getEdgeSchema(type, sourceType, targetType, version);
105             if (edgeSchema != null) {
106                 edges.add(edgeSchema);
107             }
108         }
109
110         return edges;
111     }
112
113     @Override
114     public Map<String, VertexSchema> getVertexMap(String schemaVersion) throws SchemaProviderException {
115         return OxmSchemaLoader.getVertexLookupForVersion(schemaVersion);
116     }
117 }