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