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