re base code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / CapabilityTypeImportManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.impl;
22
23 import fj.data.Either;
24 import org.apache.commons.lang3.tuple.ImmutablePair;
25 import org.openecomp.sdc.be.dao.api.ActionStatus;
26 import org.openecomp.sdc.be.model.CapabilityTypeDefinition;
27 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
28 import org.openecomp.sdc.be.model.operations.impl.CapabilityTypeOperation;
29 import org.openecomp.sdc.be.model.utils.TypeCompareUtils;
30 import org.openecomp.sdc.be.utils.TypeUtils;
31 import org.openecomp.sdc.common.log.wrappers.Logger;
32 import org.openecomp.sdc.exception.ResponseFormat;
33 import org.springframework.stereotype.Component;
34
35 import java.util.List;
36 import java.util.Map;
37
38 @Component("capabilityTypeImportManager")
39 public class CapabilityTypeImportManager {
40
41     private static final Logger log = Logger.getLogger(CapabilityTypeImportManager.class.getName());
42     private final CapabilityTypeOperation capabilityTypeOperation;
43     private final CommonImportManager commonImportManager;
44
45     public CapabilityTypeImportManager(CapabilityTypeOperation capabilityTypeOperation, CommonImportManager commonImportManager) {
46         this.capabilityTypeOperation = capabilityTypeOperation;
47         this.commonImportManager = commonImportManager;
48
49     }
50
51     public Either<List<ImmutablePair<CapabilityTypeDefinition, Boolean>>, ResponseFormat> createCapabilityTypes(String capabilityTypesYml) {
52         return  commonImportManager.createElementTypes(capabilityTypesYml, this::createCapabilityTypesFromYml, this::upsertCapabilityTypesByDao, CommonImportManager.ElementTypeEnum.CAPABILITY_TYPE);
53
54     }
55
56     private Either<List<CapabilityTypeDefinition>, ActionStatus> createCapabilityTypesFromYml(String capabilityTypesYml) {
57         return commonImportManager.createElementTypesFromYml(capabilityTypesYml, this::createCapabilityType);
58
59     }
60
61     private Either<List<ImmutablePair<CapabilityTypeDefinition, Boolean>>, ResponseFormat> upsertCapabilityTypesByDao(List<CapabilityTypeDefinition> capabilityTypesToCreate) {
62         return commonImportManager.createElementTypesByDao(capabilityTypesToCreate,
63                 capabilityType -> Either.left(ActionStatus.OK),
64                 capabilityType -> new ImmutablePair<>(CommonImportManager.ElementTypeEnum.CAPABILITY_TYPE, capabilityType.getType()),
65                 capabilityTypeOperation::getCapabilityType,
66                 capabilityTypeOperation::addCapabilityType,
67                 this::updateCapabilityType);
68     }
69     
70     private Either<CapabilityTypeDefinition, StorageOperationStatus> updateCapabilityType(CapabilityTypeDefinition newCapabilityType, CapabilityTypeDefinition oldCapabilityType) {
71         Either<CapabilityTypeDefinition, StorageOperationStatus> validationRes = capabilityTypeOperation.validateUpdateProperties(newCapabilityType);
72         if (validationRes.isRight()) {
73             log.error("#updateCapabilityType - One or all properties of capability type {} not valid. status is {}", newCapabilityType, validationRes.right().value());
74             return validationRes;
75         }
76         
77         if (TypeCompareUtils.isCapabilityTypesEquals(newCapabilityType, oldCapabilityType)) {
78             return TypeCompareUtils.typeAlreadyExists();
79         }
80         
81         return capabilityTypeOperation.updateCapabilityType(newCapabilityType, oldCapabilityType);
82     }
83
84     private CapabilityTypeDefinition createCapabilityType(String capabilityTypeName, Map<String, Object> toscaJson) {
85         CapabilityTypeDefinition capabilityType = new CapabilityTypeDefinition();
86
87         capabilityType.setType(capabilityTypeName);
88
89         // Description
90         commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName(), capabilityType::setDescription);
91         // Derived From
92         commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DERIVED_FROM.getElementName(), capabilityType::setDerivedFrom);
93         // Properties
94         commonImportManager.setPropertiesMap(toscaJson, capabilityType::setProperties);
95
96         return capabilityType;
97     }
98
99 }