summaryrefslogtreecommitdiffstats
path: root/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/migration/v1707/Migration1707VnfFix.java
blob: d3bd494a85fd1240beab73c171f5146ad7b294d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package org.openecomp.sdc.asdctool.impl.migration.v1707;

import java.util.EnumMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
import org.openecomp.sdc.be.dao.jsongraph.utils.JsonParserUtils;
import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
import org.openecomp.sdc.be.model.LifecycleStateEnum;
import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.thinkaurelius.titan.core.TitanVertex;

import fj.data.Either;

@Component("migration1707vnfFix")
public class Migration1707VnfFix{

	private static final String VF_MODULES_METADATA = "vfModulesMetadata";

	@Autowired
	private TitanDao titanDao;

	@Autowired
	private TopologyTemplateOperation topologyTemplateOperation;

	private static Logger LOGGER = LoggerFactory.getLogger(Migration1707RelationsFix.class);

	public boolean migrate() {
		boolean result = true;

		Map<GraphPropertyEnum, Object> propsHasNot = new EnumMap<>(GraphPropertyEnum.class);
		propsHasNot.put(GraphPropertyEnum.IS_DELETED, true);

		Map<GraphPropertyEnum, Object> propsHas = new EnumMap<>(GraphPropertyEnum.class);
		propsHas.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
		propsHas.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF.name());
		propsHas.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED);

		Either<List<GraphVertex>, TitanOperationStatus> getAllTopologyTemplatesRes = titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, null, propsHasNot, JsonParseFlagEnum.ParseMetadata);
		if (getAllTopologyTemplatesRes.isRight() && getAllTopologyTemplatesRes.right().value() != TitanOperationStatus.NOT_FOUND) {
			LOGGER.debug("Failed to fetch all non marked as deleted topology templates , error {}", getAllTopologyTemplatesRes.right().value());
			result = false;
		}
		List<GraphVertex> metadataVertices = getAllTopologyTemplatesRes.left().value();
		for (GraphVertex metadataV : metadataVertices) {
			Either<Map<String, ArtifactDataDefinition>, TitanOperationStatus> dataFromGraph = topologyTemplateOperation.getDataFromGraph(metadataV.getUniqueId(), EdgeLabelEnum.DEPLOYMENT_ARTIFACTS);
			if (dataFromGraph.isLeft()) {
				Map<String, ArtifactDataDefinition> artifacts = dataFromGraph.left().value();
				if (artifacts.containsKey(VF_MODULES_METADATA)) {
					artifacts.remove(VF_MODULES_METADATA);
					Either<GraphVertex, TitanOperationStatus> vertexById = titanDao.getVertexById(metadataV.getUniqueId());
					TitanVertex vertex = vertexById.left().value().getVertex();
					Iterator<Edge> edges = vertex.edges(Direction.OUT, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS.name());
					if (edges.hasNext()) {
						Edge edge = edges.next();
						Vertex dataV = edge.inVertex();

						String jsonStr;
						try {
							jsonStr = JsonParserUtils.jsonToString(artifacts);
							dataV.property(GraphPropertyEnum.JSON.getProperty(), jsonStr);
						} catch (Exception e) {
							LOGGER.debug("Failed to update deployment artifacts for VF {}", metadataV.getUniqueId());
						}
					}
				}
			}
			TitanOperationStatus commit = titanDao.commit();
			if ( commit != TitanOperationStatus.OK){
				LOGGER.debug("Failed to commit changes for deployment artifacts for VF {} {}", metadataV.getUniqueId(), metadataV.getMetadataProperty(GraphPropertyEnum.NAME));
			}
		}

		return result;
	}

	public String description() {
		// TODO Auto-generated method stub
		return null;
	}

}