Upgrade SDC from Titan to Janus Graph
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / RelationshipTypeImportManager.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.be.components.impl;
18
19 import java.util.List;
20 import java.util.Map;
21
22 import fj.data.Either;
23 import org.apache.commons.lang3.tuple.ImmutablePair;
24 import org.openecomp.sdc.be.components.impl.CommonImportManager.ElementTypeEnum;
25 import org.openecomp.sdc.be.dao.api.ActionStatus;
26 import org.openecomp.sdc.be.impl.ComponentsUtils;
27 import org.openecomp.sdc.be.model.RelationshipTypeDefinition;
28 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
29 import org.openecomp.sdc.be.model.operations.impl.RelationshipTypeOperation;
30 import org.openecomp.sdc.be.utils.TypeUtils;
31 import org.openecomp.sdc.exception.ResponseFormat;
32 import org.springframework.stereotype.Component;
33
34 @Component("relationshipTypeImportManager")
35 public class RelationshipTypeImportManager {
36
37     private final RelationshipTypeOperation relationshipTypeOperation;
38     private final CommonImportManager commonImportManager;
39     private ComponentsUtils componentsUtils;
40
41     public RelationshipTypeImportManager(RelationshipTypeOperation relationshipTypeOperation,
42                                          CommonImportManager commonImportManager) {
43         this.relationshipTypeOperation = relationshipTypeOperation;
44         this.commonImportManager = commonImportManager;
45     }
46
47     public Either<List<ImmutablePair<RelationshipTypeDefinition, Boolean>>, ResponseFormat> createRelationshipTypes(
48             String relationshipYml) {
49         return createRelationshipTypes(relationshipYml, false);
50     }
51
52     private Either<List<ImmutablePair<RelationshipTypeDefinition, Boolean>>, ResponseFormat> createRelationshipTypes(
53             String relationshipTypeYml, boolean inTransaction) {
54         return commonImportManager.createElementTypes(relationshipTypeYml,
55                 relationshipTypesFromYml -> createRelationshipTypesFromYml(relationshipTypeYml),
56                 relationshipTypesToCreate -> createRelationshipTypesByDao(relationshipTypesToCreate,
57                         inTransaction), ElementTypeEnum.RELATIONSHIP_TYPE);
58     }
59
60     private Either<List<RelationshipTypeDefinition>, ActionStatus> createRelationshipTypesFromYml(
61             String relationshipTypeYml) {
62         return commonImportManager.createElementTypesFromYml(relationshipTypeYml,
63                 this::createRelationshipType);
64     }
65
66     private Either<List<ImmutablePair<RelationshipTypeDefinition, Boolean>>, ResponseFormat> createRelationshipTypesByDao(
67             List<RelationshipTypeDefinition> relationshipTypesToCreate, boolean inTransaction) {
68         return commonImportManager.createElementTypesByDao(relationshipTypesToCreate, this::validateRelationshipType,
69                 relationshipType -> new ImmutablePair<>(ElementTypeEnum.RELATIONSHIP_TYPE, relationshipType.getType()),
70                 relationshipTypeName -> relationshipTypeOperation.getRelationshipTypeByName(relationshipTypeName)
71                         .right().map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus),
72                 relationshipType -> relationshipTypeOperation.addRelationshipType(relationshipType, inTransaction),
73                 (newRelationshipType, oldRelationshipType) -> relationshipTypeOperation
74                         .updateRelationshipType(newRelationshipType, oldRelationshipType, inTransaction));
75     }
76
77
78     private Either<ActionStatus, ResponseFormat> validateRelationshipType(RelationshipTypeDefinition relationshipType) {
79         Either<ActionStatus, ResponseFormat> result = Either.left(ActionStatus.OK);
80         if (relationshipType.getType() == null) {
81             ResponseFormat responseFormat =
82                     componentsUtils.getResponseFormat(ActionStatus.MISSING_RELATIONSHIP_TYPE, relationshipType.getType());
83             result = Either.right(responseFormat);
84         }
85         return result;
86     }
87
88     private RelationshipTypeDefinition createRelationshipType(String relationshipTypeName,
89                                                               Map<String, Object> toscaJson) {
90         RelationshipTypeDefinition relationshipType = new RelationshipTypeDefinition();
91
92         relationshipType.setType(relationshipTypeName);
93
94         // Description
95         commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName(),
96                 relationshipType::setDescription);
97         // Derived From
98         commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DERIVED_FROM.getElementName(),
99                 relationshipType::setDerivedFrom);
100         // Properties
101         commonImportManager.setPropertiesMap(toscaJson, relationshipType::setProperties);
102         //valid-target-types
103         if(toscaJson.get("valid_target_types") instanceof List)
104             relationshipType.setValidTargetTypes((List<String>) toscaJson.get("valid_target_types"));
105
106         return relationshipType;
107     }
108
109 }