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 com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import com.google.common.collect.Multimap;
27 import com.thinkaurelius.titan.core.Cardinality;
28 import com.thinkaurelius.titan.core.Multiplicity;
29 import com.thinkaurelius.titan.core.PropertyKey;
30 import com.thinkaurelius.titan.core.TitanGraph;
31 import com.thinkaurelius.titan.core.schema.TitanManagement;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.openecomp.aai.db.props.AAIProperties;
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;
46 public class SchemaGenerator{
48 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(SchemaGenerator.class);
49 private static boolean addDefaultCR = true;
53 * Load schema into titan.
55 * @param graph the graph
56 * @param graphMgmt the graph mgmt
57 * @param addDefaultCloudRegion the add default cloud region
59 public static void loadSchemaIntoTitan(final TitanGraph graph, final TitanManagement graphMgmt, boolean addDefaultCloudRegion) {
60 addDefaultCR = addDefaultCloudRegion;
61 loadSchemaIntoTitan(graph, graphMgmt);
65 * Load schema into titan.
67 * @param graph the graph
68 * @param graphMgmt the graph mgmt
70 public static void loadSchemaIntoTitan(final TitanGraph graph, final TitanManagement graphMgmt) {
76 LOGGER.error(" ERROR - Could not run AAIConfig.init(). ", ex);
77 System.out.println(" ERROR - Could not run AAIConfig.init(). ");
81 // NOTE - Titan 0.5.3 doesn't keep a list of legal node Labels.
82 // They are only used when a vertex is actually being created. Titan 1.1 will keep track (we think).
85 // Use EdgeRules to make sure edgeLabels are defined in the db. NOTE: the multiplicty used here is
86 // always "MULTI". This is not the same as our internal "Many2Many", "One2One", "One2Many" or "Many2One"
87 // We use the same edge-label for edges between many different types of nodes and our internal
88 // multiplicty definitions depends on which two types of nodes are being connected.
90 Multimap<String, EdgeRule> edges = null;
91 Set<String> labels = new HashSet<>();
93 edges = EdgeRules.getInstance().getAllRules();
94 for (EdgeRule rule : edges.values()) {
95 labels.add(rule.getLabel());
98 for( String label: labels){
99 if( graphMgmt.containsRelationType(label) ) {
100 String dmsg = " EdgeLabel [" + label + "] already existed. ";
101 System.out.println(dmsg);
104 String dmsg = "Making EdgeLabel: [" + label + "]";
105 System.out.println(dmsg);
107 graphMgmt.makeEdgeLabel(label).multiplicity(Multiplicity.valueOf("MULTI")).make();
111 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST);
112 Map<String, Introspector> objs = loader.getAllObjects();
113 Map<String, PropertyKey> seenProps = new HashMap<>();
115 for (Introspector obj : objs.values()) {
116 for (String propName : obj.getProperties()) {
117 String dbPropName = propName;
118 Optional<String> alias = obj.getPropertyMetadata(propName, PropertyMetadata.DB_ALIAS);
119 if (alias.isPresent()) {
120 dbPropName = alias.get();
122 if( graphMgmt.containsRelationType(propName) ){
123 String dmsg = " PropertyKey [" + propName + "] already existed in the DB. ";
124 System.out.println(dmsg);
127 Class<?> type = obj.getClass(propName);
128 Cardinality cardinality = Cardinality.SINGLE;
129 boolean process = false;
130 if (obj.isListType(propName) && obj.isSimpleGenericType(propName)) {
131 cardinality = Cardinality.SET;
132 type = obj.getGenericTypeClass(propName);
134 } else if (obj.isSimpleType(propName)) {
140 String imsg = "Creating PropertyKey: [" + dbPropName + "], ["+ type.getSimpleName() + "], [" + cardinality + "]";
141 System.out.println(imsg);
144 if (!seenProps.containsKey(dbPropName)) {
145 propK = graphMgmt.makePropertyKey(dbPropName).dataType(type).cardinality(cardinality).make();
146 seenProps.put(dbPropName, propK);
148 propK = seenProps.get(dbPropName);
150 if (graphMgmt.containsGraphIndex(dbPropName)) {
151 String dmsg = " Index [" + dbPropName + "] already existed in the DB. ";
152 System.out.println(dmsg);
155 if( obj.getIndexedProperties().contains(propName) ){
156 if( obj.getUniqueProperties().contains(propName) ){
157 imsg = "Add Unique index for PropertyKey: [" + dbPropName + "]";
158 System.out.println(imsg);
160 graphMgmt.buildIndex(dbPropName,Vertex.class).addKey(propK).unique().buildCompositeIndex();
162 imsg = "Add index for PropertyKey: [" + dbPropName + "]";
163 System.out.println(imsg);
165 graphMgmt.buildIndex(dbPropName,Vertex.class).addKey(propK).buildCompositeIndex();
168 imsg = "No index added for PropertyKey: [" + dbPropName + "]";
169 System.out.println(imsg);
178 String imsg = "-- About to call graphMgmt commit";
179 System.out.println(imsg);
184 if (!graph.traversal().V().has("cloud-owner", "att-aic").has("cloud-region-id", "AAIAIC25").hasNext()) {
185 imsg = "Adding default cloud region to graph...";
186 System.out.println(imsg);
188 final Vertex cloudRegion = graph.addVertex();
190 final String ts = String.valueOf(System.currentTimeMillis() / 1000L);
192 cloudRegion.property("aai-node-type", "cloud-region");
193 cloudRegion.property("cloud-owner", "att-aic");
194 cloudRegion.property("cloud-region-id", "AAIAIC25");
195 cloudRegion.property("cloud-region-version", "2.5");
196 cloudRegion.property("complex-name", "AAIAIC25");
197 cloudRegion.property("aai-created-ts", ts);
198 cloudRegion.property("resource-version", ts);
199 cloudRegion.property("source-of-truth", "aai-schema-loader");
200 cloudRegion.property("last-mod-source-of-truth", "aai-schema-loader");
201 cloudRegion.property(AAIProperties.AAI_URI, "/cloud-infrastructure/cloud-regions/cloud-region/att-aic/AAIAIC25");
205 }// End of loadSchemaIntoTitan()