Fixed concurrency issue in PNF uploading
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / dbgen / SchemaGenerator.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.onap.aai.util.AAIConstants;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import com.google.common.collect.Multimap;
27
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.Map;
31 import java.util.Optional;
32 import java.util.Set;
33
34 import org.apache.tinkerpop.gremlin.structure.Vertex;
35 import org.janusgraph.core.Cardinality;
36 import org.janusgraph.core.JanusGraph;
37 import org.janusgraph.core.Multiplicity;
38 import org.janusgraph.core.PropertyKey;
39 import org.janusgraph.core.schema.JanusGraphManagement;
40 import org.janusgraph.core.schema.JanusGraphIndex;
41 import org.janusgraph.core.schema.ConsistencyModifier;
42 import org.onap.aai.config.SpringContextAware;
43 import org.onap.aai.db.props.AAIProperties;
44 import org.onap.aai.edges.EdgeIngestor;
45 import org.onap.aai.edges.EdgeRule;
46 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
47 import org.onap.aai.introspection.*;
48 import org.onap.aai.logging.LogFormatTools;
49 import org.onap.aai.schema.enums.PropertyMetadata;
50 import org.onap.aai.setup.SchemaVersions;
51 import org.onap.aai.util.AAIConfig;
52
53 public class SchemaGenerator {
54
55     private static final Logger LOGGER = LoggerFactory.getLogger(SchemaGenerator.class);
56
57     /**
58      * Load schema into JanusGraph.
59      *
60      * @param graph
61      *        the graph
62      * @param graphMgmt
63      *        the graph mgmt
64      */
65     public static void loadSchemaIntoJanusGraph(final JanusGraph graph, final JanusGraphManagement graphMgmt,
66             String backend) {
67
68         try {
69             AAIConfig.init();
70         } catch (Exception ex) {
71             LOGGER.error(" ERROR - Could not run AAIConfig.init(). " + LogFormatTools.getStackTop(ex));
72             // System.out.println(" ERROR - Could not run AAIConfig.init(). ");
73             System.exit(1);
74         }
75
76         // NOTE - JanusGraph 0.5.3 doesn't keep a list of legal node Labels.
77         // They are only used when a vertex is actually being created.
78         // JanusGraph 1.1 will keep track (we think).
79
80         // Use EdgeRules to make sure edgeLabels are defined in the db. NOTE:
81         // the multiplicty used here is
82         // always "MULTI". This is not the same as our internal "Many2Many",
83         // "One2One", "One2Many" or "Many2One"
84         // We use the same edge-label for edges between many different types of
85         // nodes and our internal
86         // multiplicty definitions depends on which two types of nodes are being
87         // connected.
88
89         Multimap<String, EdgeRule> edges = null;
90         Set<String> labels = new HashSet<>();
91
92         EdgeIngestor edgeIngestor = SpringContextAware.getBean(EdgeIngestor.class);
93
94         try {
95             edges = edgeIngestor.getAllCurrentRules();
96         } catch (EdgeRuleNotFoundException e) {
97             LOGGER.error("Unable to find all rules {}", LogFormatTools.getStackTop(e));
98         }
99
100         for (EdgeRule rule : edges.values()) {
101             labels.add(rule.getLabel());
102         }
103
104         for (String label : labels) {
105             if (graphMgmt.containsRelationType(label)) {
106                 String dmsg = " EdgeLabel  [" + label + "] already existed. ";
107                 LOGGER.debug(dmsg);
108             } else {
109                 String dmsg = "Making EdgeLabel: [" + label + "]";
110                 LOGGER.debug(dmsg);
111                 graphMgmt.makeEdgeLabel(label).multiplicity(Multiplicity.valueOf("MULTI")).make();
112             }
113         }
114
115         Loader loader = LoaderUtil.getLatestVersion();
116
117         Map<String, Introspector> objs = loader.getAllObjects();
118         Map<String, PropertyKey> seenProps = new HashMap<>();
119
120         for (Introspector obj : objs.values()) {
121             for (String propName : obj.getProperties()) {
122                 String dbPropName = propName;
123                 Optional<String> alias = obj.getPropertyMetadata(propName, PropertyMetadata.DB_ALIAS);
124                 if (alias.isPresent()) {
125                     dbPropName = alias.get();
126                 }
127                 if (graphMgmt.containsRelationType(dbPropName)) {
128                     String dmsg = " PropertyKey  [" + dbPropName + "] already existed in the DB. ";
129                     LOGGER.debug(dmsg);
130                 } else {
131                     Class<?> type = obj.getClass(propName);
132                     Cardinality cardinality = Cardinality.SINGLE;
133                     boolean process = false;
134                     if (obj.isListType(propName) && obj.isSimpleGenericType(propName)) {
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                             if (dbPropName.equals("aai-uri")) {
152                                 String aai_uri_lock_enabled = AAIConfig.get(AAIConstants.AAI_LOCK_URI_ENABLED, "false");
153                                 LOGGER.info(" Info: aai_uri_lock_enabled:" + aai_uri_lock_enabled);
154                                 if ("true".equals(aai_uri_lock_enabled)) {
155                                     LOGGER.info(" Lock is being set for aai-uri Property.");
156                                     graphMgmt.setConsistency(propK, ConsistencyModifier.LOCK);
157                                 }
158                             }
159                             seenProps.put(dbPropName, propK);
160                         } else {
161                             propK = seenProps.get(dbPropName);
162                         }
163                         if (graphMgmt.containsGraphIndex(dbPropName)) {
164                             String dmsg = " Index  [" + dbPropName + "] already existed in the DB. ";
165                             LOGGER.debug(dmsg);
166                         } else {
167                             if (obj.getIndexedProperties().contains(propName)) {
168                                 JanusGraphIndex indexG = null;
169                                 if (obj.getUniqueProperties().contains(propName)) {
170                                     imsg = "Add Unique index for PropertyKey: [" + dbPropName + "]";
171                                     LOGGER.info(imsg);
172                                     indexG = graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK).unique().buildCompositeIndex();
173                                 } else {
174                                     imsg = "Add index for PropertyKey: [" + dbPropName + "]";
175                                     LOGGER.info(imsg);
176                                     indexG = graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK).buildCompositeIndex();
177                                 }
178                                 if (indexG != null && dbPropName.equals("aai-uri")) {
179                                     String aai_uri_lock_enabled = AAIConfig.get(AAIConstants.AAI_LOCK_URI_ENABLED, "false");
180                                     LOGGER.info(" Info:: aai_uri_lock_enabled:" + aai_uri_lock_enabled);
181                                     if ("true".equals(aai_uri_lock_enabled)) {
182                                         LOGGER.info("Lock is being set for aai-uri Index.");
183                                         graphMgmt.setConsistency(indexG, ConsistencyModifier.LOCK);
184                                     }
185                                 }
186                             } else {
187                                 imsg = "No index added for PropertyKey: [" + dbPropName + "]";
188                                 LOGGER.info(imsg);
189                             }
190                         }
191                     }
192                 }
193             }
194         }
195
196         String imsg = "-- About to call graphMgmt commit";
197         LOGGER.info(imsg);
198         if (backend != null) {
199             LOGGER.info("Successfully loaded the schema to " + backend);
200         }
201
202         graphMgmt.commit();
203     }
204
205 }