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.Loader;
35 import org.onap.aai.introspection.LoaderUtil;
36 import org.onap.aai.logging.LogFormatTools;
37 import org.onap.aai.schema.enums.PropertyMetadata;
38 import org.onap.aai.util.AAIConfig;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import java.util.HashMap;
43 import java.util.HashSet;
45 import java.util.Optional;
48 import static org.onap.aai.db.props.AAIProperties.*;
50 public class SchemaGenerator4Hist {
52 private static final Logger LOGGER = LoggerFactory.getLogger(SchemaGenerator4Hist.class);
54 private SchemaGenerator4Hist(){
58 * Load schema into JanusGraph.
63 public static void loadSchemaIntoJanusGraph(final JanusGraphManagement graphMgmt, String backend) {
67 } catch (Exception ex) {
68 LOGGER.error(" ERROR - Could not run AAIConfig.init(). {}", LogFormatTools.getStackTop(ex));
72 // NOTE - JanusGraph 0.5.3 doesn't keep a list of legal node Labels.
73 // They are only used when a vertex is actually being created.
74 // JanusGraph 1.1 will keep track (we think).
76 // Use EdgeRules to make sure edgeLabels are defined in the db. NOTE:
77 // the multiplicty used here is
78 // always "MULTI". This is not the same as our internal "Many2Many",
79 // "One2One", "One2Many" or "Many2One"
80 // We use the same edge-label for edges between many different types of
81 // nodes and our internal
82 // multiplicty definitions depends on which two types of nodes are being
85 Multimap<String, EdgeRule> edges = null;
86 Set<String> labels = new HashSet<>();
88 EdgeIngestor edgeIngestor = SpringContextAware.getBean(EdgeIngestor.class);
91 edges = edgeIngestor.getAllCurrentRules();
92 } catch (EdgeRuleNotFoundException e) {
93 LOGGER.error("Unable to find all rules {}", LogFormatTools.getStackTop(e));
96 for (EdgeRule rule : edges.values()) {
97 labels.add(rule.getLabel());
100 for (String label : labels) {
101 if (graphMgmt.containsRelationType(label)) {
102 LOGGER.debug(" EdgeLabel [{}] already existed. ", label);
104 LOGGER.debug("Making EdgeLabel: [{}]", label);
105 graphMgmt.makeEdgeLabel(label).multiplicity(Multiplicity.valueOf("MULTI")).make();
109 Loader loader = LoaderUtil.getLatestVersion();
111 Map<String, Introspector> objs = loader.getAllObjects();
112 Map<String, PropertyKey> seenProps = new HashMap<>();
114 for (Introspector obj : objs.values()) {
115 for (String propName : obj.getProperties()) {
116 String dbPropName = propName;
117 Optional<String> alias = obj.getPropertyMetadata(propName, PropertyMetadata.DB_ALIAS);
118 if (alias.isPresent()) {
119 dbPropName = alias.get();
121 if (graphMgmt.containsRelationType(dbPropName)) {
122 LOGGER.debug(" PropertyKey [{}] already existed in the DB. ", dbPropName);
124 Class<?> type = obj.getClass(propName);
125 Cardinality cardinality = Cardinality.LIST;
126 boolean process = false;
127 if (obj.isListType(propName) && obj.isSimpleGenericType(propName)) {
128 // NOTE - For history - All properties have cardinality = LIST
129 // It is assumed that there is special processing in the Resources MS
130 // for History to turn what used to be SET (referred to as isListType
131 // above) will be stored in our db as a single String. And that
132 // single string will have Cardinality = LIST so we can track its
134 type = obj.getGenericTypeClass(propName);
136 } else if (obj.isSimpleType(propName)) {
142 LOGGER.info(" Creating PropertyKey: [{}], [{}], [{}]",
143 dbPropName, type.getSimpleName(), cardinality);
145 if (!seenProps.containsKey(dbPropName)) {
146 propK = graphMgmt.makePropertyKey(dbPropName).dataType(type).cardinality(cardinality)
148 seenProps.put(dbPropName, propK);
150 propK = seenProps.get(dbPropName);
152 if (graphMgmt.containsGraphIndex(dbPropName)) {
153 LOGGER.debug(" Index [{}] already existed in the DB. ", dbPropName);
155 if (obj.getIndexedProperties().contains(propName)) {
156 // NOTE - for History we never add a unique index - just a regular index
157 LOGGER.info("Add index for PropertyKey: [{}]", dbPropName);
158 graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK).buildCompositeIndex();
160 LOGGER.info("No index needed/added for PropertyKey: [{}]", dbPropName);
166 }// Done processing all properties defined in the OXM
168 // Add the 3 new History properties are in the DB
169 // They are all Cardinality=Single since instance of a Node, Edge or Property can
170 // only have one of them. That is, a Property can show up many times in a
171 // node, but each instance of that property will only have a single start-ts,
172 // end-ts, end-source-of-truth. Same goes for a node or edge itself.
173 makeNewProperty(graphMgmt, seenProps, String.class, END_SOT);
174 makeNewProperty(graphMgmt, seenProps, Long.class, START_TS);
175 makeNewProperty(graphMgmt, seenProps, Long.class, END_TS);
176 makeNewProperty(graphMgmt, seenProps, String.class, START_TX_ID);
177 makeNewProperty(graphMgmt, seenProps, String.class, END_TX_ID);
180 String imsg = "-- About to call graphMgmt commit";
183 if (backend != null) {
184 LOGGER.info("Successfully loaded the schema to {}", backend);
189 private static <T> void makeNewProperty(JanusGraphManagement graphMgmt,
190 Map<String, PropertyKey> seenProps,
192 String propertyName) {
193 if (graphMgmt.containsRelationType(propertyName)) {
194 LOGGER.debug("PropertyKey [{}] already existed in the DB.", propertyName);
195 } else if (!seenProps.containsKey(propertyName)) {
196 LOGGER.info("Creating PropertyKey: [{}], [{}], [{}]",
197 propertyName, type.getSimpleName(), Cardinality.SINGLE);
198 graphMgmt.makePropertyKey(propertyName).dataType(type).cardinality(Cardinality.SINGLE)