Update the aai-common with the latest code
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / dbgen / SchemaGenerator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.aai.dbgen;
22
23
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Optional;
28 import java.util.Set;
29
30 import org.apache.tinkerpop.gremlin.structure.Vertex;
31
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;
50
51
52
53 public class SchemaGenerator{
54
55         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(SchemaGenerator.class);
56         private static boolean addDefaultCR = true;
57         
58         
59          /**
60          * Load schema into titan.
61          *
62          * @param graph the graph
63          * @param graphMgmt the graph mgmt
64          * @param addDefaultCloudRegion the add default cloud region
65          */
66         public static void loadSchemaIntoTitan(final TitanGraph graph, final TitanManagement graphMgmt, boolean addDefaultCloudRegion) {
67                  addDefaultCR = addDefaultCloudRegion;
68                  loadSchemaIntoTitan(graph, graphMgmt);
69          }
70         
71     /**
72      * Load schema into titan.
73      *
74      * @param graph the graph
75      * @param graphMgmt the graph mgmt
76      */
77     public static void loadSchemaIntoTitan(final TitanGraph graph, final TitanManagement graphMgmt) {
78
79         try {
80                 AAIConfig.init();
81         }
82         catch (Exception ex){
83                         LOGGER.error(" ERROR - Could not run AAIConfig.init(). ", ex);
84                         System.out.println(" ERROR - Could not run AAIConfig.init(). ");
85                         System.exit(1);
86                 }
87         
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).
90                 
91
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.
96
97                 Multimap<String, EdgeRule> edges = null;
98                 Set<String> labels = new HashSet<>();
99                 try {
100                         edges = EdgeRules.getInstance().getAllRules();
101                         for (EdgeRule rule : edges.values()) {
102                                 labels.add(rule.getLabel());
103                         }
104                 } catch (AAIException e) {
105                         LOGGER.error("could not get edge rules", e);
106                         System.out.println("could not get edge rules");
107                         System.exit(1);
108                 }
109                 for( String label: labels){
110                         if( graphMgmt.containsRelationType(label) ) {
111                                 String dmsg = " EdgeLabel  [" + label + "] already existed. ";
112                 System.out.println(dmsg);
113                 LOGGER.debug(dmsg);
114             } else {
115                 String dmsg = "Making EdgeLabel: [" + label + "]";
116                 System.out.println(dmsg);
117                 LOGGER.debug(dmsg);
118                 graphMgmt.makeEdgeLabel(label).multiplicity(Multiplicity.valueOf("MULTI")).make();
119             }
120         }     
121
122                 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST);
123                 Map<String, Introspector> objs = loader.getAllObjects();
124                 Map<String, PropertyKey> seenProps = new HashMap<>();
125                 
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();
132                                 }
133                                 if( graphMgmt.containsRelationType(propName) ){
134                         String dmsg = " PropertyKey  [" + propName + "] already existed in the DB. ";
135                         System.out.println(dmsg);
136                         LOGGER.debug(dmsg);
137                     } else {
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);
144                                 process = true;
145                         } else if (obj.isSimpleType(propName)) {
146                                 process = true;
147                         }
148
149                         if (process) {
150
151                                 String imsg = "Creating PropertyKey: [" + dbPropName + "], ["+ type.getSimpleName() + "], [" + cardinality + "]";
152                                 System.out.println(imsg);
153                                 LOGGER.info(imsg);
154                                 PropertyKey propK;
155                                 if (!seenProps.containsKey(dbPropName)) {
156                                         propK = graphMgmt.makePropertyKey(dbPropName).dataType(type).cardinality(cardinality).make();
157                                         seenProps.put(dbPropName, propK);
158                                 } else {
159                                         propK = seenProps.get(dbPropName);
160                                 }
161                                 if (graphMgmt.containsGraphIndex(dbPropName)) {
162                                         String dmsg = " Index  [" + dbPropName + "] already existed in the DB. ";
163                                         System.out.println(dmsg);
164                                         LOGGER.debug(dmsg);
165                                 } else {
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);
170                                                         LOGGER.info(imsg);
171                                                 graphMgmt.buildIndex(dbPropName,Vertex.class).addKey(propK).unique().buildCompositeIndex();
172                                              } else {
173                                                 imsg = "Add index for PropertyKey: [" + dbPropName + "]";
174                                                         System.out.println(imsg);
175                                                         LOGGER.info(imsg);
176                                                 graphMgmt.buildIndex(dbPropName,Vertex.class).addKey(propK).buildCompositeIndex();
177                                              }
178                                          } else {
179                                                 imsg = "No index added for PropertyKey: [" + dbPropName + "]";
180                                                 System.out.println(imsg);
181                                                 LOGGER.info(imsg);
182                                          }
183                                 }
184                         }
185                     }
186                         }
187                 }
188         
189         String imsg = "-- About to call graphMgmt commit";
190         System.out.println(imsg);
191         LOGGER.info(imsg);
192
193         graphMgmt.commit();
194     }// End of loadSchemaIntoTitan()
195
196 }
197
198