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 org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import com.google.common.collect.Multimap;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.janusgraph.core.Cardinality;
28 import org.janusgraph.core.JanusGraph;
29 import org.janusgraph.core.Multiplicity;
30 import org.janusgraph.core.PropertyKey;
31 import org.janusgraph.core.schema.JanusGraphManagement;
32 import org.onap.aai.config.SpringContextAware;
33 import org.onap.aai.edges.EdgeIngestor;
34 import org.onap.aai.edges.EdgeRule;
35 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
36 import org.onap.aai.introspection.Introspector;
37 import org.onap.aai.introspection.Loader;
38 import org.onap.aai.introspection.LoaderUtil;
39 import org.onap.aai.logging.LogFormatTools;
40 import org.onap.aai.schema.enums.PropertyMetadata;
41 import org.onap.aai.util.AAIConfig;
45 import static org.onap.aai.db.props.AAIProperties.*;
47 public class SchemaGenerator4Hist {
49 private static final Logger LOGGER = LoggerFactory.getLogger(SchemaGenerator4Hist.class);
52 * Load schema into JanusGraph.
59 public static void loadSchemaIntoJanusGraph(final JanusGraph graph, final JanusGraphManagement graphMgmt,
64 } catch (Exception ex) {
65 LOGGER.error(" ERROR - Could not run AAIConfig.init(). " + LogFormatTools.getStackTop(ex));
66 // System.out.println(" ERROR - Could not run AAIConfig.init(). ");
70 // NOTE - JanusGraph 0.5.3 doesn't keep a list of legal node Labels.
71 // They are only used when a vertex is actually being created.
72 // JanusGraph 1.1 will keep track (we think).
74 // Use EdgeRules to make sure edgeLabels are defined in the db. NOTE:
75 // the multiplicty used here is
76 // always "MULTI". This is not the same as our internal "Many2Many",
77 // "One2One", "One2Many" or "Many2One"
78 // We use the same edge-label for edges between many different types of
79 // nodes and our internal
80 // multiplicty definitions depends on which two types of nodes are being
83 Multimap<String, EdgeRule> edges = null;
84 Set<String> labels = new HashSet<>();
86 EdgeIngestor edgeIngestor = SpringContextAware.getBean(EdgeIngestor.class);
89 edges = edgeIngestor.getAllCurrentRules();
90 } catch (EdgeRuleNotFoundException e) {
91 LOGGER.error("Unable to find all rules {}", LogFormatTools.getStackTop(e));
94 for (EdgeRule rule : edges.values()) {
95 labels.add(rule.getLabel());
98 for (String label : labels) {
99 if (graphMgmt.containsRelationType(label)) {
100 String dmsg = " EdgeLabel [" + label + "] already existed. ";
103 String dmsg = "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 String dmsg = " PropertyKey [" + dbPropName + "] already existed in the DB. ";
125 Class<?> type = obj.getClass(propName);
126 Cardinality cardinality = Cardinality.LIST;
127 boolean process = false;
128 if (obj.isListType(propName) && obj.isSimpleGenericType(propName)) {
129 // NOTE - For history - All properties have cardinality = LIST
130 // It is assumed that there is special processing in the Resources MS
131 // for History to turn what used to be SET (referred to as isListType
132 // above) will be stored in our db as a single String. And that
133 // single string will have Cardinality = LIST so we can track its
135 //cardinality = Cardinality.SET;
136 type = obj.getGenericTypeClass(propName);
138 } else if (obj.isSimpleType(propName)) {
144 String imsg = " Creating PropertyKey: [" + dbPropName + "], [" + type.getSimpleName() + "], ["
148 if (!seenProps.containsKey(dbPropName)) {
149 propK = graphMgmt.makePropertyKey(dbPropName).dataType(type).cardinality(cardinality)
151 seenProps.put(dbPropName, propK);
153 propK = seenProps.get(dbPropName);
155 if (graphMgmt.containsGraphIndex(dbPropName)) {
156 String dmsg = " Index [" + dbPropName + "] already existed in the DB. ";
159 if (obj.getIndexedProperties().contains(propName)) {
160 // NOTE - for History we never add a unique index - just a regular index
161 imsg = "Add index for PropertyKey: [" + dbPropName + "]";
163 graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK).buildCompositeIndex();
165 imsg = "No index needed/added for PropertyKey: [" + dbPropName + "]";
172 }// Done processing all properties defined in the OXM
174 // Add the 3 new History properties are in the DB
175 // They are all Cardinality=Single since instance of a Node, Edge or Property can
176 // only have one of them. That is, a Property can show up many times in a
177 // node, but each instance of that property will only have a single start-ts,
178 // end-ts, end-source-of-truth. Same goes for a node or edge itself.
179 if (graphMgmt.containsRelationType(END_SOT)) {
180 String dmsg = "PropertyKey [" + END_SOT + "] already existed in the DB. ";
182 } else if (!seenProps.containsKey(END_SOT) ) {
183 String imsg = " Creating PropertyKey: [" + END_SOT + "], [String], [SINGLE]";
185 graphMgmt.makePropertyKey(END_SOT).dataType(String.class)
186 .cardinality(Cardinality.SINGLE).make();
189 if (graphMgmt.containsRelationType(START_TS)) {
190 String dmsg = " PropertyKey [" + START_TS + "] already existed in the DB. ";
192 } else if (!seenProps.containsKey(START_TS) ) {
193 String imsg = " Creating PropertyKey: [" + START_TS + "], [Long], [SINGLE]";
195 graphMgmt.makePropertyKey(START_TS).dataType(Long.class)
196 .cardinality(Cardinality.SINGLE).make();
199 if (graphMgmt.containsRelationType(END_TS)) {
200 String dmsg = "PropertyKey [" + END_TS + "] already existed in the DB. ";
202 } else if (!seenProps.containsKey(END_TS) ) {
203 String imsg = " Creating PropertyKey: [" + END_TS + "], [Long], [SINGLE]";
205 graphMgmt.makePropertyKey(END_TS).dataType(Long.class)
206 .cardinality(Cardinality.SINGLE).make();
209 if (graphMgmt.containsRelationType(START_TX_ID)) {
210 String dmsg = "PropertyKey [" + START_TX_ID + "] already existed in the DB. ";
212 } else if (!seenProps.containsKey(START_TX_ID) ) {
213 String imsg = " Creating PropertyKey: [" + START_TX_ID + "], [String], [SINGLE]";
215 graphMgmt.makePropertyKey(START_TX_ID).dataType(String.class)
216 .cardinality(Cardinality.SINGLE).make();
219 if (graphMgmt.containsRelationType(END_TX_ID)) {
220 String dmsg = "PropertyKey [" + END_TX_ID + "] already existed in the DB. ";
222 } else if (!seenProps.containsKey(END_TX_ID) ) {
223 String imsg = " Creating PropertyKey: [" + END_TX_ID + "], [String], [SINGLE]";
225 graphMgmt.makePropertyKey(END_TX_ID).dataType(String.class)
226 .cardinality(Cardinality.SINGLE).make();
229 String imsg = "-- About to call graphMgmt commit";
232 if (backend != null) {
233 LOGGER.info("Successfully loaded the schema to " + backend);