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.onap.aai.util.AAIConstants;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import com.google.common.collect.Multimap;
28 import java.util.HashMap;
29 import java.util.HashSet;
31 import java.util.Optional;
34 import org.apache.tinkerpop.gremlin.structure.Vertex;
35 import org.janusgraph.core.Cardinality;
36 import org.janusgraph.core.JanusGraph;
37 import org.janusgraph.core.Multiplicity;
38 import org.janusgraph.core.PropertyKey;
39 import org.janusgraph.core.schema.JanusGraphManagement;
40 import org.janusgraph.core.schema.JanusGraphIndex;
41 import org.janusgraph.core.schema.ConsistencyModifier;
42 import org.onap.aai.config.SpringContextAware;
43 import org.onap.aai.db.props.AAIProperties;
44 import org.onap.aai.edges.EdgeIngestor;
45 import org.onap.aai.edges.EdgeRule;
46 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
47 import org.onap.aai.introspection.*;
48 import org.onap.aai.logging.LogFormatTools;
49 import org.onap.aai.schema.enums.PropertyMetadata;
50 import org.onap.aai.setup.SchemaVersions;
51 import org.onap.aai.util.AAIConfig;
53 public class SchemaGenerator {
55 private static final Logger LOGGER = LoggerFactory.getLogger(SchemaGenerator.class);
58 * Load schema into JanusGraph.
65 public static void loadSchemaIntoJanusGraph(final JanusGraph graph, final JanusGraphManagement graphMgmt,
70 } catch (Exception ex) {
71 LOGGER.error(" ERROR - Could not run AAIConfig.init(). " + LogFormatTools.getStackTop(ex));
72 // System.out.println(" ERROR - Could not run AAIConfig.init(). ");
76 // NOTE - JanusGraph 0.5.3 doesn't keep a list of legal node Labels.
77 // They are only used when a vertex is actually being created.
78 // JanusGraph 1.1 will keep track (we think).
80 // Use EdgeRules to make sure edgeLabels are defined in the db. NOTE:
81 // the multiplicty used here is
82 // always "MULTI". This is not the same as our internal "Many2Many",
83 // "One2One", "One2Many" or "Many2One"
84 // We use the same edge-label for edges between many different types of
85 // nodes and our internal
86 // multiplicty definitions depends on which two types of nodes are being
89 Multimap<String, EdgeRule> edges = null;
90 Set<String> labels = new HashSet<>();
92 EdgeIngestor edgeIngestor = SpringContextAware.getBean(EdgeIngestor.class);
95 edges = edgeIngestor.getAllCurrentRules();
96 } 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 Loader loader = LoaderUtil.getLatestVersion();
117 Map<String, Introspector> objs = loader.getAllObjects();
118 Map<String, PropertyKey> seenProps = new HashMap<>();
120 for (Introspector obj : objs.values()) {
121 for (String propName : obj.getProperties()) {
122 String dbPropName = propName;
123 Optional<String> alias = obj.getPropertyMetadata(propName, PropertyMetadata.DB_ALIAS);
124 if (alias.isPresent()) {
125 dbPropName = alias.get();
127 if (graphMgmt.containsRelationType(dbPropName)) {
128 String dmsg = " PropertyKey [" + dbPropName + "] already existed in the DB. ";
131 Class<?> type = obj.getClass(propName);
132 Cardinality cardinality = Cardinality.SINGLE;
133 boolean process = false;
134 if (obj.isListType(propName) && obj.isSimpleGenericType(propName)) {
135 cardinality = Cardinality.SET;
136 type = obj.getGenericTypeClass(propName);
138 } else if (obj.isSimpleType(propName)) {
144 String imsg = "Creating PropertyKey: [" + dbPropName + "], [" + type.getSimpleName() + "], ["
148 if (!seenProps.containsKey(dbPropName)) {
149 propK = graphMgmt.makePropertyKey(dbPropName).dataType(type).cardinality(cardinality)
151 if (dbPropName.equals("aai-uri")) {
152 String aai_uri_lock_enabled = AAIConfig.get(AAIConstants.AAI_LOCK_URI_ENABLED, "false");
153 LOGGER.info(" Info: aai_uri_lock_enabled:" + aai_uri_lock_enabled);
154 if ("true".equals(aai_uri_lock_enabled)) {
155 LOGGER.info(" Lock is being set for aai-uri Property.");
156 graphMgmt.setConsistency(propK, ConsistencyModifier.LOCK);
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(propName)) {
168 JanusGraphIndex indexG = null;
169 if (obj.getUniqueProperties().contains(propName)) {
170 imsg = "Add Unique index for PropertyKey: [" + dbPropName + "]";
172 indexG = graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK).unique().buildCompositeIndex();
174 imsg = "Add index for PropertyKey: [" + dbPropName + "]";
176 indexG = graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK).buildCompositeIndex();
178 if (indexG != null && dbPropName.equals("aai-uri")) {
179 String aai_uri_lock_enabled = AAIConfig.get(AAIConstants.AAI_LOCK_URI_ENABLED, "false");
180 LOGGER.info(" Info:: aai_uri_lock_enabled:" + aai_uri_lock_enabled);
181 if ("true".equals(aai_uri_lock_enabled)) {
182 LOGGER.info("Lock is being set for aai-uri Index.");
183 graphMgmt.setConsistency(indexG, ConsistencyModifier.LOCK);
187 imsg = "No index added for PropertyKey: [" + dbPropName + "]";
196 String imsg = "-- About to call graphMgmt commit";
198 if (backend != null) {
199 LOGGER.info("Successfully loaded the schema to " + backend);