2 * ============LICENSE_START=======================================================
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
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.impl.migration.v1707.jsonmodel;
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;
31 import javax.annotation.Resource;
32 import java.util.List;
34 public abstract class JsonModelMigration<T> implements Migration1707Task {
36 private static final Logger LOGGER = LoggerFactory.getLogger(JsonModelMigration.class);
38 @Resource(name = "titan-dao")
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())));
48 boolean doPostSaveOperation(T element) {
52 boolean doPostMigrateOperation(List<T> elements) {
56 void doPreMigrationOperation(List<T> elements){}
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) {
69 return postMigrate(elementsToMigrate);
72 private boolean migrateElement(T node) {
73 boolean savedSuccessfully = saveElementIfNotExists(node);
74 return savedSuccessfully && doPostSaveOperation(node);
77 private boolean postMigrate(List<T> elements) {
78 boolean postMigrateSuccessfully = doPostMigrateOperation(elements);
79 if (!postMigrateSuccessfully) {
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())));
93 private Either<Boolean, ?> isExists(T element) {
94 Either<T, ?> byId = getElementFromNewGraph(element);
95 return byId.either(existingVal -> Either.left(true),
96 this::getEitherNotExistOrErrorStatus);
99 private <S> Either<Boolean, S> getEitherNotExistOrErrorStatus(S status) {
100 return status == getNotFoundErrorStatus() ? Either.left(false) : Either.right(status);
103 abstract Either<List<T>, ?> getElementsToMigrate();
105 abstract Either<T, ?> getElementFromNewGraph(T element);
107 abstract boolean save(T element);
109 abstract <S extends Enum> S getNotFoundErrorStatus();