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