643517d9e93e33ede8dbdab71548c48e1926f262
[aai/resources.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright (C) 2017 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.migration.v12;
22
23 import java.util.Optional;
24
25 import org.apache.commons.lang.exception.ExceptionUtils;
26 import org.apache.tinkerpop.gremlin.structure.Edge;
27 import org.onap.aai.migration.MigrationDangerRating;
28 import org.onap.aai.migration.Enabled;
29 import org.onap.aai.migration.MigrationPriority;
30 import org.onap.aai.migration.Migrator;
31 import org.onap.aai.migration.Status;
32 import org.onap.aai.serialization.db.AAIDirection;
33 import org.onap.aai.serialization.db.EdgeProperty;
34 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
35
36
37
38 @Enabled
39 @MigrationPriority(-100)
40 @MigrationDangerRating(10)
41 public class ContainmentDeleteOtherVPropertyMigration extends Migrator {
42
43         private boolean success = true;
44         
45         public ContainmentDeleteOtherVPropertyMigration(TransactionalGraphEngine engine) {
46                 super(engine);
47         }
48         
49         //just for testing using test edge rule files
50         public ContainmentDeleteOtherVPropertyMigration(TransactionalGraphEngine engine, String edgeRulesFile) {
51                 super(engine);
52         }
53         
54         @Override
55         public void run() {
56                 try {
57                         engine.asAdmin().getTraversalSource().E().sideEffect(t -> {
58                                 Edge e = t.get();
59                                 logger.info("out vertex: " + e.outVertex().property("aai-node-type").value() + 
60                                                                 " in vertex: " + e.inVertex().property("aai-node-type").value() +
61                                                                 " label : " + e.label());
62                                 if (e.property(EdgeProperty.CONTAINS.toString()).isPresent() &&
63                                                 e.property(EdgeProperty.DELETE_OTHER_V.toString()).isPresent()) {
64                                         //in case of orphans
65                                         if (!("constrained-element-set".equals(e.inVertex().property("aai-node-type").value())
66                                                         && "model-element".equals(e.outVertex().property("aai-node-type").value()))) {
67                                                 //skip the weird horrible problem child edge
68                                                 String containment = (String) e.property(EdgeProperty.CONTAINS.toString()).value();
69                                                 if (AAIDirection.OUT.toString().equalsIgnoreCase(containment) ||
70                                                                 AAIDirection.IN.toString().equalsIgnoreCase(containment) ||
71                                                                 AAIDirection.BOTH.toString().equalsIgnoreCase(containment)) {
72                                                         logger.info("updating delete-other-v property");
73                                                         e.property(EdgeProperty.DELETE_OTHER_V.toString(), containment);
74                                                 }
75                                         }
76                                 }
77                         }).iterate();
78                 } catch (Exception e) {
79                         logger.info("error encountered " + e.getClass() + " " + e.getMessage() + " " + ExceptionUtils.getFullStackTrace(e));
80                         logger.error("error encountered " + e.getClass() + " " + e.getMessage() + " " + ExceptionUtils.getFullStackTrace(e));
81                         success = false;
82                 }
83                 
84         }
85
86         @Override
87         public Status getStatus() {
88                 if (success) {
89                         return Status.SUCCESS;
90                 } else {
91                         return Status.FAILURE;
92                 }
93         }
94
95         @Override
96         public Optional<String[]> getAffectedNodeTypes() {
97                 return Optional.empty();
98         }
99
100         @Override
101         public String getMigrationName() {
102                 return "migrate-containment-delete-other-v";
103         }
104
105 }