2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.migration.tasks.mig1802;
23 import fj.data.Either;
24 import org.apache.commons.collections.ListUtils;
25 import org.apache.tinkerpop.gremlin.structure.Direction;
26 import org.openecomp.sdc.asdctool.migration.core.DBVersion;
27 import org.openecomp.sdc.asdctool.migration.core.task.Migration;
28 import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult;
29 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
30 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
31 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
32 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
33 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
34 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
35 import org.openecomp.sdc.be.dao.jsongraph.utils.IdBuilderUtils;
36 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
37 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
38 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
39 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementOperation;
40 import org.openecomp.sdc.common.log.wrappers.Logger;
41 import org.springframework.stereotype.Component;
43 import java.math.BigInteger;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.List;
47 import java.util.stream.Collectors;
50 public class SdcCatalogMigration implements Migration {
51 private static final Logger LOGGER = Logger.getLogger(SdcCatalogMigration.class);
52 private static final List<ResourceTypeEnum> EXCLUDE_TYPES = Arrays.asList(ResourceTypeEnum.VFCMT, ResourceTypeEnum.Configuration);
54 private ToscaElementOperation toscaElementOperation;
55 private JanusGraphDao janusGraphDao;
57 public SdcCatalogMigration(TopologyTemplateOperation toscaElementOperation, JanusGraphDao janusGraphDao) {
58 this.toscaElementOperation = toscaElementOperation;
59 this.janusGraphDao = janusGraphDao;
63 public String description() {
64 return "optimize sdc catalog vertices";
68 public DBVersion getVersion() {
69 return DBVersion.from(BigInteger.valueOf(1802), BigInteger.valueOf(0));
73 public MigrationResult migrate() {
74 JanusGraphOperationStatus status = null;
76 status = getOrCreateCatalogRoot()
77 .either(this::associateCatalogRootToCatalogElements,
78 err -> {LOGGER.error("failed to create catalog root. err: {}", err); return err;});
79 return status == JanusGraphOperationStatus.OK ? MigrationResult.success() : MigrationResult.error("failed to create and associate catalog root. error: " + status);
81 commitOrRollBack(status);
85 private void commitOrRollBack(JanusGraphOperationStatus status) {
86 if (status == JanusGraphOperationStatus.OK) {
87 janusGraphDao.commit();
89 janusGraphDao.rollback();
93 private Either<GraphVertex, JanusGraphOperationStatus> getOrCreateCatalogRoot() {
94 LOGGER.info("creating or getting catalog root vertex");
95 return janusGraphDao.getVertexByLabel(VertexTypeEnum.CATALOG_ROOT)
97 .bind(this::createRootCatalogVertexOrError);
101 private Either<GraphVertex, JanusGraphOperationStatus> createRootCatalogVertexOrError(JanusGraphOperationStatus janusGraphOperationStatus) {
102 return janusGraphOperationStatus == JanusGraphOperationStatus.NOT_FOUND ? createRootCatalogVertex() : Either.right(
103 janusGraphOperationStatus);
106 private Either<GraphVertex, JanusGraphOperationStatus> createRootCatalogVertex() {
107 LOGGER.info("Creating root catalog vertex");
108 GraphVertex catalogRootVertex = new GraphVertex(VertexTypeEnum.CATALOG_ROOT);
109 catalogRootVertex.setUniqueId(IdBuilderUtils.generateUniqueId());
110 return janusGraphDao.createVertex(catalogRootVertex);
113 private Either<List<GraphVertex>, JanusGraphOperationStatus> getAllCatalogVertices() {
114 LOGGER.info("fetching all catalog resources");
115 return toscaElementOperation.getListOfHighestComponents(ComponentTypeEnum.RESOURCE, EXCLUDE_TYPES, JsonParseFlagEnum.ParseMetadata)
117 .bind(this::errOrEmptyListIfNotFound)
119 .bind(this::getAllCatalogVertices);
122 private Either<List<GraphVertex>, JanusGraphOperationStatus> errOrEmptyListIfNotFound(JanusGraphOperationStatus err) {
123 return JanusGraphOperationStatus.NOT_FOUND.equals(err) ? Either.left(new ArrayList<>()) : Either.right(err);
126 @SuppressWarnings("unchecked")
127 private Either<List<GraphVertex>, JanusGraphOperationStatus> getAllCatalogVertices(List<GraphVertex> allResourceCatalogVertices) {
128 LOGGER.info("number of resources: {}", allResourceCatalogVertices.size());
129 LOGGER.info("fetching all catalog services");
130 return toscaElementOperation.getListOfHighestComponents(ComponentTypeEnum.SERVICE, EXCLUDE_TYPES, JsonParseFlagEnum.ParseMetadata)
132 .bind(this::errOrEmptyListIfNotFound)
134 .map(allServiceVertices -> ListUtils.union(allServiceVertices, allResourceCatalogVertices));
137 private JanusGraphOperationStatus associateCatalogRootToCatalogElements(GraphVertex root) {
138 return getAllCatalogVertices()
139 .either(catalogVertices -> associateCatalogRootToCatalogElements(root, catalogVertices),
143 private JanusGraphOperationStatus associateCatalogRootToCatalogElements(GraphVertex root, List<GraphVertex> catalogElements) {
144 LOGGER.info("number of catalog elements: {}", catalogElements.size());
145 LOGGER.info("connect all catalog elements to root edge");
146 List<GraphVertex> nonConnectedElements = catalogElements.stream().filter(this::edgeNotAlreadyExists).collect(Collectors.toList());
147 int numOfCreatedEdges = 0;
148 for (GraphVertex catalogElement : nonConnectedElements) {
149 JanusGraphOperationStatus
150 edgeCreationStatus = janusGraphDao
151 .createEdge(root, catalogElement, EdgeLabelEnum.CATALOG_ELEMENT, null);
152 if (edgeCreationStatus != JanusGraphOperationStatus.OK) {
153 LOGGER.error("failed to create edge from catalog element to vertex {}", catalogElement.getUniqueId());
154 return edgeCreationStatus;
156 LOGGER.debug("created edge from catalog root to element {}", catalogElement.getUniqueId());
159 LOGGER.info("number edges created: {}", numOfCreatedEdges);
160 return JanusGraphOperationStatus.OK;
163 private boolean edgeNotAlreadyExists(GraphVertex catalogElement) {
164 return !catalogElement.getVertex().edges(Direction.IN, EdgeLabelEnum.CATALOG_ELEMENT.name()).hasNext();