250c1263ce1caa890ac112e726d76eb0a81c56b7
[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 com.google.common.collect.Multimap;
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 import java.util.function.Function;
31 import java.util.stream.Collectors;
32
33 import org.apache.tinkerpop.gremlin.structure.Vertex;
34 import org.janusgraph.core.Cardinality;
35 import org.janusgraph.core.Multiplicity;
36 import org.janusgraph.core.PropertyKey;
37 import org.janusgraph.core.schema.ConsistencyModifier;
38 import org.janusgraph.core.schema.JanusGraphIndex;
39 import org.janusgraph.core.schema.JanusGraphManagement;
40 import org.onap.aai.config.SpringContextAware;
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.Introspector;
45 import org.onap.aai.introspection.LoaderUtil;
46 import org.onap.aai.logging.LogFormatTools;
47 import org.onap.aai.schema.enums.PropertyMetadata;
48 import org.onap.aai.util.AAIConfig;
49 import org.onap.aai.util.AAIConstants;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 public class SchemaGenerator {
54
55     private static final Logger LOGGER = LoggerFactory.getLogger(SchemaGenerator.class);
56
57     private SchemaGenerator() {
58     }
59
60     /**
61      * Load schema into JanusGraph.
62      *
63      * @param graphMgmt
64      *        the graph mgmt
65      */
66     public static void loadSchemaIntoJanusGraph(final JanusGraphManagement graphMgmt, String backend) {
67
68         try {
69             AAIConfig.init();
70         } catch (Exception ex) {
71             LOGGER.error(" ERROR - Could not run AAIConfig.init(). {}", LogFormatTools.getStackTop(ex));
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         makeEdgeLabels(graphMgmt);
89
90         Map<String, Introspector> objs = LoaderUtil.getLatestVersion().getAllObjects();
91         Map<String, PropertyKey> seenProps = new HashMap<>();
92
93         for (Introspector obj : objs.values()) {
94             for (String propName : obj.getProperties()) {
95                 String dbPropName = propName;
96                 Optional<String> alias = obj.getPropertyMetadata(propName, PropertyMetadata.DB_ALIAS);
97                 if (alias.isPresent()) {
98                     dbPropName = alias.get();
99                 }
100                 if (graphMgmt.containsRelationType(dbPropName)) {
101                     LOGGER.debug(" PropertyKey  [{}] already existed in the DB. ", dbPropName);
102                 } else {
103                     Class<?> type = obj.getClass(propName);
104                     Cardinality cardinality = Cardinality.SINGLE;
105                     boolean process = false;
106                     if (obj.isListType(propName) && obj.isSimpleGenericType(propName)) {
107                         cardinality = Cardinality.SET;
108                         type = obj.getGenericTypeClass(propName);
109                         process = true;
110                     } else if (obj.isSimpleType(propName)) {
111                         process = true;
112                     }
113
114                     if (process) {
115
116                         LOGGER.info("Creating PropertyKey: [{}], [{}], [{}]", dbPropName, type.getSimpleName(),
117                                 cardinality);
118                         PropertyKey propK;
119                         if (!seenProps.containsKey(dbPropName)) {
120                             propK = graphMgmt.makePropertyKey(dbPropName).dataType(type).cardinality(cardinality)
121                                     .make();
122                             if (dbPropName.equals("aai-uri")) {
123                                 String aai_uri_lock_enabled = AAIConfig.get(AAIConstants.AAI_LOCK_URI_ENABLED, "false");
124                                 LOGGER.info(" Info: aai_uri_lock_enabled:" + aai_uri_lock_enabled);
125                                 if ("true".equals(aai_uri_lock_enabled)) {
126                                     LOGGER.info(" Lock is being set for aai-uri Property.");
127                                     graphMgmt.setConsistency(propK, ConsistencyModifier.LOCK);
128                                 }
129                             } else if (dbPropName.equals("resource-version")) {
130                                 String aai_rv_lock_enabled = AAIConfig.get(AAIConstants.AAI_LOCK_RV_ENABLED, "false");
131                                 LOGGER.info(" Info: aai_rv_lock_enabled:" + aai_rv_lock_enabled);
132                                 if ("true".equals(aai_rv_lock_enabled)) {
133                                     LOGGER.info(" Lock is being set for resource-version Property.");
134                                     graphMgmt.setConsistency(propK, ConsistencyModifier.LOCK);
135                                 }
136                             }
137                             seenProps.put(dbPropName, propK);
138                         } else {
139                             propK = seenProps.get(dbPropName);
140                         }
141                         if (graphMgmt.containsGraphIndex(dbPropName)) {
142                             LOGGER.debug(" Index  [{}] already existed in the DB. ", dbPropName);
143                         } else {
144                             if (obj.getIndexedProperties().contains(propName)) {
145                                 JanusGraphIndex indexG = null;
146                                 if (obj.getUniqueProperties().contains(propName)) {
147                                     LOGGER.info("Add Unique index for PropertyKey: [{}]", dbPropName);
148                                     indexG = graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK).unique()
149                                             .buildCompositeIndex();
150                                 } else {
151                                     LOGGER.info("Add index for PropertyKey: [{}]", dbPropName);
152                                     indexG = graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK)
153                                             .buildCompositeIndex();
154                                 }
155                                 if (indexG != null && dbPropName.equals("aai-uri")) {
156                                     String aai_uri_lock_enabled =
157                                             AAIConfig.get(AAIConstants.AAI_LOCK_URI_ENABLED, "false");
158                                     LOGGER.info(" Info:: aai_uri_lock_enabled:" + aai_uri_lock_enabled);
159                                     if ("true".equals(aai_uri_lock_enabled)) {
160                                         LOGGER.info("Lock is being set for aai-uri Index.");
161                                         graphMgmt.setConsistency(indexG, ConsistencyModifier.LOCK);
162                                     }
163                                 } else if (indexG != null && dbPropName.equals("resource-version")) {
164                                     String aai_rv_lock_enabled =
165                                             AAIConfig.get(AAIConstants.AAI_LOCK_RV_ENABLED, "false");
166                                     LOGGER.info(" Info:: aai_rv_lock_enabled:" + aai_rv_lock_enabled);
167                                     if ("true".equals(aai_rv_lock_enabled)) {
168                                         LOGGER.info("Lock is being set for resource-version Index.");
169                                         graphMgmt.setConsistency(indexG, ConsistencyModifier.LOCK);
170                                     }
171                                 }
172                             } else {
173                                 LOGGER.info("No index added for PropertyKey: [{}]", dbPropName);
174                             }
175                         }
176                     }
177                 }
178             }
179         }
180
181         LOGGER.info("-- About to call graphMgmt commit");
182         if (backend != null) {
183             LOGGER.info("Successfully loaded the schema to {}", backend);
184         }
185
186         graphMgmt.commit();
187     }
188
189     private static void makeEdgeLabels(JanusGraphManagement graphMgmt) {
190         try {
191             EdgeIngestor edgeIngestor = SpringContextAware.getBean(EdgeIngestor.class);
192
193             Set<String> labels = Optional.ofNullable(edgeIngestor.getAllCurrentRules())
194                     .map(collectValues(EdgeRule::getLabel)).orElseGet(HashSet::new);
195
196             labels.forEach(label -> {
197                 if (graphMgmt.containsRelationType(label)) {
198                     LOGGER.debug(" EdgeLabel  [{}] already existed. ", label);
199                 } else {
200                     LOGGER.debug("Making EdgeLabel: [{}]", label);
201                     graphMgmt.makeEdgeLabel(label).multiplicity(Multiplicity.valueOf("MULTI")).make();
202                 }
203             });
204         } catch (EdgeRuleNotFoundException e) {
205             LOGGER.error("Unable to find all rules {}", LogFormatTools.getStackTop(e));
206         }
207     }
208
209     /**
210      * Returns a function collecting all the values in a {@link com.google.common.collect.Multimap}
211      * given a mapping function
212      *
213      * @param f The mapper function
214      * @param <K> The type of key used by the provided {@link com.google.common.collect.Multimap}
215      * @param <V> The type of value used by the provided {@link com.google.common.collect.Multimap}
216      * @param <V0> The type which <V> is mapped to
217      */
218     private static <K, V, V0> Function<Multimap<K, V>, Set<V0>> collectValues(Function<V, V0> f) {
219         return as -> as.values().stream().map(f).collect(Collectors.toSet());
220     }
221
222 }