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=========================================================
21 package org.onap.aai.dbgen;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import com.google.common.collect.Multimap;
27 import java.util.HashMap;
28 import java.util.HashSet;
30 import java.util.Optional;
33 import org.apache.tinkerpop.gremlin.structure.Vertex;
34 import org.janusgraph.core.Cardinality;
35 import org.janusgraph.core.JanusGraph;
36 import org.janusgraph.core.Multiplicity;
37 import org.janusgraph.core.PropertyKey;
38 import org.janusgraph.core.schema.JanusGraphManagement;
39 import org.onap.aai.config.SpringContextAware;
40 import org.onap.aai.db.props.AAIProperties;
41 import org.onap.aai.edges.EdgeIngestor;
42 import org.onap.aai.edges.EdgeRule;
43 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
44 import org.onap.aai.introspection.*;
45 import org.onap.aai.logging.LogFormatTools;
46 import org.onap.aai.schema.enums.PropertyMetadata;
47 import org.onap.aai.setup.SchemaVersions;
48 import org.onap.aai.util.AAIConfig;
50 public class SchemaGenerator {
52 private static final Logger LOGGER = LoggerFactory.getLogger(SchemaGenerator.class);
55 * Load schema into JanusGraph.
62 public static void loadSchemaIntoJanusGraph(final JanusGraph graph, final JanusGraphManagement graphMgmt,
67 } catch (Exception ex) {
68 LOGGER.error(" ERROR - Could not run AAIConfig.init(). " + LogFormatTools.getStackTop(ex));
69 // System.out.println(" ERROR - Could not run AAIConfig.init(). ");
73 // NOTE - JanusGraph 0.5.3 doesn't keep a list of legal node Labels.
74 // They are only used when a vertex is actually being created.
75 // JanusGraph 1.1 will keep track (we think).
77 // Use EdgeRules to make sure edgeLabels are defined in the db. NOTE:
78 // the multiplicty used here is
79 // always "MULTI". This is not the same as our internal "Many2Many",
80 // "One2One", "One2Many" or "Many2One"
81 // We use the same edge-label for edges between many different types of
82 // nodes and our internal
83 // multiplicty definitions depends on which two types of nodes are being
86 Multimap<String, EdgeRule> edges = null;
87 Set<String> labels = new HashSet<>();
89 EdgeIngestor edgeIngestor = SpringContextAware.getBean(EdgeIngestor.class);
92 edges = edgeIngestor.getAllCurrentRules();
93 } catch (EdgeRuleNotFoundException e) {
94 LOGGER.error("Unable to find all rules {}", LogFormatTools.getStackTop(e));
97 for (EdgeRule rule : edges.values()) {
98 labels.add(rule.getLabel());
101 for (String label : labels) {
102 if (graphMgmt.containsRelationType(label)) {
103 String dmsg = " EdgeLabel [" + label + "] already existed. ";
106 String dmsg = "Making EdgeLabel: [" + label + "]";
108 graphMgmt.makeEdgeLabel(label).multiplicity(Multiplicity.valueOf("MULTI")).make();
112 Loader loader = LoaderUtil.getLatestVersion();
114 Map<String, Introspector> objs = loader.getAllObjects();
115 Map<String, PropertyKey> seenProps = new HashMap<>();
117 for (Introspector obj : objs.values()) {
118 for (String propName : obj.getProperties()) {
119 String dbPropName = propName;
120 Optional<String> alias = obj.getPropertyMetadata(propName, PropertyMetadata.DB_ALIAS);
121 if (alias.isPresent()) {
122 dbPropName = alias.get();
124 if (graphMgmt.containsRelationType(dbPropName)) {
125 String dmsg = " PropertyKey [" + dbPropName + "] already existed in the DB. ";
128 Class<?> type = obj.getClass(propName);
129 Cardinality cardinality = Cardinality.SINGLE;
130 boolean process = false;
131 if (obj.isListType(propName) && obj.isSimpleGenericType(propName)) {
132 cardinality = Cardinality.SET;
133 type = obj.getGenericTypeClass(propName);
135 } else if (obj.isSimpleType(propName)) {
141 String imsg = "Creating PropertyKey: [" + dbPropName + "], [" + type.getSimpleName() + "], ["
145 if (!seenProps.containsKey(dbPropName)) {
146 propK = graphMgmt.makePropertyKey(dbPropName).dataType(type).cardinality(cardinality)
148 seenProps.put(dbPropName, propK);
150 propK = seenProps.get(dbPropName);
152 if (graphMgmt.containsGraphIndex(dbPropName)) {
153 String dmsg = " Index [" + dbPropName + "] already existed in the DB. ";
156 if (obj.getIndexedProperties().contains(propName)) {
157 if (obj.getUniqueProperties().contains(propName)) {
158 imsg = "Add Unique index for PropertyKey: [" + dbPropName + "]";
160 graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK).unique()
161 .buildCompositeIndex();
163 imsg = "Add index for PropertyKey: [" + dbPropName + "]";
165 graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK).buildCompositeIndex();
168 imsg = "No index added for PropertyKey: [" + dbPropName + "]";
177 String imsg = "-- About to call graphMgmt commit";
179 if (backend != null) {
180 LOGGER.info("Successfully loaded the schema to " + backend);