76c66de1c9082cc2a66e733cd660d7e4b8b79ce5
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.impl.migration.v1707.jsonmodel;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.asdctool.impl.migration.Migration1707Task;
25 import org.openecomp.sdc.asdctool.impl.migration.MigrationMsg;
26 import org.openecomp.sdc.asdctool.impl.migration.v1707.MigrationUtils;
27 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import javax.annotation.Resource;
32 import java.util.List;
33
34 public abstract class JsonModelMigration<T> implements Migration1707Task {
35
36     private static final Logger LOGGER = LoggerFactory.getLogger(JsonModelMigration.class);
37
38     @Resource(name = "titan-dao")
39     TitanDao titanDao;
40
41     @Override
42     public boolean migrate() {
43         Either<List<T>, ?> elementsToMigrate = getElementsToMigrate();
44         return elementsToMigrate.either(this::migrateElementsToNewGraph,
45                                         errorStatus -> MigrationUtils.handleError(MigrationMsg.FAILED_TO_RETRIEVE_NODES.getMessage(errorStatus.toString())));
46     }
47
48     boolean doPostSaveOperation(T element) {
49         return true;
50     }
51
52     boolean doPostMigrateOperation(List<T> elements) {
53         return true;
54     }
55
56     void doPreMigrationOperation(List<T> elements){}
57
58     private boolean migrateElementsToNewGraph(List<T> elementsToMigrate) {
59         LOGGER.info(this.description() + ": starting to migrate elements to new graph. elements to migrate: {}", elementsToMigrate.size());
60         doPreMigrationOperation(elementsToMigrate);
61         for (T node : elementsToMigrate) {
62             boolean migratedSuccessfully = migrateElement(node);
63             if (!migratedSuccessfully) {
64                 titanDao.rollback();
65                 return false;
66             }
67             titanDao.commit();
68         }
69         return postMigrate(elementsToMigrate);
70     }
71
72     private boolean migrateElement(T node) {
73         boolean savedSuccessfully = saveElementIfNotExists(node);
74         return savedSuccessfully && doPostSaveOperation(node);
75     }
76
77     private boolean postMigrate(List<T> elements) {
78         boolean postMigrateSuccessfully = doPostMigrateOperation(elements);
79         if (!postMigrateSuccessfully) {
80             titanDao.rollback();
81             return false;
82         }
83         titanDao.commit();
84         return true;
85     }
86
87     private boolean saveElementIfNotExists(T element) {
88         return isExists(element).either(isExist -> isExist || save(element),
89                                         status -> MigrationUtils.handleError(MigrationMsg.FAILED_TO_GET_NODE_FROM_GRAPH.getMessage(status.toString())));
90     }
91
92
93     private Either<Boolean, ?> isExists(T element) {
94         Either<T, ?> byId = getElementFromNewGraph(element);
95         return byId.either(existingVal -> Either.left(true),
96                            this::getEitherNotExistOrErrorStatus);
97     }
98
99     private <S> Either<Boolean, S> getEitherNotExistOrErrorStatus(S status) {
100         return status == getNotFoundErrorStatus() ? Either.left(false) : Either.right(status);
101     }
102
103     abstract Either<List<T>, ?> getElementsToMigrate();
104
105     abstract Either<T, ?> getElementFromNewGraph(T element);
106
107     abstract boolean save(T element);
108
109     abstract <S extends Enum> S getNotFoundErrorStatus();
110
111
112 }