0f88de2407403a97e27eda9cf66df0ea62e52573
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / dbgen / SchemaGenerator4Hist.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 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;
42
43 import java.util.*;
44
45 import static org.onap.aai.db.props.AAIProperties.*;
46
47 public class SchemaGenerator4Hist {
48
49     private static final Logger LOGGER = LoggerFactory.getLogger(SchemaGenerator4Hist.class);
50
51     /**
52      * Load schema into JanusGraph.
53      *
54      * @param graph
55      *        the graph
56      * @param graphMgmt
57      *        the graph mgmt
58      */
59     public static void loadSchemaIntoJanusGraph(final JanusGraph graph, final JanusGraphManagement graphMgmt,
60             String backend) {
61
62         try {
63             AAIConfig.init();
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(). ");
67             System.exit(1);
68         }
69
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).
73
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
81         // connected.
82
83         Multimap<String, EdgeRule> edges = null;
84         Set<String> labels = new HashSet<>();
85
86         EdgeIngestor edgeIngestor = SpringContextAware.getBean(EdgeIngestor.class);
87
88         try {
89             edges = edgeIngestor.getAllCurrentRules();
90         } catch (EdgeRuleNotFoundException e) {
91             LOGGER.error("Unable to find all rules {}", LogFormatTools.getStackTop(e));
92         }
93
94         for (EdgeRule rule : edges.values()) {
95             labels.add(rule.getLabel());
96         }
97
98         for (String label : labels) {
99             if (graphMgmt.containsRelationType(label)) {
100                 String dmsg = " EdgeLabel  [" + label + "] already existed. ";
101                 LOGGER.debug(dmsg);
102             } else {
103                 String dmsg = "Making EdgeLabel: [" + label + "]";
104                 LOGGER.debug(dmsg);
105                 graphMgmt.makeEdgeLabel(label).multiplicity(Multiplicity.valueOf("MULTI")).make();
106             }
107         }
108
109         Loader loader = LoaderUtil.getLatestVersion();
110
111         Map<String, Introspector> objs = loader.getAllObjects();
112         Map<String, PropertyKey> seenProps = new HashMap<>();
113
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();
120                 }
121                 if (graphMgmt.containsRelationType(dbPropName)) {
122                     String dmsg = " PropertyKey  [" + dbPropName + "] already existed in the DB. ";
123                     LOGGER.debug(dmsg);
124                 } else {
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
134                         // history.
135                         //cardinality = Cardinality.SET;
136                         type = obj.getGenericTypeClass(propName);
137                         process = true;
138                     } else if (obj.isSimpleType(propName)) {
139                         process = true;
140                     }
141
142                     if (process) {
143
144                         String imsg = " Creating PropertyKey: [" + dbPropName + "], [" + type.getSimpleName() + "], ["
145                                 + cardinality + "]";
146                         LOGGER.info(imsg);
147                         PropertyKey propK;
148                         if (!seenProps.containsKey(dbPropName)) {
149                             propK = graphMgmt.makePropertyKey(dbPropName).dataType(type).cardinality(cardinality)
150                                     .make();
151                             seenProps.put(dbPropName, propK);
152                         } else {
153                             propK = seenProps.get(dbPropName);
154                         }
155                         if (graphMgmt.containsGraphIndex(dbPropName)) {
156                             String dmsg = " Index  [" + dbPropName + "] already existed in the DB. ";
157                             LOGGER.debug(dmsg);
158                         } else {
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 + "]";
162                                 LOGGER.info(imsg);
163                                 graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK).buildCompositeIndex();
164                             } else {
165                                 imsg = "No index needed/added for PropertyKey: [" + dbPropName + "]";
166                                 LOGGER.info(imsg);
167                             }
168                         }
169                     }
170                 }
171             }
172         }// Done processing all properties defined in the OXM
173
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. ";
181             LOGGER.debug(dmsg);
182         } else if (!seenProps.containsKey(END_SOT)  ) {
183                 String imsg = " Creating PropertyKey: [" + END_SOT + "], [String], [SINGLE]";
184                 LOGGER.info(imsg);
185                 graphMgmt.makePropertyKey(END_SOT).dataType(String.class)
186                         .cardinality(Cardinality.SINGLE).make();
187         }
188
189         if (graphMgmt.containsRelationType(START_TS)) {
190             String dmsg = " PropertyKey [" + START_TS + "] already existed in the DB. ";
191             LOGGER.debug(dmsg);
192         } else if (!seenProps.containsKey(START_TS)  ) {
193                 String imsg = " Creating PropertyKey: [" + START_TS + "], [Long], [SINGLE]";
194                 LOGGER.info(imsg);
195                 graphMgmt.makePropertyKey(START_TS).dataType(Long.class)
196                         .cardinality(Cardinality.SINGLE).make();
197         }
198
199         if (graphMgmt.containsRelationType(END_TS)) {
200             String dmsg = "PropertyKey [" + END_TS + "] already existed in the DB. ";
201             LOGGER.debug(dmsg);
202         } else if (!seenProps.containsKey(END_TS)  ) {
203                 String imsg = " Creating PropertyKey: [" + END_TS + "], [Long], [SINGLE]";
204                 LOGGER.info(imsg);
205                 graphMgmt.makePropertyKey(END_TS).dataType(Long.class)
206                         .cardinality(Cardinality.SINGLE).make();
207         }
208
209         if (graphMgmt.containsRelationType(START_TX_ID)) {
210             String dmsg = "PropertyKey [" + START_TX_ID + "] already existed in the DB. ";
211             LOGGER.debug(dmsg);
212         } else if (!seenProps.containsKey(START_TX_ID)  ) {
213             String imsg = " Creating PropertyKey: [" + START_TX_ID + "], [String], [SINGLE]";
214             LOGGER.info(imsg);
215             graphMgmt.makePropertyKey(START_TX_ID).dataType(String.class)
216                 .cardinality(Cardinality.SINGLE).make();
217         }
218
219         if (graphMgmt.containsRelationType(END_TX_ID)) {
220             String dmsg = "PropertyKey [" + END_TX_ID + "] already existed in the DB. ";
221             LOGGER.debug(dmsg);
222         } else if (!seenProps.containsKey(END_TX_ID)  ) {
223             String imsg = " Creating PropertyKey: [" + END_TX_ID + "], [String], [SINGLE]";
224             LOGGER.info(imsg);
225             graphMgmt.makePropertyKey(END_TX_ID).dataType(String.class)
226                 .cardinality(Cardinality.SINGLE).make();
227         }
228
229         String imsg = "-- About to call graphMgmt commit";
230         LOGGER.info(imsg);
231         graphMgmt.commit();
232         if (backend != null) {
233             LOGGER.info("Successfully loaded the schema to " + backend);
234         }
235
236     }
237
238 }