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;
24 import org.apache.tinkerpop.gremlin.structure.Vertex;
25 import org.janusgraph.core.Cardinality;
26 import org.janusgraph.core.Multiplicity;
27 import org.janusgraph.core.PropertyKey;
28 import org.janusgraph.core.schema.JanusGraphManagement;
29 import org.onap.aai.config.SpringContextAware;
30 import org.onap.aai.edges.EdgeIngestor;
31 import org.onap.aai.edges.EdgeRule;
32 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
33 import org.onap.aai.introspection.Introspector;
34 import org.onap.aai.introspection.LoaderUtil;
35 import org.onap.aai.logging.LogFormatTools;
36 import org.onap.aai.schema.enums.PropertyMetadata;
37 import org.onap.aai.util.AAIConfig;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import java.util.HashMap;
42 import java.util.HashSet;
44 import java.util.Optional;
46 import java.util.function.Function;
47 import java.util.stream.Collectors;
49 public class SchemaGenerator {
51 private static final Logger LOGGER = LoggerFactory.getLogger(SchemaGenerator.class);
53 private SchemaGenerator() {
57 * Load schema into JanusGraph.
62 public static void loadSchemaIntoJanusGraph(final JanusGraphManagement graphMgmt, String backend) {
66 } catch (Exception ex) {
67 LOGGER.error(" ERROR - Could not run AAIConfig.init(). {}", LogFormatTools.getStackTop(ex));
71 // NOTE - JanusGraph 0.5.3 doesn't keep a list of legal node Labels.
72 // They are only used when a vertex is actually being created.
73 // JanusGraph 1.1 will keep track (we think).
75 // Use EdgeRules to make sure edgeLabels are defined in the db. NOTE:
76 // the multiplicty used here is
77 // always "MULTI". This is not the same as our internal "Many2Many",
78 // "One2One", "One2Many" or "Many2One"
79 // We use the same edge-label for edges between many different types of
80 // nodes and our internal
81 // multiplicty definitions depends on which two types of nodes are being
84 makeEdgeLabels(graphMgmt);
87 Map<String, Introspector> objs = LoaderUtil.getLatestVersion().getAllObjects();
88 Map<String, PropertyKey> seenProps = new HashMap<>();
90 for (Introspector obj : objs.values()) {
91 for (String propName : obj.getProperties()) {
92 String dbPropName = propName;
93 Optional<String> alias = obj.getPropertyMetadata(propName, PropertyMetadata.DB_ALIAS);
94 if (alias.isPresent()) {
95 dbPropName = alias.get();
97 if (graphMgmt.containsRelationType(dbPropName)) {
98 LOGGER.debug(" PropertyKey [{}] already existed in the DB. ", dbPropName);
100 Class<?> type = obj.getClass(propName);
101 Cardinality cardinality = Cardinality.SINGLE;
102 boolean process = false;
103 if (obj.isListType(propName) && obj.isSimpleGenericType(propName)) {
104 cardinality = Cardinality.SET;
105 type = obj.getGenericTypeClass(propName);
107 } else if (obj.isSimpleType(propName)) {
113 LOGGER.info("Creating PropertyKey: [{}], [{}], [{}]",
114 dbPropName, type.getSimpleName(), cardinality);
116 if (!seenProps.containsKey(dbPropName)) {
117 propK = graphMgmt.makePropertyKey(dbPropName).dataType(type).cardinality(cardinality)
119 seenProps.put(dbPropName, propK);
121 propK = seenProps.get(dbPropName);
123 if (graphMgmt.containsGraphIndex(dbPropName)) {
124 LOGGER.debug(" Index [{}] already existed in the DB. ", dbPropName);
126 if (obj.getIndexedProperties().contains(propName)) {
127 if (obj.getUniqueProperties().contains(propName)) {
128 LOGGER.info("Add Unique index for PropertyKey: [{}]", dbPropName);
129 graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK).unique()
130 .buildCompositeIndex();
132 LOGGER.info("Add index for PropertyKey: [{}]", dbPropName);
133 graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK).buildCompositeIndex();
136 LOGGER.info("No index added for PropertyKey: [{}]", dbPropName);
144 LOGGER.info("-- About to call graphMgmt commit");
145 if (backend != null) {
146 LOGGER.info("Successfully loaded the schema to {}", backend);
152 private static void makeEdgeLabels(JanusGraphManagement graphMgmt) {
154 EdgeIngestor edgeIngestor = SpringContextAware.getBean(EdgeIngestor.class);
156 Set<String> labels = Optional.ofNullable(edgeIngestor.getAllCurrentRules())
157 .map(collectValues(EdgeRule::getLabel))
158 .orElseGet(HashSet::new);
160 labels.forEach(label -> {
161 if (graphMgmt.containsRelationType(label)) {
162 LOGGER.debug(" EdgeLabel [{}] already existed. ", label);
164 LOGGER.debug("Making EdgeLabel: [{}]", label);
165 graphMgmt.makeEdgeLabel(label).multiplicity(Multiplicity.valueOf("MULTI")).make();
168 } catch (EdgeRuleNotFoundException e) {
169 LOGGER.error("Unable to find all rules {}", LogFormatTools.getStackTop(e));
174 * Returns a function collecting all the values in a {@link com.google.common.collect.Multimap}
175 * given a mapping function
177 * @param f The mapper function
178 * @param <K> The type of key used by the provided {@link com.google.common.collect.Multimap}
179 * @param <V> The type of value used by the provided {@link com.google.common.collect.Multimap}
180 * @param <V0> The type which <V> is mapped to
182 private static <K, V, V0> Function<Multimap<K, V>, Set<V0>> collectValues(Function<V, V0> f) {
183 return as -> as.values().stream().map(f).collect(Collectors.toSet());