Fix minor sonar issues in SchemaGenerator4Hist
[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 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;
41
42 import java.util.HashMap;
43 import java.util.HashSet;
44 import java.util.Map;
45 import java.util.Optional;
46 import java.util.Set;
47
48 import static org.onap.aai.db.props.AAIProperties.*;
49
50 public class SchemaGenerator4Hist {
51
52     private static final Logger LOGGER = LoggerFactory.getLogger(SchemaGenerator4Hist.class);
53
54     private SchemaGenerator4Hist(){
55
56     }
57     /**
58      * Load schema into JanusGraph.
59      *
60      * @param graphMgmt
61      *        the graph mgmt
62      */
63     public static void loadSchemaIntoJanusGraph(final JanusGraphManagement graphMgmt, String backend) {
64
65         try {
66             AAIConfig.init();
67         } catch (Exception ex) {
68             LOGGER.error(" ERROR - Could not run AAIConfig.init(). {}", LogFormatTools.getStackTop(ex));
69             System.exit(1);
70         }
71
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).
75
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
83         // connected.
84
85         Multimap<String, EdgeRule> edges = null;
86         Set<String> labels = new HashSet<>();
87
88         EdgeIngestor edgeIngestor = SpringContextAware.getBean(EdgeIngestor.class);
89
90         try {
91             edges = edgeIngestor.getAllCurrentRules();
92         } catch (EdgeRuleNotFoundException e) {
93             LOGGER.error("Unable to find all rules {}", LogFormatTools.getStackTop(e));
94         }
95
96         for (EdgeRule rule : edges.values()) {
97             labels.add(rule.getLabel());
98         }
99
100         for (String label : labels) {
101             if (graphMgmt.containsRelationType(label)) {
102                 LOGGER.debug(" EdgeLabel  [{}] already existed. ", label);
103             } else {
104                 LOGGER.debug("Making EdgeLabel: [{}]", label);
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                     LOGGER.debug(" PropertyKey  [{}] already existed in the DB. ", dbPropName);
123                 } else {
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
133                         // history.
134                         type = obj.getGenericTypeClass(propName);
135                         process = true;
136                     } else if (obj.isSimpleType(propName)) {
137                         process = true;
138                     }
139
140                     if (process) {
141
142                         LOGGER.info(" Creating PropertyKey: [{}], [{}], [{}]",
143                                 dbPropName, type.getSimpleName(), cardinality);
144                         PropertyKey propK;
145                         if (!seenProps.containsKey(dbPropName)) {
146                             propK = graphMgmt.makePropertyKey(dbPropName).dataType(type).cardinality(cardinality)
147                                     .make();
148                             seenProps.put(dbPropName, propK);
149                         } else {
150                             propK = seenProps.get(dbPropName);
151                         }
152                         if (graphMgmt.containsGraphIndex(dbPropName)) {
153                             LOGGER.debug(" Index  [{}] already existed in the DB. ", dbPropName);
154                         } else {
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();
159                             } else {
160                                 LOGGER.info("No index needed/added for PropertyKey: [{}]", dbPropName);
161                             }
162                         }
163                     }
164                 }
165             }
166         }// Done processing all properties defined in the OXM
167
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);
178
179
180         String imsg = "-- About to call graphMgmt commit";
181         LOGGER.info(imsg);
182         graphMgmt.commit();
183         if (backend != null) {
184             LOGGER.info("Successfully loaded the schema to {}", backend);
185         }
186
187     }
188
189     private static <T> void makeNewProperty(JanusGraphManagement graphMgmt,
190                                             Map<String, PropertyKey> seenProps,
191                                             Class<T> type,
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)
199                     .make();
200         }
201     }
202 }