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.enums.LifecycleStateEnum;
25 import org.openecomp.sdc.asdctool.migration.core.DBVersion;
26 import org.openecomp.sdc.asdctool.migration.core.task.Migration;
27 import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult;
28 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
29 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
30 import org.openecomp.sdc.be.config.ConfigurationManager;
31 import org.openecomp.sdc.be.dao.api.ActionStatus;
32 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
33 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
34 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
35 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
36 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
37 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
38 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
39 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
40 import org.openecomp.sdc.be.model.User;
41 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
42 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
43 import org.openecomp.sdc.be.model.operations.impl.UserAdminOperation;
44 import org.openecomp.sdc.common.log.wrappers.Logger;
45 import org.openecomp.sdc.exception.ResponseFormat;
46 import org.springframework.stereotype.Component;
48 import java.math.BigInteger;
49 import java.util.EnumMap;
50 import java.util.List;
54 public class ResourceLifecycleMigration implements Migration {
56 private JanusGraphDao janusGraphDao;
57 private LifecycleBusinessLogic lifecycleBusinessLogic;
58 private UserAdminOperation userAdminOperation;
60 private User user = null;
62 private static final Logger log = Logger.getLogger(ResourceLifecycleMigration.class);
64 public ResourceLifecycleMigration(JanusGraphDao janusGraphDao, LifecycleBusinessLogic lifecycleBusinessLogic, UserAdminOperation userAdminOperation) {
65 this.janusGraphDao = janusGraphDao;
66 this.lifecycleBusinessLogic = lifecycleBusinessLogic;
67 this.userAdminOperation = userAdminOperation;
71 public String description() {
72 return "change resource lifecycle state from testing to certified";
76 public DBVersion getVersion() {
77 return DBVersion.from(BigInteger.valueOf(1806), BigInteger.valueOf(0));
81 public MigrationResult migrate() {
82 log.info("start change resource lifecycle states migration");
83 final String userId = ConfigurationManager.getConfigurationManager().getConfiguration().getAutoHealingOwner();
85 Either<User, ActionStatus> userReq = userAdminOperation.getUserData(userId, false);
86 if (userReq.isRight()) {
87 log.error("Upgrade migration failed. User {} resolve failed: {} ", userId, userReq.right().value());
88 return MigrationResult.error("failed to change lifecycle state of resources. Failed to resolve user : " + userId + " error " + userReq.right().value());
90 user = userReq.left().value();
91 log.info("User {} will perform upgrade operation with role {}", user.getUserId(), user.getRole());
94 StorageOperationStatus status = changeResourceLifecycleState();
96 return status == StorageOperationStatus.OK ? MigrationResult.success() : MigrationResult.error("failed to change lifecycle state of resources. Error : " + status);
99 private StorageOperationStatus changeResourceLifecycleState() {
100 StorageOperationStatus status;
101 status = findResourcesAndChangeStatus(VertexTypeEnum.NODE_TYPE);
102 if (StorageOperationStatus.OK == status) {
103 status = findResourcesAndChangeStatus(VertexTypeEnum.TOPOLOGY_TEMPLATE);
105 janusGraphDao.commit();
109 private StorageOperationStatus findResourcesAndChangeStatus(VertexTypeEnum type) {
110 StorageOperationStatus status;
111 Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
112 props.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
113 props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.READY_FOR_CERTIFICATION.name());
114 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
116 Map<GraphPropertyEnum, Object> hasNot = new EnumMap<>(GraphPropertyEnum.class);
117 hasNot.put(GraphPropertyEnum.IS_DELETED, true);
119 log.info("findResourcesAndChangeStatus for type {} and state {}", type ,LifecycleStateEnum.READY_FOR_CERTIFICATION);
120 status = janusGraphDao.getByCriteria(type, props, hasNot, JsonParseFlagEnum.ParseAll).either(this::changeState, this::handleError);
121 log.info("status {} for type {} and state {}", status, type ,LifecycleStateEnum.READY_FOR_CERTIFICATION);
123 log.info("findResourcesAndChangeStatus for type {} and state {}", type ,LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
124 props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFICATION_IN_PROGRESS.name());
125 status = janusGraphDao.getByCriteria(type, props, hasNot, JsonParseFlagEnum.ParseAll).either(this::changeState, this::handleError);
126 log.info("status {} for type {} and state {}", status, type ,LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
132 private StorageOperationStatus changeState(List<GraphVertex> resourcesV) {
133 StorageOperationStatus status = StorageOperationStatus.OK;
135 for (GraphVertex resourceV : resourcesV) {
136 status = changeResourceState(resourceV);
137 if (status != StorageOperationStatus.OK) {
138 log.info("Failed to change state to certified of resource with id {} , continue to next, reset status", resourceV.getUniqueId() );
139 status = StorageOperationStatus.OK;
145 private StorageOperationStatus changeResourceState(GraphVertex resourceV) {
146 log.debug("Change state to certified to resource with id {} ", resourceV.getUniqueId() );
148 LifecycleChangeInfoWithAction changeInfo = new LifecycleChangeInfoWithAction("change resource state by migration");
149 final Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> changeComponentState = lifecycleBusinessLogic.changeComponentState(ComponentTypeEnum.RESOURCE, resourceV.getUniqueId(), user, LifeCycleTransitionEnum.CERTIFY, changeInfo, false, true);
150 return changeComponentState.isLeft() ? StorageOperationStatus.OK : StorageOperationStatus.GENERAL_ERROR;
153 private StorageOperationStatus handleError(JanusGraphOperationStatus err) {
154 log.debug("receive janusgraph error {}", err);
155 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(
156 JanusGraphOperationStatus.NOT_FOUND == err ? JanusGraphOperationStatus.OK : err);