6e0a8a924717fc50e15054fc149fa8056eec5798
[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.be.dao.api.ActionStatus;
25 import org.openecomp.sdc.be.dao.utils.UserStatusEnum;
26 import org.openecomp.sdc.be.model.User;
27 import org.openecomp.sdc.be.model.operations.api.IUserAdminOperation;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import javax.annotation.Resource;
32 import java.util.List;
33
34 import static org.openecomp.sdc.asdctool.impl.migration.v1707.MigrationUtils.handleError;
35
36 public class UsersMigration extends JsonModelMigration<User> {
37
38     private static Logger LOGGER = LoggerFactory.getLogger(UsersMigration.class);
39
40     @Resource(name = "user-operation")
41     IUserAdminOperation userAdminOperation;
42
43     @Resource(name = "user-operation-migration")
44     IUserAdminOperation userAdminOperationMigration;
45
46
47     @Override
48     Either<List<User>, ActionStatus> getElementsToMigrate() {
49         LOGGER.debug("fetching users to migrate from old graph");
50         return userAdminOperation.getAllUsers();
51     }
52
53     @Override
54     Either<User, ActionStatus> getElementFromNewGraph(User user) {
55         LOGGER.debug(String.format("trying to load user %s from new graph", user.getUserId()));
56         return user.getStatus().equals(UserStatusEnum.ACTIVE) ? userAdminOperationMigration.getUserData(user.getUserId(), false) :
57                                                                 userAdminOperationMigration.getInactiveUserData(user.getUserId());
58     }
59
60     @Override
61     boolean save(User user) {
62         LOGGER.debug(String.format("trying to save user %s to new graph", user.getUserId()));
63         return userAdminOperationMigration.saveUserData(user)
64                 .either(savedUser -> true,
65                         err -> handleError(String.format("failed when saving user %s. error %s", user.getUserId(), err.name())));
66     }
67
68     @Override
69     public ActionStatus getNotFoundErrorStatus() {
70         return ActionStatus.USER_NOT_FOUND;
71     }
72
73     @Override
74     public String description() {
75         return "migrate users";
76     }
77
78 }