Enhancements for the aai-common library
[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-2018 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.onap.aai.dbgen;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import com.google.common.collect.Multimap;
26
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Map;
30 import java.util.Optional;
31 import java.util.Set;
32
33 import org.apache.tinkerpop.gremlin.structure.Vertex;
34 import org.janusgraph.core.Cardinality;
35 import org.janusgraph.core.JanusGraph;
36 import org.janusgraph.core.Multiplicity;
37 import org.janusgraph.core.PropertyKey;
38 import org.janusgraph.core.schema.JanusGraphManagement;
39 import org.onap.aai.config.SpringContextAware;
40 import org.onap.aai.db.props.AAIProperties;
41 import org.onap.aai.edges.EdgeIngestor;
42 import org.onap.aai.edges.EdgeRule;
43 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
44 import org.onap.aai.introspection.*;
45 import org.onap.aai.logging.LogFormatTools;
46 import org.onap.aai.schema.enums.PropertyMetadata;
47 import org.onap.aai.setup.SchemaVersions;
48 import org.onap.aai.util.AAIConfig;
49
50 public class SchemaGenerator {
51
52     private static final Logger LOGGER = LoggerFactory.getLogger(SchemaGenerator.class);
53
54     /**
55      * Load schema into JanusGraph.
56      *
57      * @param graph
58      *        the graph
59      * @param graphMgmt
60      *        the graph mgmt
61      */
62     public static void loadSchemaIntoJanusGraph(final JanusGraph graph, final JanusGraphManagement graphMgmt,
63             String backend) {
64
65         try {
66             AAIConfig.init();
67         } catch (Exception ex) {
68             LOGGER.error(" ERROR - Could not run AAIConfig.init(). " + LogFormatTools.getStackTop(ex));
69             // System.out.println(" ERROR - Could not run AAIConfig.init(). ");
70             System.exit(1);
71         }
72
73         // NOTE - JanusGraph 0.5.3 doesn't keep a list of legal node Labels.
74         // They are only used when a vertex is actually being created.
75         // JanusGraph 1.1 will keep track (we think).
76
77         // Use EdgeRules to make sure edgeLabels are defined in the db. NOTE:
78         // the multiplicty used here is
79         // always "MULTI". This is not the same as our internal "Many2Many",
80         // "One2One", "One2Many" or "Many2One"
81         // We use the same edge-label for edges between many different types of
82         // nodes and our internal
83         // multiplicty definitions depends on which two types of nodes are being
84         // connected.
85
86         Multimap<String, EdgeRule> edges = null;
87         Set<String> labels = new HashSet<>();
88
89         EdgeIngestor edgeIngestor = SpringContextAware.getBean(EdgeIngestor.class);
90
91         try {
92             edges = edgeIngestor.getAllCurrentRules();
93         } catch (EdgeRuleNotFoundException e) {
94             LOGGER.error("Unable to find all rules {}", LogFormatTools.getStackTop(e));
95         }
96
97         for (EdgeRule rule : edges.values()) {
98             labels.add(rule.getLabel());
99         }
100
101         for (String label : labels) {
102             if (graphMgmt.containsRelationType(label)) {
103                 String dmsg = " EdgeLabel  [" + label + "] already existed. ";
104                 LOGGER.debug(dmsg);
105             } else {
106                 String dmsg = "Making EdgeLabel: [" + label + "]";
107                 LOGGER.debug(dmsg);
108                 graphMgmt.makeEdgeLabel(label).multiplicity(Multiplicity.valueOf("MULTI")).make();
109             }
110         }
111
112         Loader loader = LoaderUtil.getLatestVersion();
113
114         Map<String, Introspector> objs = loader.getAllObjects();
115         Map<String, PropertyKey> seenProps = new HashMap<>();
116
117         for (Introspector obj : objs.values()) {
118             for (String propName : obj.getProperties()) {
119                 String dbPropName = propName;
120                 Optional<String> alias = obj.getPropertyMetadata(propName, PropertyMetadata.DB_ALIAS);
121                 if (alias.isPresent()) {
122                     dbPropName = alias.get();
123                 }
124                 if (graphMgmt.containsRelationType(dbPropName)) {
125                     String dmsg = " PropertyKey  [" + dbPropName + "] already existed in the DB. ";
126                     LOGGER.debug(dmsg);
127                 } else {
128                     Class<?> type = obj.getClass(propName);
129                     Cardinality cardinality = Cardinality.SINGLE;
130                     boolean process = false;
131                     if (obj.isListType(propName) && obj.isSimpleGenericType(propName)) {
132                         cardinality = Cardinality.SET;
133                         type = obj.getGenericTypeClass(propName);
134                         process = true;
135                     } else if (obj.isSimpleType(propName)) {
136                         process = true;
137                     }
138
139                     if (process) {
140
141                         String imsg = "Creating PropertyKey: [" + dbPropName + "], [" + type.getSimpleName() + "], ["
142                                 + cardinality + "]";
143                         LOGGER.info(imsg);
144                         PropertyKey propK;
145                         if (!seenProps.containsKey(dbPropName)) {
146                             propK = graphMgmt.makePropertyKey(dbPropName).dataType(type).cardinality(cardinality)
147                                     .make();
148                             seenProps.put(dbPropName, propK);
149                         } else {
150                             propK = seenProps.get(dbPropName);
151                         }
152                         if (graphMgmt.containsGraphIndex(dbPropName)) {
153                             String dmsg = " Index  [" + dbPropName + "] already existed in the DB. ";
154                             LOGGER.debug(dmsg);
155                         } else {
156                             if (obj.getIndexedProperties().contains(propName)) {
157                                 if (obj.getUniqueProperties().contains(propName)) {
158                                     imsg = "Add Unique index for PropertyKey: [" + dbPropName + "]";
159                                     LOGGER.info(imsg);
160                                     graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK).unique()
161                                             .buildCompositeIndex();
162                                 } else {
163                                     imsg = "Add index for PropertyKey: [" + dbPropName + "]";
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                                 LOGGER.info(imsg);
170                             }
171                         }
172                     }
173                 }
174             }
175         }
176
177         String imsg = "-- About to call graphMgmt commit";
178         LOGGER.info(imsg);
179         if (backend != null) {
180             LOGGER.info("Successfully loaded the schema to " + backend);
181         }
182
183         graphMgmt.commit();
184     }
185
186 }