[SDC] sync
[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.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;
36
37 import javax.annotation.Resource;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.function.Consumer;
41
42 @Component("policyTypeImportManager")
43 public class PolicyTypeImportManager {
44
45         @Resource
46         private IPolicyTypeOperation policyTypeOperation;
47         @Resource
48         private ComponentsUtils componentsUtils;
49         @Autowired
50         protected IGroupOperation groupOperation;
51         @Autowired
52         private ToscaOperationFacade toscaOperationFacade;
53
54         @Resource
55         private CommonImportManager commonImportManager;
56
57         public Either<List<ImmutablePair<PolicyTypeDefinition, Boolean>>, ResponseFormat> createPolicyTypes(String groupTypesYml) {
58                 return commonImportManager.createElementTypes(groupTypesYml, elementTypeYml -> createPolicyTypesFromYml(elementTypeYml), groupTypesList -> createPolicyTypesByDao(groupTypesList), ElementTypeEnum.PolicyType);
59         }
60
61         private Either<List<PolicyTypeDefinition>, ActionStatus> createPolicyTypesFromYml(String policyTypesYml) {
62
63                 return commonImportManager.createElementTypesFromYml(policyTypesYml, (policyTypeName, groupTypeJsonData) -> createPolicyType(policyTypeName, groupTypeJsonData));
64         }
65
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);
69         }
70
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);
77                         }
78                         if (result.isLeft()) {
79                                 for (String targetName : policyType.getTargets()) {
80
81                                         boolean isValid = toscaOperationFacade.getLatestByToscaResourceName(targetName).isLeft();;
82                                         if (!isValid) {
83                                                 isValid = groupOperation.isGroupExist(targetName, false);
84                                         }
85                                         if (!isValid) {
86                                                 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.TARGETS_NON_VALID, policyType.getType(), targetName);
87                                                 result = Either.right(responseFormat);
88                                                 break;
89                                         }
90                                 }
91                         }
92                 }
93
94                 return result;
95         }
96
97         private PolicyTypeDefinition createPolicyType(String groupTypeName, Map<String, Object> toscaJson) {
98
99                 PolicyTypeDefinition policyType = new PolicyTypeDefinition();
100
101                 if (toscaJson != null) {
102                         // Description
103                         final Consumer<String> descriptionSetter = description -> policyType.setDescription(description);
104                         commonImportManager.setField(toscaJson, ToscaTagNamesEnum.DESCRIPTION.getElementName(), descriptionSetter);
105                         // Derived From
106                         final Consumer<String> derivedFromSetter = derivedFrom -> policyType.setDerivedFrom(derivedFrom);
107                         commonImportManager.setField(toscaJson, ToscaTagNamesEnum.DERIVED_FROM.getElementName(), derivedFromSetter);
108                         // Properties
109                         commonImportManager.setProperties(toscaJson, (values) -> policyType.setProperties(values));
110                         // Metadata
111                         final Consumer<Map<String, String>> metadataSetter = metadata -> policyType.setMetadata(metadata);
112                         commonImportManager.setField(toscaJson, ToscaTagNamesEnum.METADATA.getElementName(), metadataSetter);
113                         // Targets
114                         final Consumer<List<String>> targetsSetter = targets -> policyType.setTargets(targets);
115                         commonImportManager.setField(toscaJson, ToscaTagNamesEnum.TARGETS.getElementName(), targetsSetter);
116
117                         policyType.setType(groupTypeName);
118
119                         policyType.setHighestVersion(true);
120
121                         policyType.setVersion(ImportUtils.Constants.FIRST_CERTIFIED_VERSION_VERSION);
122                 }
123                 return policyType;
124         }
125
126 }