Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / property / propertytopolicydeclarators / ComponentInstancePropertyToPolicyDeclarator.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.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
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.PropertyDataDefinition;
28 import org.openecomp.sdc.be.impl.ComponentsUtils;
29 import org.openecomp.sdc.be.model.Component;
30 import org.openecomp.sdc.be.model.ComponentInstance;
31 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
32 import org.openecomp.sdc.be.model.InputDefinition;
33 import org.openecomp.sdc.be.model.PolicyDefinition;
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.Collections;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Optional;
42
43 @org.springframework.stereotype.Component
44 public class ComponentInstancePropertyToPolicyDeclarator extends
45         DefaultPropertyDeclarator<ComponentInstance, ComponentInstanceProperty> {
46
47     private ToscaOperationFacade toscaOperationFacade;
48     PropertyBusinessLogic propertyBl;
49     private ComponentInstanceBusinessLogic componentInstanceBl;
50
51     public ComponentInstancePropertyToPolicyDeclarator(ComponentsUtils componentsUtils,
52             PropertyOperation propertyOperation, ToscaOperationFacade toscaOperationFacade,
53             PropertyBusinessLogic propertyBl, ComponentInstanceBusinessLogic componentInstanceBl) {
54         super(componentsUtils, propertyOperation);
55         this.toscaOperationFacade = toscaOperationFacade;
56         this.propertyBl = propertyBl;
57         this.componentInstanceBl = componentInstanceBl;
58     }
59
60     @Override
61     protected ComponentInstanceProperty createDeclaredProperty(PropertyDataDefinition prop) {
62         return new ComponentInstanceProperty(prop);
63     }
64
65     @Override
66     protected Either<?, StorageOperationStatus> updatePropertiesValues(Component component, String componentInstanceId,
67             List<ComponentInstanceProperty> properties) {
68         Map<String, List<ComponentInstanceProperty>>
69                 instProperties = Collections.singletonMap(componentInstanceId, properties);
70         return toscaOperationFacade.addComponentInstancePropertiesToComponent(component, instProperties);
71     }
72
73     @Override
74     protected Optional<ComponentInstance> resolvePropertiesOwner(Component component, String componentInstanceId) {
75         return component.getComponentInstanceById(componentInstanceId);
76     }
77
78     @Override
79     protected void addPropertiesListToInput(ComponentInstanceProperty declaredProp, InputDefinition input) {
80         return;
81     }
82
83     @Override
84     public StorageOperationStatus unDeclarePropertiesAsInputs(Component component, InputDefinition input) {
85         return StorageOperationStatus.OK;
86     }
87
88     @Override
89     public StorageOperationStatus unDeclarePropertiesAsListInputs(Component component, InputDefinition input) {
90         return StorageOperationStatus.OK;
91     }
92
93     @Override
94     public StorageOperationStatus unDeclarePropertiesAsPolicies(Component component, PolicyDefinition policy) {
95
96         Optional<ComponentInstanceProperty> propertyCandidate =
97                 componentInstanceBl.getComponentInstancePropertyByPolicyId(component, policy);
98
99
100         if(propertyCandidate.isPresent()) {
101             return toscaOperationFacade
102                            .updateComponentInstanceProperty(component, policy.getInstanceUniqueId(), propertyCandidate.get());
103         }
104
105         return StorageOperationStatus.OK;
106     }
107 }