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 java.util.List;
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;
34 @Component("relationshipTypeImportManager")
35 public class RelationshipTypeImportManager {
37 private final RelationshipTypeOperation relationshipTypeOperation;
38 private final CommonImportManager commonImportManager;
39 private ComponentsUtils componentsUtils;
41 public RelationshipTypeImportManager(RelationshipTypeOperation relationshipTypeOperation,
42 CommonImportManager commonImportManager) {
43 this.relationshipTypeOperation = relationshipTypeOperation;
44 this.commonImportManager = commonImportManager;
47 public Either<List<ImmutablePair<RelationshipTypeDefinition, Boolean>>, ResponseFormat> createRelationshipTypes(
48 String relationshipYml) {
49 return createRelationshipTypes(relationshipYml, false);
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);
60 private Either<List<RelationshipTypeDefinition>, ActionStatus> createRelationshipTypesFromYml(
61 String relationshipTypeYml) {
62 return commonImportManager.createElementTypesFromYml(relationshipTypeYml,
63 this::createRelationshipType);
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));
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);
88 private RelationshipTypeDefinition createRelationshipType(String relationshipTypeName,
89 Map<String, Object> toscaJson) {
90 RelationshipTypeDefinition relationshipType = new RelationshipTypeDefinition();
92 relationshipType.setType(relationshipTypeName);
95 commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName(),
96 relationshipType::setDescription);
98 commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DERIVED_FROM.getElementName(),
99 relationshipType::setDerivedFrom);
101 commonImportManager.setPropertiesMap(toscaJson, relationshipType::setProperties);
103 if(toscaJson.get("valid_target_types") instanceof List)
104 relationshipType.setValidTargetTypes((List<String>) toscaJson.get("valid_target_types"));
106 return relationshipType;