Added oparent to sdc main
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / migration / tasks / mig1806 / ResourceLifecycleMigration.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.components.lifecycle.LifecycleBusinessLogic;
28 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
29 import org.openecomp.sdc.be.config.ConfigurationManager;
30 import org.openecomp.sdc.be.dao.api.ActionStatus;
31 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
32 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
33 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
34 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
35 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
36 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
37 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
38 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
39 import org.openecomp.sdc.be.model.LifecycleStateEnum;
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;
47
48 import java.math.BigInteger;
49 import java.util.EnumMap;
50 import java.util.List;
51 import java.util.Map;
52
53 @Component  
54 public class ResourceLifecycleMigration implements Migration {
55
56     private JanusGraphDao janusGraphDao;
57     private LifecycleBusinessLogic lifecycleBusinessLogic;
58     private UserAdminOperation userAdminOperation;
59     
60     private User user = null;
61
62     private static final Logger log = Logger.getLogger(ResourceLifecycleMigration.class);
63
64     public ResourceLifecycleMigration(JanusGraphDao janusGraphDao, LifecycleBusinessLogic lifecycleBusinessLogic, UserAdminOperation userAdminOperation) {
65         this.janusGraphDao = janusGraphDao;
66         this.lifecycleBusinessLogic = lifecycleBusinessLogic;
67         this.userAdminOperation = userAdminOperation;
68     }
69
70     @Override
71     public String description() {
72         return "change resource lifecycle state from testing to certified";
73     }
74
75     @Override
76     public DBVersion getVersion() {
77         return DBVersion.from(BigInteger.valueOf(1806), BigInteger.valueOf(0));
78     }
79
80     @Override
81     public MigrationResult migrate() {
82         log.info("start change resource lifecycle states migration");
83         final String userId = ConfigurationManager.getConfigurationManager().getConfiguration().getAutoHealingOwner();
84
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());
89         } else {
90             user = userReq.left().value();
91             log.info("User {} will perform upgrade operation with role {}", user.getUserId(), user.getRole());
92         }
93
94         StorageOperationStatus status = changeResourceLifecycleState();
95
96         return status == StorageOperationStatus.OK ? MigrationResult.success() : MigrationResult.error("failed to change lifecycle state of resources. Error : " + status);
97     }
98
99     private StorageOperationStatus changeResourceLifecycleState() {
100         StorageOperationStatus status;
101         status = findResourcesAndChangeStatus(VertexTypeEnum.NODE_TYPE);
102         if (StorageOperationStatus.OK == status) {
103             status = findResourcesAndChangeStatus(VertexTypeEnum.TOPOLOGY_TEMPLATE);
104         }
105         janusGraphDao.commit();
106         return status;
107     }
108
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);
115
116         Map<GraphPropertyEnum, Object> hasNot = new EnumMap<>(GraphPropertyEnum.class);
117         hasNot.put(GraphPropertyEnum.IS_DELETED, true);
118
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);
122         
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);
127         
128         
129         return status;
130     }
131
132     private StorageOperationStatus changeState(List<GraphVertex> resourcesV) {
133         StorageOperationStatus status = StorageOperationStatus.OK;
134         
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;
140             }
141         }
142         return status;
143     }
144
145     private StorageOperationStatus changeResourceState(GraphVertex resourceV) {
146         log.debug("Change state to certified to resource with id {} ", resourceV.getUniqueId() );
147         
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;
151     }
152
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);
157     }
158
159 }