2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.impl;
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;
35 import java.util.List;
38 @Component("capabilityTypeImportManager")
39 public class CapabilityTypeImportManager {
41 private static final Logger log = Logger.getLogger(CapabilityTypeImportManager.class.getName());
42 private final CapabilityTypeOperation capabilityTypeOperation;
43 private final CommonImportManager commonImportManager;
45 public CapabilityTypeImportManager(CapabilityTypeOperation capabilityTypeOperation, CommonImportManager commonImportManager) {
46 this.capabilityTypeOperation = capabilityTypeOperation;
47 this.commonImportManager = commonImportManager;
51 public Either<List<ImmutablePair<CapabilityTypeDefinition, Boolean>>, ResponseFormat> createCapabilityTypes(String capabilityTypesYml) {
52 return commonImportManager.createElementTypes(capabilityTypesYml, this::createCapabilityTypesFromYml, this::upsertCapabilityTypesByDao, CommonImportManager.ElementTypeEnum.CAPABILITY_TYPE);
56 private Either<List<CapabilityTypeDefinition>, ActionStatus> createCapabilityTypesFromYml(String capabilityTypesYml) {
57 return commonImportManager.createElementTypesFromYml(capabilityTypesYml, this::createCapabilityType);
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);
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());
77 if (TypeCompareUtils.isCapabilityTypesEquals(newCapabilityType, oldCapabilityType)) {
78 return TypeCompareUtils.typeAlreadyExists();
81 return capabilityTypeOperation.updateCapabilityType(newCapabilityType, oldCapabilityType);
84 private CapabilityTypeDefinition createCapabilityType(String capabilityTypeName, Map<String, Object> toscaJson) {
85 CapabilityTypeDefinition capabilityType = new CapabilityTypeDefinition();
87 capabilityType.setType(capabilityTypeName);
90 commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName(), capabilityType::setDescription);
92 commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DERIVED_FROM.getElementName(), capabilityType::setDerivedFrom);
94 commonImportManager.setPropertiesMap(toscaJson, capabilityType::setProperties);
96 return capabilityType;