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;
32 import org.openecomp.aai.db.props.AAIProperties;
33 import org.openecomp.aai.exceptions.AAIException;
34 import org.openecomp.aai.introspection.Introspector;
35 import org.openecomp.aai.introspection.Loader;
36 import org.openecomp.aai.introspection.LoaderFactory;
37 import org.openecomp.aai.introspection.ModelType;
38 import org.openecomp.aai.schema.enums.PropertyMetadata;
39 import org.openecomp.aai.serialization.db.EdgeRule;
40 import org.openecomp.aai.serialization.db.EdgeRules;
41 import org.openecomp.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());
104 } catch (AAIException e) {
105 LOGGER.error("could not get edge rules", e);
106 System.out.println("could not get edge rules");
109 for( String label: labels){
110 if( graphMgmt.containsRelationType(label) ) {
111 String dmsg = " EdgeLabel [" + label + "] already existed. ";
112 System.out.println(dmsg);
115 String dmsg = "Making EdgeLabel: [" + label + "]";
116 System.out.println(dmsg);
118 graphMgmt.makeEdgeLabel(label).multiplicity(Multiplicity.valueOf("MULTI")).make();
122 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST);
123 Map<String, Introspector> objs = loader.getAllObjects();
124 Map<String, PropertyKey> seenProps = new HashMap<>();
126 for (Introspector obj : objs.values()) {
127 for (String propName : obj.getProperties()) {
128 String dbPropName = propName;
129 Optional<String> alias = obj.getPropertyMetadata(propName, PropertyMetadata.DB_ALIAS);
130 if (alias.isPresent()) {
131 dbPropName = alias.get();
133 if( graphMgmt.containsRelationType(propName) ){
134 String dmsg = " PropertyKey [" + propName + "] already existed in the DB. ";
135 System.out.println(dmsg);
138 Class<?> type = obj.getClass(propName);
139 Cardinality cardinality = Cardinality.SINGLE;
140 boolean process = false;
141 if (obj.isListType(propName) && obj.isSimpleGenericType(propName)) {
142 cardinality = Cardinality.SET;
143 type = obj.getGenericTypeClass(propName);
145 } else if (obj.isSimpleType(propName)) {
151 String imsg = "Creating PropertyKey: [" + dbPropName + "], ["+ type.getSimpleName() + "], [" + cardinality + "]";
152 System.out.println(imsg);
155 if (!seenProps.containsKey(dbPropName)) {
156 propK = graphMgmt.makePropertyKey(dbPropName).dataType(type).cardinality(cardinality).make();
157 seenProps.put(dbPropName, propK);
159 propK = seenProps.get(dbPropName);
161 if (graphMgmt.containsGraphIndex(dbPropName)) {
162 String dmsg = " Index [" + dbPropName + "] already existed in the DB. ";
163 System.out.println(dmsg);
166 if( obj.getIndexedProperties().contains(propName) ){
167 if( obj.getUniqueProperties().contains(propName) ){
168 imsg = "Add Unique index for PropertyKey: [" + dbPropName + "]";
169 System.out.println(imsg);
171 graphMgmt.buildIndex(dbPropName,Vertex.class).addKey(propK).unique().buildCompositeIndex();
173 imsg = "Add index for PropertyKey: [" + dbPropName + "]";
174 System.out.println(imsg);
176 graphMgmt.buildIndex(dbPropName,Vertex.class).addKey(propK).buildCompositeIndex();
179 imsg = "No index added for PropertyKey: [" + dbPropName + "]";
180 System.out.println(imsg);
189 String imsg = "-- About to call graphMgmt commit";
190 System.out.println(imsg);
194 }// End of loadSchemaIntoTitan()