2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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;
23 import fj.data.Either;
24 import org.apache.tinkerpop.gremlin.structure.Direction;
25 import org.apache.tinkerpop.gremlin.structure.Edge;
26 import org.janusgraph.core.JanusGraphVertex;
27 import org.openecomp.sdc.asdctool.migration.tasks.mig2002.SdcCollapsingRolesRFCstateMigration;
28 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
29 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
30 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
31 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
32 import org.openecomp.sdc.be.dao.jsongraph.types.EdgePropertyEnum;
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.datatypes.enums.GraphPropertyEnum;
36 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
37 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
38 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.EnumMap;
45 import java.util.HashMap;
46 import java.util.Iterator;
47 import java.util.List;
50 public abstract class InstanceMigrationBase {
52 private static final Logger log = LoggerFactory.getLogger(InstanceMigrationBase.class);
53 protected JanusGraphDao janusGraphDao;
55 public InstanceMigrationBase(JanusGraphDao janusGraphDao) {
56 this.janusGraphDao = janusGraphDao;
59 protected StorageOperationStatus upgradeTopologyTemplates() {
60 Map<GraphPropertyEnum, Object> hasNotProps = new EnumMap<>(GraphPropertyEnum.class);
61 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
62 hasNotProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.CVFC);
64 return janusGraphDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, null, hasNotProps, JsonParseFlagEnum.ParseAll)
65 .either(this::proceed, this::handleError);
68 protected abstract StorageOperationStatus handleOneContainer(GraphVertex containerV);
70 protected StorageOperationStatus proceed(List<GraphVertex> containersV) {
71 int failureCounter = 0;
72 log.info("found {} vertices to migrate ", containersV.size());
73 for (GraphVertex container : containersV) {
74 StorageOperationStatus storageOperationStatus = handleOneContainer(container);
75 if (storageOperationStatus != StorageOperationStatus.OK) {
80 if (failureCounter > 0) {
81 log.info("Failed to update {} vertices", failureCounter);
83 log.info("All vertices were successfully updated");
86 return StorageOperationStatus.OK;
89 protected GraphVertex getVertexById(String vertexId) {
90 Either<GraphVertex, JanusGraphOperationStatus> vertexById = janusGraphDao.getVertexById(vertexId);
91 if (vertexById.isRight()) {
92 log.info("Exception occurred while query vertexId: {} exception: {} " + vertexId + vertexById.right().value());
95 else return vertexById.left().value();
98 protected StorageOperationStatus updateVertexAndCommit(GraphVertex graphVertex) {
99 StorageOperationStatus status;
100 if ((status = janusGraphDao.updateVertex(graphVertex)
101 .either(v -> StorageOperationStatus.OK, this::handleError)) != StorageOperationStatus.OK) {
104 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(janusGraphDao.commit());
107 protected StorageOperationStatus handleError(JanusGraphOperationStatus err) {
108 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(JanusGraphOperationStatus.NOT_FOUND == err ? JanusGraphOperationStatus.OK : err);
111 protected void removeEdges(Iterator<Edge> edges) {
113 while (edges.hasNext()) {
114 Edge edge = edges.next();
119 protected void removeEdgesInState(Iterator<Edge> edges, String state) {
121 while (edges.hasNext()) {
122 Edge edge = edges.next();
123 String edgeState = (String) janusGraphDao.getProperty(edge, EdgePropertyEnum.STATE);
124 if (edgeState.equals(state)) {
131 protected void updateEdgeProperty(EdgePropertyEnum property, String value, Iterator<Edge> edges) throws IOException {
132 while (edges.hasNext()) {
133 Edge edge = edges.next();
134 Map<EdgePropertyEnum, Object> prop = new HashMap<>();
135 prop.put(property, value);
136 janusGraphDao.setEdgeProperties(edge, prop);
142 // check if user has both edges state and last_state
143 protected boolean sameUser(List<JanusGraphVertex> stateList, List<JanusGraphVertex> lastStateList) {
145 for (JanusGraphVertex lsVertex : lastStateList) {
146 String idLs = (String) janusGraphDao.getProperty(lsVertex, GraphPropertyEnum.USERID.getProperty());
147 String idSt = (String) janusGraphDao.getProperty(stateList.get(0), GraphPropertyEnum.USERID.getProperty());
148 if (idLs.equals(idSt)) {
155 protected List<JanusGraphVertex> getVertexByEdgeSide(Iterator<Edge> edges, SdcCollapsingRolesRFCstateMigration.EdgeSide side) {
156 List<JanusGraphVertex> vertexList = new ArrayList();
157 while (edges.hasNext()) {
158 Edge edge = edges.next();
160 if (side == SdcCollapsingRolesRFCstateMigration.EdgeSide.OUT) {
161 vertexList.add((JanusGraphVertex) edge.outVertex());
163 vertexList.add((JanusGraphVertex) edge.inVertex());
170 protected Iterator<Edge> getVertexEdge(GraphVertex containerV, Direction direction, EdgeLabelEnum edgeLabel) {
171 return containerV.getVertex().edges(direction, edgeLabel.name());
174 public enum EdgeSide {