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.components.impl.CommonImportManager.ElementTypeEnum;
26 import org.openecomp.sdc.be.components.impl.ImportUtils.ToscaTagNamesEnum;
27 import org.openecomp.sdc.be.components.impl.utils.PolicyTypeImportUtils;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.impl.ComponentsUtils;
30 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
31 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
32 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
33 import org.openecomp.sdc.be.model.operations.api.IPolicyTypeOperation;
34 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
35 import org.openecomp.sdc.exception.ResponseFormat;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
39 import javax.annotation.Resource;
40 import java.util.List;
42 import java.util.function.Consumer;
44 @Component("policyTypeImportManager")
45 public class PolicyTypeImportManager {
48 private IPolicyTypeOperation policyTypeOperation;
50 private ComponentsUtils componentsUtils;
52 protected IGroupOperation groupOperation;
54 private ToscaOperationFacade toscaOperationFacade;
57 private CommonImportManager commonImportManager;
59 public Either<List<ImmutablePair<PolicyTypeDefinition, Boolean>>, ResponseFormat> createPolicyTypes(String policyTypesYml) {
60 return commonImportManager.createElementTypes(policyTypesYml, this::createPolicyTypesFromYml, this::upsertPolicyTypesByDao, ElementTypeEnum.PolicyType);
63 private Either<List<PolicyTypeDefinition>, ActionStatus> createPolicyTypesFromYml(String policyTypesYml) {
65 return commonImportManager.createElementTypesFromYml(policyTypesYml, this::createPolicyType);
68 private Either<List<ImmutablePair<PolicyTypeDefinition, Boolean>>, ResponseFormat> upsertPolicyTypesByDao(List<PolicyTypeDefinition> policyTypesToCreate) {
69 return commonImportManager.createElementTypesByDao(policyTypesToCreate, this::validatePolicyType, policyType -> new ImmutablePair<>(ElementTypeEnum.PolicyType, policyType.getType()),
70 policyTypeName -> policyTypeOperation.getLatestPolicyTypeByType(policyTypeName), policyType -> policyTypeOperation.addPolicyType(policyType), this::updatePolicyType);
73 private Either<PolicyTypeDefinition, StorageOperationStatus> updatePolicyType(PolicyTypeDefinition newPolicyType, PolicyTypeDefinition oldPolicyType) {
74 if (PolicyTypeImportUtils.isPolicyTypesEquals(newPolicyType, oldPolicyType)) {
75 return policyTypeAlreadyExists();
77 return policyTypeOperation.updatePolicyType(newPolicyType, oldPolicyType);
80 private Either<PolicyTypeDefinition, StorageOperationStatus> policyTypeAlreadyExists() {
81 return Either.right(StorageOperationStatus.OK);
84 private Either<ActionStatus, ResponseFormat> validatePolicyType(PolicyTypeDefinition policyType) {
85 Either<ActionStatus, ResponseFormat> result = Either.left(ActionStatus.OK);
86 if (policyType.getTargets() != null) {
87 if (policyType.getTargets().isEmpty()) {
88 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.TARGETS_EMPTY, policyType.getType());
89 result = Either.right(responseFormat);
91 if (result.isLeft()) {
92 for (String targetId : policyType.getTargets()) {
93 boolean isValid = toscaOperationFacade.getLatestByToscaResourceName(targetId).isLeft();
95 isValid = groupOperation.isGroupExist(targetId, false);
98 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.TARGETS_NON_VALID, policyType.getType(), targetId);
99 result = Either.right(responseFormat);
110 private PolicyTypeDefinition createPolicyType(String groupTypeName, Map<String, Object> toscaJson) {
112 PolicyTypeDefinition policyType = new PolicyTypeDefinition();
114 if (toscaJson != null) {
116 final Consumer<String> descriptionSetter = description -> policyType.setDescription(description);
117 commonImportManager.setField(toscaJson, ToscaTagNamesEnum.DESCRIPTION.getElementName(), descriptionSetter);
119 final Consumer<String> derivedFromSetter = derivedFrom -> policyType.setDerivedFrom(derivedFrom);
120 commonImportManager.setField(toscaJson, ToscaTagNamesEnum.DERIVED_FROM.getElementName(), derivedFromSetter);
122 commonImportManager.setProperties(toscaJson, (values) -> policyType.setProperties(values));
124 final Consumer<Map<String, String>> metadataSetter = metadata -> policyType.setMetadata(metadata);
125 commonImportManager.setField(toscaJson, ToscaTagNamesEnum.METADATA.getElementName(), metadataSetter);
127 final Consumer <List<String>> targetsSetter = targets -> policyType.setTargets(targets);
128 commonImportManager.setField(toscaJson, ToscaTagNamesEnum.TARGETS.getElementName(), targetsSetter);
130 policyType.setType(groupTypeName);
132 policyType.setHighestVersion(true);
134 policyType.setVersion(ImportUtils.Constants.FIRST_CERTIFIED_VERSION_VERSION);