Fix sonar issues in SchemaGenerator
[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 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.LoaderUtil;
35 import org.onap.aai.logging.LogFormatTools;
36 import org.onap.aai.schema.enums.PropertyMetadata;
37 import org.onap.aai.util.AAIConfig;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 import java.util.HashMap;
42 import java.util.HashSet;
43 import java.util.Map;
44 import java.util.Optional;
45 import java.util.Set;
46 import java.util.function.Function;
47 import java.util.stream.Collectors;
48
49 public class SchemaGenerator {
50
51     private static final Logger LOGGER = LoggerFactory.getLogger(SchemaGenerator.class);
52
53     private SchemaGenerator() {
54     }
55
56     /**
57      * Load schema into JanusGraph.
58      *
59      * @param graphMgmt
60      *        the graph mgmt
61      */
62     public static void loadSchemaIntoJanusGraph(final JanusGraphManagement graphMgmt, String backend) {
63
64         try {
65             AAIConfig.init();
66         } catch (Exception ex) {
67             LOGGER.error(" ERROR - Could not run AAIConfig.init(). {}", LogFormatTools.getStackTop(ex));
68             System.exit(1);
69         }
70
71         // NOTE - JanusGraph 0.5.3 doesn't keep a list of legal node Labels.
72         // They are only used when a vertex is actually being created.
73         // JanusGraph 1.1 will keep track (we think).
74
75         // Use EdgeRules to make sure edgeLabels are defined in the db. NOTE:
76         // the multiplicty used here is
77         // always "MULTI". This is not the same as our internal "Many2Many",
78         // "One2One", "One2Many" or "Many2One"
79         // We use the same edge-label for edges between many different types of
80         // nodes and our internal
81         // multiplicty definitions depends on which two types of nodes are being
82         // connected.
83
84         makeEdgeLabels(graphMgmt);
85
86
87         Map<String, Introspector> objs = LoaderUtil.getLatestVersion().getAllObjects();
88         Map<String, PropertyKey> seenProps = new HashMap<>();
89
90         for (Introspector obj : objs.values()) {
91             for (String propName : obj.getProperties()) {
92                 String dbPropName = propName;
93                 Optional<String> alias = obj.getPropertyMetadata(propName, PropertyMetadata.DB_ALIAS);
94                 if (alias.isPresent()) {
95                     dbPropName = alias.get();
96                 }
97                 if (graphMgmt.containsRelationType(dbPropName)) {
98                     LOGGER.debug(" PropertyKey  [{}] already existed in the DB. ", dbPropName);
99                 } else {
100                     Class<?> type = obj.getClass(propName);
101                     Cardinality cardinality = Cardinality.SINGLE;
102                     boolean process = false;
103                     if (obj.isListType(propName) && obj.isSimpleGenericType(propName)) {
104                         cardinality = Cardinality.SET;
105                         type = obj.getGenericTypeClass(propName);
106                         process = true;
107                     } else if (obj.isSimpleType(propName)) {
108                         process = true;
109                     }
110
111                     if (process) {
112
113                         LOGGER.info("Creating PropertyKey: [{}], [{}], [{}]",
114                                 dbPropName, type.getSimpleName(), cardinality);
115                         PropertyKey propK;
116                         if (!seenProps.containsKey(dbPropName)) {
117                             propK = graphMgmt.makePropertyKey(dbPropName).dataType(type).cardinality(cardinality)
118                                     .make();
119                             seenProps.put(dbPropName, propK);
120                         } else {
121                             propK = seenProps.get(dbPropName);
122                         }
123                         if (graphMgmt.containsGraphIndex(dbPropName)) {
124                             LOGGER.debug(" Index  [{}] already existed in the DB. ", dbPropName);
125                         } else {
126                             if (obj.getIndexedProperties().contains(propName)) {
127                                 if (obj.getUniqueProperties().contains(propName)) {
128                                     LOGGER.info("Add Unique index for PropertyKey: [{}]", dbPropName);
129                                     graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK).unique()
130                                             .buildCompositeIndex();
131                                 } else {
132                                     LOGGER.info("Add index for PropertyKey: [{}]", dbPropName);
133                                     graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK).buildCompositeIndex();
134                                 }
135                             } else {
136                                 LOGGER.info("No index added for PropertyKey: [{}]", dbPropName);
137                             }
138                         }
139                     }
140                 }
141             }
142         }
143
144         LOGGER.info("-- About to call graphMgmt commit");
145         if (backend != null) {
146             LOGGER.info("Successfully loaded the schema to {}", backend);
147         }
148
149         graphMgmt.commit();
150     }
151
152     private static void makeEdgeLabels(JanusGraphManagement graphMgmt) {
153         try {
154             EdgeIngestor edgeIngestor = SpringContextAware.getBean(EdgeIngestor.class);
155
156             Set<String> labels = Optional.ofNullable(edgeIngestor.getAllCurrentRules())
157                     .map(collectValues(EdgeRule::getLabel))
158                     .orElseGet(HashSet::new);
159
160             labels.forEach(label -> {
161                 if (graphMgmt.containsRelationType(label)) {
162                     LOGGER.debug(" EdgeLabel  [{}] already existed. ", label);
163                 } else {
164                     LOGGER.debug("Making EdgeLabel: [{}]", label);
165                     graphMgmt.makeEdgeLabel(label).multiplicity(Multiplicity.valueOf("MULTI")).make();
166                 }
167             });
168         } catch (EdgeRuleNotFoundException e) {
169             LOGGER.error("Unable to find all rules {}", LogFormatTools.getStackTop(e));
170         }
171     }
172
173     /**
174      * Returns a function collecting all the values in a {@link com.google.common.collect.Multimap}
175      * given a mapping function
176      *
177      * @param f The mapper function
178      * @param <K> The type of key used by the provided {@link com.google.common.collect.Multimap}
179      * @param <V> The type of value used by the provided {@link com.google.common.collect.Multimap}
180      * @param <V0> The type which <V> is mapped to
181      */
182     private static <K, V, V0> Function<Multimap<K, V>, Set<V0>> collectValues(Function<V, V0> f) {
183         return as -> as.values().stream().map(f).collect(Collectors.toSet());
184     }
185
186 }