857bcd9b43c0090873f2d8eb713bea14b2cfb92b
[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.datatypes.enums.NodeTypeEnum;
26 import org.openecomp.sdc.be.model.category.CategoryDefinition;
27 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
28 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
29
30 import javax.annotation.Resource;
31 import java.util.List;
32
33 import static org.openecomp.sdc.asdctool.impl.migration.v1707.MigrationUtils.handleError;
34
35 public class ServiceCategoriesMigration extends JsonModelMigration<CategoryDefinition> {
36
37     @Resource(name = "element-operation")
38     private IElementOperation elementOperation;
39
40     @Resource(name = "element-operation-migration")
41     private IElementOperation elementOperationMigration;
42
43
44     @Override
45     public String description() {
46         return "migrate services categories";
47     }
48
49     @Override
50     Either<List<CategoryDefinition>, ?> getElementsToMigrate() {
51         return elementOperation.getAllCategories(NodeTypeEnum.ServiceNewCategory, false).left().map(CategoriesUtils::filterOldCategories);
52     }
53
54     @Override
55     Either<CategoryDefinition, ?> getElementFromNewGraph(CategoryDefinition node) {
56         String categoryUid = UniqueIdBuilder.buildCategoryUid(node.getNormalizedName(), NodeTypeEnum.ServiceNewCategory);//in malformed graph there are some categories with different id but same normalized name. so in new graph they same id
57         return elementOperationMigration.getCategory(NodeTypeEnum.ServiceNewCategory, categoryUid);
58     }
59
60     @Override
61     boolean save(CategoryDefinition graphNode) {
62         return elementOperationMigration.createCategory(graphNode, NodeTypeEnum.ServiceNewCategory)
63                 .either(savedCategory -> true,
64                         err -> handleError(String.format("failed to save category %s. error: %s", graphNode.getName(), err.name())));
65     }
66
67     @Override
68     ActionStatus getNotFoundErrorStatus() {
69         return ActionStatus.COMPONENT_CATEGORY_NOT_FOUND;
70     }
71 }