2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.asdctool.impl.migration.v1707;
23 import java.util.EnumMap;
24 import java.util.Iterator;
25 import java.util.List;
27 import org.apache.tinkerpop.gremlin.structure.Direction;
28 import org.apache.tinkerpop.gremlin.structure.Edge;
29 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
30 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
31 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
32 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
33 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
34 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
35 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
36 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
42 import fj.data.Either;
43 @Component("migration1707relationsFix")
44 public class Migration1707RelationsFix {
46 private static Logger LOGGER = LoggerFactory.getLogger(Migration1707RelationsFix.class);
49 private TitanDao titanDao;
51 public boolean migrate() {
52 boolean result = true;
55 Map<GraphPropertyEnum, Object> propsHasNot = new EnumMap<>(GraphPropertyEnum.class);
56 propsHasNot.put(GraphPropertyEnum.IS_DELETED, true);
57 Either<List<GraphVertex>, TitanOperationStatus> getAllTopologyTemplatesRes = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, null, propsHasNot, JsonParseFlagEnum.ParseMetadata);
58 if (getAllTopologyTemplatesRes.isRight() && getAllTopologyTemplatesRes.right().value() != TitanOperationStatus.NOT_FOUND) {
59 LOGGER.debug("Failed to fetch all non marked as deleted topology templates , error {}", getAllTopologyTemplatesRes.right().value());
62 if(getAllTopologyTemplatesRes.isLeft()){
63 fixComponentsRelations(getAllTopologyTemplatesRes.left().value());
66 Either<List<GraphVertex>, TitanOperationStatus> getAllNodeTypesRes = titanDao.getByCriteria(VertexTypeEnum.NODE_TYPE, null, propsHasNot, JsonParseFlagEnum.ParseMetadata);
67 if (getAllNodeTypesRes.isRight() && getAllNodeTypesRes.right().value() != TitanOperationStatus.NOT_FOUND) {
68 LOGGER.debug("Failed to fetch all non marked as deleted node types , error {}", getAllNodeTypesRes.right().value());
71 if(getAllNodeTypesRes.isLeft()){
72 fixComponentsRelations(getAllNodeTypesRes.left().value());
75 } catch (Exception e){
76 LOGGER.debug("The exception {} occured upon migration 1707 relations fixing. ", e.getMessage());
90 private void fixComponentsRelations(List<GraphVertex> notDeletedComponentVerticies) {
91 notDeletedComponentVerticies.stream().forEach(this::fixComponentRelations);
94 private void fixComponentRelations(GraphVertex componentV) {
95 fixCreatorComponentRelation(componentV);
96 fixLastModifierComponentRelation(componentV);
97 fixStateComponentRelation(componentV);
100 private void fixStateComponentRelation(GraphVertex componentV) {
101 boolean relevantEdgeFound = false;
102 Iterator<Edge> edges = componentV.getVertex().edges(Direction.IN, EdgeLabelEnum.STATE.name());
103 String getState = (String) componentV.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE);
104 while(edges.hasNext()){
105 Edge edge = edges.next();
106 String edgeState = (String) edge.property(GraphPropertyEnum.STATE.getProperty()).orElse(null);
107 if(getState.equals(edgeState) && !relevantEdgeFound){
108 relevantEdgeFound = true;
115 private void fixCreatorComponentRelation(GraphVertex componentV) {
116 boolean relevantUserFound = false;
117 Iterator<Edge> edges = componentV.getVertex().edges(Direction.IN, EdgeLabelEnum.CREATOR.name());
118 String getCreatorUserId = (String) componentV.getJsonMetadataField(JsonPresentationFields.USER_ID_CREATOR);
119 while(edges.hasNext()){
120 Edge edge = edges.next();
121 String userId = (String) edge.outVertex().property(GraphPropertyEnum.USERID.getProperty()).orElse(null);
122 if(getCreatorUserId.equals(userId) && !relevantUserFound){
123 relevantUserFound = true;
130 private void fixLastModifierComponentRelation(GraphVertex componentV) {
131 boolean relevantUserFound = false;
132 Iterator<Edge> edges = componentV.getVertex().edges(Direction.IN, EdgeLabelEnum.LAST_MODIFIER.name());
133 String getLastUpdaterUserId = (String) componentV.getJsonMetadataField(JsonPresentationFields.USER_ID_LAST_UPDATER);
134 while(edges.hasNext()){
135 Edge edge = edges.next();
136 String updaterId = (String) edge.outVertex().property(GraphPropertyEnum.USERID.getProperty()).orElse(null);
137 if(getLastUpdaterUserId.equals(updaterId) && !relevantUserFound){
138 relevantUserFound = true;
145 private void removeEdge(Edge edge) {
146 LOGGER.debug("Going to remove edge {} upon migration 1707 relations fixing. ", edge.id());
148 LOGGER.debug("The edge {} has been removed. ", edge.id());