Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / property / propertytopolicydeclarators / ComponentPropertyToPolicyDeclarator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.property.propertytopolicydeclarators;
22
23 import fj.data.Either;
24 import org.apache.commons.collections4.CollectionUtils;
25 import org.openecomp.sdc.be.components.impl.PropertyBusinessLogic;
26 import org.openecomp.sdc.be.components.property.DefaultPropertyDeclarator;
27 import org.openecomp.sdc.be.datatypes.elements.GetPolicyValueDataDefinition;
28 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
29 import org.openecomp.sdc.be.impl.ComponentsUtils;
30 import org.openecomp.sdc.be.model.Component;
31 import org.openecomp.sdc.be.model.InputDefinition;
32 import org.openecomp.sdc.be.model.PolicyDefinition;
33 import org.openecomp.sdc.be.model.PropertyDefinition;
34 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
35 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
36 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
37
38 import java.util.List;
39 import java.util.Optional;
40
41 @org.springframework.stereotype.Component
42 public class ComponentPropertyToPolicyDeclarator extends DefaultPropertyDeclarator<Component, PropertyDataDefinition> {
43
44     private ToscaOperationFacade toscaOperationFacade;
45     PropertyBusinessLogic propertyBL;
46
47
48     public ComponentPropertyToPolicyDeclarator(ComponentsUtils componentsUtils, PropertyOperation propertyOperation,
49             ToscaOperationFacade toscaOperationFacade, PropertyBusinessLogic propertyBusinessLogic) {
50         super(componentsUtils, propertyOperation);
51         this.toscaOperationFacade = toscaOperationFacade;
52         this.propertyBL = propertyBusinessLogic;
53     }
54
55     @Override
56     public PropertyDataDefinition createDeclaredProperty(PropertyDataDefinition prop) {
57         return new PropertyDataDefinition(prop);
58     }
59
60     @Override
61     public Either<?, StorageOperationStatus> updatePropertiesValues(Component component, String policyId,
62             List<PropertyDataDefinition> properties) {
63         if(CollectionUtils.isNotEmpty(properties)) {
64             for(PropertyDataDefinition property : properties) {
65                 Either<PropertyDefinition, StorageOperationStatus>
66                         storageStatus = toscaOperationFacade
67                                                 .updatePropertyOfComponent(component, new PropertyDefinition(property));
68                 if(storageStatus.isRight()) {
69                     return Either.right(storageStatus.right().value());
70                 }
71             }
72         }
73         return Either.left(properties);
74
75     }
76
77     @Override
78     public Optional<Component> resolvePropertiesOwner(Component component, String propertyId) {
79         return Optional.of(component);
80     }
81
82     @Override
83     public StorageOperationStatus unDeclarePropertiesAsInputs(Component component, InputDefinition input) {
84         // no need for implementation since we are in a policy scenario
85         return StorageOperationStatus.OK;
86     }
87
88     @Override
89     public StorageOperationStatus unDeclarePropertiesAsListInputs(Component component, InputDefinition input) {
90         // no need for implementation since we are in a policy scenario
91         return StorageOperationStatus.OK;
92     }
93
94     @Override
95     public void addPropertiesListToInput(PropertyDataDefinition declaredProp, InputDefinition input) {
96         // no need for implementation since we are in a policy scenario
97     }
98
99     @Override
100     public StorageOperationStatus unDeclarePropertiesAsPolicies(Component component, PolicyDefinition policy) {
101         Optional<PropertyDefinition> propertyToUpdateCandidate =
102                 getDeclaredPropertyByPolicyId(component, policy.getUniqueId());
103
104         if(propertyToUpdateCandidate.isPresent()) {
105             return unDeclarePolicy(component, propertyToUpdateCandidate.get(), policy);
106         }
107
108         return StorageOperationStatus.OK;
109     }
110
111     private StorageOperationStatus unDeclarePolicy(Component component, PropertyDefinition propertyToUpdate, PolicyDefinition policy) {
112         updatePropertyAfterUndeclaration(propertyToUpdate, policy);
113
114         Either<PropertyDefinition, StorageOperationStatus> status = toscaOperationFacade
115                                                                             .updatePropertyOfComponent(component, propertyToUpdate);
116         if(status.isRight()) {
117             return status.right().value();
118         }
119
120         return StorageOperationStatus.OK;
121     }
122
123     private void updatePropertyAfterUndeclaration(PropertyDefinition propertyToUpdate, PolicyDefinition policy) {
124         List<GetPolicyValueDataDefinition> getPolicyValues = propertyToUpdate.getGetPolicyValues();
125         Optional<GetPolicyValueDataDefinition> getPolicyCandidateToRemove = getPolicyValues.stream()
126                                                                                     .filter(getPolicyValue -> getPolicyValue.getPolicyId()
127                                                                                                                       .equals(policy.getUniqueId()))
128                                                                                     .findAny();
129
130         getPolicyCandidateToRemove.ifPresent(getPolicyValue -> {
131             getPolicyValues.remove(getPolicyValue);
132             propertyToUpdate.setValue(getPolicyValue.getOrigPropertyValue());
133         });
134     }
135
136     private Optional<PropertyDefinition> getDeclaredPropertyByPolicyId(Component component,
137             String policyId) {
138         List<PropertyDefinition> properties = component.getProperties();
139
140         if(CollectionUtils.isEmpty(properties)) {
141             return Optional.empty();
142         }
143
144         for(PropertyDefinition propertyDefinition : properties) {
145             List<GetPolicyValueDataDefinition> getPolicyValues = propertyDefinition.getGetPolicyValues();
146             if(CollectionUtils.isEmpty(getPolicyValues)) {
147                 continue;
148             }
149
150
151             Optional<GetPolicyValueDataDefinition> getPolicyCandidate =
152                     getPolicyValues.stream().filter(getPolicy -> getPolicy.getPolicyId().equals(policyId)).findAny();
153
154             if(getPolicyCandidate.isPresent()) {
155                 propertyDefinition.setValue(getPolicyCandidate.get().getOrigPropertyValue());
156                 return Optional.of(propertyDefinition);
157             }
158         }
159
160         return Optional.empty();
161     }
162
163 }