Catalog alignment
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / migration / tasks / InstanceMigrationBase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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;
22
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;
41
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;
48 import java.util.Map;
49
50 public abstract class InstanceMigrationBase {
51
52     private static final Logger log = LoggerFactory.getLogger(InstanceMigrationBase.class);
53     protected JanusGraphDao janusGraphDao;
54
55     public InstanceMigrationBase(JanusGraphDao janusGraphDao) {
56         this.janusGraphDao = janusGraphDao;
57     }
58
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);
63
64         return janusGraphDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, null, hasNotProps, JsonParseFlagEnum.ParseAll)
65                 .either(this::proceed, this::handleError);
66     }
67
68     protected abstract StorageOperationStatus handleOneContainer(GraphVertex containerV);
69
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) {
76                 failureCounter++;
77             }
78         }
79
80         if (failureCounter > 0) {
81             log.info("Failed to update {} vertices", failureCounter);
82         } else {
83             log.info("All vertices were successfully updated");
84         }
85
86         return StorageOperationStatus.OK;
87     }
88
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());
93             return null;
94         }
95         else return vertexById.left().value();
96     }
97
98     protected StorageOperationStatus updateVertexAndCommit(GraphVertex graphVertex) {
99         StorageOperationStatus status;
100         if ((status = janusGraphDao.updateVertex(graphVertex)
101                 .either(v -> StorageOperationStatus.OK, this::handleError)) != StorageOperationStatus.OK) {
102             return status;
103         }
104         return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(janusGraphDao.commit());
105     }
106
107     protected StorageOperationStatus handleError(JanusGraphOperationStatus err) {
108         return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(JanusGraphOperationStatus.NOT_FOUND == err ? JanusGraphOperationStatus.OK : err);
109     }
110
111     protected void removeEdges(Iterator<Edge> edges) {
112
113         while (edges.hasNext()) {
114             Edge edge = edges.next();
115             edge.remove();
116         }
117     }
118
119     protected void removeEdgesInState(Iterator<Edge> edges, String state) {
120
121         while (edges.hasNext()) {
122             Edge edge = edges.next();
123             String edgeState = (String) janusGraphDao.getProperty(edge, EdgePropertyEnum.STATE);
124             if (edgeState.equals(state)) {
125                 edge.remove();
126             }
127         }
128     }
129
130
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);
137         }
138
139     }
140
141
142     // check if user has both edges state and last_state
143     protected boolean sameUser(List<JanusGraphVertex> stateList, List<JanusGraphVertex> lastStateList) {
144
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)) {
149                 return true;
150             }
151         }
152         return false;
153     }
154
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();
159
160             if (side == SdcCollapsingRolesRFCstateMigration.EdgeSide.OUT) {
161                 vertexList.add((JanusGraphVertex) edge.outVertex());
162             } else {
163                 vertexList.add((JanusGraphVertex) edge.inVertex());
164             }
165         }
166
167         return vertexList;
168     }
169
170     protected Iterator<Edge> getVertexEdge(GraphVertex containerV, Direction direction, EdgeLabelEnum edgeLabel) {
171         return containerV.getVertex().edges(direction, edgeLabel.name());
172     }
173
174     public enum EdgeSide {
175         IN, OUT;
176     }
177 }
178