diff options
Diffstat (limited to 'ajsc-aai/src/main')
5 files changed, 606 insertions, 613 deletions
diff --git a/ajsc-aai/src/main/java/org/openecomp/aai/db/schema/AuditOXM.java b/ajsc-aai/src/main/java/org/openecomp/aai/db/schema/AuditOXM.java index bbdd7c6..4fcb27d 100644 --- a/ajsc-aai/src/main/java/org/openecomp/aai/db/schema/AuditOXM.java +++ b/ajsc-aai/src/main/java/org/openecomp/aai/db/schema/AuditOXM.java @@ -20,19 +20,22 @@ package org.openecomp.aai.db.schema; +import com.google.common.collect.Multimap; +import com.thinkaurelius.titan.core.Cardinality; +import com.thinkaurelius.titan.core.Multiplicity; +import com.thinkaurelius.titan.core.schema.SchemaStatus; import java.io.IOException; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; +import java.util.Objects; import java.util.Set; - import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; - import org.openecomp.aai.db.AAIProperties; import org.openecomp.aai.dbmodel.DbEdgeRules; import org.openecomp.aai.introspection.Introspector; @@ -42,200 +45,204 @@ import org.openecomp.aai.introspection.ModelType; import org.openecomp.aai.introspection.Version; import org.openecomp.aai.logging.LogLineBuilder; import org.openecomp.aai.util.AAIConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; -import com.google.common.collect.Multimap; -import com.thinkaurelius.titan.core.Cardinality; -import com.thinkaurelius.titan.core.Multiplicity; -import com.thinkaurelius.titan.core.schema.SchemaStatus; - public class AuditOXM extends Auditor { + private static final Logger log = LoggerFactory.getLogger(AuditOXM.class); + private Set<Introspector> allObjects; + private final LogLineBuilder llBuilder = new LogLineBuilder(); + + /** + * Instantiates a new audit OXM. + * + * @param version the version + */ + public AuditOXM(Version version) { + Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version, llBuilder); + Set<String> objectNames = getAllObjects(version); + allObjects = new HashSet<>(); + for (String key : objectNames) { + Introspector temp = loader.introspectorFromName(key); + allObjects.add(temp); + this.createDBProperties(temp); + } + for (Introspector temp : allObjects) { + this.createDBIndexes(temp); + } + createEdgeLabels(); + } + + /** + * Gets the all objects. + * + * @param version the version + * @return the all objects + */ + private Set<String> getAllObjects(Version version) { + String fileName = AAIConstants.AAI_HOME_ETC_OXM + "aai_oxm_" + version.toString() + ".xml"; + Set<String> result = new HashSet<>(); + DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); + try { + docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); + DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); + Document doc = docBuilder.parse(fileName); + NodeList list = doc.getElementsByTagName("java-type"); + for (int i = 0; i < list.getLength(); i++) { + result.add(list.item(i).getAttributes().getNamedItem("name").getNodeValue()); + } + } catch (ParserConfigurationException | SAXException | IOException e) { + // TODO Auto-generated catch block + log.error(e.getLocalizedMessage(), e); + } + + result.remove("EdgePropNames"); + return result; + } + + /** + * Creates the DB properties. + * + * @param temp the temp + */ + private void createDBProperties(Introspector temp) { + List<String> objectProperties = temp.getProperties(); + + for (String prop : objectProperties) { + if (!properties.containsKey(prop)) { + DBProperty dbProperty = new DBProperty(); + dbProperty.setName(prop); + if (temp.isListType(prop)) { + handleListTypePropertyForIntrospector(dbProperty, prop, temp); + } else { + handleNonListTypePropertyForIntrospector(dbProperty, prop, temp); + } + } + } + } + + private void handleListTypePropertyForIntrospector(DBProperty dbProperty, String prop, Introspector temp) { + dbProperty.setCardinality(Cardinality.SET); + if (temp.isSimpleGenericType(prop)) { + Class<?> clazz; + try { + clazz = Class.forName(temp.getGenericType(prop)); + } catch (ClassNotFoundException e) { + clazz = Object.class; + } + dbProperty.setTypeClass(clazz); + properties.put(prop, dbProperty); + } + } + + private void handleNonListTypePropertyForIntrospector(DBProperty dbProperty, String prop, Introspector temp) { + dbProperty.setCardinality(Cardinality.SINGLE); + if (temp.isSimpleType(prop)) { + Class<?> clazz; + try { + clazz = Class.forName(temp.getType(prop)); + } catch (ClassNotFoundException e) { + clazz = Object.class; + } + dbProperty.setTypeClass(clazz); + properties.put(prop, dbProperty); + } + } + + /** + * Creates the DB indexes. + * + * @param temp the temp + */ + private void createDBIndexes(Introspector temp) { + String uniqueProps = temp.getMetadata("uniqueProps"); + String namespace = temp.getMetadata("namespace"); + if (uniqueProps == null) { + uniqueProps = ""; + } + if (namespace == null) { + namespace = ""; + } + boolean isTopLevel = !Objects.equals(namespace, ""); + List<String> unique = Arrays.asList(uniqueProps.split(",")); + List<String> indexed = temp.getIndexedProperties(); + List<String> keys = temp.getKeys(); + + setDbIndexAttributes(indexed, unique); + + if (keys.size() > 1 || isTopLevel) { + setDbIndexAttributes(isTopLevel, temp, unique, keys); + } + } + + private void setDbIndexAttributes(List<String> indexed, List<String> unique) { + for (String prop : indexed) { + DBIndex dbIndex = new DBIndex(); + LinkedHashSet<DBProperty> properties = new LinkedHashSet<>(); + if (!this.indexes.containsKey(prop)) { + dbIndex.setName(prop); + dbIndex.setUnique(unique.contains(prop)); + properties.add(this.properties.get(prop)); + dbIndex.setProperties(properties); + dbIndex.setStatus(SchemaStatus.ENABLED); + this.indexes.put(prop, dbIndex); + } + } + } + + private void setDbIndexAttributes(boolean isTopLevel, Introspector temp, List<String> unique, List<String> keys) { + DBIndex dbIndex = new DBIndex(); + LinkedHashSet<DBProperty> properties = new LinkedHashSet<>(); + dbIndex.setName("key-for-" + temp.getDbName()); + if (!this.indexes.containsKey(dbIndex.getName())) { + boolean isUnique = false; + if (isTopLevel) { + properties.add(this.properties.get(AAIProperties.NODE_TYPE)); + } + for (String key : keys) { + properties.add(this.properties.get(key)); + + if (unique.contains(key) && !isUnique) { + isUnique = true; + } + } + dbIndex.setUnique(isUnique); + dbIndex.setProperties(properties); + dbIndex.setStatus(SchemaStatus.ENABLED); + this.indexes.put(dbIndex.getName(), dbIndex); + } + } + + /** + * Creates the edge labels. + */ + + private void createEdgeLabels() { + Multimap<String, String> edgeRules = DbEdgeRules.EdgeRules; + for (String key : edgeRules.keySet()) { + Collection<String> collection = edgeRules.get(key); + EdgeProperty prop = new EdgeProperty(); + //there is only ever one, they used the wrong type for EdgeRules + String label = ""; + for (String item : collection) { + label = item.split(",")[0]; + } + prop.setName(label); + prop.setMultiplicity(Multiplicity.MULTI); + this.edgeLabels.put(label, prop); + } + } - private Set<Introspector> allObjects; - private final LogLineBuilder llBuilder = new LogLineBuilder(); - - /** - * Instantiates a new audit OXM. - * - * @param version the version - */ - public AuditOXM(Version version) { - Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version, llBuilder); - Set<String> objectNames = getAllObjects(version); - allObjects = new HashSet<>(); - for (String key : objectNames) { - Introspector temp = loader.introspectorFromName(key); - allObjects.add(temp); - this.createDBProperties(temp); - } - for (Introspector temp : allObjects) { - this.createDBIndexes(temp); - } - createEdgeLabels(); - - } - - /** - * Gets the all objects. - * - * @param version the version - * @return the all objects - */ - private Set<String> getAllObjects(Version version) { - String fileName = AAIConstants.AAI_HOME_ETC_OXM + "aai_oxm_" + version.toString() + ".xml"; - Set<String> result = new HashSet<>(); - DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); - try { - docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); - DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); - Document doc = docBuilder.parse(fileName); - NodeList list = doc.getElementsByTagName("java-type"); - for (int i = 0; i < list.getLength(); i++) { - result.add(list.item(i).getAttributes().getNamedItem("name").getNodeValue()); - } - } catch (ParserConfigurationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (SAXException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - result.remove("EdgePropNames"); - return result; - - } - - /** - * Creates the DB properties. - * - * @param temp the temp - */ - private void createDBProperties(Introspector temp) { - List<String> objectProperties = temp.getProperties(); - - for (String prop : objectProperties) { - if (!properties.containsKey(prop)) { - DBProperty dbProperty = new DBProperty(); - dbProperty.setName(prop); - if (temp.isListType(prop)) { - dbProperty.setCardinality(Cardinality.SET); - if (temp.isSimpleGenericType(prop)) { - Class<?> clazz = null; - try { - clazz = Class.forName(temp.getGenericType(prop)); - } catch (ClassNotFoundException e) { - clazz = Object.class; - } - dbProperty.setTypeClass(clazz); - properties.put(prop, dbProperty); - } - } else { - dbProperty.setCardinality(Cardinality.SINGLE); - if (temp.isSimpleType(prop)) { - Class<?> clazz = null; - try { - clazz = Class.forName(temp.getType(prop)); - } catch (ClassNotFoundException e) { - clazz = Object.class; - } - dbProperty.setTypeClass(clazz); - properties.put(prop, dbProperty); - } - } - } - } - - } - - /** - * Creates the DB indexes. - * - * @param temp the temp - */ - private void createDBIndexes(Introspector temp) { - String uniqueProps = temp.getMetadata("uniqueProps"); - String namespace = temp.getMetadata("namespace"); - if (uniqueProps == null) { - uniqueProps = ""; - } - if (namespace == null) { - namespace = ""; - } - boolean isTopLevel = namespace != ""; - List<String> unique = Arrays.asList(uniqueProps.split(",")); - List<String> indexed = temp.getIndexedProperties(); - List<String> keys = temp.getKeys(); - - for (String prop : indexed) { - DBIndex dbIndex = new DBIndex(); - LinkedHashSet<DBProperty> properties = new LinkedHashSet<>(); - if (!this.indexes.containsKey(prop)) { - dbIndex.setName(prop); - dbIndex.setUnique(unique.contains(prop)); - properties.add(this.properties.get(prop)); - dbIndex.setProperties(properties); - dbIndex.setStatus(SchemaStatus.ENABLED); - this.indexes.put(prop, dbIndex); - } - } - if (keys.size() > 1 || isTopLevel) { - DBIndex dbIndex = new DBIndex(); - LinkedHashSet<DBProperty> properties = new LinkedHashSet<>(); - dbIndex.setName("key-for-" + temp.getDbName()); - if (!this.indexes.containsKey(dbIndex.getName())) { - boolean isUnique = false; - if (isTopLevel) { - properties.add(this.properties.get(AAIProperties.NODE_TYPE)); - } - for (String key : keys) { - properties.add(this.properties.get(key)); - - if (unique.contains(key) && !isUnique) { - isUnique = true; - } - } - dbIndex.setUnique(isUnique); - dbIndex.setProperties(properties); - dbIndex.setStatus(SchemaStatus.ENABLED); - this.indexes.put(dbIndex.getName(), dbIndex); - } - } - - } - - /** - * Creates the edge labels. - */ - private void createEdgeLabels() { - Multimap<String, String> edgeRules = DbEdgeRules.EdgeRules; - for (String key : edgeRules.keySet()) { - Collection<String> collection = edgeRules.get(key); - EdgeProperty prop = new EdgeProperty(); - //there is only ever one, they used the wrong type for EdgeRules - String label = ""; - for (String item : collection) { - label = item.split(",")[0]; - } - prop.setName(label); - prop.setMultiplicity(Multiplicity.MULTI); - this.edgeLabels.put(label, prop); - } - } - - /** - * Gets the all introspectors. - * - * @return the all introspectors - */ - public Set<Introspector> getAllIntrospectors() { - return this.allObjects; - } - + /** + * Gets the all introspectors. + * + * @return the all introspectors + */ + public Set<Introspector> getAllIntrospectors() { + return this.allObjects; + } } diff --git a/ajsc-aai/src/main/java/org/openecomp/aai/db/schema/AuditTitan.java b/ajsc-aai/src/main/java/org/openecomp/aai/db/schema/AuditTitan.java index 814afd6..c29d94a 100644 --- a/ajsc-aai/src/main/java/org/openecomp/aai/db/schema/AuditTitan.java +++ b/ajsc-aai/src/main/java/org/openecomp/aai/db/schema/AuditTitan.java @@ -20,108 +20,100 @@ package org.openecomp.aai.db.schema; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; - -import org.apache.tinkerpop.gremlin.structure.Vertex; - import com.thinkaurelius.titan.core.EdgeLabel; import com.thinkaurelius.titan.core.PropertyKey; import com.thinkaurelius.titan.core.TitanGraph; import com.thinkaurelius.titan.core.schema.TitanGraphIndex; import com.thinkaurelius.titan.core.schema.TitanManagement; +import java.util.Iterator; +import java.util.LinkedHashSet; +import org.apache.tinkerpop.gremlin.structure.Vertex; public class AuditTitan extends Auditor { - private final TitanGraph graph; - - /** - * Instantiates a new audit titan. - * - * @param g the g - */ - public AuditTitan (TitanGraph g) { - this.graph = g; - buildSchema(); - } - - /** - * Builds the schema. - */ - private void buildSchema() { - populateProperties(); - populateIndexes(); - populateEdgeLabels(); - } - - /** - * Populate properties. - */ - private void populateProperties() { - TitanManagement mgmt = graph.openManagement(); - Iterable<PropertyKey> iterable = mgmt.getRelationTypes(PropertyKey.class); - Iterator<PropertyKey> titanProperties = iterable.iterator(); - PropertyKey propKey = null; - while (titanProperties.hasNext()) { - propKey = titanProperties.next(); - DBProperty prop = new DBProperty(); - - prop.setName(propKey.name()); - prop.setCardinality(propKey.cardinality()); - prop.setTypeClass(propKey.dataType()); - - this.properties.put(prop.getName(), prop); - } - } - - /** - * Populate indexes. - */ - private void populateIndexes() { - TitanManagement mgmt = graph.openManagement(); - Iterable<TitanGraphIndex> iterable = mgmt.getGraphIndexes(Vertex.class); - Iterator<TitanGraphIndex> titanIndexes = iterable.iterator(); - TitanGraphIndex titanIndex = null; - while (titanIndexes.hasNext()) { - titanIndex = titanIndexes.next(); - if (titanIndex.isCompositeIndex()) { - DBIndex index = new DBIndex(); - LinkedHashSet<DBProperty> dbProperties = new LinkedHashSet<>(); - index.setName(titanIndex.name()); - index.setUnique(titanIndex.isUnique()); - PropertyKey[] keys = titanIndex.getFieldKeys(); - for (PropertyKey key : keys) { - dbProperties.add(this.properties.get(key.name())); - } - index.setProperties(dbProperties); - index.setStatus(titanIndex.getIndexStatus(keys[0])); - this.indexes.put(index.getName(), index); - } - } - } - - /** - * Populate edge labels. - */ - private void populateEdgeLabels() { - TitanManagement mgmt = graph.openManagement(); - Iterable<EdgeLabel> iterable = mgmt.getRelationTypes(EdgeLabel.class); - Iterator<EdgeLabel> titanEdgeLabels = iterable.iterator(); - EdgeLabel edgeLabel = null; - while (titanEdgeLabels.hasNext()) { - edgeLabel = titanEdgeLabels.next(); - EdgeProperty edgeProperty = new EdgeProperty(); - - edgeProperty.setName(edgeLabel.name()); - edgeProperty.setMultiplicity(edgeLabel.multiplicity()); - - this.edgeLabels.put(edgeProperty.getName(), edgeProperty); - } - } - + private final TitanGraph graph; + + /** + * Instantiates a new audit titan. + * + * @param g the g + */ + public AuditTitan(TitanGraph g) { + this.graph = g; + buildSchema(); + } + + /** + * Builds the schema. + */ + private void buildSchema() { + populateProperties(); + populateIndexes(); + populateEdgeLabels(); + } + + /** + * Populate properties. + */ + private void populateProperties() { + TitanManagement mgmt = graph.openManagement(); + Iterable<PropertyKey> iterable = mgmt.getRelationTypes(PropertyKey.class); + Iterator<PropertyKey> titanProperties = iterable.iterator(); + PropertyKey propKey; + while (titanProperties.hasNext()) { + propKey = titanProperties.next(); + DBProperty prop = new DBProperty(); + + prop.setName(propKey.name()); + prop.setCardinality(propKey.cardinality()); + prop.setTypeClass(propKey.dataType()); + + this.properties.put(prop.getName(), prop); + } + } + + /** + * Populate indexes. + */ + private void populateIndexes() { + TitanManagement mgmt = graph.openManagement(); + Iterable<TitanGraphIndex> iterable = mgmt.getGraphIndexes(Vertex.class); + Iterator<TitanGraphIndex> titanIndexes = iterable.iterator(); + TitanGraphIndex titanIndex; + while (titanIndexes.hasNext()) { + titanIndex = titanIndexes.next(); + if (titanIndex.isCompositeIndex()) { + DBIndex index = new DBIndex(); + LinkedHashSet<DBProperty> dbProperties = new LinkedHashSet<>(); + index.setName(titanIndex.name()); + index.setUnique(titanIndex.isUnique()); + PropertyKey[] keys = titanIndex.getFieldKeys(); + for (PropertyKey key : keys) { + dbProperties.add(this.properties.get(key.name())); + } + index.setProperties(dbProperties); + index.setStatus(titanIndex.getIndexStatus(keys[0])); + this.indexes.put(index.getName(), index); + } + } + } + + /** + * Populate edge labels. + */ + private void populateEdgeLabels() { + TitanManagement mgmt = graph.openManagement(); + Iterable<EdgeLabel> iterable = mgmt.getRelationTypes(EdgeLabel.class); + Iterator<EdgeLabel> titanEdgeLabels = iterable.iterator(); + EdgeLabel edgeLabel; + while (titanEdgeLabels.hasNext()) { + edgeLabel = titanEdgeLabels.next(); + EdgeProperty edgeProperty = new EdgeProperty(); + + edgeProperty.setName(edgeLabel.name()); + edgeProperty.setMultiplicity(edgeLabel.multiplicity()); + + this.edgeLabels.put(edgeProperty.getName(), edgeProperty); + } + } } diff --git a/ajsc-aai/src/main/java/org/openecomp/aai/db/schema/Auditor.java b/ajsc-aai/src/main/java/org/openecomp/aai/db/schema/Auditor.java index 67cf841..997b46c 100644 --- a/ajsc-aai/src/main/java/org/openecomp/aai/db/schema/Auditor.java +++ b/ajsc-aai/src/main/java/org/openecomp/aai/db/schema/Auditor.java @@ -21,38 +21,37 @@ package org.openecomp.aai.db.schema; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; public abstract class Auditor { - protected Map<String, DBProperty> properties = new HashMap<>(); - protected Map<String, DBIndex> indexes = new HashMap<>(); - protected Map<String, EdgeProperty> edgeLabels = new HashMap<>(); - - /** - * Gets the audit doc. - * - * @return the audit doc - */ - public AuditDoc getAuditDoc() { - AuditDoc doc = new AuditDoc(); - List<DBProperty> propertyList = new ArrayList<>(); - List<DBIndex> indexList = new ArrayList<>(); - List<EdgeProperty> edgeLabelList = new ArrayList<>(); - propertyList.addAll(this.properties.values()); - indexList.addAll(this.indexes.values()); - edgeLabelList.addAll(this.edgeLabels.values()); - Collections.sort(propertyList, new CompareByName()); - Collections.sort(indexList, new CompareByName()); - Collections.sort(edgeLabelList, new CompareByName()); - - doc.setProperties(propertyList); - doc.setIndexes(indexList); - doc.setEdgeLabels(edgeLabelList); - - return doc; - } + protected Map<String, DBProperty> properties = new HashMap<>(); + protected Map<String, DBIndex> indexes = new HashMap<>(); + protected Map<String, EdgeProperty> edgeLabels = new HashMap<>(); + + /** + * Gets the audit doc. + * + * @return the audit doc + */ + public AuditDoc getAuditDoc() { + AuditDoc doc = new AuditDoc(); + List<DBProperty> propertyList = new ArrayList<>(); + List<DBIndex> indexList = new ArrayList<>(); + List<EdgeProperty> edgeLabelList = new ArrayList<>(); + propertyList.addAll(this.properties.values()); + indexList.addAll(this.indexes.values()); + edgeLabelList.addAll(this.edgeLabels.values()); + propertyList.sort(new CompareByName()); + indexList.sort(new CompareByName()); + edgeLabelList.sort(new CompareByName()); + + doc.setProperties(propertyList); + doc.setIndexes(indexList); + doc.setEdgeLabels(edgeLabelList); + + return doc; + } } diff --git a/ajsc-aai/src/main/java/org/openecomp/aai/db/schema/ManageTitanSchema.java b/ajsc-aai/src/main/java/org/openecomp/aai/db/schema/ManageTitanSchema.java index 0824f61..ae0d08c 100644 --- a/ajsc-aai/src/main/java/org/openecomp/aai/db/schema/ManageTitanSchema.java +++ b/ajsc-aai/src/main/java/org/openecomp/aai/db/schema/ManageTitanSchema.java @@ -20,310 +20,305 @@ package org.openecomp.aai.db.schema; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -import org.apache.tinkerpop.gremlin.structure.Vertex; -import org.openecomp.aai.introspection.Version; - import com.thinkaurelius.titan.core.PropertyKey; import com.thinkaurelius.titan.core.TitanGraph; import com.thinkaurelius.titan.core.schema.SchemaStatus; import com.thinkaurelius.titan.core.schema.TitanGraphIndex; import com.thinkaurelius.titan.core.schema.TitanManagement; import com.thinkaurelius.titan.core.schema.TitanManagement.IndexBuilder; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.openecomp.aai.introspection.Version; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ManageTitanSchema { - - private TitanManagement graphMgmt; - private TitanGraph graph; - private List<DBProperty> aaiProperties; - private List<DBIndex> aaiIndexes; - private List<EdgeProperty> aaiEdgeProperties; - private Auditor oxmInfo = null; - private Auditor graphInfo = null; - - /** - * Instantiates a new manage titan schema. - * - * @param graph the graph - */ - public ManageTitanSchema(final TitanGraph graph) { - this.graph = graph; - oxmInfo = AuditorFactory.getOXMAuditor(Version.v8); - graphInfo = AuditorFactory.getGraphAuditor(graph); - } - - - /** - * Builds the schema. - */ - public void buildSchema() { - - this.graphMgmt = graph.openManagement(); - aaiProperties = new ArrayList<>(); - aaiEdgeProperties = new ArrayList<>(); - aaiIndexes = new ArrayList<>(); - aaiProperties.addAll(oxmInfo.getAuditDoc().getProperties()); - aaiIndexes.addAll(oxmInfo.getAuditDoc().getIndexes()); - aaiEdgeProperties.addAll(oxmInfo.getAuditDoc().getEdgeLabels()); - try { - createPropertyKeys(); - createIndexes(); - createEdgeLabels(); - } catch (Exception e) { - e.printStackTrace(); - graphMgmt.rollback(); - } - graphMgmt.commit(); - } - - /** - * Creates the property keys. - */ - private void createPropertyKeys() { - - - for (DBProperty prop : aaiProperties) { - - if (graphMgmt.containsPropertyKey(prop.getName())) { - PropertyKey key = graphMgmt.getPropertyKey(prop.getName()); - boolean isChanged = false; - if (!prop.getCardinality().equals(key.cardinality())) { - isChanged = true; - } - if (!prop.getTypeClass().equals(key.dataType())) { - isChanged = true; - } - if (isChanged) { - //must modify! - this.replaceProperty(prop); - } - } else { - //create a new property key - System.out.println("Key: " + prop.getName() + " not found - adding"); - graphMgmt.makePropertyKey(prop.getName()).dataType(prop.getTypeClass()).cardinality(prop.getCardinality()).make(); - } - } - - } - - /** - * Creates the indexes. - */ - private void createIndexes() { - - for (DBIndex index : aaiIndexes) { - Set<DBProperty> props = index.getProperties(); - boolean isChanged = false; - boolean isNew = false; - List<PropertyKey> keyList = new ArrayList<>(); - for (DBProperty prop : props) { - keyList.add(graphMgmt.getPropertyKey(prop.getName())); - } - if (graphMgmt.containsGraphIndex(index.getName())) { - TitanGraphIndex titanIndex = graphMgmt.getGraphIndex(index.getName()); - PropertyKey[] dbKeys = titanIndex.getFieldKeys(); - if (dbKeys.length != keyList.size()) { - isChanged = true; - } else { - int i = 0; - for (PropertyKey key : keyList) { - if (!dbKeys[i].equals(key)) { - isChanged = true; - break; - } - i++; - } - } - } else { - isNew = true; - } - if (keyList.size() > 0) { - this.createIndex(graphMgmt, index.getName(), keyList, index.isUnique(), isNew, isChanged); - } - } - } - - // Use EdgeRules to make sure edgeLabels are defined in the db. NOTE: the multiplicty used here is - // always "MULTI". This is not the same as our internal "Many2Many", "One2One", "One2Many" or "Many2One" - // We use the same edge-label for edges between many different types of nodes and our internal - // multiplicty definitions depends on which two types of nodes are being connected. - /** - * Creates the edge labels. - */ - private void createEdgeLabels() { - - - for (EdgeProperty prop : aaiEdgeProperties) { - - if (graphMgmt.containsEdgeLabel(prop.getName())) { - // see what changed - } else { - graphMgmt.makeEdgeLabel(prop.getName()).multiplicity(prop.getMultiplicity()).make(); - } - - } - - - } - - /** - * Creates the property. - * - * @param mgmt the mgmt - * @param prop the prop - */ - private void createProperty(TitanManagement mgmt, DBProperty prop) { - if (mgmt.containsPropertyKey(prop.getName())) { - PropertyKey key = mgmt.getPropertyKey(prop.getName()); - boolean isChanged = false; - if (!prop.getCardinality().equals(key.cardinality())) { - isChanged = true; - } - if (!prop.getTypeClass().equals(key.dataType())) { - isChanged = true; - } - if (isChanged) { - //must modify! - this.replaceProperty(prop); - } - } else { - //create a new property key - System.out.println("Key: " + prop.getName() + " not found - adding"); - mgmt.makePropertyKey(prop.getName()).dataType(prop.getTypeClass()).cardinality(prop.getCardinality()).make(); - } - } - - /** - * Creates the index. - * - * @param mgmt the mgmt - * @param indexName the index name - * @param keys the keys - * @param isUnique the is unique - * @param isNew the is new - * @param isChanged the is changed - */ - private void createIndex(TitanManagement mgmt, String indexName, List<PropertyKey> keys, boolean isUnique, boolean isNew, boolean isChanged) { - + private static final Logger log = LoggerFactory.getLogger(ManageTitanSchema.class); + private TitanManagement graphMgmt; + private TitanGraph graph; + private List<DBProperty> aaiProperties; + private List<DBIndex> aaiIndexes; + private List<EdgeProperty> aaiEdgeProperties; + private Auditor oxmInfo = null; + private Auditor graphInfo = null; + + /** + * Instantiates a new manage titan schema. + * + * @param graph the graph + */ + public ManageTitanSchema(final TitanGraph graph) { + this.graph = graph; + oxmInfo = AuditorFactory.getOXMAuditor(Version.v8); + graphInfo = AuditorFactory.getGraphAuditor(graph); + } + + + /** + * Builds the schema. + */ + public void buildSchema() { + + this.graphMgmt = graph.openManagement(); + aaiProperties = new ArrayList<>(); + aaiEdgeProperties = new ArrayList<>(); + aaiIndexes = new ArrayList<>(); + aaiProperties.addAll(oxmInfo.getAuditDoc().getProperties()); + aaiIndexes.addAll(oxmInfo.getAuditDoc().getIndexes()); + aaiEdgeProperties.addAll(oxmInfo.getAuditDoc().getEdgeLabels()); + try { + createPropertyKeys(); + createIndexes(); + createEdgeLabels(); + } catch (Exception e) { + log.error(e.getLocalizedMessage(), e); + graphMgmt.rollback(); + } + graphMgmt.commit(); + } + + /** + * Creates the property keys. + */ + private void createPropertyKeys() { + + for (DBProperty prop : aaiProperties) { + + if (graphMgmt.containsPropertyKey(prop.getName())) { + PropertyKey key = graphMgmt.getPropertyKey(prop.getName()); + boolean isChanged = false; + if (!prop.getCardinality().equals(key.cardinality())) { + isChanged = true; + } + if (!prop.getTypeClass().equals(key.dataType())) { + isChanged = true; + } + if (isChanged) { + //must modify! + this.replaceProperty(prop); + } + } else { + //create a new property key + log.info(String.format("Key: %s not found - adding", prop.getName())); + graphMgmt.makePropertyKey(prop.getName()).dataType(prop.getTypeClass()) + .cardinality(prop.getCardinality()).make(); + } + } + } + + /** + * Creates the indexes. + */ + private void createIndexes() { + + for (DBIndex index : aaiIndexes) { + Set<DBProperty> props = index.getProperties(); + boolean isChanged = false; + boolean isNew = false; + List<PropertyKey> keyList = new ArrayList<>(); + for (DBProperty prop : props) { + keyList.add(graphMgmt.getPropertyKey(prop.getName())); + } + if (graphMgmt.containsGraphIndex(index.getName())) { + isChanged = handleTitanGraphIndex(index, keyList); + } else { + isNew = true; + } + if (!keyList.isEmpty()) { + this.createIndex(graphMgmt, index.getName(), keyList, index.isUnique(), isNew, isChanged); + } + } + } + + private boolean handleTitanGraphIndex(DBIndex index, List<PropertyKey> keyList) { + boolean isChanged = false; + TitanGraphIndex titanIndex = graphMgmt.getGraphIndex(index.getName()); + PropertyKey[] dbKeys = titanIndex.getFieldKeys(); + if (dbKeys.length != keyList.size()) { + isChanged = true; + } else { + int i = 0; + for (PropertyKey key : keyList) { + if (!dbKeys[i].equals(key)) { + isChanged = true; + break; + } + i++; + } + } + return isChanged; + } + + // Use EdgeRules to make sure edgeLabels are defined in the db. NOTE: the multiplicty used here is + // always "MULTI". This is not the same as our internal "Many2Many", "One2One", "One2Many" or "Many2One" + // We use the same edge-label for edges between many different types of nodes and our internal + // multiplicty definitions depends on which two types of nodes are being connected. + + /** + * Creates the edge labels. + */ + private void createEdgeLabels() { + + for (EdgeProperty prop : aaiEdgeProperties) { + + if (graphMgmt.containsEdgeLabel(prop.getName())) { + // see what changed + } else { + graphMgmt.makeEdgeLabel(prop.getName()).multiplicity(prop.getMultiplicity()).make(); + } + } + } + + /** + * Creates the property. + * + * @param mgmt the mgmt + * @param prop the prop + */ + private void createProperty(TitanManagement mgmt, DBProperty prop) { + if (mgmt.containsPropertyKey(prop.getName())) { + PropertyKey key = mgmt.getPropertyKey(prop.getName()); + boolean isChanged = false; + if (!prop.getCardinality().equals(key.cardinality())) { + isChanged = true; + } + if (!prop.getTypeClass().equals(key.dataType())) { + isChanged = true; + } + if (isChanged) { + //must modify! + this.replaceProperty(prop); + } + } else { + //create a new property key + log.info(String.format("Key: %s not found - adding", prop.getName())); + mgmt.makePropertyKey(prop.getName()).dataType(prop.getTypeClass()).cardinality(prop.getCardinality()) + .make(); + } + } + + /** + * Creates the index. + * + * @param mgmt the mgmt + * @param indexName the index name + * @param keys the keys + * @param isUnique the is unique + * @param isNew the is new + * @param isChanged the is changed + */ + private void createIndex(TitanManagement mgmt, String indexName, List<PropertyKey> keys, boolean isUnique, + boolean isNew, boolean isChanged) { + /*if (isChanged) { - System.out.println("Changing index: " + indexName); + log.info("Changing index: " + indexName); TitanGraphIndex oldIndex = mgmt.getGraphIndex(indexName); mgmt.updateIndex(oldIndex, SchemaAction.DISABLE_INDEX); mgmt.commit(); //cannot remove indexes //graphMgmt.updateIndex(oldIndex, SchemaAction.REMOVE_INDEX); }*/ - if (isNew || isChanged) { - - if (isNew) { - IndexBuilder builder = mgmt.buildIndex(indexName,Vertex.class); - for (PropertyKey k : keys) { - builder.addKey(k); - } - if (isUnique) { - builder.unique(); - } - builder.buildCompositeIndex(); - System.out.println("Built index for " + indexName + " with keys: " + keys); - - //mgmt.commit(); - } - - //mgmt = graph.getManagementSystem(); - //mgmt.updateIndex(mgmt.getGraphIndex(indexName), SchemaAction.REGISTER_INDEX); - //mgmt.commit(); - - try { - //waitForCompletion(indexName); - //TitanIndexRepair.hbaseRepair(AAIConstants.AAI_CONFIG_FILENAME, indexName, ""); - } catch (Exception e) { - // TODO Auto-generated catch block - graph.tx().rollback(); - graph.close(); - e.printStackTrace(); - } - - //mgmt = graph.getManagementSystem(); - //mgmt.updateIndex(mgmt.getGraphIndex(indexName), SchemaAction.REINDEX); - - //mgmt.updateIndex(mgmt.getGraphIndex(indexName), SchemaAction.ENABLE_INDEX); - - //mgmt.commit(); - - } - } - - /** - * Wait for completion. - * - * @param name the name - * @throws InterruptedException the interrupted exception - */ - private void waitForCompletion(String name) throws InterruptedException { - - boolean registered = false; - long before = System.currentTimeMillis(); - while (!registered) { - Thread.sleep(500L); - TitanManagement mgmt = graph.openManagement(); - TitanGraphIndex idx = mgmt.getGraphIndex(name); - registered = true; - for (PropertyKey k : idx.getFieldKeys()) { - SchemaStatus s = idx.getIndexStatus(k); - registered &= s.equals(SchemaStatus.REGISTERED); - } - mgmt.rollback(); - } - System.out.println("Index REGISTERED in " + (System.currentTimeMillis() - before) + " ms"); - } - - /** - * Replace property. - * - * @param key the key - */ - private void replaceProperty(DBProperty key) { - - - - - } - - /** - * Update index. - * - * @param index the index - */ - public void updateIndex(DBIndex index) { - - TitanManagement mgmt = graph.openManagement(); - List<PropertyKey> keys = new ArrayList<>(); - boolean isNew = false; - boolean isChanged = false; - for (DBProperty prop : index.getProperties()) { - createProperty(mgmt, prop); - keys.add(mgmt.getPropertyKey(prop.getName())); - } - if (mgmt.containsGraphIndex(index.getName())) { - System.out.println("index already exists"); - isNew = false; - isChanged = true; - } else { - isNew = true; - isChanged = false; - } - this.createIndex(mgmt, index.getName(), keys, index.isUnique(), isNew, isChanged); - - mgmt.commit(); - - } - - - - - + if (isNew || isChanged) { + + if (isNew) { + IndexBuilder builder = mgmt.buildIndex(indexName, Vertex.class); + for (PropertyKey k : keys) { + builder.addKey(k); + } + if (isUnique) { + builder.unique(); + } + builder.buildCompositeIndex(); + log.info(String.format("Built index for %s with keys: %s", indexName, keys)); + + //mgmt.commit(); + } + + //mgmt = graph.getManagementSystem(); + //mgmt.updateIndex(mgmt.getGraphIndex(indexName), SchemaAction.REGISTER_INDEX); + //mgmt.commit(); + + try { + //waitForCompletion(indexName); + //TitanIndexRepair.hbaseRepair(AAIConstants.AAI_CONFIG_FILENAME, indexName, ""); + } catch (Exception e) { + // TODO Auto-generated catch block + graph.tx().rollback(); + graph.close(); + log.error(e.getLocalizedMessage(), e); + } + + //mgmt = graph.getManagementSystem(); + //mgmt.updateIndex(mgmt.getGraphIndex(indexName), SchemaAction.REINDEX); + + //mgmt.updateIndex(mgmt.getGraphIndex(indexName), SchemaAction.ENABLE_INDEX); + + //mgmt.commit(); + + } + } + + /** + * Wait for completion. + * + * @param name the name + * @throws InterruptedException the interrupted exception + */ + private void waitForCompletion(String name) throws InterruptedException { + + boolean registered = false; + long before = System.currentTimeMillis(); + while (!registered) { + Thread.sleep(500L); + TitanManagement mgmt = graph.openManagement(); + TitanGraphIndex idx = mgmt.getGraphIndex(name); + registered = true; + for (PropertyKey k : idx.getFieldKeys()) { + SchemaStatus s = idx.getIndexStatus(k); + registered &= s.equals(SchemaStatus.REGISTERED); + } + mgmt.rollback(); + } + log.info("Index REGISTERED in " + (System.currentTimeMillis() - before) + " ms"); + } + + /** + * Replace property. + * + * @param key the key + */ + private void replaceProperty(DBProperty key) { + + } + + /** + * Update index. + * + * @param index the index + */ + public void updateIndex(DBIndex index) { + + TitanManagement mgmt = graph.openManagement(); + List<PropertyKey> keys = new ArrayList<>(); + boolean isNew = false; + boolean isChanged = false; + for (DBProperty prop : index.getProperties()) { + createProperty(mgmt, prop); + keys.add(mgmt.getPropertyKey(prop.getName())); + } + if (mgmt.containsGraphIndex(index.getName())) { + log.info("index already exists"); + isNew = false; + isChanged = true; + } else { + isNew = true; + isChanged = false; + } + this.createIndex(mgmt, index.getName(), keys, index.isUnique(), isNew, isChanged); + + mgmt.commit(); + } } diff --git a/ajsc-aai/src/main/java/org/openecomp/aai/db/schema/Named.java b/ajsc-aai/src/main/java/org/openecomp/aai/db/schema/Named.java index 850af57..7e3d233 100644 --- a/ajsc-aai/src/main/java/org/openecomp/aai/db/schema/Named.java +++ b/ajsc-aai/src/main/java/org/openecomp/aai/db/schema/Named.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -22,10 +22,10 @@ package org.openecomp.aai.db.schema; public interface Named { - /** - * Gets the name. - * - * @return the name - */ - public String getName(); + /** + * Gets the name. + * + * @return the name + */ + String getName(); } |