[AAI-178 Amsterdam] Make Edge Properties to be
[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 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;
42
43 import java.util.*;
44
45
46 public class SchemaGenerator{
47
48         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(SchemaGenerator.class);
49         private static boolean addDefaultCR = true;
50         
51         
52          /**
53          * Load schema into titan.
54          *
55          * @param graph the graph
56          * @param graphMgmt the graph mgmt
57          * @param addDefaultCloudRegion the add default cloud region
58          */
59         public static void loadSchemaIntoTitan(final TitanGraph graph, final TitanManagement graphMgmt, boolean addDefaultCloudRegion) {
60                  addDefaultCR = addDefaultCloudRegion;
61                  loadSchemaIntoTitan(graph, graphMgmt);
62          }
63         
64     /**
65      * Load schema into titan.
66      *
67      * @param graph the graph
68      * @param graphMgmt the graph mgmt
69      */
70     public static void loadSchemaIntoTitan(final TitanGraph graph, final TitanManagement graphMgmt) {
71
72         try {
73                 AAIConfig.init();
74         }
75         catch (Exception ex){
76                         LOGGER.error(" ERROR - Could not run AAIConfig.init(). ", ex);
77                         System.out.println(" ERROR - Could not run AAIConfig.init(). ");
78                         System.exit(1);
79                 }
80         
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).
83                 
84
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.
89
90                 Multimap<String, EdgeRule> edges = null;
91                 Set<String> labels = new HashSet<>();
92                 
93                 edges = EdgeRules.getInstance().getAllRules();
94                 for (EdgeRule rule : edges.values()) {
95                         labels.add(rule.getLabel());
96                 }
97                 
98                 for( String label: labels){
99                         if( graphMgmt.containsRelationType(label) ) {
100                                 String dmsg = " EdgeLabel  [" + label + "] already existed. ";
101                 System.out.println(dmsg);
102                 LOGGER.debug(dmsg);
103             } else {
104                 String dmsg = "Making EdgeLabel: [" + label + "]";
105                 System.out.println(dmsg);
106                 LOGGER.debug(dmsg);
107                 graphMgmt.makeEdgeLabel(label).multiplicity(Multiplicity.valueOf("MULTI")).make();
108             }
109         }     
110
111                 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST);
112                 Map<String, Introspector> objs = loader.getAllObjects();
113                 Map<String, PropertyKey> seenProps = new HashMap<>();
114                 
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();
121                                 }
122                                 if( graphMgmt.containsRelationType(propName) ){
123                         String dmsg = " PropertyKey  [" + propName + "] already existed in the DB. ";
124                         System.out.println(dmsg);
125                         LOGGER.debug(dmsg);
126                     } else {
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);
133                                 process = true;
134                         } else if (obj.isSimpleType(propName)) {
135                                 process = true;
136                         }
137
138                         if (process) {
139
140                                 String imsg = "Creating PropertyKey: [" + dbPropName + "], ["+ type.getSimpleName() + "], [" + cardinality + "]";
141                                 System.out.println(imsg);
142                                 LOGGER.info(imsg);
143                                 PropertyKey propK;
144                                 if (!seenProps.containsKey(dbPropName)) {
145                                         propK = graphMgmt.makePropertyKey(dbPropName).dataType(type).cardinality(cardinality).make();
146                                         seenProps.put(dbPropName, propK);
147                                 } else {
148                                         propK = seenProps.get(dbPropName);
149                                 }
150                                 if (graphMgmt.containsGraphIndex(dbPropName)) {
151                                         String dmsg = " Index  [" + dbPropName + "] already existed in the DB. ";
152                                         System.out.println(dmsg);
153                                         LOGGER.debug(dmsg);
154                                 } else {
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);
159                                                         LOGGER.info(imsg);
160                                                 graphMgmt.buildIndex(dbPropName,Vertex.class).addKey(propK).unique().buildCompositeIndex();
161                                              } else {
162                                                 imsg = "Add index for PropertyKey: [" + dbPropName + "]";
163                                                         System.out.println(imsg);
164                                                         LOGGER.info(imsg);
165                                                 graphMgmt.buildIndex(dbPropName,Vertex.class).addKey(propK).buildCompositeIndex();
166                                              }
167                                          } else {
168                                                 imsg = "No index added for PropertyKey: [" + dbPropName + "]";
169                                                 System.out.println(imsg);
170                                                 LOGGER.info(imsg);
171                                          }
172                                 }
173                         }
174                     }
175                         }
176                 }
177         
178         String imsg = "-- About to call graphMgmt commit";
179         System.out.println(imsg);
180         LOGGER.info(imsg);
181         
182         graphMgmt.commit();
183         if (addDefaultCR) {
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);
187                 LOGGER.info(imsg);
188                         final Vertex cloudRegion = graph.addVertex();
189         
190                         final String ts = String.valueOf(System.currentTimeMillis() / 1000L);
191         
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");
202                         graph.tx().commit();
203                 }
204                 }
205     }// End of loadSchemaIntoTitan()
206
207 }
208
209