Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / PolicyTypeImportManager.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.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;
38
39 import javax.annotation.Resource;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.function.Consumer;
43
44 @Component("policyTypeImportManager")
45 public class PolicyTypeImportManager {
46
47     @Resource
48     private IPolicyTypeOperation policyTypeOperation;
49     @Resource
50     private ComponentsUtils componentsUtils;
51     @Autowired
52     protected IGroupOperation groupOperation;
53     @Autowired
54     private ToscaOperationFacade toscaOperationFacade;
55
56     @Resource
57     private CommonImportManager commonImportManager;
58
59     public Either<List<ImmutablePair<PolicyTypeDefinition, Boolean>>, ResponseFormat> createPolicyTypes(String policyTypesYml) {
60         return commonImportManager.createElementTypes(policyTypesYml, this::createPolicyTypesFromYml, this::upsertPolicyTypesByDao, ElementTypeEnum.PolicyType);
61     }
62
63     private Either<List<PolicyTypeDefinition>, ActionStatus> createPolicyTypesFromYml(String policyTypesYml) {
64
65         return commonImportManager.createElementTypesFromYml(policyTypesYml, this::createPolicyType);
66     }
67
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);
71     }
72
73     private Either<PolicyTypeDefinition, StorageOperationStatus> updatePolicyType(PolicyTypeDefinition newPolicyType, PolicyTypeDefinition oldPolicyType) {
74         if (PolicyTypeImportUtils.isPolicyTypesEquals(newPolicyType, oldPolicyType)) {
75             return policyTypeAlreadyExists();
76         }
77         return policyTypeOperation.updatePolicyType(newPolicyType, oldPolicyType);
78     }
79
80     private Either<PolicyTypeDefinition, StorageOperationStatus> policyTypeAlreadyExists() {
81         return Either.right(StorageOperationStatus.OK);
82     }
83
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);
90             }
91             if (result.isLeft()) {
92                 for (String targetId : policyType.getTargets()) {
93                         boolean isValid = toscaOperationFacade.getLatestByToscaResourceName(targetId).isLeft();
94                         if (!isValid) {
95                             isValid = groupOperation.isGroupExist(targetId, false);
96                         }
97                         if (!isValid) {
98                             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.TARGETS_NON_VALID, policyType.getType(), targetId);
99                             result = Either.right(responseFormat);
100                             break;
101                         }
102                     }
103                 }
104             }
105
106
107         return result;
108     }
109
110     private PolicyTypeDefinition createPolicyType(String groupTypeName, Map<String, Object> toscaJson) {
111
112         PolicyTypeDefinition policyType = new PolicyTypeDefinition();
113
114         if (toscaJson != null) {
115             // Description
116             final Consumer<String> descriptionSetter = description -> policyType.setDescription(description);
117             commonImportManager.setField(toscaJson, ToscaTagNamesEnum.DESCRIPTION.getElementName(), descriptionSetter);
118             // Derived From
119             final Consumer<String> derivedFromSetter = derivedFrom -> policyType.setDerivedFrom(derivedFrom);
120             commonImportManager.setField(toscaJson, ToscaTagNamesEnum.DERIVED_FROM.getElementName(), derivedFromSetter);
121             // Properties
122             commonImportManager.setProperties(toscaJson, (values) -> policyType.setProperties(values));
123             // Metadata
124             final Consumer<Map<String, String>> metadataSetter = metadata -> policyType.setMetadata(metadata);
125             commonImportManager.setField(toscaJson, ToscaTagNamesEnum.METADATA.getElementName(), metadataSetter);
126             // Targets
127             final Consumer <List<String>> targetsSetter = targets -> policyType.setTargets(targets);
128             commonImportManager.setField(toscaJson, ToscaTagNamesEnum.TARGETS.getElementName(), targetsSetter);
129
130             policyType.setType(groupTypeName);
131
132             policyType.setHighestVersion(true);
133
134             policyType.setVersion(ImportUtils.Constants.FIRST_CERTIFIED_VERSION_VERSION);
135         }
136         return policyType;
137     }
138
139 }