2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.be.components.impl;
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;
32 import java.util.List;
35 @Component("relationshipTypeImportManager")
36 public class RelationshipTypeImportManager {
38 private final RelationshipTypeOperation relationshipTypeOperation;
39 private final CommonImportManager commonImportManager;
40 private final ComponentsUtils componentsUtils;
43 public RelationshipTypeImportManager(RelationshipTypeOperation relationshipTypeOperation,
44 CommonImportManager commonImportManager, ComponentsUtils componentsUtils) {
45 this.relationshipTypeOperation = relationshipTypeOperation;
46 this.commonImportManager = commonImportManager;
47 this.componentsUtils = componentsUtils;
50 public Either<List<ImmutablePair<RelationshipTypeDefinition, Boolean>>, ResponseFormat> createRelationshipTypes(
51 String relationshipYml) {
52 return createRelationshipTypes(relationshipYml, false);
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);
63 private Either<List<RelationshipTypeDefinition>, ActionStatus> createRelationshipTypesFromYml(
64 String relationshipTypeYml) {
65 return commonImportManager.createElementTypesFromYml(relationshipTypeYml,
66 this::createRelationshipType);
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));
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);
91 private RelationshipTypeDefinition createRelationshipType(String relationshipTypeName,
92 Map<String, Object> toscaJson) {
93 RelationshipTypeDefinition relationshipType = new RelationshipTypeDefinition();
95 relationshipType.setType(relationshipTypeName);
98 commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName(),
99 relationshipType::setDescription);
101 commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DERIVED_FROM.getElementName(),
102 relationshipType::setDerivedFrom);
104 commonImportManager.setPropertiesMap(toscaJson, relationshipType::setProperties);
106 if(toscaJson.get("valid_target_types") instanceof List)
107 relationshipType.setValidTargetTypes((List<String>) toscaJson.get("valid_target_types"));
109 return relationshipType;