2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.aai.dbgen;
22 import java.util.HashMap;
23 import java.util.HashSet;
25 import java.util.Optional;
28 import org.apache.tinkerpop.gremlin.structure.Vertex;
29 import org.onap.aai.config.SpringContextAware;
30 import org.onap.aai.db.props.AAIProperties;
31 import org.onap.aai.edges.EdgeIngestor;
32 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
33 import org.onap.aai.introspection.Introspector;
34 import org.onap.aai.introspection.Loader;
35 import org.onap.aai.introspection.LoaderFactory;
36 import org.onap.aai.introspection.ModelType;
37 import org.onap.aai.logging.LogFormatTools;
38 import org.onap.aai.schema.enums.PropertyMetadata;
39 import org.onap.aai.edges.EdgeRule;
40 import org.onap.aai.setup.SchemaVersions;
41 import org.onap.aai.util.AAIConfig;
43 import com.att.eelf.configuration.EELFLogger;
44 import com.att.eelf.configuration.EELFManager;
45 import com.google.common.collect.Multimap;
46 import org.janusgraph.core.Cardinality;
47 import org.janusgraph.core.Multiplicity;
48 import org.janusgraph.core.PropertyKey;
49 import org.janusgraph.core.JanusGraph;
50 import org.janusgraph.core.schema.JanusGraphManagement;
52 public class SchemaGenerator {
54 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(SchemaGenerator.class);
57 * Load schema into JanusGraph.
64 public static void loadSchemaIntoJanusGraph(final JanusGraph graph, final JanusGraphManagement graphMgmt,
69 } catch (Exception ex) {
70 LOGGER.error(" ERROR - Could not run AAIConfig.init(). " + LogFormatTools.getStackTop(ex));
71 System.out.println(" ERROR - Could not run AAIConfig.init(). ");
75 // NOTE - JanusGraph 0.5.3 doesn't keep a list of legal node Labels.
76 // They are only used when a vertex is actually being created.
77 // JanusGraph 1.1 will keep track (we think).
79 // Use EdgeRules to make sure edgeLabels are defined in the db. NOTE:
80 // the multiplicty used here is
81 // always "MULTI". This is not the same as our internal "Many2Many",
82 // "One2One", "One2Many" or "Many2One"
83 // We use the same edge-label for edges between many different types of
84 // nodes and our internal
85 // multiplicty definitions depends on which two types of nodes are being
88 Multimap<String, EdgeRule> edges = null;
89 Set<String> labels = new HashSet<>();
91 EdgeIngestor edgeIngestor = SpringContextAware.getBean(EdgeIngestor.class);
94 edges = edgeIngestor.getAllCurrentRules();
95 } catch (EdgeRuleNotFoundException e) {
97 LOGGER.error("Unable to find all rules {}", LogFormatTools.getStackTop(e));
100 for (EdgeRule rule : edges.values()) {
101 labels.add(rule.getLabel());
104 for (String label : labels) {
105 if (graphMgmt.containsRelationType(label)) {
106 String dmsg = " EdgeLabel [" + label + "] already existed. ";
109 String dmsg = "Making EdgeLabel: [" + label + "]";
111 graphMgmt.makeEdgeLabel(label).multiplicity(Multiplicity.valueOf("MULTI")).make();
115 // ApplicationContext ctx = SpringContextAware.getApplicationContext();
117 // ctx.getBean(LoaderFactory.class).createLoaderForVersion(ModelType.MOXY,
118 // AAIProperties.LATEST);
119 LoaderFactory loaderFactory = SpringContextAware.getBean(LoaderFactory.class);
120 SchemaVersions schemaVersions = SpringContextAware.getBean(SchemaVersions.class);
122 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
123 // Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY,
124 // AAIProperties.LATEST);
125 Map<String, Introspector> objs = loader.getAllObjects();
126 Map<String, PropertyKey> seenProps = new HashMap<>();
128 for (Introspector obj : objs.values()) {
129 for (String propName : obj.getProperties()) {
130 String dbPropName = propName;
131 Optional<String> alias = obj.getPropertyMetadata(propName, PropertyMetadata.DB_ALIAS);
132 if (alias.isPresent()) {
133 dbPropName = alias.get();
135 if (graphMgmt.containsRelationType(dbPropName)) {
136 String dmsg = " PropertyKey [" + dbPropName + "] already existed in the DB. ";
139 Class<?> type = obj.getClass(propName);
140 Cardinality cardinality = Cardinality.SINGLE;
141 boolean process = false;
142 if (obj.isListType(propName) && obj.isSimpleGenericType(propName)) {
143 cardinality = Cardinality.SET;
144 type = obj.getGenericTypeClass(propName);
146 } else if (obj.isSimpleType(propName)) {
152 String imsg = "Creating PropertyKey: [" + dbPropName + "], [" + type.getSimpleName() + "], ["
156 if (!seenProps.containsKey(dbPropName)) {
157 propK = graphMgmt.makePropertyKey(dbPropName).dataType(type).cardinality(cardinality)
159 seenProps.put(dbPropName, propK);
161 propK = seenProps.get(dbPropName);
163 if (graphMgmt.containsGraphIndex(dbPropName)) {
164 String dmsg = " Index [" + dbPropName + "] already existed in the DB. ";
167 if (obj.getIndexedProperties().contains(dbPropName)) {
168 if (obj.getUniqueProperties().contains(dbPropName)) {
169 imsg = "Add Unique index for PropertyKey: [" + dbPropName + "]";
171 graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK).unique()
172 .buildCompositeIndex();
174 imsg = "Add index for PropertyKey: [" + dbPropName + "]";
176 graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK).buildCompositeIndex();
179 imsg = "No index added for PropertyKey: [" + dbPropName + "]";
188 String imsg = "-- About to call graphMgmt commit";
191 System.out.println("Successfully loaded the schema to " + backend);