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