Added oparent to sdc main
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / migration / tasks / mig1802 / SdcCatalogMigration.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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.openecomp.sdc.asdctool.migration.tasks.mig1802;
22
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;
42
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;
48
49 @Component
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);
53
54     private ToscaElementOperation toscaElementOperation;
55     private JanusGraphDao janusGraphDao;
56
57     public SdcCatalogMigration(TopologyTemplateOperation toscaElementOperation, JanusGraphDao janusGraphDao) {
58         this.toscaElementOperation = toscaElementOperation;
59         this.janusGraphDao = janusGraphDao;
60     }
61
62     @Override
63     public String description() {
64         return "optimize sdc catalog vertices";
65     }
66
67     @Override
68     public DBVersion getVersion() {
69         return DBVersion.from(BigInteger.valueOf(1802), BigInteger.valueOf(0));
70     }
71
72     @Override
73     public MigrationResult migrate() {
74         JanusGraphOperationStatus status = null;
75         try {
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);
80         } finally {
81             commitOrRollBack(status);
82         }
83     }
84
85     private void commitOrRollBack(JanusGraphOperationStatus status) {
86         if (status == JanusGraphOperationStatus.OK) {
87             janusGraphDao.commit();
88         } else {
89             janusGraphDao.rollback();
90         }
91     }
92
93     private Either<GraphVertex, JanusGraphOperationStatus> getOrCreateCatalogRoot() {
94         LOGGER.info("creating or getting catalog root vertex");
95         return janusGraphDao.getVertexByLabel(VertexTypeEnum.CATALOG_ROOT)
96                 .right()
97                 .bind(this::createRootCatalogVertexOrError);
98     }
99
100
101     private Either<GraphVertex, JanusGraphOperationStatus> createRootCatalogVertexOrError(JanusGraphOperationStatus janusGraphOperationStatus) {
102         return janusGraphOperationStatus == JanusGraphOperationStatus.NOT_FOUND ? createRootCatalogVertex() : Either.right(
103             janusGraphOperationStatus);
104     }
105
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);
111     }
112
113     private Either<List<GraphVertex>, JanusGraphOperationStatus> getAllCatalogVertices() {
114         LOGGER.info("fetching all catalog resources");
115         return toscaElementOperation.getListOfHighestComponents(ComponentTypeEnum.RESOURCE, EXCLUDE_TYPES, JsonParseFlagEnum.ParseMetadata)
116                 .right()
117                 .bind(this::errOrEmptyListIfNotFound)
118                 .left()
119                 .bind(this::getAllCatalogVertices);
120     }
121
122     private Either<List<GraphVertex>, JanusGraphOperationStatus> errOrEmptyListIfNotFound(JanusGraphOperationStatus err) {
123         return JanusGraphOperationStatus.NOT_FOUND.equals(err) ? Either.left(new ArrayList<>()) : Either.right(err);
124     }
125
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)
131                 .right()
132                 .bind(this::errOrEmptyListIfNotFound)
133                 .left()
134                 .map(allServiceVertices -> ListUtils.union(allServiceVertices, allResourceCatalogVertices));
135     }
136
137     private JanusGraphOperationStatus associateCatalogRootToCatalogElements(GraphVertex root) {
138         return getAllCatalogVertices()
139                 .either(catalogVertices -> associateCatalogRootToCatalogElements(root, catalogVertices),
140                         err -> err);
141     }
142
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;
155                 }
156                 LOGGER.debug("created edge from catalog root to element {}", catalogElement.getUniqueId());
157                 numOfCreatedEdges++;
158         }
159         LOGGER.info("number edges created: {}", numOfCreatedEdges);
160         return JanusGraphOperationStatus.OK;
161     }
162
163     private boolean edgeNotAlreadyExists(GraphVertex catalogElement) {
164         return !catalogElement.getVertex().edges(Direction.IN, EdgeLabelEnum.CATALOG_ELEMENT.name()).hasNext();
165     }
166
167
168 }