2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.dbgen;
23 import com.google.common.collect.Multimap;
25 import java.util.HashMap;
26 import java.util.HashSet;
28 import java.util.Optional;
30 import java.util.function.Function;
31 import java.util.stream.Collectors;
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;
53 public class SchemaGenerator {
55 private static final Logger LOGGER = LoggerFactory.getLogger(SchemaGenerator.class);
57 private SchemaGenerator() {
61 * Load schema into JanusGraph.
66 public static void loadSchemaIntoJanusGraph(final JanusGraphManagement graphMgmt, String backend) {
70 } catch (Exception ex) {
71 LOGGER.error(" ERROR - Could not run AAIConfig.init(). {}", LogFormatTools.getStackTop(ex));
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).
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
88 makeEdgeLabels(graphMgmt);
90 Map<String, Introspector> objs = LoaderUtil.getLatestVersion().getAllObjects();
91 Map<String, PropertyKey> seenProps = new HashMap<>();
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();
100 if (graphMgmt.containsRelationType(dbPropName)) {
101 LOGGER.debug(" PropertyKey [{}] already existed in the DB. ", dbPropName);
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);
110 } else if (obj.isSimpleType(propName)) {
116 LOGGER.info("Creating PropertyKey: [{}], [{}], [{}]", dbPropName, type.getSimpleName(),
119 if (!seenProps.containsKey(dbPropName)) {
120 propK = graphMgmt.makePropertyKey(dbPropName).dataType(type).cardinality(cardinality)
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);
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);
137 seenProps.put(dbPropName, propK);
139 propK = seenProps.get(dbPropName);
141 if (graphMgmt.containsGraphIndex(dbPropName)) {
142 LOGGER.debug(" Index [{}] already existed in the DB. ", dbPropName);
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();
151 LOGGER.info("Add index for PropertyKey: [{}]", dbPropName);
152 indexG = graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK)
153 .buildCompositeIndex();
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);
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);
173 LOGGER.info("No index added for PropertyKey: [{}]", dbPropName);
181 LOGGER.info("-- About to call graphMgmt commit");
182 if (backend != null) {
183 LOGGER.info("Successfully loaded the schema to {}", backend);
189 private static void makeEdgeLabels(JanusGraphManagement graphMgmt) {
191 EdgeIngestor edgeIngestor = SpringContextAware.getBean(EdgeIngestor.class);
193 Set<String> labels = Optional.ofNullable(edgeIngestor.getAllCurrentRules())
194 .map(collectValues(EdgeRule::getLabel)).orElseGet(HashSet::new);
196 labels.forEach(label -> {
197 if (graphMgmt.containsRelationType(label)) {
198 LOGGER.debug(" EdgeLabel [{}] already existed. ", label);
200 LOGGER.debug("Making EdgeLabel: [{}]", label);
201 graphMgmt.makeEdgeLabel(label).multiplicity(Multiplicity.valueOf("MULTI")).make();
204 } catch (EdgeRuleNotFoundException e) {
205 LOGGER.error("Unable to find all rules {}", LogFormatTools.getStackTop(e));
210 * Returns a function collecting all the values in a {@link com.google.common.collect.Multimap}
211 * given a mapping function
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
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());