Added oparent to sdc main
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / migration / tasks / mig1806 / SdcArchiveMigration.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.mig1806;
22
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;
35
36 import java.math.BigInteger;
37
38 @Component
39 public class SdcArchiveMigration implements Migration {
40     private static final Logger logger = Logger.getLogger(SdcArchiveMigration.class);
41
42     private JanusGraphDao janusGraphDao;
43
44     public SdcArchiveMigration(JanusGraphDao janusGraphDao) {
45         this.janusGraphDao = janusGraphDao;
46     }
47
48     @Override
49     public String description() {
50         return "add archive node for archiving/restoring components ";
51     }
52
53     @Override
54     public DBVersion getVersion() {
55         return DBVersion.from(BigInteger.valueOf(1806), BigInteger.valueOf(0));
56     }
57
58     @Override
59     public MigrationResult migrate() {
60         JanusGraphOperationStatus status = null;
61         try {
62             status = getOrCreateArchiveRoot();
63             return status == JanusGraphOperationStatus.OK ? MigrationResult.success() : MigrationResult.error("failed to create archive root node. error: " + status);
64         } finally {
65             commitOrRollBack(status);
66         }
67     }
68
69     private void commitOrRollBack(JanusGraphOperationStatus status) {
70         if (status == JanusGraphOperationStatus.OK) {
71             janusGraphDao.commit();
72         } else {
73             janusGraphDao.rollback();
74         }
75     }
76
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());
81     }
82
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());
88
89         logger.info("Creating root archive vertex {}", archiveRootVertex.getUniqueId());
90
91         final Either<GraphVertex, JanusGraphOperationStatus> vertexE = janusGraphDao.createVertex(archiveRootVertex);
92
93         return vertexE.isLeft() ? JanusGraphOperationStatus.OK : vertexE.right().value();
94     }
95
96 }