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.dao.api.ActionStatus;
28 import org.openecomp.sdc.be.impl.ComponentsUtils;
29 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
30 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
31 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
32 import org.openecomp.sdc.be.model.operations.api.IPolicyTypeOperation;
33 import org.openecomp.sdc.exception.ResponseFormat;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Component;
37 import javax.annotation.Resource;
38 import java.util.List;
40 import java.util.function.Consumer;
42 @Component("policyTypeImportManager")
43 public class PolicyTypeImportManager {
46 private IPolicyTypeOperation policyTypeOperation;
48 private ComponentsUtils componentsUtils;
50 protected IGroupOperation groupOperation;
52 private ToscaOperationFacade toscaOperationFacade;
55 private CommonImportManager commonImportManager;
57 public Either<List<ImmutablePair<PolicyTypeDefinition, Boolean>>, ResponseFormat> createPolicyTypes(String groupTypesYml) {
58 return commonImportManager.createElementTypes(groupTypesYml, elementTypeYml -> createPolicyTypesFromYml(elementTypeYml), groupTypesList -> createPolicyTypesByDao(groupTypesList), ElementTypeEnum.PolicyType);
61 private Either<List<PolicyTypeDefinition>, ActionStatus> createPolicyTypesFromYml(String policyTypesYml) {
63 return commonImportManager.createElementTypesFromYml(policyTypesYml, (policyTypeName, groupTypeJsonData) -> createPolicyType(policyTypeName, groupTypeJsonData));
66 private Either<List<ImmutablePair<PolicyTypeDefinition, Boolean>>, ResponseFormat> createPolicyTypesByDao(List<PolicyTypeDefinition> policyTypesToCreate) {
67 return commonImportManager.createElementTypesByDao(policyTypesToCreate, policyType -> validatePolicyType(policyType), policyType -> new ImmutablePair<>(ElementTypeEnum.PolicyType, policyType.getType()),
68 policyTypeName -> policyTypeOperation.getLatestPolicyTypeByType(policyTypeName), policyType -> policyTypeOperation.addPolicyType(policyType), null);
71 private Either<ActionStatus, ResponseFormat> validatePolicyType(PolicyTypeDefinition policyType) {
72 Either<ActionStatus, ResponseFormat> result = Either.left(ActionStatus.OK);
73 if (policyType.getTargets() != null) {
74 if (policyType.getTargets().isEmpty()) {
75 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.TARGETS_EMPTY, policyType.getType());
76 result = Either.right(responseFormat);
78 if (result.isLeft()) {
79 for (String targetName : policyType.getTargets()) {
81 boolean isValid = toscaOperationFacade.getLatestByToscaResourceName(targetName).isLeft();;
83 isValid = groupOperation.isGroupExist(targetName, false);
86 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.TARGETS_NON_VALID, policyType.getType(), targetName);
87 result = Either.right(responseFormat);
97 private PolicyTypeDefinition createPolicyType(String groupTypeName, Map<String, Object> toscaJson) {
99 PolicyTypeDefinition policyType = new PolicyTypeDefinition();
101 if (toscaJson != null) {
103 final Consumer<String> descriptionSetter = description -> policyType.setDescription(description);
104 commonImportManager.setField(toscaJson, ToscaTagNamesEnum.DESCRIPTION.getElementName(), descriptionSetter);
106 final Consumer<String> derivedFromSetter = derivedFrom -> policyType.setDerivedFrom(derivedFrom);
107 commonImportManager.setField(toscaJson, ToscaTagNamesEnum.DERIVED_FROM.getElementName(), derivedFromSetter);
109 commonImportManager.setProperties(toscaJson, (values) -> policyType.setProperties(values));
111 final Consumer<Map<String, String>> metadataSetter = metadata -> policyType.setMetadata(metadata);
112 commonImportManager.setField(toscaJson, ToscaTagNamesEnum.METADATA.getElementName(), metadataSetter);
114 final Consumer<List<String>> targetsSetter = targets -> policyType.setTargets(targets);
115 commonImportManager.setField(toscaJson, ToscaTagNamesEnum.TARGETS.getElementName(), targetsSetter);
117 policyType.setType(groupTypeName);
119 policyType.setHighestVersion(true);
121 policyType.setVersion(ImportUtils.Constants.FIRST_CERTIFIED_VERSION_VERSION);