Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / property / PropertyDeclarationOrchestrator.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;
22
23 import fj.data.Either;
24 import org.apache.commons.collections.CollectionUtils;
25 import org.apache.commons.lang3.tuple.Pair;
26 import org.openecomp.sdc.be.components.property.propertytopolicydeclarators.ComponentInstancePropertyToPolicyDeclarator;
27 import org.openecomp.sdc.be.components.property.propertytopolicydeclarators.ComponentPropertyToPolicyDeclarator;
28 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
29 import org.openecomp.sdc.be.model.Component;
30 import org.openecomp.sdc.be.model.ComponentInstInputsMap;
31 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
32 import org.openecomp.sdc.be.model.InputDefinition;
33 import org.openecomp.sdc.be.model.PolicyDefinition;
34 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
35 import org.openecomp.sdc.common.log.wrappers.Logger;
36
37 import java.util.Arrays;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Optional;
41 import java.util.stream.Collectors;
42
43 import static org.apache.commons.collections.MapUtils.isNotEmpty;
44
45 @org.springframework.stereotype.Component
46 public class PropertyDeclarationOrchestrator {
47
48     private static final Logger log = Logger.getLogger(PropertyDeclarationOrchestrator.class);
49     private ComponentInstanceInputPropertyDeclarator componentInstanceInputPropertyDeclarator;
50     private ComponentInstancePropertyDeclarator componentInstancePropertyDeclarator;
51     private PolicyPropertyDeclarator policyPropertyDeclarator;
52     private GroupPropertyDeclarator groupPropertyDeclarator;
53     private ComponentPropertyDeclarator servicePropertyDeclarator;
54     private List<PropertyDeclarator> propertyDeclaratorsToInput;
55     private List<PropertyDeclarator> propertyDeclaratorsToPolicy;
56     private ComponentPropertyToPolicyDeclarator componentPropertyToPolicyDeclarator;
57     private ComponentInstancePropertyToPolicyDeclarator componentInstancePropertyToPolicyDeclarator;
58
59     public PropertyDeclarationOrchestrator(ComponentInstanceInputPropertyDeclarator componentInstanceInputPropertyDeclarator,
60             ComponentInstancePropertyDeclarator componentInstancePropertyDeclarator, PolicyPropertyDeclarator policyPropertyDeclarator,
61             GroupPropertyDeclarator groupPropertyDeclarator, ComponentPropertyDeclarator servicePropertyDeclarator,
62             ComponentPropertyToPolicyDeclarator componentPropertyToPolicyDeclarator,
63             ComponentInstancePropertyToPolicyDeclarator componentInstancePropertyToPolicyDeclarator) {
64         this.componentInstanceInputPropertyDeclarator = componentInstanceInputPropertyDeclarator;
65         this.componentInstancePropertyDeclarator = componentInstancePropertyDeclarator;
66         this.policyPropertyDeclarator = policyPropertyDeclarator;
67         this.groupPropertyDeclarator = groupPropertyDeclarator;
68         this.servicePropertyDeclarator = servicePropertyDeclarator;
69         this.componentPropertyToPolicyDeclarator = componentPropertyToPolicyDeclarator;
70         this.componentInstancePropertyToPolicyDeclarator = componentInstancePropertyToPolicyDeclarator;
71         propertyDeclaratorsToInput = Arrays.asList(componentInstanceInputPropertyDeclarator, componentInstancePropertyDeclarator, policyPropertyDeclarator, groupPropertyDeclarator, servicePropertyDeclarator);
72         propertyDeclaratorsToPolicy = Arrays.asList(componentPropertyToPolicyDeclarator, componentInstancePropertyToPolicyDeclarator);
73     }
74
75     public Either<List<InputDefinition>, StorageOperationStatus> declarePropertiesToInputs(Component component, ComponentInstInputsMap componentInstInputsMap) {
76         updatePropertiesConstraints(component, componentInstInputsMap);
77         PropertyDeclarator propertyDeclarator = getPropertyDeclarator(componentInstInputsMap);
78         Pair<String, List<ComponentInstancePropInput>> propsToDeclare = componentInstInputsMap.resolvePropertiesToDeclare();
79         return propertyDeclarator.declarePropertiesAsInputs(component, propsToDeclare.getLeft(), propsToDeclare.getRight());
80     }
81
82     private void updatePropertiesConstraints(Component component, ComponentInstInputsMap componentInstInputsMap) {
83         componentInstInputsMap.getComponentInstanceProperties().forEach((k, v) -> updatePropsConstraints(component.safeGetComponentInstancesProperties(), k, v));
84         componentInstInputsMap.getComponentInstanceInputsMap().forEach((k, v) -> updatePropsConstraints(component.safeGetComponentInstancesInputs(), k, v));
85         componentInstInputsMap.getGroupProperties().forEach((k, v) -> updatePropsConstraints(component.safeGetPolicyProperties(), k, v));
86         componentInstInputsMap.getPolicyProperties().forEach((k, v) -> updatePropsConstraints(component.safeGetGroupsProperties(), k, v));
87     }
88
89     public Either<List<PolicyDefinition>, StorageOperationStatus> declarePropertiesToPolicies(Component component, ComponentInstInputsMap componentInstInputsMap) {
90         PropertyDeclarator propertyDeclarator = getPropertyDeclarator(componentInstInputsMap);
91         Pair<String, List<ComponentInstancePropInput>> propsToDeclare = componentInstInputsMap.resolvePropertiesToDeclare();
92         return propertyDeclarator.declarePropertiesAsPolicies(component, propsToDeclare.getLeft(), propsToDeclare.getRight());
93     }
94
95     public Either<InputDefinition, StorageOperationStatus> declarePropertiesToListInput(Component component, ComponentInstInputsMap componentInstInputsMap, InputDefinition input) {
96         PropertyDeclarator propertyDeclarator = getPropertyDeclarator(componentInstInputsMap);
97         Pair<String, List<ComponentInstancePropInput>> propsToDeclare = componentInstInputsMap.resolvePropertiesToDeclare();
98         log.debug("#declarePropertiesToInputs: componentId={}, propOwnerId={}", component.getUniqueId(), propsToDeclare.getLeft());
99         return propertyDeclarator.declarePropertiesAsListInput(component, propsToDeclare.getLeft(), propsToDeclare.getRight(), input);
100     }
101
102     private <T extends PropertyDataDefinition> void updatePropsConstraints(Map<String, List<T>> instancesProperties , String ownerId, List<ComponentInstancePropInput> inputs) {
103         Optional<List<T>> propertiesOpt = instancesProperties.entrySet()
104                 .stream()
105                 .filter(e -> e.getKey().equals(ownerId))
106                 .map(Map.Entry::getValue)
107                 .findFirst();
108         if(propertiesOpt.isPresent()){
109             Map<String, PropertyDataDefinition> instProps = propertiesOpt.get()
110                     .stream()
111                     .collect(Collectors.toMap(PropertyDataDefinition::getName, p->p));
112             inputs.stream()
113                     .filter(i->instProps.containsKey(i.getName()))
114                     .forEach(i->updatePropConstraints(i, instProps.get(i.getName())));
115
116         }
117     }
118
119     private void updatePropConstraints(PropertyDataDefinition input, PropertyDataDefinition property) {
120         if(CollectionUtils.isNotEmpty(property.getPropertyConstraints())){
121             input.setPropertyConstraints(property.getPropertyConstraints());
122         } else if(property.getSchemaProperty() != null && CollectionUtils.isNotEmpty(property.getSchemaProperty().getPropertyConstraints())){
123             input.setPropertyConstraints(property.getSchemaProperty().getPropertyConstraints());
124         }
125     }
126
127
128     public StorageOperationStatus unDeclarePropertiesAsInputs(Component component, InputDefinition inputToDelete) {
129         log.debug("#unDeclarePropertiesAsInputs - removing input declaration for input {} on component {}", inputToDelete.getName(), component.getUniqueId());
130         for (PropertyDeclarator propertyDeclarator : propertyDeclaratorsToInput) {
131             StorageOperationStatus storageOperationStatus = propertyDeclarator.unDeclarePropertiesAsInputs(component, inputToDelete);
132             if (StorageOperationStatus.OK != storageOperationStatus) {
133                 log.debug("#unDeclarePropertiesAsInputs - failed to remove input declaration for input {} on component {}. reason {}", inputToDelete.getName(), component.getUniqueId(), storageOperationStatus);
134                 return storageOperationStatus;
135             }
136         }
137         return StorageOperationStatus.OK;
138
139     }
140     /**
141      * Un declare properties declared as list type input
142      *
143      * @param component
144      * @param inputToDelete
145      * @return
146      */
147     public StorageOperationStatus unDeclarePropertiesAsListInputs(Component component, InputDefinition inputToDelete) {
148         log.debug("#unDeclarePropertiesAsListInputs - removing input declaration for input {} on component {}", inputToDelete.getName(), component.getUniqueId());
149         for (PropertyDeclarator propertyDeclarator : propertyDeclaratorsToInput) {
150             StorageOperationStatus storageOperationStatus = propertyDeclarator.unDeclarePropertiesAsListInputs(component, inputToDelete);
151             if (StorageOperationStatus.OK != storageOperationStatus) {
152                 log.debug("#unDeclarePropertiesAsListInputs - failed to remove input declaration for input {} on component {}. reason {}", inputToDelete.getName(), component.getUniqueId(), storageOperationStatus);
153                 return storageOperationStatus;
154             }
155         }
156         return StorageOperationStatus.OK;
157
158     }
159
160     /**
161      * Get properties owner id
162      *
163      * @param componentInstInputsMap
164      * @return
165      */
166     public String getPropOwnerId(ComponentInstInputsMap componentInstInputsMap) {
167         Pair<String, List<ComponentInstancePropInput>> propsToDeclare = componentInstInputsMap.resolvePropertiesToDeclare();
168         return propsToDeclare.getLeft();
169     }
170
171     public StorageOperationStatus unDeclarePropertiesAsPolicies(Component component, PolicyDefinition policyToDelete) {
172         log.debug("#unDeclarePropertiesAsInputs - removing policy declaration for input {} on component {}", policyToDelete
173                                                                                                                      .getName(), component.getUniqueId());
174         for(PropertyDeclarator propertyDeclarator : propertyDeclaratorsToPolicy) {
175             StorageOperationStatus storageOperationStatus =
176                     propertyDeclarator.unDeclarePropertiesAsPolicies(component, policyToDelete);
177             if (StorageOperationStatus.OK != storageOperationStatus) {
178                 log.debug("#unDeclarePropertiesAsInputs - failed to remove policy declaration for policy {} on component {}. reason {}", policyToDelete
179                                                                                                                                                  .getName(), component.getUniqueId(), storageOperationStatus);
180                 return storageOperationStatus;
181             }
182         }
183
184         return StorageOperationStatus.OK;
185
186     }
187
188     private PropertyDeclarator getPropertyDeclarator(ComponentInstInputsMap componentInstInputsMap) {
189         if (isNotEmpty(componentInstInputsMap.getComponentInstanceInputsMap())) {
190             return componentInstanceInputPropertyDeclarator;
191         }
192         if (isNotEmpty(componentInstInputsMap.getComponentInstanceProperties())) {
193             return componentInstancePropertyDeclarator;
194         }
195         if (isNotEmpty(componentInstInputsMap.getPolicyProperties())) {
196             return policyPropertyDeclarator;
197         }
198         if (isNotEmpty(componentInstInputsMap.getGroupProperties())) {
199             return groupPropertyDeclarator;
200         }
201         if(isNotEmpty(componentInstInputsMap.getServiceProperties())) {
202             return servicePropertyDeclarator;
203         }
204         if(isNotEmpty(componentInstInputsMap.getComponentPropertiesToPolicies())) {
205             return componentPropertyToPolicyDeclarator;
206         }
207         if(isNotEmpty(componentInstInputsMap.getComponentInstancePropertiesToPolicies())) {
208             return componentInstancePropertyToPolicyDeclarator;
209         }
210         throw new IllegalStateException("there are no properties selected for declaration");
211
212     }
213
214 }