Merge "Update shema for VFC"
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / dbgen / SchemaGenerator.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.dbgen;
23
24
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Map;
28 import java.util.Optional;
29 import java.util.Set;
30
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;
41
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                 
100                 edges = EdgeRules.getInstance().getAllRules();
101                 for (EdgeRule rule : edges.values()) {
102                         labels.add(rule.getLabel());
103                 }
104                 
105                 for( String label: labels){
106                         if( graphMgmt.containsRelationType(label) ) {
107                                 String dmsg = " EdgeLabel  [" + label + "] already existed. ";
108                 System.out.println(dmsg);
109                 LOGGER.debug(dmsg);
110             } else {
111                 String dmsg = "Making EdgeLabel: [" + label + "]";
112                 System.out.println(dmsg);
113                 LOGGER.debug(dmsg);
114                 graphMgmt.makeEdgeLabel(label).multiplicity(Multiplicity.valueOf("MULTI")).make();
115             }
116         }     
117
118                 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST);
119                 Map<String, Introspector> objs = loader.getAllObjects();
120                 Map<String, PropertyKey> seenProps = new HashMap<>();
121                 
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();
128                                 }
129                                 if( graphMgmt.containsRelationType(propName) ){
130                         String dmsg = " PropertyKey  [" + propName + "] already existed in the DB. ";
131                         System.out.println(dmsg);
132                         LOGGER.debug(dmsg);
133                     } else {
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);
140                                 process = true;
141                         } else if (obj.isSimpleType(propName)) {
142                                 process = true;
143                         }
144
145                         if (process) {
146
147                                 String imsg = "Creating PropertyKey: [" + dbPropName + "], ["+ type.getSimpleName() + "], [" + cardinality + "]";
148                                 System.out.println(imsg);
149                                 LOGGER.info(imsg);
150                                 PropertyKey propK;
151                                 if (!seenProps.containsKey(dbPropName)) {
152                                         propK = graphMgmt.makePropertyKey(dbPropName).dataType(type).cardinality(cardinality).make();
153                                         seenProps.put(dbPropName, propK);
154                                 } else {
155                                         propK = seenProps.get(dbPropName);
156                                 }
157                                 if (graphMgmt.containsGraphIndex(dbPropName)) {
158                                         String dmsg = " Index  [" + dbPropName + "] already existed in the DB. ";
159                                         System.out.println(dmsg);
160                                         LOGGER.debug(dmsg);
161                                 } else {
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);
166                                                         LOGGER.info(imsg);
167                                                 graphMgmt.buildIndex(dbPropName,Vertex.class).addKey(propK).unique().buildCompositeIndex();
168                                              } else {
169                                                 imsg = "Add index for PropertyKey: [" + dbPropName + "]";
170                                                         System.out.println(imsg);
171                                                         LOGGER.info(imsg);
172                                                 graphMgmt.buildIndex(dbPropName,Vertex.class).addKey(propK).buildCompositeIndex();
173                                              }
174                                          } else {
175                                                 imsg = "No index added for PropertyKey: [" + dbPropName + "]";
176                                                 System.out.println(imsg);
177                                                 LOGGER.info(imsg);
178                                          }
179                                 }
180                         }
181                     }
182                         }
183                 }
184         
185         String imsg = "-- About to call graphMgmt commit";
186         System.out.println(imsg);
187         LOGGER.info(imsg);
188         
189         graphMgmt.commit();
190     }// End of loadSchemaIntoTitan()
191
192 }
193
194