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.mig1806;
23 import fj.data.Either;
24 import org.openecomp.sdc.asdctool.migration.core.DBVersion;
25 import org.openecomp.sdc.asdctool.migration.core.task.Migration;
26 import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult;
27 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
28 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
29 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
30 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
31 import org.openecomp.sdc.be.dao.jsongraph.utils.IdBuilderUtils;
32 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
33 import org.openecomp.sdc.common.log.wrappers.Logger;
34 import org.springframework.stereotype.Component;
36 import java.math.BigInteger;
39 public class SdcArchiveMigration implements Migration {
40 private static final Logger logger = Logger.getLogger(SdcArchiveMigration.class);
42 private JanusGraphDao janusGraphDao;
44 public SdcArchiveMigration(JanusGraphDao janusGraphDao) {
45 this.janusGraphDao = janusGraphDao;
49 public String description() {
50 return "add archive node for archiving/restoring components ";
54 public DBVersion getVersion() {
55 return DBVersion.from(BigInteger.valueOf(1806), BigInteger.valueOf(0));
59 public MigrationResult migrate() {
60 JanusGraphOperationStatus status = null;
62 status = getOrCreateArchiveRoot();
63 return status == JanusGraphOperationStatus.OK ? MigrationResult.success() : MigrationResult.error("failed to create archive root node. error: " + status);
65 commitOrRollBack(status);
69 private void commitOrRollBack(JanusGraphOperationStatus status) {
70 if (status == JanusGraphOperationStatus.OK) {
71 janusGraphDao.commit();
73 janusGraphDao.rollback();
77 private JanusGraphOperationStatus getOrCreateArchiveRoot() {
78 logger.info("creating or getting catalog archive vertex");
79 return janusGraphDao.getVertexByLabel(VertexTypeEnum.ARCHIVE_ROOT)
80 .either(v -> JanusGraphOperationStatus.OK, s -> this.createRootArchiveVertex());
83 private JanusGraphOperationStatus createRootArchiveVertex() {
84 GraphVertex archiveRootVertex = new GraphVertex(VertexTypeEnum.ARCHIVE_ROOT);
85 archiveRootVertex.setUniqueId(IdBuilderUtils.generateUniqueId());
86 archiveRootVertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.ARCHIVE_ROOT);
87 archiveRootVertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, archiveRootVertex.getUniqueId());
89 logger.info("Creating root archive vertex {}", archiveRootVertex.getUniqueId());
91 final Either<GraphVertex, JanusGraphOperationStatus> vertexE = janusGraphDao.createVertex(archiveRootVertex);
93 return vertexE.isLeft() ? JanusGraphOperationStatus.OK : vertexE.right().value();