2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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=========================================================
21 package org.openecomp.aai.dbgen;
24 import java.util.HashMap;
25 import java.util.HashSet;
27 import java.util.Optional;
30 import org.apache.tinkerpop.gremlin.structure.Vertex;
31 import org.openecomp.aai.db.props.AAIProperties;
32 import org.openecomp.aai.introspection.Introspector;
33 import org.openecomp.aai.introspection.Loader;
34 import org.openecomp.aai.introspection.LoaderFactory;
35 import org.openecomp.aai.introspection.ModelType;
36 import org.openecomp.aai.schema.enums.PropertyMetadata;
37 import org.openecomp.aai.serialization.db.EdgeRule;
38 import org.openecomp.aai.serialization.db.EdgeRules;
39 import org.openecomp.aai.util.AAIConfig;
41 import com.att.eelf.configuration.EELFLogger;
42 import com.att.eelf.configuration.EELFManager;
43 import com.google.common.collect.Multimap;
44 import com.thinkaurelius.titan.core.Cardinality;
45 import com.thinkaurelius.titan.core.Multiplicity;
46 import com.thinkaurelius.titan.core.PropertyKey;
47 import com.thinkaurelius.titan.core.TitanGraph;
48 import com.thinkaurelius.titan.core.schema.TitanManagement;
52 public class SchemaGenerator{
54 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(SchemaGenerator.class);
55 private static boolean addDefaultCR = true;
59 * Load schema into titan.
61 * @param graph the graph
62 * @param graphMgmt the graph mgmt
63 * @param addDefaultCloudRegion the add default cloud region
65 public static void loadSchemaIntoTitan(final TitanGraph graph, final TitanManagement graphMgmt, boolean addDefaultCloudRegion) {
66 addDefaultCR = addDefaultCloudRegion;
67 loadSchemaIntoTitan(graph, graphMgmt);
71 * Load schema into titan.
73 * @param graph the graph
74 * @param graphMgmt the graph mgmt
76 public static void loadSchemaIntoTitan(final TitanGraph graph, final TitanManagement graphMgmt) {
82 LOGGER.error(" ERROR - Could not run AAIConfig.init(). ", ex);
83 System.out.println(" ERROR - Could not run AAIConfig.init(). ");
87 // NOTE - Titan 0.5.3 doesn't keep a list of legal node Labels.
88 // They are only used when a vertex is actually being created. Titan 1.1 will keep track (we think).
91 // Use EdgeRules to make sure edgeLabels are defined in the db. NOTE: the multiplicty used here is
92 // always "MULTI". This is not the same as our internal "Many2Many", "One2One", "One2Many" or "Many2One"
93 // We use the same edge-label for edges between many different types of nodes and our internal
94 // multiplicty definitions depends on which two types of nodes are being connected.
96 Multimap<String, EdgeRule> edges = null;
97 Set<String> labels = new HashSet<>();
99 edges = EdgeRules.getInstance().getAllRules();
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. ";
107 System.out.println(dmsg);
110 String dmsg = "Making EdgeLabel: [" + label + "]";
111 System.out.println(dmsg);
113 graphMgmt.makeEdgeLabel(label).multiplicity(Multiplicity.valueOf("MULTI")).make();
117 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST);
118 Map<String, Introspector> objs = loader.getAllObjects();
119 Map<String, PropertyKey> seenProps = new HashMap<>();
121 for (Introspector obj : objs.values()) {
122 for (String propName : obj.getProperties()) {
123 String dbPropName = propName;
124 Optional<String> alias = obj.getPropertyMetadata(propName, PropertyMetadata.DB_ALIAS);
125 if (alias.isPresent()) {
126 dbPropName = alias.get();
128 if( graphMgmt.containsRelationType(propName) ){
129 String dmsg = " PropertyKey [" + propName + "] already existed in the DB. ";
130 System.out.println(dmsg);
133 Class<?> type = obj.getClass(propName);
134 Cardinality cardinality = Cardinality.SINGLE;
135 boolean process = false;
136 if (obj.isListType(propName) && obj.isSimpleGenericType(propName)) {
137 cardinality = Cardinality.SET;
138 type = obj.getGenericTypeClass(propName);
140 } else if (obj.isSimpleType(propName)) {
146 String imsg = "Creating PropertyKey: [" + dbPropName + "], ["+ type.getSimpleName() + "], [" + cardinality + "]";
147 System.out.println(imsg);
150 if (!seenProps.containsKey(dbPropName)) {
151 propK = graphMgmt.makePropertyKey(dbPropName).dataType(type).cardinality(cardinality).make();
152 seenProps.put(dbPropName, propK);
154 propK = seenProps.get(dbPropName);
156 if (graphMgmt.containsGraphIndex(dbPropName)) {
157 String dmsg = " Index [" + dbPropName + "] already existed in the DB. ";
158 System.out.println(dmsg);
161 if( obj.getIndexedProperties().contains(propName) ){
162 if( obj.getUniqueProperties().contains(propName) ){
163 imsg = "Add Unique index for PropertyKey: [" + dbPropName + "]";
164 System.out.println(imsg);
166 graphMgmt.buildIndex(dbPropName,Vertex.class).addKey(propK).unique().buildCompositeIndex();
168 imsg = "Add index for PropertyKey: [" + dbPropName + "]";
169 System.out.println(imsg);
171 graphMgmt.buildIndex(dbPropName,Vertex.class).addKey(propK).buildCompositeIndex();
174 imsg = "No index added for PropertyKey: [" + dbPropName + "]";
175 System.out.println(imsg);
184 String imsg = "-- About to call graphMgmt commit";
185 System.out.println(imsg);
189 }// End of loadSchemaIntoTitan()