From 161147bd76f0c1b07227a6c37177e5d2add6e23b Mon Sep 17 00:00:00 2001 From: Venkata Harish K Kajur Date: Wed, 16 Aug 2017 08:51:43 -0400 Subject: [AAI-177 Amsterdam] Remove edge migrator from list Change-Id: I2dc3a9ae57e41a6258468536e2598e64471e144a Signed-off-by: Venkata Harish K Kajur --- .../org/openecomp/aai/migration/EdgeMigrator.java | 162 +++++++++++++++++++++ .../aai/migration/MigrationControllerInternal.java | 6 + 2 files changed, 168 insertions(+) create mode 100644 aai-resources/src/main/java/org/openecomp/aai/migration/EdgeMigrator.java (limited to 'aai-resources/src/main/java') diff --git a/aai-resources/src/main/java/org/openecomp/aai/migration/EdgeMigrator.java b/aai-resources/src/main/java/org/openecomp/aai/migration/EdgeMigrator.java new file mode 100644 index 0000000..dccee23 --- /dev/null +++ b/aai-resources/src/main/java/org/openecomp/aai/migration/EdgeMigrator.java @@ -0,0 +1,162 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.aai.migration; + +import java.util.Map; +import java.util.List; +import java.util.ArrayList; +import org.javatuples.Pair; + +import org.apache.tinkerpop.gremlin.structure.Edge; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.openecomp.aai.db.props.AAIProperties; +import org.openecomp.aai.serialization.engines.TransactionalGraphEngine; +import org.openecomp.aai.serialization.db.EdgeRule; +import org.openecomp.aai.serialization.db.EdgeRules; + +/** + * A migration template for migrating all edge properties between "from" and "to" node from the DbedgeRules.json + * + */ +public abstract class EdgeMigrator extends Migrator { + + private boolean success = true; + private EdgeRules rules; + + public EdgeMigrator() { + // used for not great reflection implementation + super(); + } + + public EdgeMigrator(TransactionalGraphEngine engine) { + super(engine); + rules = EdgeRules.getInstance(); + } + + public EdgeMigrator(TransactionalGraphEngine engine, List> nodePairList) { + super(engine); + rules = EdgeRules.getInstance(); + } + + + /** + * Do not override this method as an inheritor of this class + */ + @Override + public void run() { + + executeModifyOperation(); + + } + + /** + * This is where inheritors should add their logic + */ + protected void executeModifyOperation() { + + changeEdgeProperties(); + + } + + protected void changeEdgeLabels() { + //TODO: when json file has edge label as well as edge property changes + } + + + + protected void changeEdgeProperties() { + try { + List> nodePairList = this.getAffectedNodePairTypes(); + for (Pair nodePair : nodePairList) { + + String NODE_A = nodePair.getValue0(); + String NODE_B = nodePair.getValue1(); + Map result = rules.getEdgeRules(NODE_A, NODE_B); + + GraphTraversal g = this.engine.asAdmin().getTraversalSource().V(); + /* + * Find Out-Edges from Node A to Node B and change them + * Also Find Out-Edges from Node B to Node A and change them + */ + g.union(__.has(AAIProperties.NODE_TYPE, NODE_A).outE().where(__.inV().has(AAIProperties.NODE_TYPE, NODE_B)), + __.has(AAIProperties.NODE_TYPE, NODE_B).outE().where(__.inV().has(AAIProperties.NODE_TYPE, NODE_A))) + .sideEffect(t -> { + Edge e = t.get(); + try { + Vertex out = e.outVertex(); + Vertex in = e.inVertex(); + if (out == null || in == null) { + logger.error( + e.id() + " invalid because one vertex was null: out=" + out + " in=" + in); + } else { + if (result.containsKey(e.label())) { + EdgeRule rule = result.get(e.label()); + e.properties().forEachRemaining(prop -> prop.remove()); + rules.addProperties(e, rule); + } else { + logger.info("found vertices connected by unkwown label: out=" + out + " label=" + + e.label() + " in=" + in); + } + } + } catch (Exception e1) { + throw new RuntimeException(e1); + } + }).iterate(); + } + + } catch (Exception e) { + logger.error("error encountered", e); + success = false; + } + } + + @Override + public Status getStatus() { + if (success) { + return Status.SUCCESS; + } else { + return Status.FAILURE; + } + } + + @Override + public int getPriority() { + return 0; + } + + /* + * Higher danger rating of 10 only for all edge property changes + * or when a quorum of edges change which can be overridden by inheritors + */ + @Override + public int getDangerRating() { + return 1; + } + + /** + * List of node pairs("from" and "to"), you would like EdgeMigrator to migrate from json files + * @return + */ + public abstract List> getAffectedNodePairTypes() ; + +} diff --git a/aai-resources/src/main/java/org/openecomp/aai/migration/MigrationControllerInternal.java b/aai-resources/src/main/java/org/openecomp/aai/migration/MigrationControllerInternal.java index 875d7cb..be9b68d 100644 --- a/aai-resources/src/main/java/org/openecomp/aai/migration/MigrationControllerInternal.java +++ b/aai-resources/src/main/java/org/openecomp/aai/migration/MigrationControllerInternal.java @@ -227,7 +227,13 @@ public class MigrationControllerInternal { } private Set> findClasses(Reflections reflections) { Set> migratorClasses = reflections.getSubTypesOf(Migrator.class); + /* + * TODO- Change this to make sure only classes in the specific $release are added in the runList + * Or add a annotation like exclude which folks again need to remember to add ?? + */ + migratorClasses.remove(PropertyMigrator.class); + migratorClasses.remove(EdgeMigrator.class); return migratorClasses; } -- cgit 1.2.3-korg