2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.dbgen;
25 import java.util.HashMap;
26 import java.util.HashSet;
28 import java.util.Optional;
31 import org.apache.tinkerpop.gremlin.structure.Vertex;
32 import org.onap.aai.db.props.AAIProperties;
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.schema.enums.PropertyMetadata;
38 import org.onap.aai.serialization.db.EdgeRule;
39 import org.onap.aai.serialization.db.EdgeRules;
40 import org.onap.aai.util.AAIConfig;
42 import com.att.eelf.configuration.EELFLogger;
43 import com.att.eelf.configuration.EELFManager;
44 import com.google.common.collect.Multimap;
45 import com.thinkaurelius.titan.core.Cardinality;
46 import com.thinkaurelius.titan.core.Multiplicity;
47 import com.thinkaurelius.titan.core.PropertyKey;
48 import com.thinkaurelius.titan.core.TitanGraph;
49 import com.thinkaurelius.titan.core.schema.TitanManagement;
53 public class SchemaGenerator{
55 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(SchemaGenerator.class);
56 private static boolean addDefaultCR = true;
60 * Load schema into titan.
62 * @param graph the graph
63 * @param graphMgmt the graph mgmt
64 * @param addDefaultCloudRegion the add default cloud region
66 public static void loadSchemaIntoTitan(final TitanGraph graph, final TitanManagement graphMgmt, boolean addDefaultCloudRegion) {
67 addDefaultCR = addDefaultCloudRegion;
68 loadSchemaIntoTitan(graph, graphMgmt);
72 * Load schema into titan.
74 * @param graph the graph
75 * @param graphMgmt the graph mgmt
77 public static void loadSchemaIntoTitan(final TitanGraph graph, final TitanManagement graphMgmt) {
83 LOGGER.error(" ERROR - Could not run AAIConfig.init(). ", ex);
84 System.out.println(" ERROR - Could not run AAIConfig.init(). ");
88 // NOTE - Titan 0.5.3 doesn't keep a list of legal node Labels.
89 // They are only used when a vertex is actually being created. Titan 1.1 will keep track (we think).
92 // Use EdgeRules to make sure edgeLabels are defined in the db. NOTE: the multiplicty used here is
93 // always "MULTI". This is not the same as our internal "Many2Many", "One2One", "One2Many" or "Many2One"
94 // We use the same edge-label for edges between many different types of nodes and our internal
95 // multiplicty definitions depends on which two types of nodes are being connected.
97 Multimap<String, EdgeRule> edges = null;
98 Set<String> labels = new HashSet<>();
100 edges = EdgeRules.getInstance().getAllRules();
101 for (EdgeRule rule : edges.values()) {
102 labels.add(rule.getLabel());
105 for( String label: labels){
106 if( graphMgmt.containsRelationType(label) ) {
107 String dmsg = " EdgeLabel [" + label + "] already existed. ";
108 System.out.println(dmsg);
111 String dmsg = "Making EdgeLabel: [" + label + "]";
112 System.out.println(dmsg);
114 graphMgmt.makeEdgeLabel(label).multiplicity(Multiplicity.valueOf("MULTI")).make();
118 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST);
119 Map<String, Introspector> objs = loader.getAllObjects();
120 Map<String, PropertyKey> seenProps = new HashMap<>();
122 for (Introspector obj : objs.values()) {
123 for (String propName : obj.getProperties()) {
124 String dbPropName = propName;
125 Optional<String> alias = obj.getPropertyMetadata(propName, PropertyMetadata.DB_ALIAS);
126 if (alias.isPresent()) {
127 dbPropName = alias.get();
129 if( graphMgmt.containsRelationType(propName) ){
130 String dmsg = " PropertyKey [" + propName + "] already existed in the DB. ";
131 System.out.println(dmsg);
134 Class<?> type = obj.getClass(propName);
135 Cardinality cardinality = Cardinality.SINGLE;
136 boolean process = false;
137 if (obj.isListType(propName) && obj.isSimpleGenericType(propName)) {
138 cardinality = Cardinality.SET;
139 type = obj.getGenericTypeClass(propName);
141 } else if (obj.isSimpleType(propName)) {
147 String imsg = "Creating PropertyKey: [" + dbPropName + "], ["+ type.getSimpleName() + "], [" + cardinality + "]";
148 System.out.println(imsg);
151 if (!seenProps.containsKey(dbPropName)) {
152 propK = graphMgmt.makePropertyKey(dbPropName).dataType(type).cardinality(cardinality).make();
153 seenProps.put(dbPropName, propK);
155 propK = seenProps.get(dbPropName);
157 if (graphMgmt.containsGraphIndex(dbPropName)) {
158 String dmsg = " Index [" + dbPropName + "] already existed in the DB. ";
159 System.out.println(dmsg);
162 if( obj.getIndexedProperties().contains(propName) ){
163 if( obj.getUniqueProperties().contains(propName) ){
164 imsg = "Add Unique index for PropertyKey: [" + dbPropName + "]";
165 System.out.println(imsg);
167 graphMgmt.buildIndex(dbPropName,Vertex.class).addKey(propK).unique().buildCompositeIndex();
169 imsg = "Add index for PropertyKey: [" + dbPropName + "]";
170 System.out.println(imsg);
172 graphMgmt.buildIndex(dbPropName,Vertex.class).addKey(propK).buildCompositeIndex();
175 imsg = "No index added for PropertyKey: [" + dbPropName + "]";
176 System.out.println(imsg);
185 String imsg = "-- About to call graphMgmt commit";
186 System.out.println(imsg);
190 }// End of loadSchemaIntoTitan()