fix bug - Error 500 while adding an Operation
[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 fj.data.Either;
20 import org.apache.commons.lang3.tuple.ImmutablePair;
21 import org.openecomp.sdc.be.components.impl.CommonImportManager.ElementTypeEnum;
22 import org.openecomp.sdc.be.dao.api.ActionStatus;
23 import org.openecomp.sdc.be.impl.ComponentsUtils;
24 import org.openecomp.sdc.be.model.RelationshipTypeDefinition;
25 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
26 import org.openecomp.sdc.be.model.operations.impl.RelationshipTypeOperation;
27 import org.openecomp.sdc.be.utils.TypeUtils;
28 import org.openecomp.sdc.exception.ResponseFormat;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.stereotype.Component;
31
32 import java.util.List;
33 import java.util.Map;
34
35 @Component("relationshipTypeImportManager")
36 public class RelationshipTypeImportManager {
37
38     private final RelationshipTypeOperation relationshipTypeOperation;
39     private final CommonImportManager commonImportManager;
40     private final ComponentsUtils componentsUtils;
41
42     @Autowired
43     public RelationshipTypeImportManager(RelationshipTypeOperation relationshipTypeOperation,
44         CommonImportManager commonImportManager, ComponentsUtils componentsUtils) {
45         this.relationshipTypeOperation = relationshipTypeOperation;
46         this.commonImportManager = commonImportManager;
47         this.componentsUtils = componentsUtils;
48     }
49
50     public Either<List<ImmutablePair<RelationshipTypeDefinition, Boolean>>, ResponseFormat> createRelationshipTypes(
51             String relationshipYml) {
52         return createRelationshipTypes(relationshipYml, false);
53     }
54
55     private Either<List<ImmutablePair<RelationshipTypeDefinition, Boolean>>, ResponseFormat> createRelationshipTypes(
56             String relationshipTypeYml, boolean inTransaction) {
57         return commonImportManager.createElementTypes(relationshipTypeYml,
58                 relationshipTypesFromYml -> createRelationshipTypesFromYml(relationshipTypeYml),
59                 relationshipTypesToCreate -> createRelationshipTypesByDao(relationshipTypesToCreate,
60                         inTransaction), ElementTypeEnum.RELATIONSHIP_TYPE);
61     }
62
63     private Either<List<RelationshipTypeDefinition>, ActionStatus> createRelationshipTypesFromYml(
64             String relationshipTypeYml) {
65         return commonImportManager.createElementTypesFromYml(relationshipTypeYml,
66                 this::createRelationshipType);
67     }
68
69     private Either<List<ImmutablePair<RelationshipTypeDefinition, Boolean>>, ResponseFormat> createRelationshipTypesByDao(
70             List<RelationshipTypeDefinition> relationshipTypesToCreate, boolean inTransaction) {
71         return commonImportManager.createElementTypesByDao(relationshipTypesToCreate, this::validateRelationshipType,
72                 relationshipType -> new ImmutablePair<>(ElementTypeEnum.RELATIONSHIP_TYPE, relationshipType.getType()),
73                 relationshipTypeName -> relationshipTypeOperation.getRelationshipTypeByName(relationshipTypeName)
74                         .right().map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus),
75                 relationshipType -> relationshipTypeOperation.addRelationshipType(relationshipType, inTransaction),
76                 (newRelationshipType, oldRelationshipType) -> relationshipTypeOperation
77                         .updateRelationshipType(newRelationshipType, oldRelationshipType, inTransaction));
78     }
79
80
81     private Either<ActionStatus, ResponseFormat> validateRelationshipType(RelationshipTypeDefinition relationshipType) {
82         Either<ActionStatus, ResponseFormat> result = Either.left(ActionStatus.OK);
83         if (relationshipType.getType() == null) {
84             ResponseFormat responseFormat =
85                     componentsUtils.getResponseFormat(ActionStatus.MISSING_RELATIONSHIP_TYPE, relationshipType.getType());
86             result = Either.right(responseFormat);
87         }
88         return result;
89     }
90
91     private RelationshipTypeDefinition createRelationshipType(String relationshipTypeName,
92                                                               Map<String, Object> toscaJson) {
93         RelationshipTypeDefinition relationshipType = new RelationshipTypeDefinition();
94
95         relationshipType.setType(relationshipTypeName);
96
97         // Description
98         commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName(),
99                 relationshipType::setDescription);
100         // Derived From
101         commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DERIVED_FROM.getElementName(),
102                 relationshipType::setDerivedFrom);
103         // Properties
104         commonImportManager.setPropertiesMap(toscaJson, relationshipType::setProperties);
105         //valid-target-types
106         if(toscaJson.get("valid_target_types") instanceof List)
107             relationshipType.setValidTargetTypes((List<String>) toscaJson.get("valid_target_types"));
108
109         return relationshipType;
110     }
111
112 }